summaryrefslogtreecommitdiff
path: root/src/browser.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/browser.rs')
-rw-r--r--src/browser.rs77
1 files changed, 58 insertions, 19 deletions
diff --git a/src/browser.rs b/src/browser.rs
index b3b72d8..7763ef9 100644
--- a/src/browser.rs
+++ b/src/browser.rs
@@ -9,6 +9,7 @@ use serde::Deserialize;
use tokio::time::sleep;
use crate::config::{notes_url, Paths};
+use crate::format::{build_paragraphs, ParagraphStyle};
use crate::helpers::{format_note_text, looks_logged_out, LoggedOutSignals};
#[derive(Debug, Deserialize)]
@@ -221,14 +222,62 @@ fn note_list_helpers_js() -> &'static str {
"#
}
+fn paragraphs_json(title: &str, body: &str) -> Result<String> {
+ let paras = build_paragraphs(title, body);
+ let wire: Vec<serde_json::Value> = paras
+ .iter()
+ .map(|p| {
+ serde_json::json!({
+ "style": match p.style {
+ ParagraphStyle::Title => "title",
+ ParagraphStyle::Heading => "heading",
+ ParagraphStyle::Subheading => "subheading",
+ ParagraphStyle::Body => "body",
+ ParagraphStyle::Bullet => "bullet",
+ ParagraphStyle::Dashed => "dashed",
+ ParagraphStyle::Numbered => "numbered",
+ ParagraphStyle::Checklist => "checklist",
+ ParagraphStyle::Monospace => "mono",
+ },
+ "text": p.text,
+ "checked": p.checked,
+ })
+ })
+ .collect();
+ Ok(serde_json::to_string(&wire)?)
+}
+
+fn style_helpers_js() -> &'static str {
+ r#"
+ function writeNoteText(Note, dataManager, note, text) {
+ if (typeof Note.createInitialTopoTextString === "function"
+ && dataManager.topoTextManager
+ && typeof dataManager.topoTextManager.load === "function") {
+ const replacement = Note.createInitialTopoTextString(text);
+ dataManager.topoTextManager.load(note.id, replacement);
+ if (typeof note.userDidChangeTopoText === "function") note.userDidChangeTopoText();
+ return;
+ }
+ if (typeof note.userDidChangeTopoText === "function") {
+ note.userDidChangeTopoText(text);
+ }
+ }
+ "#
+}
+
async fn create_via_notes_app(page: &Page, title: &str, body: &str) -> Result<CreatedNote> {
+ // Prefer Rust-formatted plain text (Apple Notes spacing/lists); pass paras for future rich path
let full = format_note_text(title, body);
let text_js = serde_json::to_string(&full)?;
+ let paras_js = paragraphs_json(title, body)?;
let helpers = note_list_helpers_js();
+ let styles = style_helpers_js();
let expr = format!(
r#"async () => {{
const text = {text_js};
+ const paras = {paras_js};
{helpers}
+ {styles}
const iframe = document.querySelector("iframe");
const w = iframe?.contentWindow;
const app = w?.NotesApp;
@@ -253,20 +302,22 @@ 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") {{
- note = Note.createNoteWithTitleText(text, folder);
+ note = Note.createNoteWithTitleText(payload, folder);
}} else {{
note = Note.createEmptyNote(folder);
- note.userDidChangeTopoText?.(text);
- if (note.Title !== undefined) note.Title = text.split("\n")[0] || "Note";
+ writeNoteText(Note, dataManager, note, payload);
+ if (note.Title !== undefined) note.Title = payload.split("\n")[0] || "Note";
}}
if (typeof dataManager.userDidCreateNote === "function") {{
dataManager.userDidCreateNote(note);
}}
await note.save(true);
return {{
- title: String(note.Title || text.split("\n")[0] || "(untitled)"),
+ title: String(note.Title || payload.split("\n")[0] || "(untitled)"),
record_name: String(note.recordName || note.id || "")
}};
}}"#
@@ -281,37 +332,25 @@ async fn update_via_notes_app(page: &Page, match_title: &str, title: &str, body:
let text_js = serde_json::to_string(&full)?;
let match_js = serde_json::to_string(match_title)?;
let helpers = note_list_helpers_js();
+ let styles = style_helpers_js();
let expr = format!(
r#"async () => {{
const text = {text_js};
const matchTitle = {match_js};
{helpers}
+ {styles}
const iframe = document.querySelector("iframe");
const w = iframe?.contentWindow;
const app = w?.NotesApp;
if (!app?.dataManager) throw new Error("NotesApp unavailable");
const dataManager = app.dataManager;
- const mv = app.mainViewModel;
const noteList = dataManager.allNotes || dataManager.__CW__allNotes;
const note = findByTitle(noteList, matchTitle);
if (!note) {{
throw new Error("No note titled " + JSON.stringify(matchTitle));
}}
const Note = note.constructor;
- // Preferred path: replace TopoText via NotesApp helpers
- if (typeof Note.createInitialTopoTextString === "function"
- && dataManager.topoTextManager
- && typeof dataManager.topoTextManager.load === "function") {{
- const replacement = Note.createInitialTopoTextString(text);
- dataManager.topoTextManager.load(note.id, replacement);
- if (typeof note.userDidChangeTopoText === "function") {{
- note.userDidChangeTopoText();
- }}
- }} else if (typeof note.userDidChangeTopoText === "function") {{
- note.userDidChangeTopoText(text);
- }} else {{
- throw new Error("No supported update path on Note model");
- }}
+ writeNoteText(Note, dataManager, note, text);
if (note.Title !== undefined) {{
note.Title = text.split("\n")[0] || matchTitle;
}}