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: the Aa menu — Title, Heading, Subheading, Body, Monospaced, bulleted, dashed and numbered lists, checklist — on the line the cursor is on, shown at once and written to that line alone; leaving the text shows the note formatted again

Yashwanth Kamireddia125cdc6019e
diff --git a/.uiverify/notes-formatted.mjs b/.uiverify/notes-formatted.mjs
index 9087705..92ba276 100644
--- a/.uiverify/notes-formatted.mjs
+++ b/.uiverify/notes-formatted.mjs
@@ -28,5 +28,18 @@ await page.locator(".nf-body, .nf-item:not(.nf-check)").first().click();
await page.waitForTimeout(200);
ok(await page.evaluate(() => document.activeElement?.classList.contains("note-text") && document.activeElement.tagName === "TEXTAREA"),
"a click into it is editing");
+// Aa: a line clicked, then Heading — shown at once, and asked of Apple for that line alone.
+await page.keyboard.press("Escape");
+await page.locator(".note-row", { hasText: "Curtains" }).click();
+await page.waitForTimeout(300);
+const fan = page.locator(".note-read [data-start]", { hasText: "Fan" }).first();
+await fan.click();
+await page.waitForTimeout(200);
+await page.getByRole("button", { name: "Format" }).click();
+await page.getByRole("menuitem", { name: "Heading", exact: true }).click();
+await page.waitForTimeout(300);
+ok((await page.locator(".nf-heading", { hasText: "Fan" }).count()) === 1, "Aa → Heading shows the line as a heading at once");
+const styled = await page.evaluate(() => window.__paragraphs?.at(-1));
+ok(styled?.style === 1 && styled?.expect === "Fan", `and asks Apple for that line alone ${JSON.stringify(styled)}`);
await b.close();
process.exitCode = failures ? 1 : 0;
diff --git a/src/screens/NotesView.tsx b/src/screens/NotesView.tsx
index 505ae12..fc62526 100644
--- a/src/screens/NotesView.tsx
+++ b/src/screens/NotesView.tsx
@@ -6,7 +6,7 @@ import { LongTextarea } from "../components/LongTextarea";
import { editorDate, groupNotes, joinTitle, listDate, mergeEdits, splitTitle } from "./notes/model";
import { api, messageOf, nestFolders, type Note, type NoteFolder } from "../lib/ipc";
import { RowMenu } from "../components/RowMenu";
-import { FormattedBody, runsFit, type Run } from "./notes/Formatted";
+import { FormattedBody, restyle, runsFit, STYLE, type Run } from "./notes/Formatted";
import { useSync } from "../lib/sync";
import { Picker } from "../components/Picker";
@@ -182,8 +182,12 @@ export function NotesView() {
const [loadedRuns, setLoadedRuns] = useState<{ guid: string; runs: Run[] } | null>(null);
/** Showing the note formatted; a click into its text switches to editing it. */
const [reading, setReading] = useState(true);
+ /** Where the line the Aa menu acts on begins, in the note's whole text. */
+ const [lineAt, setLineAt] = useState<number | null>(null);
+ const [styleMenu, setStyleMenu] = useState<{ x: number; y: number; alignRight: boolean } | null>(null);
useEffect(() => {
setReading(true);
+ setLineAt(null);
if (!openGuid) return setLoadedRuns(null);
let cancelled = false;
api.noteRuns(openGuid).then(
@@ -221,6 +225,37 @@ export function NotesView() {
}
};
+ /**
+ * Set the line the cursor is on to one of Notes' styles, as the Aa menu does. Anything
+ * typed is saved first, so the line is the line Apple holds; the style shows at once, the
+ * note shown formatted, and a refusal puts it back with the reason.
+ */
+ const applyStyle = async (style: number) => {
+ if (!open || lineAt === null) return;
+ const guid = open.guid;
+ await flush();
+ // Only over text Apple has: a save that did not go through leaves the typing on screen,
+ // and showing the formatted copy instead would hide it.
+ if (pending.current) return;
+ const text = (base.current?.guid === guid ? base.current.text : null) ?? (loadedBody?.guid === guid ? loadedBody.text : null);
+ if (text === null || (draft !== null && draft !== text)) return;
+ const nl = text.indexOf("\n", lineAt);
+ const expect = text.slice(lineAt, nl < 0 ? text.length : nl);
+ const before = loadedRuns?.guid === guid ? loadedRuns.runs : null;
+ if (before && runsFit(text, before)) {
+ setLoadedRuns({ guid, runs: restyle(text, before, lineAt, style) });
+ setDraft(null);
+ setLoadedBody({ guid, text });
+ setReading(true);
+ }
+ try {
+ await api.noteParagraph(guid, lineAt, { style, expect });
+ } catch (e) {
+ if (before) setLoadedRuns({ guid, runs: before });
+ setError(messageOf(e));
+ }
+ };
+
/** What the open note says, or null while that is still unknown. */
const body = open && loadedBody?.guid === open.guid ? loadedBody.text : null;
const runs = open && loadedRuns?.guid === open.guid ? loadedRuns.runs : null;
@@ -763,6 +798,39 @@ export function NotesView() {
</p>
{!composing && open && !open.locked && (
<div className="note-bar-actions">
+ {styleMenu && (
+ <RowMenu
+ at={styleMenu}
+ onClose={() => setStyleMenu(null)}
+ entries={[
+ { label: "Title", run: () => void applyStyle(STYLE.title) },
+ { label: "Heading", run: () => void applyStyle(STYLE.heading) },
+ { label: "Subheading", run: () => void applyStyle(STYLE.subheading) },
+ { label: "Body", run: () => void applyStyle(STYLE.body) },
+ { label: "Monospaced", run: () => void applyStyle(STYLE.mono) },
+ null,
+ { label: "• Bulleted List", run: () => void applyStyle(STYLE.bulleted) },
+ { label: "– Dashed List", run: () => void applyStyle(STYLE.dashed) },
+ { label: "1. Numbered List", run: () => void applyStyle(STYLE.numbered) },
+ { label: "Checklist", run: () => void applyStyle(STYLE.checklist) },
+ ]}
+ />
+ )}
+ {!inTrash && open.editable && (
+ <button
+ className="notes-icon notes-aa"
+ aria-label="Format"
+ aria-haspopup="menu"
+ title={lineAt === null ? "Format — click a line first" : "Format"}
+ disabled={busy || lineAt === null}
+ onClick={(e) => {
+ const r = e.currentTarget.getBoundingClientRect();
+ setStyleMenu({ x: r.right, y: r.bottom + 4, alignRight: true });
+ }}
+ >
+ Aa
+ </button>
+ )}
{!inTrash && <Picker
label={`Folder for ${open.title}`}
value={open.folderGuid ?? ALL}
@@ -833,6 +901,7 @@ export function NotesView() {
key="title"
ref={editor}
className="note-title"
+ onSelect={() => setLineAt(0)}
rows={1}
aria-label={composing ? "New note" : `Title of ${open?.title ?? "note"}`}
placeholder={composing ? "New Note" : undefined}
@@ -869,9 +938,21 @@ export function NotesView() {
aria-readonly="false"
aria-label={`Body of ${open?.title ?? "note"}`}
tabIndex={0}
- onClick={() => {
+ onClick={(e) => {
+ const line = (e.target as HTMLElement).closest("[data-start]");
+ const at = line ? Number(line.getAttribute("data-start")) : null;
+ if (at !== null) setLineAt(at);
setReading(false);
- window.setTimeout(() => bodyEditor.current?.focus(), 0);
+ // Into the text at the line clicked, so the cursor and the Aa menu agree.
+ window.setTimeout(() => {
+ const el = bodyEditor.current;
+ if (!el) return;
+ el.focus();
+ if (at !== null) {
+ const pos = Math.max(0, at - titleLine.length - 1);
+ el.setSelectionRange(pos, pos);
+ }
+ }, 0);
}}
onKeyDown={(e) => {
if (e.key === "Enter") {
@@ -893,6 +974,13 @@ export function NotesView() {
key="body"
ref={bodyEditor}
className="note-text"
+ onSelect={(e) => {
+ // The line the cursor is on, in the whole note: title line, then the body.
+ const el = e.currentTarget;
+ const offset = titleLine.length + 1 + el.selectionStart;
+ const all = titleLine + "\n" + el.value;
+ setLineAt(all.lastIndexOf("\n", offset - 1) + 1);
+ }}
aria-label={composing ? "New note text" : `Body of ${open?.title ?? "note"}`}
value={rest}
spellCheck
@@ -912,6 +1000,9 @@ export function NotesView() {
}}
onBlur={(e) => {
if (e.relatedTarget === editor.current) return;
+ // Leaving the text — not for the Aa menu, which acts on it — shows the note
+ // formatted again once nothing is left unsaved.
+ if (!(e.relatedTarget as HTMLElement | null)?.closest(".notes-aa")) setReading(true);
void (composingRef.current ? commitComposing() : flush());
}}
/>
diff --git a/src/screens/notes/Formatted.tsx b/src/screens/notes/Formatted.tsx
index 3b62f17..e808bfe 100644
--- a/src/screens/notes/Formatted.tsx
+++ b/src/screens/notes/Formatted.tsx
@@ -33,6 +33,33 @@ export const STYLE = {
checklist: 103,
} as const;
+/**
+ * The runs with the paragraph beginning at `start` set to `style` — split at its edges, as
+ * the write to Apple splits them — so a change shows the moment it is chosen.
+ */
+export function restyle(text: string, runs: Run[], start: number, style: number): Run[] {
+ const nl = text.indexOf("\n", start);
+ const end = nl < 0 ? text.length : nl + 1;
+ const out: Run[] = [];
+ let at = 0;
+ for (const run of runs) {
+ const from = at;
+ const to = at + run.length;
+ at = to;
+ const cuts = [from, ...[start, end].filter((c) => c > from && c < to), to];
+ for (let i = 0; i + 1 < cuts.length; i++) {
+ const a = cuts[i], b = cuts[i + 1];
+ const inside = a >= start && b <= end;
+ out.push({
+ ...run,
+ length: b - a,
+ ...(inside ? { style, checked: style === STYLE.checklist ? (run.style === STYLE.checklist ? run.checked : false) : null } : {}),
+ });
+ }
+ }
+ return out;
+}
+
/** Whether `runs` describe exactly this text. */
export function runsFit(text: string, runs: Run[]): boolean {
return runs.length > 0 && runs.reduce((n, r) => n + r.length, 0) === text.length;
@@ -110,15 +137,16 @@ export function FormattedBody({
const style = p.run?.style ?? STYLE.body;
number = style === STYLE.numbered ? number + 1 : 0;
const indent = { paddingInlineStart: `${(p.run?.indent ?? 0) * 1.5}em` };
+ const at = { "data-start": p.start } as Record<string, number>;
const content = p.spans.length ? p.spans.map((s, j) => <Inline key={j} span={s} />) : <br />;
- if (style === STYLE.title) return <h2 key={i} className="nf-title" style={indent}>{content}</h2>;
- if (style === STYLE.heading) return <h3 key={i} className="nf-heading" style={indent}>{content}</h3>;
- if (style === STYLE.subheading) return <h4 key={i} className="nf-subheading" style={indent}>{content}</h4>;
- if (style === STYLE.mono) return <pre key={i} className="nf-mono" style={indent}>{content}</pre>;
+ if (style === STYLE.title) return <h2 key={i} className="nf-title" style={indent} {...at}>{content}</h2>;
+ if (style === STYLE.heading) return <h3 key={i} className="nf-heading" style={indent} {...at}>{content}</h3>;
+ if (style === STYLE.subheading) return <h4 key={i} className="nf-subheading" style={indent} {...at}>{content}</h4>;
+ if (style === STYLE.mono) return <pre key={i} className="nf-mono" style={indent} {...at}>{content}</pre>;
if (style === STYLE.checklist) {
const done = Boolean(p.run?.checked);
return (
- <p key={i} className={`nf-item nf-check${done ? " is-done" : ""}`} style={indent}>
+ <p key={i} className={`nf-item nf-check${done ? " is-done" : ""}`} style={indent} {...at}>
<button
type="button"
className="nf-circle"
@@ -138,13 +166,13 @@ export function FormattedBody({
if (style === STYLE.bulleted || style === STYLE.dashed || style === STYLE.numbered) {
const mark = style === STYLE.bulleted ? "•" : style === STYLE.dashed ? "–" : `${number}.`;
return (
- <p key={i} className="nf-item" style={indent}>
+ <p key={i} className="nf-item" style={indent} {...at}>
<span className="nf-mark" aria-hidden="true">{mark}</span>
<span>{content}</span>
</p>
);
}
- return <p key={i} className="nf-body" style={indent}>{content}</p>;
+ return <p key={i} className="nf-body" style={indent} {...at}>{content}</p>;
})}
</div>
);
diff --git a/src/styles/notes.css b/src/styles/notes.css
index 09ce069..4ddd0bd 100644
--- a/src/styles/notes.css
+++ b/src/styles/notes.css
@@ -288,3 +288,5 @@ textarea.note-title::placeholder { color: var(--text-3); }
}
.nf-check.is-done > span:last-child { color: var(--text-3); }
.note-formatted a { color: var(--gold); text-decoration: underline; }
+/* Aa, as Notes writes it on its toolbar. */
+.notes-aa { font-weight: 600; font-size: 14px; letter-spacing: -0.02em; width: auto; padding-inline: 8px; }