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";
|
2023-12-21 10:14:53 +05:00
|
|
|
import { useTabContext } from "../hooks/useTabStore";
|
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
|
|
|
});
|
2023-12-21 10:14:53 +05:00
|
|
|
const tab = useTabContext();
|
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);
|
2022-06-23 19:14:55 +05:00
|
|
|
const statusBar = useRef({
|
2023-04-01 16:16:52 +05:00
|
|
|
set: setStatus,
|
|
|
|
|
updateWords: () => {
|
2023-12-21 10:14:53 +05:00
|
|
|
const editor = editors[tab.id];
|
2023-04-01 16:16:52 +05:00
|
|
|
const words = getTotalWords(editor as Editor) + " words";
|
|
|
|
|
if (currentWords.current === words) return;
|
|
|
|
|
setWords(words);
|
2024-02-15 10:30:07 +05:00
|
|
|
},
|
|
|
|
|
resetWords: () => {
|
|
|
|
|
currentWords.current = `0 words`;
|
|
|
|
|
setWords(`0 words`);
|
2023-04-01 16:16:52 +05:00
|
|
|
}
|
2022-06-23 19:14:55 +05:00
|
|
|
});
|
2023-12-21 10:14:53 +05:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
globalThis.statusBars[tab.id] = statusBar;
|
|
|
|
|
return () => {
|
|
|
|
|
globalThis.statusBars[tab.id] = undefined;
|
|
|
|
|
};
|
|
|
|
|
}, [tab.id, statusBar]);
|
2022-06-23 19:14:55 +05:00
|
|
|
|
2023-11-30 15:02:37 +05:00
|
|
|
const onScroll = React.useCallback((event: Event) => {
|
|
|
|
|
const currentOffset = (event.target as HTMLElement)?.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]);
|
|
|
|
|
|
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",
|
2023-08-01 12:07:21 +05:00
|
|
|
color: "var(--nn_secondary_paragraph)",
|
2022-06-23 19:14:55 +05:00
|
|
|
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,
|
2023-08-01 12:07:21 +05:00
|
|
|
backgroundColor: "var(--nn_primary_background)",
|
2022-06-27 18:37:12 +05:00
|
|
|
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);
|