diff --git a/crates/engine/src/keys.rs b/crates/engine/src/keys.rs index 0501a0299..4909424e7 100644 --- a/crates/engine/src/keys.rs +++ b/crates/engine/src/keys.rs @@ -74,18 +74,11 @@ impl Session { let r = match k { "enter" | "return" => Some(self.execute("text.insert", &json!({"text": if mods.shift { "\u{b}" } else { "\n" }}))), "escape" | "esc" => Some(self.execute("text.exit", &json!({}))), - "tab" => { - // Tab at the start of a paragraph changes the list level; elsewhere it types a tab. - let at_start = self.active().and_then(|d| d.selection.text.as_ref()).is_some_and(|t| t.ordered().0.1 == 0 || t.is_range()); - let in_cell = self.active().and_then(|d| d.selection.text.as_ref()).is_some_and(|t| t.cell.is_some()); - if in_cell { - Some(self.next_cell(mods.shift)) - } else if at_start { - Some(self.execute(if mods.shift { "format.outdent" } else { "format.indent" }, &json!({}))) - } else { - Some(self.execute("text.insert", &json!({"text": "\t"}))) - } - } + "tab" => match self.text_tab(mods.shift) { + Ok(true) => Some(Ok(Value::Null)), + Ok(false) => Some(self.execute("text.insert", &json!({"text": "\t"}))), + Err(error) => Some(Err(error)), + }, _ => None, }; if let Some(r) = r { @@ -314,6 +307,21 @@ impl Session { }) } + /// Tab (`back`: Shift+Tab) in edited text where it is not a tab character: in a table cell it goes to the next or + /// previous cell, at the start of a paragraph or over a range it changes the list level. `false` elsewhere, where + /// Tab types a tab. + pub fn text_tab(&mut self, back: bool) -> Result { + let Some(t) = self.active().and_then(|d| d.selection.text.clone()) else { return Ok(false) }; + if t.cell.is_some() { + self.next_cell(back)?; + } else if t.ordered().0.1 == 0 || t.is_range() { + self.execute(if back { "format.outdent" } else { "format.indent" }, &json!({}))?; + } else { + return Ok(false); + } + Ok(true) + } + fn next_cell(&mut self, back: bool) -> Result { let st = self.doc()?; let Some(t) = st.selection.text.clone() else { return Ok(Value::Null) }; diff --git a/crates/ui-egui/src/app/catalog.rs b/crates/ui-egui/src/app/catalog.rs index 2e43a5460..732940ae9 100644 --- a/crates/ui-egui/src/app/catalog.rs +++ b/crates/ui-egui/src/app/catalog.rs @@ -126,6 +126,10 @@ pub(crate) static DEFAULT_CHORDS: &[DefaultChord] = &[ DefaultChord { keys: &["Shift+F7"], id: "review.thesaurusPane", params: "", label: "Thesaurus" }, ]; +/// Chords that mean something else while slide text is edited, in the canvas editor's scope: Ctrl+] and Ctrl+[ grow +/// and shrink the font there, as Ctrl+Shift+> and < do, where they arrange the shape elsewhere. +const TEXT_EDITING_CHORDS: &[(&str, &str)] = &[("format.grow", "textEditor: Cmd+]"), ("format.shrink", "textEditor: Cmd+[")]; + // The framework owns these ids; the engine keeps its command vocabulary. const EDIT_IDS: &[(&str, &str)] = &[ ("edit.cut", "edit.cut"), @@ -189,6 +193,9 @@ pub(crate) fn build(session: &Session, generation: u64) -> Result EngineTextHost<'a> { } } + /// Reads back the text and the selection a command left in the session. + fn adopt_selection(&mut self) -> Option { + self.target = self.session.active()?.selection.text.clone()?; + self.refresh(); + Some(self.offsets.selection(&self.target)) + } + fn bounds(&self, space: &CanvasSpace) -> Option { let (layout, transform) = self.frame.as_ref()?; // The inner box also gives empty paragraphs a hit surface, without inventing padding around glyphs. @@ -285,6 +292,59 @@ impl TextEditorHost for EngineTextHost<'_> { }; props.vert.unwrap_or(TextDir::Horizontal) != TextDir::Horizontal } + + /// Tab in a cell goes to the next cell, at a paragraph start or over a range it changes the list level; Shift+Tab + /// elsewhere types a tab as Tab does. + fn tab(&mut self, selection: TextSelection, back: bool) -> Option { + self.select(selection); + match self.session.text_tab(back) { + Ok(true) => self.adopt_selection(), + Ok(false) if back => { + let replaced = self.replace(selection.range(), "\t", EditKind::Typed)?; + Some(replaced.selection) + } + Ok(false) => None, + Err(error) => { + self.error = Some(error.to_string()); + Some(selection) + } + } + } + + fn caret_stop(&mut self, offset: usize, stop: CaretStop, forward: bool) -> Option { + let body = text::body_of(self.session.active()?, &self.target)?; + let pos = self.offsets.pos(offset); + let pos = (pos.para, pos.ch); + let to = match (stop, forward) { + (CaretStop::Word, _) => edit::word_move(body, pos, forward), + (CaretStop::Paragraph, false) => edit::para_up(body, pos), + (CaretStop::Paragraph, true) => edit::para_down(body, pos), + _ => return None, + }; + Some(self.offsets.offset(to)) + } + + /// PowerPoint's word delete: from the selection's start, leaving the rest of it, with its smart spacing. + fn delete_word(&mut self, selection: TextSelection, forward: bool) -> Option { + self.select(selection); + let dir = if forward { "wordForward" } else { "wordBackward" }; + if let Err(error) = self.session.execute("text.delete", &serde_json::json!({"dir": dir})) { + self.error = Some(error.to_string()); + return Some(selection); + } + self.adopt_selection() + } + + fn copy_html(&mut self, selection: TextSelection) -> Option { + self.select(selection); + match self.session.execute("edit.copy", &serde_json::json!({})) { + Ok(reply) => reply.get("html").and_then(serde_json::Value::as_str).filter(|html| !html.is_empty()).map(str::to_owned), + Err(error) => { + self.error = Some(error.to_string()); + None + } + } + } } fn layout_affinity(affinity: CaretAffinity) -> Affinity { diff --git a/crates/ui-egui/src/tests.rs b/crates/ui-egui/src/tests.rs index 161a056b6..a0768921c 100644 --- a/crates/ui-egui/src/tests.rs +++ b/crates/ui-egui/src/tests.rs @@ -929,7 +929,11 @@ fn editing_notes_in_the_pane_keeps_their_formatting() { session.execute("slide.notes", &json!({"index": 0, "body": notes})).unwrap(); let mut harness = crate::test_harness::harness(session, Services::default()); harness.run_steps(2); - harness.get_by_role_and_label(egui::accesskit::Role::MultilineTextInput, "Notes").click(); + // Five paragraphs run past the pane's visible height, so the click goes on the first line, not the node's centre. + let first_line = harness.get_by_role_and_label(egui::accesskit::Role::MultilineTextInput, "Notes").rect().left_top() + egui::vec2(40.0, 8.0); + harness.hover_at(first_line); + harness.drag_at(first_line); + harness.drop_at(first_line); harness.run_steps(1); harness.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::Home); harness.key_press(egui::Key::End);