From ce0e823dccdf6ed2556c36b1bec7256255897d41 Mon Sep 17 00:00:00 2001 From: DWAA1660 Date: Sun, 29 Nov 2026 09:51:19 -0700 Subject: [PATCH 1/2] fix(editor): preserve typed styles and new-page links --- crates/ui-egui/src/app.rs | 60 ++++++--- crates/ui-egui/src/pageedit/inline_edit.rs | 20 ++- crates/ui-egui/tests/ui.rs | 141 +++++++++++++++++++++ docs/gaps.md | 14 +- docs/how-to/write-a-page.md | 47 ++++--- 5 files changed, 235 insertions(+), 47 deletions(-) diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index f2296a59..04111f96 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -1533,7 +1533,8 @@ enum Dialog { colour: egui::Color32, }, Link { - block: usize, + page: PathBuf, + block: Option, cell: Option<(usize, usize)>, range: (usize, usize), text: String, @@ -5300,8 +5301,13 @@ impl NoteCraftApp { /// (the top of the page, in a box of its own) and read every box back from the text /// (issue #72). fn insert_snippet_blocks(&mut self, text: &str) { - let Some(ed) = self.editor.as_mut() else { return }; + let Some(ed) = self.editor.as_ref() else { return }; let (added, _) = notecraft_formats::boxes::read(ed.format, text); + self.insert_blocks_after_caret(added); + } + + fn insert_blocks_after_caret(&mut self, added: Vec) { + let Some(ed) = self.editor.as_mut() else { return }; if added.is_empty() { return; } @@ -11213,18 +11219,35 @@ impl NoteCraftApp { self.say(crate::tl_id!("ui.select_words_in_one_line_for_a_link")); return; } - Self::place_caret_for_format(ed); - let Some(caret) = ed.caret.as_ref() else { - self.say(crate::tl_id!("ui.click_in_the_page_first_then_use_the_ribbon")); - return; - }; - let (lo, hi) = (caret.selection.0.min(caret.selection.1), caret.selection.0.max(caret.selection.1)); - let found = ed.blocks.get(caret.block).and_then(|block| pageedit::link_at_caret(block, caret)); - let (range, text, address, editing) = match found { - Some((start, end, words, address)) => ((start, end), words, address, true), - None => ((lo, hi), caret.text.chars().skip(lo).take(hi - lo).collect(), String::new(), false), + // A new page can have only its generated date. Leave that alone and append the link + // on confirmation, so Cancel never materializes a blank paragraph. + if ed.blocks.iter().enumerate().any(|(i, block)| { + pageedit::is_writable(block) && !pageedit::is_page_stamp(&ed.blocks, i) && !pageedit::is_title_echo(&ed.blocks, i, &ed.title) + }) { + Self::place_caret_for_format(ed); + } + let caret = ed.caret.as_ref(); + let (range, text, address, editing) = match caret { + Some(caret) => { + let (lo, hi) = (caret.selection.0.min(caret.selection.1), caret.selection.0.max(caret.selection.1)); + match ed.blocks.get(caret.block).and_then(|block| pageedit::link_at_caret(block, caret)) { + Some((start, end, words, address)) => ((start, end), words, address, true), + None => ((lo, hi), caret.text.chars().skip(lo).take(hi - lo).collect(), String::new(), false), + } + } + None => ((0, 0), String::new(), String::new(), false), }; - self.dialog = Some(Dialog::Link { block: caret.block, cell: caret.cell, range, text, address, editing, first: true, find: String::new() }); + self.dialog = Some(Dialog::Link { + page: ed.dir.clone(), + block: caret.map(|caret| caret.block), + cell: caret.and_then(|caret| caret.cell), + range, + text, + address, + editing, + first: true, + find: String::new(), + }); } /// Opens Insert > Online Video (issue #284): the video goes in at the caret, in place of @@ -15019,7 +15042,7 @@ impl NoteCraftApp { crate::colour_picker::Outcome::Pending => {} } } - Dialog::Link { block, cell, range, text, address, editing, first, find } => { + Dialog::Link { page, block, cell, range, text, address, editing, first, find } => { let (mut go, mut remove) = (false, false); // Every notebook, section group, section and page there is, as "title" over where // it is, with the link it would make (issue #539). @@ -15124,7 +15147,14 @@ impl NoteCraftApp { let (block, cell, range) = (*block, *cell, *range); // Removing keeps the words and lets go of the address. let replacement = if remove { text.trim().to_string() } else { pageedit::link_markup(text, address) }; - self.replace_in_caret_line(block, cell, range, &replacement); + if self.editor.as_ref().is_none_or(|ed| !ed.writable || ed.dir != *page) { + self.say(crate::tl_id!("ui.the_line_changed_put_the_link_in_again")); + } else if let Some(block) = block { + self.replace_in_caret_line(block, cell, range, &replacement); + } else { + let spans = notecraft_formats::markdown::parse_inline(&replacement); + self.insert_blocks_after_caret(vec![notecraft_formats::Block::Paragraph(spans, Vec::new())]); + } keep = false; } } diff --git a/crates/ui-egui/src/pageedit/inline_edit.rs b/crates/ui-egui/src/pageedit/inline_edit.rs index 101dd889..e3787a92 100644 --- a/crates/ui-egui/src/pageedit/inline_edit.rs +++ b/crates/ui-egui/src/pageedit/inline_edit.rs @@ -23,6 +23,8 @@ pub struct InlineEdit { spans: Vec, text: String, literal: Vec, + /// The surrounding style to resume when typing after a just-closed Markdown run. + trailing_style: Option, /// True while `spans`/`text` are the caret's view: the tag marker was taken off the /// front and goes back on when the block is written. caret: bool, @@ -198,7 +200,7 @@ pub(super) fn link_span(spans: &[Span], at: usize) -> Option<(usize, usize, Stri impl InlineEdit { pub(super) fn from_spans(spans: &[Span]) -> Self { let text = visible_text(spans); - Self { spans: spans.to_vec(), literal: vec![true; text.chars().count()], text, caret: false } + Self { spans: spans.to_vec(), literal: vec![true; text.chars().count()], text, trailing_style: None, caret: false } } pub(super) fn new(block: &Block, caret: bool) -> Self { @@ -299,8 +301,8 @@ impl InlineEdit { let fresh = matched.len() * 2 < old_run.len(); // Where the run lands: the span holding its first character, for inherited style. let mut at = 0usize; - let mut donor = None; - if !whole { + let mut donor = self.trailing_style.take().filter(|_| prefix == old_chars.len()); + if donor.is_none() && !whole { for (index, span) in self.spans.iter().enumerate() { let len = visible_len(span); if run_start < at + len || (run_start == at + len && index + 1 == self.spans.len()) { @@ -351,6 +353,9 @@ impl InlineEdit { } } let fragment = parse_inline(&projected); + // A styled run produced by wrapping existing words (such as a ribbon font choice) + // remains the typing style. Resume the donor only after completing markup by appending. + let closed_at_end = prefix == old_chars.len() && fragment.last().is_some_and(|span| !plain(span)); let mut spans = take_spans(&self.spans, 0..run_start); let mut literal = self.literal[..run_start.min(self.literal.len())].to_vec(); for span in fragment { @@ -419,6 +424,9 @@ impl InlineEdit { spans.extend(take_spans(&self.spans, run_end..usize::MAX)); literal.extend_from_slice(&self.literal[run_end.min(self.literal.len())..]); self.adopt_with_literal(spans, literal); + if closed_at_end { + self.trailing_style = Some(Span { text: String::new(), ..donor.unwrap_or_default() }); + } self.spans.clone() } @@ -430,6 +438,7 @@ impl InlineEdit { literal = vec![true; self.text.chars().count()]; } self.literal = literal; + self.trailing_style = None; } /// The serialized-source entry point kept for callers that still hand over `block_text`: @@ -477,7 +486,7 @@ impl InlineEdit { let spans = take_spans(&self.spans, range.clone()); let text = visible_text(&spans); let literal = self.literal.get(range.start..range.end).map(<[bool]>::to_vec).unwrap_or_else(|| vec![true; text.chars().count()]); - Self { spans, text, literal, caret: self.caret } + Self { spans, text, literal, trailing_style: None, caret: self.caret } } pub(super) fn join(&mut self, other: Self) { @@ -485,6 +494,7 @@ impl InlineEdit { self.spans = merge(std::mem::take(&mut self.spans)); self.text = visible_text(&self.spans); self.literal.extend(other.literal); + self.trailing_style = None; if self.literal.len() != self.text.chars().count() { self.literal = vec![true; self.text.chars().count()]; } @@ -499,6 +509,7 @@ impl InlineEdit { } self.spans = merge(spans); self.text = visible_text(&self.spans); + self.trailing_style = None; Some((self.text.clone(), selection)) } @@ -510,6 +521,7 @@ impl InlineEdit { } self.spans = merge(spans); self.text = visible_text(&self.spans); + self.trailing_style = None; Some((self.text.clone(), selection)) } diff --git a/crates/ui-egui/tests/ui.rs b/crates/ui-egui/tests/ui.rs index 98c17046..5a6cc3f0 100644 --- a/crates/ui-egui/tests/ui.rs +++ b/crates/ui-egui/tests/ui.rs @@ -7365,6 +7365,95 @@ fn the_link_dialog_puts_in_edits_and_removes_a_link() { } } +/// #407: the Link dialog on a new note puts its link beneath the generated date. +#[test] +fn link_on_a_new_page_leaves_the_date_line_alone() { + use notecraft_model::PageFormat; + for format in [PageFormat::Html, PageFormat::Cmgfm] { + let (_t, app, sample) = sample_app("ui-link-new-page"); + let mut h = harness(app, LIGHT); + settle(&mut h); + let section = sample.md_page.parent().expect("section").to_string_lossy(); + let made = ask( + &mut h, + "engine.execute", + json!({"command": "node.create", "params": {"parent": section, "kind": "page", "format": if format == PageFormat::Html { "html" } else { "cmgfm" }}}), + ) + .expect("create a page"); + let page = made.as_str().expect("page path"); + ask(&mut h, "ui.open", json!({"path": page})).expect("open"); + settle(&mut h); + let old_body = h.state().body().expect("page body").to_owned(); + let (before, _) = notecraft_formats::boxes::read(format, &old_body); + assert!(notecraft_ui_egui::pageedit::is_page_stamp(&before, 0), "the new page has a date"); + + ask(&mut h, "ui.key", json!({"key": "K", "ctrl": true})).expect("open Link"); + settle(&mut h); + assert!(h.query_by_label("Text to display").is_some(), "Link is enabled on a new page"); + h.get_by_label("Cancel").click_accesskit(); + settle(&mut h); + assert_eq!(h.state().body(), Some(old_body.as_str()), "Cancel leaves the new note alone"); + + ask(&mut h, "ui.key", json!({"key": "K", "ctrl": true})).expect("open Link again"); + settle(&mut h); + ask(&mut h, "ui.text", json!({"text": "Plan"})).expect("text"); + ask(&mut h, "ui.key", json!({"key": "Tab"})).expect("address field"); + ask(&mut h, "ui.text", json!({"text": "https://example.test/plan"})).expect("address"); + settle(&mut h); + h.get_by_label("OK").click_accesskit(); + settle(&mut h); + h.state_mut().save(); + let body = h.state().body().expect("page body"); + let (after, _) = notecraft_formats::boxes::read(format, body); + assert_eq!(after.first(), before.first(), "the original date is intact"); + assert!( + matches!( + after.get(1), + Some(notecraft_formats::Block::Paragraph(spans, _)) + if spans.iter().any(|span| span.text == "Plan" && span.link.as_deref() == Some("https://example.test/plan")) + ), + "the link is on its own line: {body}" + ); + ask(&mut h, "ui.undo", json!({})).expect("undo link insertion"); + settle(&mut h); + let (undone, _) = notecraft_formats::boxes::read(format, h.state().body().expect("body after Undo")); + assert_eq!(undone, before); + } +} + +/// #407: punctuation typed in the live editor produces links, bold, and code in both formats. +#[test] +fn typing_inline_markdown_a_character_at_a_time_formats_the_words() { + use notecraft_model::PageFormat; + for format in [PageFormat::Html, PageFormat::Cmgfm] { + let (_t, app, sample) = sample_app("ui-typed-markdown"); + let page = if format == PageFormat::Html { sample.html_page } else { sample.md_page }; + let initial = if format == PageFormat::Html { "

Start here.

" } else { "Start here." }; + notecraft_store::page::save_body(&page, initial).expect("fixture"); + let mut h = harness(app, LIGHT); + ask(&mut h, "ui.open", json!({"path": page.to_string_lossy()})).expect("open"); + settle(&mut h); + ask(&mut h, "ui.caret", json!({"block": 0})).expect("caret"); + ask(&mut h, "ui.key", json!({"key": "End"})).expect("end of the line"); + for ch in " **bold** `token` [the plan](../plan/)".chars() { + ask(&mut h, "ui.text", json!({"text": ch.to_string()})).expect("type punctuation and words"); + } + settle(&mut h); + h.state_mut().save(); + let persisted = notecraft_store::page::load_page(&page).expect("saved note"); + let (blocks, _) = notecraft_formats::boxes::read(format, &persisted.body); + let [notecraft_formats::Block::Paragraph(spans, _)] = &blocks[..] else { + panic!("one paragraph in {format:?}: {blocks:?}"); + }; + assert!(spans.iter().any(|span| span.text == "bold" && span.bold), "{format:?}: {spans:?}"); + assert!(spans.iter().any(|span| span.text == "token" && span.code), "{format:?}: {spans:?}"); + assert!( + spans.iter().any(|span| span.text == "the plan" && !span.code && !span.bold && span.link.as_deref() == Some("../plan/")), + "{format:?}: {spans:?}" + ); + } +} + /// Copy link to page, the way OneNote copies one: the link names the page from the /// notebooks folder down, so it works wherever it is pasted, and clicking it opens the /// page. @@ -11751,6 +11840,58 @@ fn font_size_applies_to_selected_text_and_survives_reopen_in_both_formats() { } } +/// #60: selecting a supported size preserves the other marks and draws as an authored page. +#[test] +fn a_large_font_size_matches_an_independently_authored_page() { + let _pixels = support::gui::pixel_test(); + for html in [false, true] { + let (_t, app, sample) = sample_app("ui-font-scope-render"); + let page = if html { &sample.html_page } else { &sample.md_page }; + let original = if html { "

Sample

Outside the selection.

" } else { "**Sample**\n\nOutside the selection." }; + let expected = if html { + "

Sample

Outside the selection.

" + } else { + "**Sample**\n\nOutside the selection." + }; + notecraft_store::page::save_body(page, original).expect("fixture"); + let mut h = sized_harness(app, LIGHT, egui::vec2(1600.0, 900.0)); + h.state_mut().open_path(&page.to_string_lossy()); + settle(&mut h); + ask(&mut h, "ui.caret", json!({"block": 0})).expect("caret"); + ask(&mut h, "ui.key", json!({"key": "A", "ctrl": true})).expect("select paragraph"); + h.get_by_label("Font Size").click_accesskit(); + settle(&mut h); + h.get_by_label("72").click_accesskit(); + settle(&mut h); + assert_eq!(h.get_by_label("Font Size").value().as_deref(), Some("72")); + h.state_mut().save_page_now(); + + let open = |h: &mut Harness<'_, NoteCraftApp>| { + h.state_mut().open_path(&page.to_string_lossy()); + settle(h); + }; + let leave = |h: &mut Harness<'_, NoteCraftApp>| { + h.state_mut().open_path(&sample.deep_page.to_string_lossy()); + settle(h); + }; + let capture = |h: &mut Harness<'_, NoteCraftApp>| { + let canvas = h.state().page_rect().expect("page canvas"); + let image = support::gui::render(h).expect("render"); + image::imageops::crop_imm(&image, canvas.left() as u32, canvas.top() as u32, canvas.width() as u32, 330).to_image() + }; + + leave(&mut h); + open(&mut h); + let actual = capture(&mut h); + review(&mut h, &format!("font-size-72-{html}")); + leave(&mut h); + notecraft_store::page::save_body(page, expected).expect("independent reference"); + open(&mut h); + let reference = capture(&mut h); + assert!(actual == reference, "the saved formatting renders like the authored reference, HTML={html}"); + } +} + /// Format Painter: click in formatted words, click Format Painter, pick other words, and they /// take that formatting; Escape puts a double-clicked painter down (issue #165). #[test] diff --git a/docs/gaps.md b/docs/gaps.md index a0e122bd..34808cd9 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -146,14 +146,12 @@ narrower than 600 points, on Android, a narrow desktop window or the browser bui than changing the wrong text. Ctrl+B, Ctrl+I and Ctrl+U invoke the existing Bold, Italic and Underline actions from focused page text (Command on macOS). Their selection limits are the same as the ribbon actions. -- **Links follow by relative path only.** A click on a link opens the page or folder it - names, a file in the program the system has for it, or a web address in the browser; the - target is a path as written, so a renamed page breaks the links to it. Links by id are not - defined (`folder-format.md` section 10). Not clickable yet: a link inside a table cell, and - one in a line that mixes left-to-right and right-to-left text. Insert > Link (Ctrl+K) can also find a - notebook, section or page by name and link to it (issue #539). A web address typed in the - words of a paragraph, heading, list item or quote becomes a link when a space or a new line - ends it (issue #540); not yet in a table cell, nor when pasted. +- **Link limits.** A hand-written relative path can break when the target moves. Links copied + from the notebook tree include the node's ID to find it after it moves within open + notebooks. Insert > Link can find a notebook, section or page by name. The live editor + still has limits on clicking links inside table cells and mixed-direction lines. Typed + web addresses become links when a space or newline ends them in a paragraph, heading, + list item or quote. This automatic linking is unavailable for table cells and pasted text. - **One weight per bundled script.** Arabic, Hebrew, Devanagari, Chinese, Japanese and Korean draw from bundled Noto faces (the CJK one is 16 MB, most of the app's size), but in one weight each and with Simplified Chinese forms for characters the CJK scripts share; a diff --git a/docs/how-to/write-a-page.md b/docs/how-to/write-a-page.md index c5db9095..cd1f706e 100644 --- a/docs/how-to/write-a-page.md +++ b/docs/how-to/write-a-page.md @@ -3,9 +3,9 @@ Click a page in the tree and it opens. Click anywhere on the page and the caret lands where you clicked. Type. -The page is shown the way it will be read: headings in heading type, bold and links in their own -colours, lists with their bullets. The marks that make them (`**`, `#`, `-`) stay in the text but -are dimmed, so the words come first. +The page shows headings, bold, links, and list bullets as they will be read. Type `**bold**` for +bold or `` `code` `` for inline code in either HTML or CMGFM. Once you close the marks, the editor +formats the words inside them and resumes the surrounding style. ## Links @@ -23,6 +23,9 @@ web address opens in the browser. While the caret is in the line, hold Ctrl (Cmd and click, so a plain click can still place the caret. A link never reaches outside the notebooks folder. +Use **Insert > Link** or Ctrl+K (Cmd+K on Mac) to enter an address or find a page by name. +You can use it immediately on a new note. The link goes below the generated date. + ## Keys | Key | What it does | @@ -48,24 +51,28 @@ A style or a tag you ask for twice turns itself off again, as it does elsewhere. ## Font controls -Select the words first, then use the Home font name or size box. A selection can contain -different sizes and other marks. Changing its size keeps its words, bold/italic marks and -unselected text. Safely mapped text selections across paragraphs are supported too. -At a plain caret, a chosen size applies to the next text you type. - -The size box accepts the same whole-point values as its list: 8, 9, 10, 11, 12, 14, 16, -18, 20, 22, 24, 26, 28, 36, 48 and 72. Type one and press Enter, or leave the box to apply -it. Escape cancels an entry. Unsupported entries, including fractional sizes and whole -sizes outside this list, revert without changing the page. +Use **Home** to format the words you select. The editor saves the same formatting in +HTML and CMGFM. These controls are available for the current note. -The controls show the explicit value, **Default** for an inherited value, or **Mixed** -for different values in one selection. HTML and CommonMark pages keep the same font -formatting when saved and reopened. Undo and Redo include these changes. - -Font families use regular installed desktop faces. Missing families fall back and are -reported. Loading bold/italic faces and browser access to local fonts are not supported. -Some table-cell, raw-markup and ambiguous selections also remain unsupported; see -[known limits](../gaps.md). Font colour, highlighting and emphasis are separate controls. +| Control | What it changes | +|---|---| +| Font name | Apply an installed desktop font family. When a family is missing, NoteCraft reports it and draws with a fallback. | +| Font size | Use one of these sizes in points: 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, or 72. | +| Font Colour and Text Highlight Colour | Set the color of the words and of the highlight behind them independently. Automatic and No Colour clear the corresponding color. | +| Emphasis and scripts | Apply bold, italic, underline, strikethrough, subscript, or superscript. | + +Changing the font size keeps other marks, such as bold, and leaves unselected words alone. +The size and font name selectors support selections within a paragraph and across paragraphs +when the editor can map the selected words. At a plain caret, the chosen formatting applies +to the next text you type. Undo and Redo include these edits. + +The selectors show the explicit value, **Default** when it is inherited, or **Mixed** when +selected words have different values. Type a size and press Enter or leave the field to apply +it. Escape cancels an entry. Unsupported entries, including fractional sizes, revert. + +Font family selection uses regular installed desktop faces. Browser access to installed +fonts and loading bold and italic faces remain open work. Some table-cell, raw-markup and +ambiguous selections have limits. See [known limits](../gaps.md) for details. ## Equations From fb0fee813451e06b205cf755e0d8c12df0a6304b Mon Sep 17 00:00:00 2001 From: DWAA1660 Date: Sun, 29 Nov 2026 12:48:44 -0700 Subject: [PATCH 2/2] fix(voice): box the dictation caret snapshot --- crates/ui-egui/src/app/voice.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/app/voice.rs b/crates/ui-egui/src/app/voice.rs index dc74d560..655e4f45 100644 --- a/crates/ui-egui/src/app/voice.rs +++ b/crates/ui-egui/src/app/voice.rs @@ -117,7 +117,7 @@ impl VoiceUi { /// there is no caret or it sits somewhere words cannot go (a table, code, a picture). #[derive(Clone)] enum Anchor { - Caret(pageedit::Caret), + Caret(Box), End, } @@ -488,7 +488,7 @@ impl NoteCraftApp { && ed.blocks.get(caret.block).is_some_and(|b| validate_voice_block(b).is_ok()) && caret.selection.0.max(caret.selection.1) <= caret.text.chars().count() => { - Anchor::Caret(caret.clone()) + Anchor::Caret(Box::new(caret.clone())) } _ => Anchor::End, }; @@ -618,7 +618,7 @@ impl NoteCraftApp { } if let Some(step) = ed.history.here_mut() { step.caret = match &ticket.anchor { - Anchor::Caret(c) => Some(c.clone()), + Anchor::Caret(c) => Some(c.as_ref().clone()), Anchor::End => before_caret, }; }