Skip to content

Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.

Notes: long notes type without lag

Yashwanth Kamireddic14634785f88
diff --git a/.uiverify/notes-long.mjs b/.uiverify/notes-long.mjs
new file mode 100644
index 0000000..e893e0d
--- /dev/null
+++ b/.uiverify/notes-long.mjs
@@ -0,0 +1,67 @@
+// A very long note types as a short one does, and saves exactly what was typed.
+//
+// React keeps a controlled textarea's text node equal to its value, rewriting all of it on
+// every key: in a 300,000-character note that made each keystroke a relayout of the whole
+// note. Watched here directly — the text node must not be touched while typing — because a
+// timing would pass or fail with the machine.
+import { engine } from "./engine.mjs";
+
+const b = await engine.launch();
+const ctx = await b.newContext({ viewport: { width: 1280, height: 820 } });
+await ctx.addInitScript(() => {
+ const line = "The quick brown fox jumps over the lazy dog, again and again, for a long note.\n";
+ const body = "Long note\n" + line.repeat(3800);
+ window.__bodyLength = body.length;
+ window.__saves = [];
+ const note = { guid: "L", folderGuid: null, title: "Long note", snippet: "The quick", body: null, locked: false,
+ pinned: false, createdAt: 1, modifiedAt: 1788500000, editable: true, etag: "e" };
+ window.__TAURI_INTERNALS__ = {
+ transformCallback: (cb) => cb,
+ invoke: async (cmd, args) => {
+ if (cmd === "auth_status") return { hasStoredSession: true, signedIn: true };
+ if (cmd === "auth_resume") return true;
+ if (cmd === "account_services") return ["notes", "ckdatabasews"];
+ if (cmd === "notes_in") return [{ ...note }];
+ if (cmd === "note_counts") return [1, []];
+ if (cmd === "note_body") return body;
+ if (cmd === "note_save") { window.__saves.push(args); return "L"; }
+ if (cmd === "sync_all" || cmd === "sync_status") return { services: [], asOf: 1, tookMs: 0 };
+ if (cmd === "plugin:event|listen") return 0;
+ return [];
+ },
+ };
+});
+const page = await ctx.newPage();
+await page.goto("http://localhost:4173", { waitUntil: "networkidle" });
+await page.locator(".nav-item", { hasText: "Notes" }).click();
+await page.waitForTimeout(600);
+
+let failures = 0;
+const ok = (pass, yes, no) => { console.log(pass ? `PASS ${yes}` : `FAIL ${no}`); if (!pass) failures++; };
+
+await page.locator(".note-text").click();
+await page.keyboard.press("Control+End");
+await page.evaluate(() => {
+ window.__rewrites = 0;
+ new MutationObserver((m) => { window.__rewrites += m.length; })
+ .observe(document.querySelector(".note-text"), { childList: true, characterData: true, subtree: true });
+});
+await page.keyboard.type(" and the end", { delay: 5 });
+await page.waitForTimeout(100);
+const rewrites = await page.evaluate(() => window.__rewrites);
+ok(rewrites === 0, "typing in a long note leaves its text alone — no rewrite of the whole note per key",
+ `the note's text was rewritten ${rewrites} times while typing twelve characters`);
+
+// Leaving the note saves it: every character there, and the new ones at the end.
+await page.locator(".note-title").click();
+await page.locator(".notes-search").click();
+await page.waitForTimeout(400);
+const saved = await page.evaluate(() => {
+ const last = window.__saves.at(-1);
+ return last && { length: last.body?.length, tail: last.body?.slice(-24), expected: window.__bodyLength + 12 };
+});
+ok(saved && saved.length === saved.expected && saved.tail.endsWith("note.\n and the end"),
+ "and what is saved is the whole note with what was typed", `saved ${JSON.stringify(saved)}`);
+
+await b.close();
+process.exitCode = failures === 0 ? 0 : 1;
diff --git a/check.sh b/check.sh
index 780cf97..d98b71c 100755
--- a/check.sh
+++ b/check.sh
@@ -37,6 +37,7 @@ step "somewhere else" node .uiverify/elsewhere.mjs
step "writing a note" node .uiverify/notes-compose.mjs
step "deleting a note" node .uiverify/notes-trash.mjs
step "pinning a note" node .uiverify/notes-pin.mjs
+step "a long note" node .uiverify/notes-long.mjs
step "uploading" node .uiverify/drive-upload.mjs
step "calendar grids" node .uiverify/calendar.mjs
step "every size" node .uiverify/sizes.mjs
@@ -47,7 +48,7 @@ export PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS=1
step "webkit: set up" .uiverify/webkit-libs.sh
step "webkit: nav" env ENGINE=webkit node .uiverify/assert.mjs
step "webkit: keyboard" env ENGINE=webkit node .uiverify/keyboard.mjs
-step "webkit: notes" bash -c 'ENGINE=webkit node .uiverify/notes-compose.mjs && ENGINE=webkit node .uiverify/notes-trash.mjs && ENGINE=webkit node .uiverify/notes-pin.mjs'
+step "webkit: notes" bash -c 'ENGINE=webkit node .uiverify/notes-compose.mjs && ENGINE=webkit node .uiverify/notes-trash.mjs && ENGINE=webkit node .uiverify/notes-pin.mjs && ENGINE=webkit node .uiverify/notes-long.mjs'
step "webkit: drive" env ENGINE=webkit node .uiverify/drive-upload.mjs
step "webkit: calendar" env ENGINE=webkit node .uiverify/calendar.mjs
step "webkit: sizes" env ENGINE=webkit node .uiverify/sizes.mjs
diff --git a/src/components/LongTextarea.tsx b/src/components/LongTextarea.tsx
new file mode 100644
index 0000000..1c27616
--- /dev/null
+++ b/src/components/LongTextarea.tsx
@@ -0,0 +1,27 @@
+import { forwardRef, useImperativeHandle, useLayoutEffect, useRef } from "react";
+
+type Props = Omit<React.TextareaHTMLAttributes<HTMLTextAreaElement>, "value" | "defaultValue"> & {
+ value: string;
+};
+
+/**
+ * A textarea that stays fast however long its text is.
+ *
+ * React keeps a controlled textarea's `defaultValue` — its child text node — equal to its
+ * value, so every keystroke read and rewrote the whole text. In a 300,000-character note
+ * that is a relayout of all of it per key: 35 ms each in WebKit, where a bare textarea takes
+ * 4. Here the value is written to the element only when it differs from what the element
+ * already holds, which while typing it never does.
+ */
+export const LongTextarea = forwardRef<HTMLTextAreaElement, Props>(function LongTextarea(
+ { value, ...props },
+ forwarded
+) {
+ const ref = useRef<HTMLTextAreaElement | null>(null);
+ useImperativeHandle(forwarded, () => ref.current as HTMLTextAreaElement);
+ useLayoutEffect(() => {
+ const el = ref.current;
+ if (el && el.value !== value) el.value = value;
+ }, [value]);
+ return <textarea ref={ref} {...props} />;
+});
diff --git a/src/screens/NotesView.tsx b/src/screens/NotesView.tsx
index 2088632..8e2e59c 100644
--- a/src/screens/NotesView.tsx
+++ b/src/screens/NotesView.tsx
@@ -1,6 +1,7 @@
import { Fragment, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
import { flushSync } from "react-dom";
import { Folder, FolderOpen, Pin, PinOff, SquarePen, Trash2 } from "lucide-react";
+import { LongTextarea } from "../components/LongTextarea";
import { editorDate, groupNotes, joinTitle, listDate, splitTitle } from "./notes/model";
import { api, messageOf, nestFolders, type Note, type NoteFolder } from "../lib/ipc";
import { useSync } from "../lib/sync";
@@ -624,7 +625,7 @@ export function NotesView() {
void (composingRef.current ? commitComposing() : flush());
}}
/>
- <textarea
+ <LongTextarea
key="body"
ref={bodyEditor}
className="note-text"