2022-08-31 06:33:37 +05:00
|
|
|
/*
|
|
|
|
|
This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
|
2023-01-16 13:44:52 +05:00
|
|
|
Copyright (C) 2023 Streetwriters (Private) Limited
|
2022-08-31 06:33:37 +05:00
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2022-08-30 16:13:11 +05:00
|
|
|
|
2022-06-27 18:37:12 +05:00
|
|
|
import React, { RefObject, useEffect, useRef, useState } from "react";
|
2023-02-14 12:32:25 +05:00
|
|
|
import { getTotalWords, Editor } from "@notesnook/editor";
|
2022-06-23 19:14:55 +05:00
|
|
|
|
2022-07-25 18:38:22 +05:00
|
|
|
function StatusBar({ container }: { container: RefObject<HTMLDivElement> }) {
|
2022-06-23 19:14:55 +05:00
|
|
|
const [status, setStatus] = useState({
|
|
|
|
|
date: "",
|
2022-08-30 11:05:10 +05:00
|
|
|
saved: ""
|
2022-06-23 19:14:55 +05:00
|
|
|
});
|
2022-06-27 18:37:12 +05:00
|
|
|
const [sticky, setSticky] = useState(false);
|
2022-07-25 12:07:01 +05:00
|
|
|
const stickyRef = useRef(false);
|
2022-06-27 18:37:12 +05:00
|
|
|
const prevScroll = useRef(0);
|
|
|
|
|
const lastStickyChangeTime = useRef(0);
|
2022-07-25 18:38:22 +05:00
|
|
|
const [words, setWords] = useState("0 words");
|
|
|
|
|
const currentWords = useRef(words);
|
|
|
|
|
const interval = useRef(0);
|
2022-06-23 19:14:55 +05:00
|
|
|
const statusBar = useRef({
|
2022-08-30 11:05:10 +05:00
|
|
|
set: setStatus
|
2022-06-23 19:14:55 +05:00
|
|
|
});
|
|
|
|
|
globalThis.statusBar = statusBar;
|
|
|
|
|
|
2022-06-27 18:37:12 +05:00
|
|
|
const onScroll = React.useCallback((event) => {
|
|
|
|
|
const currentOffset = event.target.scrollTop;
|
2022-07-25 12:07:01 +05:00
|
|
|
if (currentOffset < 200) {
|
|
|
|
|
if (stickyRef.current) {
|
|
|
|
|
stickyRef.current = false;
|
|
|
|
|
setSticky(false);
|
|
|
|
|
lastStickyChangeTime.current = Date.now();
|
|
|
|
|
prevScroll.current = currentOffset;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-06-27 18:37:12 +05:00
|
|
|
if (Date.now() - lastStickyChangeTime.current < 300) return;
|
|
|
|
|
if (currentOffset > prevScroll.current) {
|
|
|
|
|
setSticky(false);
|
2022-07-25 12:07:01 +05:00
|
|
|
stickyRef.current = false;
|
2022-06-27 18:37:12 +05:00
|
|
|
} else {
|
|
|
|
|
setSticky(true);
|
2022-07-25 12:07:01 +05:00
|
|
|
stickyRef.current = true;
|
2022-06-27 18:37:12 +05:00
|
|
|
}
|
|
|
|
|
lastStickyChangeTime.current = Date.now();
|
|
|
|
|
prevScroll.current = currentOffset;
|
|
|
|
|
}, []);
|
|
|
|
|
|
2022-07-25 18:38:22 +05:00
|
|
|
useEffect(() => {
|
|
|
|
|
currentWords.current = words;
|
|
|
|
|
}, [words]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
clearInterval(interval.current);
|
|
|
|
|
interval.current = setInterval(() => {
|
2023-02-14 12:32:25 +05:00
|
|
|
const words = getTotalWords(editor as Editor) + " words";
|
2022-07-25 18:38:22 +05:00
|
|
|
if (currentWords.current === words) return;
|
|
|
|
|
setWords(words);
|
2022-08-30 11:05:10 +05:00
|
|
|
}, 3000) as unknown as number;
|
2022-07-25 18:38:22 +05:00
|
|
|
return () => {
|
|
|
|
|
clearInterval(interval.current);
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
2022-06-27 18:37:12 +05:00
|
|
|
useEffect(() => {
|
|
|
|
|
const node = container.current;
|
|
|
|
|
node?.addEventListener("scroll", onScroll);
|
|
|
|
|
return () => {
|
|
|
|
|
node?.removeEventListener("scroll", onScroll);
|
|
|
|
|
};
|
|
|
|
|
}, [onScroll, container]);
|
|
|
|
|
|
2023-01-16 13:44:52 +05:00
|
|
|
const paragraphStyle: React.CSSProperties = {
|
2022-06-23 19:14:55 +05:00
|
|
|
marginTop: 0,
|
|
|
|
|
marginBottom: 0,
|
|
|
|
|
fontSize: "12px",
|
|
|
|
|
color: "var(--nn_icon)",
|
|
|
|
|
marginRight: 8,
|
2022-09-16 16:01:45 +05:00
|
|
|
paddingBottom: 0,
|
2023-01-16 13:44:52 +05:00
|
|
|
userSelect: "none",
|
|
|
|
|
pointerEvents: "none"
|
2022-06-23 19:14:55 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
style={{
|
|
|
|
|
flexDirection: "row",
|
|
|
|
|
display: "flex",
|
|
|
|
|
paddingRight: 12,
|
|
|
|
|
paddingLeft: 12,
|
2022-06-27 18:37:12 +05:00
|
|
|
position: sticky ? "sticky" : "relative",
|
2022-07-25 18:38:22 +05:00
|
|
|
top: -3,
|
2022-06-27 18:37:12 +05:00
|
|
|
backgroundColor: "var(--nn_bg)",
|
|
|
|
|
zIndex: 1,
|
2022-07-25 12:07:01 +05:00
|
|
|
justifyContent: sticky ? "center" : "flex-start",
|
2022-07-25 18:38:22 +05:00
|
|
|
paddingTop: 2,
|
2022-08-30 11:05:10 +05:00
|
|
|
paddingBottom: 2
|
2022-06-23 19:14:55 +05:00
|
|
|
}}
|
2022-09-16 16:01:45 +05:00
|
|
|
id="statusbar"
|
2022-06-23 19:14:55 +05:00
|
|
|
>
|
|
|
|
|
<p style={paragraphStyle}>{words}</p>
|
|
|
|
|
<p style={paragraphStyle}>{status.date}</p>
|
|
|
|
|
<p style={paragraphStyle}>{status.saved}</p>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-07-25 18:38:22 +05:00
|
|
|
|
|
|
|
|
export default React.memo(StatusBar, () => true);
|