Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge #84 update: paste text with no insertion point makes a text frame

aiongg53d57e7e1764
diff --cc crates/engine/src/cmd/edit.rs
index 32d7bb516,8bf12f06f..737f4cd34
--- a/crates/engine/src/cmd/edit.rs
+++ b/crates/engine/src/cmd/edit.rs
@@@ -91,15 -84,59 +84,65 @@@ fn paste_text(s: &mut Session, text: Op
s.execute("text.insert", &json!({"text": text.replace("\r\n", "\n").replace('\r', "\n"), "raw": true}))
}
+ /// Pasted text with no insertion point: a new text frame holding it, as wide as the first column
+ /// of the page's margin area and as tall as the text (at most the margin area's height), centred
+ /// on `at` (spread coordinates) or else on the margin area. The frame ends up selected.
+ fn paste_text_frame(s: &mut Session, p: &Value, text: &str) -> Result<Value> {
+ use designcraft_geom::{Point, Rect, shapes};
+ const LIMIT: f64 = 1.0e5;
+ let sr = super::spread_param(p, "spread");
+ let at = super::point_param(p, "at")
+ .filter(|a| a.x.is_finite() && a.y.is_finite())
+ .map(|a| Point::new(a.x.clamp(-LIMIT, LIMIT), a.y.clamp(-LIMIT, LIMIT)));
+ let lid = s.doc()?.active_layer;
+ let cache = s.cache.clone();
+ let text = text.replace("\r\n", "\n").replace('\r', "\n");
+ s.edit(|d, sel| {
+ let sp = d.spread(sr).ok_or_else(|| super::bad("edit.paste", "no such spread"))?;
+ let page = at.and_then(|a| sp.page_at_x(a.x)).and_then(|i| sp.pages.get(i)).or_else(|| sp.pages.first());
+ let page = page.ok_or_else(|| super::bad("edit.paste", "the spread has no pages"))?;
+ let margins = page.margin_rect();
+ let column = page.column_rects().first().copied().unwrap_or(margins);
+ let w = column.width().max(12.0);
+ let max_h = margins.height().max(12.0);
+ let c = at.unwrap_or(Point::new(column.center().x, margins.center().y));
+ let rect = |h: f64| Rect::new(c.x - w / 2.0, c.y - h / 2.0, c.x + w / 2.0, c.y + h / 2.0);
+ let para = designcraft_doc::ParaFormat { style: d.styles.default_paragraph.clone(), ..Default::default() };
+ let (id, sid) = d.add_text_frame(sr, rect(max_h), lid, &text, para)?;
+ // Shrink to the composed text unless it already fills the frame.
+ let composed = cache.get(d, sid, None);
+ if !composed.is_overset()
+ && let Some(ft) = composed.frame(id)
+ {
+ let inset = d.item(id).and_then(|i| i.text_frame()).map_or(0.0, |tf| tf.options.inset[0] + tf.options.inset[2]);
+ let h = (ft.content_height + inset + 0.5).clamp(12.0, max_h);
+ if h.is_finite()
+ && h < max_h
+ && let Some(it) = d.item_mut(id)
+ {
+ it.path = shapes::rectangle(rect(h));
+ // Keep the full height if the last line no longer fits.
+ if cache.get(d, sid, None).is_overset()
+ && let Some(it) = d.item_mut(id)
+ {
+ it.path = shapes::rectangle(rect(max_h));
+ }
+ }
+ }
+ *sel = Selection::items(vec![id]);
+ Ok(json!({"id": id.0, "story": sid.0}))
+ })
+ }
+
fn strip_objects(st: &designcraft_doc::Story) -> designcraft_doc::Story {
let mut out = st.clone();
- let marks = [designcraft_doc::FOOTNOTE_REF, designcraft_doc::XREF_MARK, designcraft_doc::TABLE_ANCHOR];
+ let marks = [
+ designcraft_doc::FOOTNOTE_REF,
+ designcraft_doc::ENDNOTE_REF,
+ designcraft_doc::NOTE_MARK,
+ designcraft_doc::XREF_MARK,
+ designcraft_doc::TABLE_ANCHOR,
+ ];
while let Some(i) = out.text.find(marks) {
let n = out.text[i..].chars().next().map_or(1, char::len_utf8);
out.delete(i..i + n);
@@@ -237,11 -270,16 +280,16 @@@ pub fn specs() -> Vec<CommandSpec>
"Paste",
["Edit"],
Some("Cmd+V"),
- "{inPlace?: bool, spread?: index (0), text?: the system clipboard's text (pastes it unless it is what was copied here)}",
- has_clip_or_text,
- "{inPlace?: bool, text?: the system clipboard's text (pastes it unless it is what was copied here; with no insertion point it becomes a new text frame), spread?, at?: [x, y] (spread coords: the new text frame's centre)}",
++ "{inPlace?: bool, spread?: index (0), text?: the system clipboard's text (pastes it unless it is what was copied here; with no insertion point it becomes a new text frame), at?: [x, y] (spread coords: the new text frame's centre)}",
+ // The system clipboard may hold text from another app, so Paste is always available.
+ has_doc,
|s, p| {
+ let text = super::str_param(p, "text").filter(|t| !t.is_empty());
if s.doc()?.selection.text.is_some() {
- return paste_text(s, super::str_param(p, "text").map(str::to_string), true);
+ return paste_text(s, text.map(str::to_string), true);
+ }
+ if let Some(text) = text {
+ return paste_text_frame(s, p, text);
}
let Some(clip) = s.clipboard.clone() else {
return Err(super::bad("edit.paste", "clipboard is empty"));
diff --cc crates/ui-egui/src/canvas.rs
index 0ce08d825,3d05062b0..1e09fbff7
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@@ -143,13 -139,17 +143,24 @@@ pub fn current_slot(app: &DesignApp, la
.map(|(i, _)| i)
}
+/// The spread under the view centre (a parent spread while parents are edited).
+pub fn current_spread(app: &DesignApp) -> Option<SpreadRef> {
+ let st = app.session.active()?;
+ let layout = CanvasLayout::new(&st.doc, st.editing_parents);
+ Some(layout.slots.get(current_slot(app, &layout)?)?.spread)
+}
+
+ /// The spread nearest the view centre and the centre in that spread's coordinates.
+ pub fn view_centre(app: &DesignApp) -> Option<(SpreadRef, Point)> {
+ let st = app.session.active()?;
+ let layout = CanvasLayout::new(&st.doc, st.editing_parents);
+ let slot = layout.slots.get(current_slot(app, &layout)?)?;
+ let v = app.view()?;
+ let r = app.canvas_rect?;
+ let c = Point::new(v.origin.x + r.width() as f64 / 2.0 / v.zoom, v.origin.y + r.height() as f64 / 2.0 / v.zoom);
+ Some((slot.spread, slot.to_spread(c)))
+ }
+
/// Absolute page index under the view centre.
pub fn current_page(app: &DesignApp) -> Option<usize> {
let st = app.session.active()?;
diff --cc crates/ui-egui/src/lib.rs
index c5144f8e0,756d4fe20..e5356ca77
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@@ -544,19 -497,10 +544,20 @@@ pub struct DesignApp
/// A control-channel request is being handled: commands it runs never read or write the
/// user's system clipboard (a control client must not see or replace it).
pub(crate) in_control: bool,
- /// What the last object copy put on the system clipboard (no native menu): without it the
- /// paste keys produce no paste event. It is never pasted as text.
+ /// The system clipboard's text that stands for the last object copy: what that copy put there
+ /// (no native menu: without it the paste keys produce no paste event), or what it held then
+ /// (native menu). It is never pasted as text: Paste with it pastes the copied objects.
object_clip_text: Option<String>,
+ /// The marked text the system IME last sent (`None` once it commits or clears). When the
+ /// composition ends some other way (a click, a tool switch), the IME is told to drop it.
+ pub(crate) ime_marked: Option<String>,
+ /// The system IME must drop its marked text (see [`Self::take_ime_discard`]).
+ pub(crate) ime_discard: bool,
+ /// The canvas asked for the system IME in the last frame (the web build's text input has the
+ /// keyboard then).
+ pub(crate) canvas_ime: bool,
+ /// The tool cursor bitmaps handed to the OS.
+ pub cursors: cursors::Images,
}
impl DesignApp {