diff options
| author | Christopher Warner <cwarner@kernelcode.com> | 2026-10-10 19:18:39 -0400 |
|---|---|---|
| committer | Christopher Warner <cwarner@kernelcode.com> | 2026-10-10 19:18:39 -0400 |
| commit | fe2aefcf68a8e491d721f5899bebbb265030ea28 (patch) | |
| tree | c7278d4e2a96ae7e37c0f2ed8b384560c96ddc05 /src/browser.rs | |
| parent | 47b1abd3da06f475650d107c5dcce0a82e6ca590 (diff) | |
| download | iphone-notes-fe2aefcf68a8e491d721f5899bebbb265030ea28.tar.gz iphone-notes-fe2aefcf68a8e491d721f5899bebbb265030ea28.tar.bz2 iphone-notes-fe2aefcf68a8e491d721f5899bebbb265030ea28.tar.xz iphone-notes-fe2aefcf68a8e491d721f5899bebbb265030ea28.zip | |
icloud.com was leaving every line as Title (style 0). Build notes by
inserting paragraphs and set paragraphStyle via addAttributeToRange.
Co-authored-by: Cursor <cursoragent@cursor.com>
Diffstat (limited to 'src/browser.rs')
| -rw-r--r-- | src/browser.rs | 101 |
1 files changed, 86 insertions, 15 deletions
diff --git a/src/browser.rs b/src/browser.rs index 994ed19..1bb1fd0 100644 --- a/src/browser.rs +++ b/src/browser.rs @@ -252,17 +252,84 @@ fn paragraphs_json(title: &str, body: &str) -> Result<String> { } fn style_helpers_js() -> &'static str { - // createInitialTopoTextString applies the "New notes start with" paragraph - // style (usually Title) to *every* line — that is why updates look huge. - // createNoteWithTitleText correctly styles first line = Title, rest = Body. - // For updates: build a scratch note that way, copy its TopoText, discard scratch. + // Apple Notes attributed strings use paragraphStyle.style: + // 0=Title, 1=Heading, 2=Body (confirmed against icloud.com/notes). + // Plain createNoteWithTitleText / createInitialTopoTextString leave Body + // lines as Title (style 0) when the account default is "start with Title". r#" - function writeNoteText(Note, dataManager, note, text, folder) { + function styleIdFor(name) { + switch (String(name || "")) { + case "title": return 0; + case "heading": return 1; + case "subheading": return 1; + default: return 2; // body, lists, checklist, mono, quote, divider + } + } + + function applyParagraphStyle(topo, startIdx, endIdx, basePs, styleName, checked) { + if (!basePs || typeof topo.addAttributeToRange !== "function") return; + let ps = basePs.setStyle(styleIdFor(styleName)); + if (styleName === "checklist" && typeof ps.setToDo === "function") { + try { + const maybe = ps.setToDo(!!checked); + if (maybe && typeof maybe.style === "number") ps = maybe; + } catch (_) {} + } + const a = topo.iteratorAt(startIdx); + const b = topo.iteratorAt(endIdx); + topo.addAttributeToRange(a, b, "paragraphStyle", ps); + } + + /** Build a note with real Title/Heading/Body paragraph styles from paras[]. */ + function createStyledNote(Note, folder, paras) { + const list = Array.isArray(paras) ? paras : []; + const titlePara = list.find((p) => p && p.style === "title") || list[0]; + const title = String((titlePara && titlePara.text) || "Note"); + const note = Note.createNoteWithTitleText(title + "\n", folder); + const topo = note.TopoTextString; + if (!topo) return note; + + let basePs = null; + try { + topo.forEachWithAttributesInRange( + topo.atStart(), + topo.iteratorAt(Math.min(1, topo.length)), + (cp, attrs) => { + if (!basePs && attrs && attrs.paragraphStyle) basePs = attrs.paragraphStyle; + } + ); + } catch (_) {} + + let skippedTitle = false; + for (const p of list) { + if (!p) continue; + if (!skippedTitle && p.style === "title") { + skippedTitle = true; + continue; + } + skippedTitle = true; + const chunk = String(p.text || "") + "\n"; + const startIdx = topo.length; + try { topo.atEnd().insert(chunk); } catch (_) { + // Fallback: some builds expose insert on the string itself + try { topo.insert?.(chunk); } catch (_) { continue; } + } + const endIdx = topo.length; + applyParagraphStyle(topo, startIdx, endIdx, basePs, p.style, p.checked); + } + if (typeof note.userDidChangeTopoText === "function") note.userDidChangeTopoText(); + return note; + } + + function writeNoteText(Note, dataManager, note, text, folder, paras) { const canLoad = dataManager.topoTextManager && typeof dataManager.topoTextManager.load === "function"; + if (typeof Note.createNoteWithTitleText === "function" && folder && canLoad) { - const scratch = Note.createNoteWithTitleText(text, folder); + const scratch = Array.isArray(paras) && paras.length + ? createStyledNote(Note, folder, paras) + : Note.createNoteWithTitleText(text, folder); try { const topo = scratch.TopoTextString; if (topo) { @@ -274,15 +341,16 @@ fn style_helpers_js() -> &'static str { } } finally { try { - if (typeof dataManager.removeUnsavedItem === "function") { - dataManager.removeUnsavedItem(scratch); - } else if (typeof dataManager.didDeleteNote === "function") { - dataManager.didDeleteNote(scratch); + if (scratch && scratch !== note) { + if (typeof dataManager.removeUnsavedItem === "function") { + dataManager.removeUnsavedItem(scratch); + } else if (typeof dataManager.didDeleteNote === "function") { + dataManager.didDeleteNote(scratch); + } } } catch (_) {} } } - // Last resort — may mark every paragraph Title; avoid when possible if (typeof Note.createInitialTopoTextString === "function" && canLoad) { const replacement = Note.createInitialTopoTextString(text); dataManager.topoTextManager.load(note.id, replacement); @@ -335,14 +403,15 @@ async fn create_via_notes_app(page: &Page, title: &str, body: &str) -> Result<Cr noteAt(folderList, 0) || null; if (!folder) throw new Error("Could not resolve a Notes folder"); - // Apple-formatted plain text from Rust (title + spaced body/lists) const payload = text; let note; - if (typeof Note.createNoteWithTitleText === "function") {{ + if (typeof Note.createNoteWithTitleText === "function" && Array.isArray(paras) && paras.length) {{ + note = createStyledNote(Note, folder, paras); + }} else if (typeof Note.createNoteWithTitleText === "function") {{ note = Note.createNoteWithTitleText(payload, folder); }} else {{ note = Note.createEmptyNote(folder); - writeNoteText(Note, dataManager, note, payload, folder); + writeNoteText(Note, dataManager, note, payload, folder, paras); if (note.Title !== undefined) note.Title = payload.split("\n")[0] || "Note"; }} if (typeof dataManager.userDidCreateNote === "function") {{ @@ -363,6 +432,7 @@ async fn create_via_notes_app(page: &Page, title: &str, body: &str) -> Result<Cr async fn update_via_notes_app(page: &Page, match_title: &str, title: &str, body: &str) -> Result<CreatedNote> { let full = format_note_text(title, body); let text_js = serde_json::to_string(&full)?; + let paras_js = paragraphs_json(title, body)?; let match_js = serde_json::to_string(match_title)?; let helpers = note_list_helpers_js(); let styles = style_helpers_js(); @@ -370,6 +440,7 @@ async fn update_via_notes_app(page: &Page, match_title: &str, title: &str, body: let expr = format!( r#"async function () {{ const text = {text_js}; + const paras = {paras_js}; const matchTitle = {match_js}; {helpers} {styles} @@ -395,7 +466,7 @@ async fn update_via_notes_app(page: &Page, match_title: &str, title: &str, body: dataManager.inboxFolder || noteAt(folderList, 0) || null; - writeNoteText(Note, dataManager, note, text, folder); + writeNoteText(Note, dataManager, note, text, folder, paras); if (note.Title !== undefined) {{ note.Title = text.split("\n")[0] || matchTitle; }} |
