Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(editor): do not take an arriving list over a half-written row
Elian Doran0bd93d2ee628
diff --git a/apps/client/src/widgets/type_widgets/options/custom_replacements.spec.tsx b/apps/client/src/widgets/type_widgets/options/custom_replacements.spec.tsxindex b2ddd0486a..5ffbc110e2 100644--- a/apps/client/src/widgets/type_widgets/options/custom_replacements.spec.tsx+++ b/apps/client/src/widgets/type_widgets/options/custom_replacements.spec.tsx@@ -115,6 +115,39 @@ describe("CustomReplacements", () => {expect(parseCustomReplacements(state.writes.at(-1))).toEqual([ { from: "CT", to: "CherryTree" } ]);});+ it("does not delete a half-written row when a list arrives mid-edit", () => {+ // Adopting the arriving list outright would take the caret's row with it. What is on screen+ // is the later intent of the two, and the blur that ends the edit is what writes it.+ state.stored = `[{"from":"TN","to":"Trilium Notes"}]`;+ const container = renderEditor();+ const [ from ] = inputs(container);++ from.focus();+ act(() => {+ from.value = "TNX";+ from.dispatchEvent(new Event("input", { bubbles: true }));+ });++ act(() => writeStore(`[{"from":"CT","to":"CherryTree"}]`));++ expect(inputs(container).map((input) => input.value)).toEqual([ "TNX", "Trilium Notes" ]);++ blur(from);+ expect(parseCustomReplacements(state.writes.at(-1))).toEqual([ { from: "TNX", to: "Trilium Notes" } ]);+ });++ it("still takes the list when a field only holds the focus", () => {+ // Focus alone is not an edit — there is nothing under the caret to lose, so waiting for a+ // blur that may never come would leave the page showing a list that no longer exists.+ state.stored = `[{"from":"TN","to":"Trilium Notes"}]`;+ const container = renderEditor();++ inputs(container)[0].focus();+ act(() => writeStore(`[{"from":"CT","to":"CherryTree"}]`));++ expect(inputs(container).map((input) => input.value)).toEqual([ "CT", "CherryTree" ]);+ });+it("keeps what is being typed when the option settles to what we just wrote", () => {// Our own save comes back through the same subscription; re-reading it then would throw away// whatever had been typed since, and move the caret.diff --git a/apps/client/src/widgets/type_widgets/options/text_notes.tsx b/apps/client/src/widgets/type_widgets/options/text_notes.tsxindex 0d5b87c2e4..c99b1b7b86 100644--- a/apps/client/src/widgets/type_widgets/options/text_notes.tsx+++ b/apps/client/src/widgets/type_widgets/options/text_notes.tsx@@ -294,15 +294,25 @@ export function CustomReplacements() {// device, or a second settings tab — and has to replace what is on screen: without this the rows// would keep their first value and the next blur would write it back over the newer list.const ownWrite = useRef(storedJson);+ const containerRef = useRef<HTMLDivElement>(null);useEffect(() => {if (storedJson === ownWrite.current) return;++ // ...except over a field being typed into, where taking the arriving list would delete the+ // half-written row under the caret. What is on screen is then the later intent of the two,+ // and the blur that ends the edit writes it. Only an edit defers it: a field merely holding+ // the focus has nothing to lose, so the list arrives at once.+ const isEditing = containerRef.current?.contains(document.activeElement)+ && serialize(rowsRef.current) !== ownWrite.current;+ if (isEditing) return;+ownWrite.current = storedJson;setRows(toRows(storedJson));}, [ storedJson ]);function persist(next: EditableReplacement[]) {- const json = JSON.stringify(next.map(({ from, to }) => ({ from, to })));+ const json = serialize(next);ownWrite.current = json;void setStoredJson(json);}@@ -324,7 +334,7 @@ export function CustomReplacements() {}return (- <div className="custom-replacements">+ <div className="custom-replacements" ref={containerRef}><div className="option-row-label"><label>{t("automatic_replacements.custom")}</label><small className="option-row-description">{t("automatic_replacements.custom_description")}</small>@@ -370,6 +380,11 @@ function toRows(storedJson: string): EditableReplacement[] {return parseCustomReplacements(storedJson).map((replacement) => ({ ...replacement, id: randomString(8) }));}+/** The rows as the option stores them, which is also how "unchanged" is told from "edited". */+function serialize(rows: EditableReplacement[]): string {+ return JSON.stringify(rows.map(({ from, to }) => ({ from, to })));+}+function Editor() {const [headingStyle, setHeadingStyle] = useTriliumOption("headingStyle");const [autoReadonlySize, setAutoReadonlySize] = useTriliumOption("autoReadonlySizeText");