Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: only allow changing title via event

Abdullah Atta2d5c8c9e6d45
diff --git a/apps/web/src/common/app-events.ts b/apps/web/src/common/app-events.ts
index 638f6ac42..84d4d5804 100644
--- a/apps/web/src/common/app-events.ts
+++ b/apps/web/src/common/app-events.ts
@@ -33,5 +33,7 @@ export const AppEvents = {
updateError: "updateError",
themeChanged: "themeChanged",
notificationClicked: "notificationClicked",
- createItem: "createItem"
+ createItem: "createItem",
+
+ changeNoteTitle: "changeNoteTitle"
};
diff --git a/apps/web/src/components/editor/index.tsx b/apps/web/src/components/editor/index.tsx
index c9c734db0..4a095e9f2 100644
--- a/apps/web/src/components/editor/index.tsx
+++ b/apps/web/src/components/editor/index.tsx
@@ -159,6 +159,11 @@ export default function EditorManager({
if (!locked && item.locked) return EV.publish(EVENTS.vaultLocked);
editorstore.get().updateSession(item);
+ if (item.title)
+ AppEventManager.publish(AppEvents.changeNoteTitle, {
+ title: item.title,
+ preventSave: true
+ });
}
}
);
diff --git a/apps/web/src/components/editor/title-box.tsx b/apps/web/src/components/editor/title-box.tsx
index 59e47f50a..99f01c30f 100644
--- a/apps/web/src/components/editor/title-box.tsx
+++ b/apps/web/src/components/editor/title-box.tsx
@@ -25,6 +25,7 @@ import useMobile from "../../hooks/use-mobile";
import useTablet from "../../hooks/use-tablet";
import { useEditorConfig } from "./context";
import { getFontById } from "@notesnook/editor";
+import { AppEventManager, AppEvents } from "../../common/app-events";
type TitleBoxProps = {
readonly: boolean;
@@ -33,41 +34,60 @@ type TitleBoxProps = {
function TitleBox(props: TitleBoxProps) {
const { readonly } = props;
const inputRef = useRef<HTMLInputElement>(null);
- const title = useStore((store) => store.session.title);
const id = useStore((store) => store.session.id);
const isMobile = useMobile();
const isTablet = useTablet();
const { editorConfig } = useEditorConfig();
-
- const MAX_FONT_SIZE = useMemo(() => {
- return isMobile || isTablet ? 1.625 : 2.625;
- }, [isMobile, isTablet]);
+ const fontFamily = useMemo(
+ () => getFontById(editorConfig.fontFamily)?.font || "heading",
+ [editorConfig.fontFamily]
+ );
const updateFontSize = useCallback(
(length) => {
if (!inputRef.current) return;
- const fontSize = textLengthToFontSize(length, MAX_FONT_SIZE);
+ const fontSize = textLengthToFontSize(
+ length,
+ isMobile || isTablet ? 1.625 : 2.625
+ );
inputRef.current.style.fontSize = `${fontSize}em`;
},
- [MAX_FONT_SIZE]
+ [isMobile, isTablet]
);
useEffect(() => {
- if (inputRef.current) {
- inputRef.current.placeholder = "Note title";
- inputRef.current.value = title;
- updateFontSize(title.length);
- }
+ if (!inputRef.current) return;
+ const { title } = useStore.getState().session;
+ inputRef.current.value = title;
+ updateFontSize(title.length);
}, [id, updateFontSize]);
useEffect(() => {
if (!inputRef.current) return;
- if (inputRef.current.value && title !== inputRef.current.value) {
- inputRef.current.placeholder = "Note title";
- inputRef.current.value = title;
- updateFontSize(title.length);
- }
- }, [title, updateFontSize]);
+ updateFontSize(inputRef.current.value.length);
+ }, [isTablet, isMobile, updateFontSize]);
+
+ useEffect(() => {
+ const { unsubscribe } = AppEventManager.subscribe(
+ AppEvents.changeNoteTitle,
+ ({ preventSave, title }: { title: string; preventSave: boolean }) => {
+ if (!inputRef.current) return;
+ withSelectionPersist(
+ inputRef.current,
+ (input) => (input.value = title)
+ );
+ updateFontSize(title.length);
+ if (!preventSave) {
+ const { sessionId, id } = store.get().session;
+ debouncedOnTitleChange(sessionId, id, title);
+ }
+ }
+ );
+
+ return () => {
+ unsubscribe();
+ };
+ }, []);
return (
<Input
@@ -81,7 +101,7 @@ function TitleBox(props: TitleBoxProps) {
dir="auto"
sx={{
p: 0,
- fontFamily: getFontById(editorConfig.fontFamily)?.font || "heading",
+ fontFamily,
fontSize: ["1.625em", "1.625em", "2.625em"],
fontWeight: "heading",
width: "100%",
@@ -102,7 +122,7 @@ export default React.memo(TitleBox, (prevProps, nextProps) => {
return prevProps.readonly === nextProps.readonly;
});
-export function onTitleChange(noteId: string | undefined, title: string) {
+function onTitleChange(noteId: string | undefined, title: string) {
store.get().setTitle(noteId, title);
}
@@ -114,3 +134,20 @@ function textLengthToFontSize(length: number, max: number) {
const steps = length / stepLength;
return Math.max(1.2, Math.min(max, max - steps * decreaseStep));
}
+
+function withSelectionPersist(
+ input: HTMLInputElement,
+ action: (input: HTMLInputElement) => void
+) {
+ const selection = {
+ start: input.selectionStart,
+ end: input.selectionEnd,
+ direction: input.selectionDirection
+ };
+ action(input);
+ input.setSelectionRange(
+ selection.start,
+ selection.end,
+ selection.direction || undefined
+ );
+}
diff --git a/apps/web/src/components/editor/toolbar.jsx b/apps/web/src/components/editor/toolbar.jsx
index 3de44c6a9..7db3587ec 100644
--- a/apps/web/src/components/editor/toolbar.jsx
+++ b/apps/web/src/components/editor/toolbar.jsx
@@ -44,8 +44,7 @@ import { AnimatedInput } from "../animated";
import { showPublishView } from "../publish-view";
import { db } from "../../common/db";
import { useEditorInstance, useHistory, useSearch } from "./context";
-import { debounceWithId } from "@notesnook/common";
-import { onTitleChange } from "./title-box";
+import { AppEventManager, AppEvents } from "../../common/app-events";
// TODO: this needs to be cleaned up!
function Toolbar() {
@@ -55,7 +54,6 @@ function Toolbar() {
const [isFullscreen, setIsFullscreen] = useState(false);
const isFocusMode = useAppStore((store) => store.isFocusMode);
const toggleFocusMode = useAppStore((store) => store.toggleFocusMode);
- const setTitle = useStore((store) => store.setTitle);
const toggleProperties = useStore((store) => store.toggleProperties);
const toggleEditorMargins = useStore((store) => store.toggleEditorMargins);
const editorMargins = useStore((store) => store.editorMargins);
@@ -230,8 +228,10 @@ function Toolbar() {
transition={{ duration: 0.5 }}
defaultValue={title}
onChange={(e) => {
- const { sessionId, id } = store.get().session;
- debouncedOnTitleChange(sessionId, id, e.target.value);
+ AppEventManager.publish(AppEvents.changeNoteTitle, {
+ title: e.target.value,
+ preventSave: false
+ });
}}
sx={{
flex: 1,
@@ -349,5 +349,3 @@ function exitFullscreen(elem) {
elem.msExitFullscreen();
}
}
-
-const debouncedOnTitleChange = debounceWithId(onTitleChange, 100);