mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-03 15:21:17 -05:00
Document Tools: Flood Fill Artboards respects isolation, color model and template layers
- The command is disabled, and refused, while a group is isolated. - A chosen color goes into the document's color model as paint.setFill puts it, so a CMYK document gets a CMYK fill unless keepModel is set. - Rectangles added to a template layer are not selected, as on a locked or hidden layer.
This commit is contained in:
@@ -67,7 +67,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Flood Fill Artboards",
|
||||
["Object", "Plug-ins", "Document Tools"],
|
||||
None,
|
||||
"{artboards?: [index…] (default: the active artboard), all?: bool (true: every artboard; not with artboards), color?: color (\"#rrggbb\", {c,m,y,k}, … as the paint commands take, #rrggbbaa and #rgba refused, as is a component too large to be finite; default: the current fill, using the live fill after earlier batch steps or the fill the preview began with in a Flood Fill preview, an error when that is None), layer?: name (default: \"Flood Fill\"; trimmed, nonempty, at most 255 characters after trimming)} add an editable rectangle exactly the size of each artboard, painted with the color and no stroke, on top of the top-level layer of that name, made at the bottom of the layers when there is none. A destination layer that holds a clipping mask is rejected before any edit. A layer that already exists keeps its lock and visibility; the rectangles are selected unless that layer is locked or hidden, and the current layer stays as it was. One undo step. Params must be an object or null (omitted options). Every option given is checked: null and values of the wrong type are errors. Refused while a symbol, pattern or opacity mask is edited → {ids: [rectangle id…], layer: layer id, created: bool (a new layer was made)}",
|
||||
"{artboards?: [index…] (default: the active artboard), all?: bool (true: every artboard; not with artboards), color?: color (\"#rrggbb\", {c,m,y,k}, … as the paint commands take, #rrggbbaa and #rgba refused, as is a component too large to be finite; explicit RGB colors convert to the document's color model as paint.setFill does; default: the current fill, using the live fill after earlier batch steps or the fill the preview began with in a Flood Fill preview, an error when that is None), layer?: name (default: \"Flood Fill\"; trimmed, nonempty, at most 255 characters after trimming)} add an editable rectangle exactly the size of each artboard, painted with the color and no stroke, on top of the top-level layer of that name, made at the bottom of the layers when there is none. A destination layer that holds a clipping mask is rejected before any edit. A layer that already exists keeps its lock and visibility; the rectangles are selected unless that layer is locked, hidden or a template, in which case the prior selection stays. The current layer stays as it was. One undo step. Params must be an object or null (omitted options). Every option given is checked: null and values of the wrong type are errors. Refused in isolation mode or while a symbol, pattern or opacity mask is edited → {ids: [rectangle id…], layer: layer id, created: bool (a new layer was made)}",
|
||||
flood_fill_enabled,
|
||||
flood_fill::flood_fill_artboards
|
||||
),
|
||||
@@ -83,6 +83,9 @@ fn flood_fill_enabled(s: &Session) -> std::result::Result<(), String> {
|
||||
if st.doc.pattern_edit.is_some() {
|
||||
return Err("finish editing the pattern before filling artboards".into());
|
||||
}
|
||||
if st.isolation.is_some() {
|
||||
return Err("exit isolation mode before filling artboards".into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,9 @@ pub(super) fn flood_fill_artboards(s: &mut Session, p: &Value) -> Result<Value>
|
||||
if d.pattern_edit.is_some() {
|
||||
return Err(bad(COMMAND, "finish editing the pattern before filling artboards"));
|
||||
}
|
||||
if st.isolation.is_some() {
|
||||
return Err(bad(COMMAND, "exit isolation mode before filling artboards"));
|
||||
}
|
||||
if d.artboards.is_empty() {
|
||||
return Err(bad(COMMAND, "the document has no artboards"));
|
||||
}
|
||||
@@ -48,7 +51,10 @@ pub(super) fn flood_fill_artboards(s: &mut Session, p: &Value) -> Result<Value>
|
||||
// batch step takes the live fill, as the steps before it left it.
|
||||
let current = st.interaction.as_ref().filter(|_| s.previewing && s.batch_stash.is_none()).map_or(&s.paint.fill, |it| &it.paint.fill);
|
||||
let fill = match p.get("color") {
|
||||
Some(v) => Paint::solid(color_value(v).filter(finite).ok_or_else(|| bad(COMMAND, format!("bad color {v}")))?),
|
||||
Some(v) => {
|
||||
let color = color_value(v).filter(finite).ok_or_else(|| bad(COMMAND, format!("bad color {v}")))?;
|
||||
Paint::solid(super::super::super::paint::new_color(s, p, color))
|
||||
}
|
||||
None => match current {
|
||||
Paint::None => return Err(bad(COMMAND, "the current fill is None: pick a fill or give `color`")),
|
||||
paint => paint.clone(),
|
||||
@@ -99,9 +105,8 @@ pub(super) fn flood_fill_artboards(s: &mut Session, p: &Value) -> Result<Value>
|
||||
look.apply(d, &mut n);
|
||||
ids.push(d.insert(Some(layer), usize::MAX, n)?);
|
||||
}
|
||||
// A reused layer keeps its lock and visibility; the rectangles on a locked or hidden one are
|
||||
// not selected.
|
||||
if d.node(layer).is_some_and(|l| l.visible && !l.locked) {
|
||||
// Rectangles on a locked, hidden or template layer are not selected.
|
||||
if d.node(layer).is_some_and(|l| l.visible && !l.locked && !l.is_template()) {
|
||||
sel.set(ids.iter().copied());
|
||||
}
|
||||
Ok((layer, created, ids))
|
||||
|
||||
@@ -278,6 +278,66 @@ fn flood_fill_reuses_its_layer_and_keeps_its_lock_and_visibility() {
|
||||
assert_eq!((r["created"].clone(), s.doc().unwrap().doc.layers[0].name.as_deref()), (json!(true), Some("Paper")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn flood_fill_preserves_selection_on_an_unlocked_template_layer() {
|
||||
let mut s = boards();
|
||||
let art = s.doc().unwrap().selection.objects[0];
|
||||
let layer = s.execute("layer.new", &json!({"name": "Paper"})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("layer.setProps", &json!({"id": layer, "template": true, "locked": false})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [art.0]})).unwrap();
|
||||
let st = s.doc().unwrap();
|
||||
let selected = st.selection.clone();
|
||||
assert!(!selected.objects.is_empty());
|
||||
let r = s.execute(FLOOD, &json!({"layer": "Paper", "all": true})).unwrap();
|
||||
let st = s.doc().unwrap();
|
||||
let node = st.doc.node(NodeId(layer)).unwrap();
|
||||
assert!(node.is_template() && node.visible && !node.locked);
|
||||
assert_eq!(r["created"], json!(false));
|
||||
assert_eq!(r["layer"].as_u64(), Some(layer));
|
||||
assert_eq!(kids(&s, layer).len(), st.doc.artboards.len());
|
||||
assert_eq!(st.selection, selected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn flood_fill_converts_explicit_rgb_to_cmyk_like_set_fill() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"colorMode": "cmyk"})).unwrap();
|
||||
s.execute("paint.setFill", &json!({"color": "#ff0000"})).unwrap();
|
||||
let expected = s.paint.fill.clone();
|
||||
assert!(matches!(expected.color(), Some(vectorcraft_color::Color::Cmyk { .. })));
|
||||
for color in [json!("#ff0000"), json!([1.0, 0.0, 0.0])] {
|
||||
let r = s.execute(FLOOD, &json!({"color": color})).unwrap();
|
||||
let id = NodeId(r["ids"][0].as_u64().unwrap());
|
||||
let fill = s.doc().unwrap().doc.node(id).unwrap().appearance.fill_paint();
|
||||
assert_eq!(fill, expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn flood_fill_refuses_isolation_without_an_edit_and_is_disabled() {
|
||||
let mut s = boards();
|
||||
let group = s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("object.isolate", &json!({"id": group})).unwrap();
|
||||
let st = s.doc().unwrap();
|
||||
assert_eq!(st.isolation, Some(NodeId(group)));
|
||||
let before = (st.doc.clone(), st.selection.clone(), st.history.undo.len(), s.paint.clone());
|
||||
let info = crate::find_command(FLOOD).unwrap().info(&s);
|
||||
assert!(!info.enabled);
|
||||
assert!(info.disabled_reason.unwrap().contains("exit isolation mode"));
|
||||
for params in [json!({}), json!({"layer": "Layer 1"}), json!({"layer": "Paper", "all": true})] {
|
||||
let error = super::flood_fill::flood_fill_artboards(&mut s, ¶ms).unwrap_err().to_string();
|
||||
assert!(error.contains("exit isolation mode"), "{error}");
|
||||
let error = s.execute(FLOOD, ¶ms).unwrap_err().to_string();
|
||||
assert!(error.contains("exit isolation mode"), "{error}");
|
||||
let st = s.doc().unwrap();
|
||||
assert_eq!(*st.doc, *before.0);
|
||||
assert_eq!(st.selection, before.1);
|
||||
assert_eq!(st.history.undo.len(), before.2);
|
||||
assert_eq!(s.paint, before.3);
|
||||
assert_eq!(st.isolation, Some(NodeId(group)));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn flood_fill_in_a_batch_uses_the_fill_set_by_an_earlier_step() {
|
||||
for none in [false, true] {
|
||||
|
||||
+4
-4
@@ -17514,12 +17514,12 @@ there is no step. → `{symbols: [name…], ids: [instance id…], skipped: [{la
|
||||
`addon.document.floodFillArtboards {artboards?, all?, color?, layer?}` adds an editable rectangle exactly the size of
|
||||
each artboard, painted with `color` and no stroke, as one undo step. `artboards` lists artboard indices (the active
|
||||
artboard by default), and `all: true` takes every artboard instead; the two can't be given together. `color` takes the
|
||||
colors the paint commands take (`#rrggbbaa` and `#rgba` are refused, as is a component too large to be finite); without it the rectangles take the current fill
|
||||
colors the paint commands take (`#rrggbbaa` and `#rgba` are refused, as is a component too large to be finite). Explicit RGB colors convert to the document's color model as `paint.setFill` does. Without `color`, the rectangles take the current fill
|
||||
(in a Flood Fill preview, the fill the preview began with; in a batch step, the live fill after preceding steps), and a current fill of None is an error. The rectangles go on top of the top-level layer named `layer` (`Flood Fill` by
|
||||
default, trimmed and nonempty, at most 255 characters after trimming), which is made at the bottom of the layers when the document has none. A destination layer that holds a clipping mask is rejected before any edit. A layer that
|
||||
already exists keeps its lock and visibility. The rectangles are selected unless that layer is locked or hidden, and
|
||||
the current layer stays as it was. Params must be an object or null (omitted options). Null option values and values
|
||||
of the wrong type are errors. The command is refused while a symbol, pattern or opacity mask is edited.
|
||||
already exists keeps its lock and visibility. The rectangles are selected unless that layer is locked, hidden or a template,
|
||||
in which case the prior selection stays. The current layer stays as it was. Params must be an object or null (omitted options). Null option values and values
|
||||
of the wrong type are errors. The command is refused in isolation mode or while a symbol, pattern or opacity mask is edited.
|
||||
The desktop app's Flood Fill Artboards… dialog picks the active artboard or all of them, the
|
||||
current fill or a color, and the layer, and previews on the canvas. The dialog disables OK for an empty or overlong
|
||||
trimmed layer name, a destination layer that holds a clipping mask, or Current Fill when there is no current fill.
|
||||
|
||||
Reference in New Issue
Block a user