A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
fix: editor focus bug & title input glitches
thecodrrbe3c8f417c54
diff --git a/apps/web/src/components/editor/header.js b/apps/web/src/components/editor/header.jsindex 7279b9e00..a9be1bce5 100644--- a/apps/web/src/components/editor/header.js+++ b/apps/web/src/components/editor/header.js@@ -1,6 +1,6 @@import { useCallback, useEffect, useMemo, useRef, useState } from "react";import TitleBox from "./title-box";-import { useStore, store } from "../../stores/editor-store";+import { useStore } from "../../stores/editor-store";import { Input } from "@rebass/forms";import * as Icon from "../icons";import { Flex } from "rebass";@@ -9,25 +9,16 @@ import { db } from "../../common/db";import { useMenuTrigger } from "../../hooks/use-menu";function Header({ readonly }) {- const title = useStore((store) => store.session.title);const id = useStore((store) => store.session.id);const tags = useStore((store) => store.session.tags);const setTag = useStore((store) => store.setTag);- const setTitle = useStore((store) => store.setTitle);const filterableTags = useMemo(() => {return db.tags.all.filter((t) => tags?.every((tag) => tag !== t?.title));}, [tags]);return (<>- <TitleBox- readonly={readonly}- title={title}- setTitle={(title) => {- const sessionId = store.get().session.id;- setTitle(sessionId, title);- }}- />+ <TitleBox readonly={readonly} />{!readonly && id && (<Flex alignItems="center" flexWrap="wrap" sx={{ lineHeight: 2.5 }}>diff --git a/apps/web/src/components/editor/index.js b/apps/web/src/components/editor/index.jsindex b9f973a79..a760e4569 100644--- a/apps/web/src/components/editor/index.js+++ b/apps/web/src/components/editor/index.js@@ -34,7 +34,7 @@ function editorSetContent(editor, content) {updateWordCount(editor);- editor.focus();+ if (!editor.hasFocus()) editor.focus();}function updateWordCount(editor) {diff --git a/apps/web/src/components/editor/title-box.tsx b/apps/web/src/components/editor/title-box.tsxnew file mode 100644index 000000000..20be81b4b--- /dev/null+++ b/apps/web/src/components/editor/title-box.tsx@@ -0,0 +1,58 @@+import React, { useEffect, useState } from "react";+import { Input } from "@rebass/forms";+import { useStore, store, SESSION_STATES } from "../../stores/editor-store";++type TitleBoxProps = {+ readonly: boolean;+};++function TitleBox(props: TitleBoxProps) {+ const { readonly } = props;++ const state = useStore((store) => store.session.state);+ const sessionId = useStore((store) => store.session.id);+ const title = useStore((store) => store.session.title);+ const setTitle = useStore((store) => store.setTitle);++ const [currentTitle, setCurrentTitle] = useState<string>();+ const [placeholder, setPlaceholder] = useState<string>();++ useEffect(() => {+ if (state === SESSION_STATES.new) {+ setCurrentTitle("");+ setPlaceholder("");+ } else if (state === SESSION_STATES.stale) {+ setCurrentTitle(store.get().session.title);+ }+ }, [state]);++ useEffect(() => {+ setPlaceholder(title);+ }, [title]);++ return (+ <Input+ value={currentTitle}+ variant="clean"+ data-test-id="editor-title"+ className="editorTitle"+ placeholder={placeholder || "Note title"}+ width="100%"+ readOnly={readonly}+ sx={{+ p: 0,+ fontFamily: "heading",+ fontSize: ["1.625em", "1.625em", "2.625em"],+ fontWeight: "heading",+ }}+ onChange={(e) => {+ setCurrentTitle(e.target.value);+ setTitle(sessionId, e.target.value);+ }}+ />+ );+}++export default React.memo(TitleBox, (prevProps, nextProps) => {+ return prevProps.readonly === nextProps.readonly;+});diff --git a/apps/web/src/components/editor/titlebox.js b/apps/web/src/components/editor/titlebox.jsdeleted file mode 100644index a08c3ffde..000000000--- a/apps/web/src/components/editor/titlebox.js+++ /dev/null@@ -1,90 +0,0 @@-import React, { useCallback, useEffect, useRef, useState } from "react";-import { Text, Flex } from "rebass";-import { Input } from "@rebass/forms";--function TitleBox(props) {- const { title, setTitle, shouldFocus, readonly } = props;-- const [height, setHeight] = useState(0);- const inputRef = useRef();-- const resize = useCallback(() => {- const textarea = document.querySelector(".editorTitle");- const dummy = document.querySelector(".dummyEditorTitle");- dummy.innerHTML = textarea.value;- setHeight(dummy.scrollHeight);- }, []);-- useEffect(() => {- if (!window.ResizeObserver) return;- const myObserver = new ResizeObserver((entries) => {- window.requestAnimationFrame(() => {- const newHeight = entries[0].contentRect.height;- setHeight(newHeight);- });- });- myObserver.observe(document.querySelector(".dummyEditorTitle"));- return () => {- myObserver.disconnect();- };- }, []);-- useEffect(() => {- if (!inputRef.current) return;- inputRef.current.value = title;- resize();- }, [title, resize]);-- useEffect(() => {- if (shouldFocus) inputRef.current.focus();- }, [shouldFocus]);-- return (- <Flex width="100%" sx={{ position: "relative" }} flexShrink={0}>- <Text- className="dummyEditorTitle"- variant="heading"- minHeight={[30, 30, 60]}- fontSize={["1.625em", "1.625em", "2.625em"]}- sx={{ whiteSpace: "pre-wrap", position: "absolute", zIndex: -1 }}- ></Text>- <Input- ref={inputRef}- variant="clean"- data-test-id="editor-title"- className="editorTitle"- autoFocus={shouldFocus}- placeholder="Note title"- as="textarea"- width="100%"- minHeight={[30, 30, 60]}- readOnly={readonly}- p={0}- sx={{- height,- overflowY: "hidden",- fontFamily: "heading",- fontSize: ["1.625em", "1.625em", "2.625em"],- fontWeight: "heading",- border: "none",- resize: "none",- }}- onKeyDown={(e) => {- if (e.key === "Enter") e.preventDefault();- }}- onChange={(e) => {- resize();- setTitle(e.target.value);- }}- />- </Flex>- );-}--export default React.memo(TitleBox, (prevProps, nextProps) => {- return (- prevProps.shouldFocus === nextProps.shouldFocus &&- prevProps.title === nextProps.title &&- prevProps.readonly === nextProps.readonly- );-});diff --git a/apps/web/src/stores/editor-store.js b/apps/web/src/stores/editor-store.jsindex a962c21a3..caf947591 100644--- a/apps/web/src/stores/editor-store.js+++ b/apps/web/src/stores/editor-store.js@@ -174,7 +174,7 @@ class EditorStore extends BaseStore {}this.set((state) => {- if (state.session.id !== note.id) return;+ if (!!state.session.id && state.session.id !== note.id) return;for (let key in session) {if (key === "content") continue;@@ -184,6 +184,7 @@ class EditorStore extends BaseStore {state.session.notebooks = note.notebooks;state.session.context = null;state.session.id = note.id;+ state.session.title = note.title;state.session.saveState = 1;state.session.dateEdited = note.dateEdited;state.session.attachmentsLength = attachments.length;@@ -191,7 +192,7 @@ class EditorStore extends BaseStore {if (!sessionId) {noteStore.setSelectedNote(id);- hashNavigate(`/notes/${id}/edit`, { replace: true });+ hashNavigate(`/notes/${id}/edit`, { replace: true, notify: false });}} catch (err) {this.setSaveState(-1);