diff options
Diffstat (limited to 'src/browser.rs')
| -rw-r--r-- | src/browser.rs | 74 |
1 files changed, 68 insertions, 6 deletions
diff --git a/src/browser.rs b/src/browser.rs index aee77ec..994ed19 100644 --- a/src/browser.rs +++ b/src/browser.rs @@ -252,11 +252,38 @@ 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. r#" - function writeNoteText(Note, dataManager, note, text) { - if (typeof Note.createInitialTopoTextString === "function" - && dataManager.topoTextManager - && typeof dataManager.topoTextManager.load === "function") { + function writeNoteText(Note, dataManager, note, text, folder) { + const canLoad = + dataManager.topoTextManager + && typeof dataManager.topoTextManager.load === "function"; + if (typeof Note.createNoteWithTitleText === "function" && folder && canLoad) { + const scratch = Note.createNoteWithTitleText(text, folder); + try { + const topo = scratch.TopoTextString; + if (topo) { + dataManager.topoTextManager.load(note.id, topo); + if (typeof note.userDidChangeTopoText === "function") { + note.userDidChangeTopoText(); + } + return; + } + } finally { + try { + 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); if (typeof note.userDidChangeTopoText === "function") note.userDidChangeTopoText(); @@ -315,7 +342,7 @@ async fn create_via_notes_app(page: &Page, title: &str, body: &str) -> Result<Cr note = Note.createNoteWithTitleText(payload, folder); }} else {{ note = Note.createEmptyNote(folder); - writeNoteText(Note, dataManager, note, payload); + writeNoteText(Note, dataManager, note, payload, folder); if (note.Title !== undefined) note.Title = payload.split("\n")[0] || "Note"; }} if (typeof dataManager.userDidCreateNote === "function") {{ @@ -357,7 +384,18 @@ async fn update_via_notes_app(page: &Page, match_title: &str, title: &str, body: throw new Error("No note titled " + JSON.stringify(matchTitle)); }} const Note = note.constructor; - writeNoteText(Note, dataManager, note, text); + const mv = app.mainViewModel; + const folderList = + dataManager.allFolders || dataManager.__CW__allFolders || dataManager.folders; + const folder = + note.Folder || + note.folder || + mv?.selectedFolder || + dataManager.defaultFolder || + dataManager.inboxFolder || + noteAt(folderList, 0) || + null; + writeNoteText(Note, dataManager, note, text, folder); if (note.Title !== undefined) {{ note.Title = text.split("\n")[0] || matchTitle; }} @@ -462,3 +500,27 @@ pub async fn cmd_update( Ok(()) } + +#[cfg(test)] +mod tests { + use super::style_helpers_js; + + #[test] + fn update_write_prefers_styled_create_scratch() { + let js = style_helpers_js(); + assert!( + js.contains("createNoteWithTitleText"), + "updates must build TopoText via createNoteWithTitleText" + ); + assert!( + js.contains("removeUnsavedItem"), + "scratch note from create path must be discarded" + ); + let scratch = js.find("createNoteWithTitleText").expect("scratch"); + let fallback = js.find("createInitialTopoTextString").expect("fallback"); + assert!( + scratch < fallback, + "styled scratch path must run before Title-everywhere fallback" + ); + } +} |
