mirror of
https://github.com/storytold/figcraft.git
synced 2026-10-28 04:16:46 -06:00
M3: reorder paints, effects and layout grids in the Design panel; grid and instance property variable bindings (#81)
Fill, stroke, effect and layout grid rows drag to reorder by their grip (a handle shows over the
row, an insertion line where it lands, the row's label follows the pointer like a dragged layer;
Escape cancels; lists that differ across the selection have nothing to drag). New commands
`fill.move`, `stroke.move` and `grid.move {ids?, from, to}` join `effect.move` (lists bottom first,
one undo step; items keep everything, linked list styles are unlinked).
Variable bindings on list items follow their items when a list is reordered or shrinks:
paint colours bind per paint (`fills/<i>/color`, `strokes/<i>/color`; `fill`/`stroke` stay the
first paint's), so a moved bound paint keeps its variable and its neighbours are never recoloured.
`fill.remove`, `stroke.remove` and `grid.remove` refuse negative indices.
Layout grids get settings in the Design panel (size; count, type, width or height, margin or
offset, gutter; colour) whose numbers bind to number variables (`layoutGrids/<i>/<field>`),
re-applied on variable and mode changes. Instance boolean and text properties bind to boolean and
string variables (`componentProperties/<name>`, from the instance's properties panel), kept across
instance rebuilds, following property renames and dropped on delete, reset and detach.
Inspect names the variable of each paint.
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -433,6 +433,8 @@ fn detach(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
n.source = None;
|
||||
n.overrides.clear();
|
||||
n.prop_refs.remove("mainComponent");
|
||||
// No properties left to drive.
|
||||
n.bound.retain(|k, _| !k.starts_with(PROP_BINDING));
|
||||
for nn in nested {
|
||||
rebase_instance(doc, nn);
|
||||
}
|
||||
@@ -471,6 +473,10 @@ fn reset_instance(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
{
|
||||
*props = defaults;
|
||||
}
|
||||
// ...and stop following variables.
|
||||
if top && doc.node(inst).is_some_and(|n| n.bound.keys().any(|k| k.starts_with(PROP_BINDING))) {
|
||||
doc.get_mut(inst)?.bound.retain(|k, _| !k.starts_with(PROP_BINDING));
|
||||
}
|
||||
done.push(inst);
|
||||
}
|
||||
if done.is_empty() {
|
||||
@@ -797,6 +803,43 @@ fn swap(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
ok()
|
||||
}
|
||||
|
||||
/// Whether instance `id` has a component property `name` that a `kind` variable can drive (a
|
||||
/// boolean property takes a boolean variable, a text property a string one).
|
||||
pub(crate) fn check_prop_binding(doc: &Document, id: NodeId, name: &str, kind: VariableType) -> std::result::Result<(), String> {
|
||||
let Some(NodeKind::Instance { component, .. }) = doc.node(id).map(|n| &n.kind) else {
|
||||
return Err(format!("{id} is not an instance (component properties are bound on instances)"));
|
||||
};
|
||||
match (prop_defs(doc, *component).get(name), kind) {
|
||||
(Some(PropDef::Boolean { .. }), VariableType::Boolean) | (Some(PropDef::Text { .. }), VariableType::String) => Ok(()),
|
||||
(Some(PropDef::Boolean { .. }), _) => Err(format!("`{name}` is a boolean property: bind a boolean variable")),
|
||||
(Some(PropDef::Text { .. }), _) => Err(format!("`{name}` is a text property: bind a string variable")),
|
||||
(Some(_), _) => Err(format!("`{name}`: only boolean and text properties can be bound to variables")),
|
||||
(None, _) => Err(format!("the instance has no property named `{name}`")),
|
||||
}
|
||||
}
|
||||
|
||||
/// Write bound variable values into instance `id`'s boolean and text property values (only what
|
||||
/// changed; the instance then follows its new values like after `instance.setProperties`).
|
||||
pub(crate) fn apply_prop_bindings(doc: &mut Document, id: NodeId, sets: &[(String, VariableValue)]) {
|
||||
let Some(NodeKind::Instance { component, props }) = doc.node(id).map(|n| &n.kind) else { return };
|
||||
let Some(NodeKind::Component { props: defs, .. }) = doc.node(*component).map(|c| &c.kind) else { return };
|
||||
let mut next = props.clone();
|
||||
for (name, val) in sets {
|
||||
let v = match (defs.get(name), val) {
|
||||
(Some(PropDef::Boolean { .. }), VariableValue::Boolean(b)) => json!(b),
|
||||
(Some(PropDef::Text { .. }), VariableValue::String(t)) => json!(t),
|
||||
_ => continue,
|
||||
};
|
||||
next.insert(name.clone(), v);
|
||||
}
|
||||
if next != *props
|
||||
&& let Ok(n) = doc.get_mut(id)
|
||||
&& let NodeKind::Instance { props, .. } = &mut n.kind
|
||||
{
|
||||
*props = next;
|
||||
}
|
||||
}
|
||||
|
||||
/// Coerce a property value to the definition's type.
|
||||
fn coerce(cmd: &str, doc: &Document, name: &str, def: &PropDef, v: &Value) -> Result<Value> {
|
||||
match def {
|
||||
@@ -1097,13 +1140,21 @@ fn rename_prop_refs(doc: &mut Document, comp: NodeId, old: &str, new: Option<&st
|
||||
}
|
||||
}
|
||||
}
|
||||
// Instances' values and the variables bound to them follow (a deleted property drops both).
|
||||
let old_key = format!("{PROP_BINDING}{old}");
|
||||
for inst in instances_of(doc, comp) {
|
||||
if let NodeKind::Instance { props, .. } = &mut doc.get_mut(inst)?.kind
|
||||
let n = doc.get_mut(inst)?;
|
||||
if let NodeKind::Instance { props, .. } = &mut n.kind
|
||||
&& let Some(v) = props.remove(old)
|
||||
&& let Some(nw) = new
|
||||
{
|
||||
props.insert(nw.to_string(), v);
|
||||
}
|
||||
if let Some(var) = n.bound.remove(&old_key)
|
||||
&& let Some(nw) = new
|
||||
{
|
||||
n.bound.insert(format!("{PROP_BINDING}{nw}"), var);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -215,37 +215,8 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
spec!("effect.remove", "Remove effect", "{ids?, index}", true, |s, p| {
|
||||
let i = req_index("effect.remove", p, "index")?;
|
||||
for id in req_targets("effect.remove", s, p)? {
|
||||
let n = s.doc_mut()?.get_mut(id)?;
|
||||
if i < n.effects.len() {
|
||||
n.effects.remove(i);
|
||||
// Its bindings go with it; later effects' move down one.
|
||||
super::styles::remap_effect_bindings(n, |k| match k.cmp(&i) {
|
||||
std::cmp::Ordering::Less => Some(k),
|
||||
std::cmp::Ordering::Equal => None,
|
||||
std::cmp::Ordering::Greater => Some(k - 1),
|
||||
});
|
||||
}
|
||||
}
|
||||
ok()
|
||||
}),
|
||||
spec!("effect.move", "Move effect", "{ids?, from, to} (indices in the effect list, bottom first)", true, |s, p| {
|
||||
let from = req_index("effect.move", p, "from")?;
|
||||
let to = req_index("effect.move", p, "to")?;
|
||||
for id in req_targets("effect.move", s, p)? {
|
||||
let n = s.doc_mut()?.get_mut(id)?;
|
||||
if from >= n.effects.len() || to >= n.effects.len() || from == to {
|
||||
continue;
|
||||
}
|
||||
let e = n.effects.remove(from);
|
||||
n.effects.insert(to, e);
|
||||
// Bindings follow their effects.
|
||||
super::styles::remap_effect_bindings(n, |k| {
|
||||
if k == from {
|
||||
return Some(to);
|
||||
}
|
||||
let k = if k > from { k - 1 } else { k };
|
||||
Some(if k >= to { k + 1 } else { k })
|
||||
});
|
||||
// Its bindings go with it; later effects' move down one.
|
||||
super::lists::remove_item(s.doc_mut()?.get_mut(id)?, super::lists::BoundList::Effects, i);
|
||||
}
|
||||
ok()
|
||||
}),
|
||||
@@ -268,12 +239,9 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
ok()
|
||||
}),
|
||||
spec!("grid.remove", "Remove layout grid", "{ids?, index}", true, |s, p| {
|
||||
let i = req_num("grid.remove", p, "index")? as usize;
|
||||
let i = req_index("grid.remove", p, "index")?;
|
||||
for id in req_targets("grid.remove", s, p)? {
|
||||
let n = s.doc_mut()?.get_mut(id)?;
|
||||
if i < n.grids.len() {
|
||||
n.grids.remove(i);
|
||||
}
|
||||
super::lists::remove_item(s.doc_mut()?.get_mut(id)?, super::lists::BoundList::Grids, i);
|
||||
}
|
||||
ok()
|
||||
}),
|
||||
@@ -980,6 +948,7 @@ fn reparent(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
fn paint_list(s: &mut Session, p: &Value, fills: bool, add: bool) -> Result<Value> {
|
||||
let cmd = if fills { "fill" } else { "stroke" };
|
||||
let ids = req_targets(cmd, s, p)?;
|
||||
let index = if add || get(p, "index").is_none() { None } else { Some(req_index(cmd, p, "index")?) };
|
||||
let doc = s.doc_mut()?;
|
||||
for i in ids {
|
||||
let n = doc.get_mut(i)?;
|
||||
@@ -994,17 +963,9 @@ fn paint_list(s: &mut Session, p: &Value, fills: bool, add: bool) -> Result<Valu
|
||||
};
|
||||
list.push(paint);
|
||||
} else {
|
||||
let idx = num(p, "index").map(|x| x as usize).unwrap_or(list.len().saturating_sub(1));
|
||||
if idx < list.len() {
|
||||
list.remove(idx);
|
||||
// A binding on the first paint's colour belongs to the paint just removed.
|
||||
if idx == 0 {
|
||||
let keys: &[&str] = if fills { &["fill", "fills", "fills/0", "fills/0/color"] } else { &["stroke", "strokes/0/color"] };
|
||||
for k in keys {
|
||||
n.bound.remove(*k);
|
||||
}
|
||||
}
|
||||
}
|
||||
let idx = index.unwrap_or(list.len().saturating_sub(1));
|
||||
// The paint's colour binding goes with it; later paints' move down one.
|
||||
super::lists::remove_item(n, if fills { super::lists::BoundList::Fills } else { super::lists::BoundList::Strokes }, idx);
|
||||
}
|
||||
}
|
||||
ok()
|
||||
|
||||
@@ -0,0 +1,225 @@
|
||||
//! A layer's ordered lists (fills, strokes, effects, layout grids): reordering them, and keeping
|
||||
//! the variable bindings on their items (`fills/<i>/color`, `effects/<i>/radius`,
|
||||
//! `layoutGrids/<i>/gutter`, …) with their items when a list is reordered or shrinks.
|
||||
//!
|
||||
//! Every list is stored bottom first: index 0 paints (or applies) first, the last one is on top;
|
||||
//! the Design panel shows them in reverse, the topmost row first.
|
||||
|
||||
use figcraft_doc::*;
|
||||
use serde_json::Value;
|
||||
|
||||
use super::{CommandSpec, ok};
|
||||
use crate::params::*;
|
||||
use crate::{Result, Session, spec};
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
vec![
|
||||
spec!("fill.move", "Move fill", "{ids?, from, to} (indices in the fill list, bottom first)", true, |s, p| {
|
||||
move_item(s, p, "fill.move", BoundList::Fills)
|
||||
}),
|
||||
spec!("stroke.move", "Move stroke", "{ids?, from, to} (indices in the stroke list, bottom first)", true, |s, p| {
|
||||
move_item(s, p, "stroke.move", BoundList::Strokes)
|
||||
}),
|
||||
spec!("effect.move", "Move effect", "{ids?, from, to} (indices in the effect list, bottom first)", true, |s, p| {
|
||||
move_item(s, p, "effect.move", BoundList::Effects)
|
||||
}),
|
||||
spec!("grid.move", "Move layout grid", "{ids?, from, to} (indices in the layout grid list, bottom first)", true, |s, p| {
|
||||
move_item(s, p, "grid.move", BoundList::Grids)
|
||||
}),
|
||||
]
|
||||
}
|
||||
|
||||
/// One of a layer's ordered lists whose items carry variable bindings.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum BoundList {
|
||||
Fills,
|
||||
Strokes,
|
||||
Effects,
|
||||
Grids,
|
||||
}
|
||||
|
||||
impl BoundList {
|
||||
/// The binding keys' prefix (`<prefix>/<index>/<field>`).
|
||||
pub fn prefix(self) -> &'static str {
|
||||
match self {
|
||||
BoundList::Fills => "fills",
|
||||
BoundList::Strokes => "strokes",
|
||||
BoundList::Effects => "effects",
|
||||
BoundList::Grids => "layoutGrids",
|
||||
}
|
||||
}
|
||||
|
||||
fn len(self, n: &Node) -> usize {
|
||||
match self {
|
||||
BoundList::Fills => n.fills.len(),
|
||||
BoundList::Strokes => n.strokes.len(),
|
||||
BoundList::Effects => n.effects.len(),
|
||||
BoundList::Grids => n.grids.len(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Unlink the style that defined the whole list (it no longer matches it).
|
||||
fn detach_style(self, n: &mut Node) {
|
||||
match self {
|
||||
BoundList::Fills => n.styles.fill = None,
|
||||
BoundList::Strokes => n.styles.stroke = None,
|
||||
BoundList::Effects => n.styles.effect = None,
|
||||
BoundList::Grids => n.styles.grid = None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The binding key of `field` on item `index` (`fills/2/color`).
|
||||
pub fn key(self, index: usize, field: &str) -> String {
|
||||
format!("{}/{index}/{field}", self.prefix())
|
||||
}
|
||||
}
|
||||
|
||||
/// The list item a binding key refers to: its list, index and field. `fill`, `fills` and
|
||||
/// `fills/0` (and `stroke`) are the first paint's colour.
|
||||
pub fn list_binding(key: &str) -> Option<(BoundList, usize, &str)> {
|
||||
match key {
|
||||
"fill" | "fills" | "fills/0" => return Some((BoundList::Fills, 0, "color")),
|
||||
"stroke" => return Some((BoundList::Strokes, 0, "color")),
|
||||
_ => {}
|
||||
}
|
||||
let (prefix, rest) = key.split_once('/')?;
|
||||
let list = [BoundList::Fills, BoundList::Strokes, BoundList::Effects, BoundList::Grids].into_iter().find(|l| l.prefix() == prefix)?;
|
||||
let (i, field) = rest.split_once('/')?;
|
||||
// Plain digits only (no sign, no "+1").
|
||||
if i.is_empty() || !i.bytes().all(|b| b.is_ascii_digit()) || field.is_empty() {
|
||||
return None;
|
||||
}
|
||||
Some((list, i.parse().ok()?, field))
|
||||
}
|
||||
|
||||
/// The binding key on paint `index`'s colour that `n` uses, if any (`fill` for the first fill's,
|
||||
/// or `fills/<index>/color`).
|
||||
pub fn paint_binding_key(n: &Node, fills: bool, index: usize) -> Option<&str> {
|
||||
let list = if fills { BoundList::Fills } else { BoundList::Strokes };
|
||||
n.bound.keys().map(String::as_str).find(|k| list_binding(k) == Some((list, index, "color")))
|
||||
}
|
||||
|
||||
/// Where item `k` lands when the item at `from` moves to `to`.
|
||||
pub fn moved_index(k: usize, from: usize, to: usize) -> usize {
|
||||
if k == from {
|
||||
return to;
|
||||
}
|
||||
let k = if k > from { k - 1 } else { k };
|
||||
if k >= to { k + 1 } else { k }
|
||||
}
|
||||
|
||||
/// Where item `k` lands when the item at `removed` goes (None: it is the one removed).
|
||||
pub fn removed_index(k: usize, removed: usize) -> Option<usize> {
|
||||
match k.cmp(&removed) {
|
||||
std::cmp::Ordering::Less => Some(k),
|
||||
std::cmp::Ordering::Equal => None,
|
||||
std::cmp::Ordering::Greater => Some(k - 1),
|
||||
}
|
||||
}
|
||||
|
||||
/// Keep `n`'s bindings on `list`'s items with their items when the list changes: `map` gives an
|
||||
/// item's new index, or None when it was removed (its bindings go with it). Bindings whose item
|
||||
/// keeps its place keep their key.
|
||||
pub(crate) fn remap_list_bindings(n: &mut Node, list: BoundList, map: impl Fn(usize) -> Option<usize>) {
|
||||
let moved: Vec<(String, Option<String>)> = n
|
||||
.bound
|
||||
.keys()
|
||||
.filter_map(|k| {
|
||||
let (l, i, field) = list_binding(k).filter(|(l, _, _)| *l == list)?;
|
||||
match map(i) {
|
||||
Some(j) if j == i => None,
|
||||
Some(j) => Some((k.clone(), Some(l.key(j, field)))),
|
||||
None => Some((k.clone(), None)),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let mut kept = Vec::new();
|
||||
for (old, new) in moved {
|
||||
if let Some(var) = n.bound.remove(&old)
|
||||
&& let Some(new) = new
|
||||
{
|
||||
kept.push((new, var));
|
||||
}
|
||||
}
|
||||
n.bound.extend(kept);
|
||||
}
|
||||
|
||||
/// Move `v[from]` to `to`; false (and nothing changes) when either is out of range or they match.
|
||||
fn reorder<T>(v: &mut Vec<T>, from: usize, to: usize) -> bool {
|
||||
if from >= v.len() || to >= v.len() || from == to {
|
||||
return false;
|
||||
}
|
||||
let item = v.remove(from);
|
||||
v.insert(to, item);
|
||||
true
|
||||
}
|
||||
|
||||
/// `fill.move` / `stroke.move` / `effect.move` / `grid.move`: the item keeps everything (an image
|
||||
/// fill its image and transform, a gradient its stops) and its bindings go with it.
|
||||
fn move_item(s: &mut Session, p: &Value, cmd: &str, list: BoundList) -> Result<Value> {
|
||||
let from = req_index(cmd, p, "from")?;
|
||||
let to = req_index(cmd, p, "to")?;
|
||||
for id in req_targets(cmd, s, p)? {
|
||||
let len = s.doc()?.node(id).map(|n| list.len(n)).unwrap_or(0);
|
||||
if from >= len || to >= len || from == to {
|
||||
continue;
|
||||
}
|
||||
let n = s.doc_mut()?.get_mut(id)?;
|
||||
let moved = match list {
|
||||
BoundList::Fills => reorder(&mut n.fills, from, to),
|
||||
BoundList::Strokes => reorder(&mut n.strokes, from, to),
|
||||
BoundList::Effects => reorder(&mut n.effects, from, to),
|
||||
BoundList::Grids => reorder(&mut n.grids, from, to),
|
||||
};
|
||||
if moved {
|
||||
remap_list_bindings(n, list, |k| Some(moved_index(k, from, to)));
|
||||
list.detach_style(n);
|
||||
}
|
||||
}
|
||||
ok()
|
||||
}
|
||||
|
||||
/// Drop item `index` of `list` on `n` with its bindings (later items' bindings move down one).
|
||||
/// False when there is no such item.
|
||||
pub(crate) fn remove_item(n: &mut Node, list: BoundList, index: usize) -> bool {
|
||||
if index >= list.len(n) {
|
||||
return false;
|
||||
}
|
||||
match list {
|
||||
BoundList::Fills => {
|
||||
let _ = n.fills.remove(index);
|
||||
}
|
||||
BoundList::Strokes => {
|
||||
let _ = n.strokes.remove(index);
|
||||
}
|
||||
BoundList::Effects => {
|
||||
let _ = n.effects.remove(index);
|
||||
}
|
||||
BoundList::Grids => {
|
||||
let _ = n.grids.remove(index);
|
||||
}
|
||||
}
|
||||
remap_list_bindings(n, list, |k| removed_index(k, index));
|
||||
true
|
||||
}
|
||||
|
||||
/// Write bound numbers into a node's layout grids (`layoutGrids/<i>/<size|count|gutter|offset>`;
|
||||
/// only what changed, so unchanged layers stay shared).
|
||||
pub(crate) fn apply_grid_bindings(doc: &mut Document, id: NodeId, sets: &[(usize, String, f64)]) {
|
||||
let Some(mut grids) = doc.node(id).map(|n| n.grids.clone()) else { return };
|
||||
for (i, field, x) in sets {
|
||||
let Some(g) = grids.get_mut(*i).filter(|_| x.is_finite()) else { continue };
|
||||
match field.as_str() {
|
||||
"size" => g.size = x.max(0.0),
|
||||
"count" => g.count = x.round().clamp(1.0, 1000.0) as u32,
|
||||
"gutter" => g.gutter = x.max(0.0),
|
||||
"offset" => g.offset = *x,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if doc.node(id).is_some_and(|n| n.grids != grids)
|
||||
&& let Ok(n) = doc.get_mut(id)
|
||||
{
|
||||
n.grids = grids;
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ pub mod file;
|
||||
pub mod image;
|
||||
pub mod layout;
|
||||
pub mod library;
|
||||
pub mod lists;
|
||||
pub mod page;
|
||||
pub mod props;
|
||||
pub mod proto;
|
||||
@@ -67,6 +68,7 @@ fn registry() -> &'static Registry {
|
||||
let mut list = Vec::new();
|
||||
list.extend(create::specs());
|
||||
list.extend(edit::specs());
|
||||
list.extend(lists::specs());
|
||||
list.extend(arrange::specs());
|
||||
list.extend(structure::specs());
|
||||
list.extend(section::specs());
|
||||
|
||||
@@ -147,7 +147,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
spec!(
|
||||
"variable.bind",
|
||||
"Bind variable",
|
||||
"{ids?, field (fill|stroke|width|height|opacity (percent)|cornerRadius|gap|paddingHorizontal|characters|visible|fontSize…), variable (null unbinds)}",
|
||||
"{ids?, field (fill|stroke (first paint)|fills/<i>/color|strokes/<i>/color|effects/<i>/<offsetX|offsetY|radius|spread|color>|layoutGrids/<i>/<size|count|gutter|offset>|componentProperties/<name> (instances: boolean/text properties)|width|height|opacity (percent)|cornerRadius|gap|paddingHorizontal|characters|visible|fontSize…; list indices bottom first), variable (null unbinds)}",
|
||||
true,
|
||||
bind
|
||||
),
|
||||
@@ -513,6 +513,12 @@ pub fn bind(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Some(v) => Some(doc.variables.get(v).map(|x| x.id.clone()).ok_or_else(|| bad(cmd, "no such variable"))?),
|
||||
None => None,
|
||||
};
|
||||
// An instance's component property takes a variable of its type.
|
||||
if let (Some(name), Some(v)) = (field.strip_prefix(crate::components::PROP_BINDING), vid.as_ref().and_then(|v| doc.variables.get(v))) {
|
||||
for i in &ids {
|
||||
super::component::check_prop_binding(doc, *i, name, v.kind).map_err(|e| bad(cmd, e))?;
|
||||
}
|
||||
}
|
||||
for i in ids {
|
||||
let n = doc.get_mut(i)?;
|
||||
match &vid {
|
||||
@@ -645,6 +651,7 @@ pub fn var_set(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
/// Write bound variable values into nodes (called during normalisation).
|
||||
pub fn apply_bindings(doc: &mut Document) {
|
||||
use super::lists::{BoundList, list_binding};
|
||||
if doc.variables.variables.is_empty() {
|
||||
return;
|
||||
}
|
||||
@@ -655,24 +662,26 @@ pub fn apply_bindings(doc: &mut Document) {
|
||||
let binds = n.bound.clone();
|
||||
let mut sets = serde_json::Map::new();
|
||||
let mut effect_sets: Vec<(usize, String, VariableValue)> = Vec::new();
|
||||
let mut paint_sets: Vec<(bool, Color)> = Vec::new();
|
||||
let mut paint_sets: Vec<(bool, usize, Color)> = Vec::new();
|
||||
let mut grid_sets: Vec<(usize, String, f64)> = Vec::new();
|
||||
let mut prop_sets: Vec<(String, VariableValue)> = Vec::new();
|
||||
for (field, var) in binds {
|
||||
let Some(val) = doc.variables.resolve(&var, &modes) else { continue };
|
||||
// The first fill's or stroke's colour (the rest of the list stays).
|
||||
let paint = match field.as_str() {
|
||||
"fill" | "fills" | "fills/0" | "fills/0/color" => Some(true),
|
||||
"stroke" | "strokes/0/color" => Some(false),
|
||||
_ => None,
|
||||
};
|
||||
if let (Some(fills), VariableValue::Color(c)) = (paint, &val) {
|
||||
paint_sets.push((fills, *c));
|
||||
// One list item's value: a paint's colour, an effect's or a layout grid's field.
|
||||
if let Some((list, i, prop)) = list_binding(&field) {
|
||||
match (list, &val) {
|
||||
(BoundList::Fills | BoundList::Strokes, VariableValue::Color(c)) if prop == "color" => {
|
||||
paint_sets.push((list == BoundList::Fills, i, *c));
|
||||
}
|
||||
(BoundList::Effects, _) => effect_sets.push((i, prop.to_string(), val)),
|
||||
(BoundList::Grids, VariableValue::Number(x)) => grid_sets.push((i, prop.to_string(), *x)),
|
||||
_ => {}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// `effects/<index>/<offsetX|offsetY|radius|spread|color>`: one effect's value.
|
||||
if let Some((i, prop)) = field.strip_prefix("effects/").and_then(|r| r.split_once('/'))
|
||||
&& let Ok(i) = i.parse::<usize>()
|
||||
{
|
||||
effect_sets.push((i, prop.to_string(), val));
|
||||
// `componentProperties/<name>`: an instance's boolean or text property value.
|
||||
if let Some(name) = field.strip_prefix(crate::components::PROP_BINDING) {
|
||||
prop_sets.push((name.to_string(), val));
|
||||
continue;
|
||||
}
|
||||
let jv = match &val {
|
||||
@@ -684,34 +693,38 @@ pub fn apply_bindings(doc: &mut Document) {
|
||||
VariableValue::Boolean(b) => json!(b),
|
||||
VariableValue::Alias(_) => continue,
|
||||
};
|
||||
let key = match field.as_str() {
|
||||
"fill" | "fills" | "fills/0" | "fills/0/color" => "fill",
|
||||
"stroke" | "strokes/0/color" => "stroke",
|
||||
other => other,
|
||||
};
|
||||
sets.insert(key.to_string(), jv);
|
||||
sets.insert(field, jv);
|
||||
}
|
||||
if !sets.is_empty() {
|
||||
let _ = super::props::apply("variables", doc, id, &sets);
|
||||
}
|
||||
let _ = super::props::apply("variables", doc, id, &sets);
|
||||
if !paint_sets.is_empty() {
|
||||
apply_paint_bindings(doc, id, &paint_sets);
|
||||
}
|
||||
if !effect_sets.is_empty() {
|
||||
apply_effect_bindings(doc, id, &effect_sets);
|
||||
}
|
||||
if !grid_sets.is_empty() {
|
||||
super::lists::apply_grid_bindings(doc, id, &grid_sets);
|
||||
}
|
||||
if !prop_sets.is_empty() {
|
||||
super::component::apply_prop_bindings(doc, id, &prop_sets);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Write bound colours into a node's first fill (`true`) or stroke, keeping the rest of the list and
|
||||
/// the paint's opacity, visibility and blend mode (only what changed, so unchanged layers stay
|
||||
/// shared).
|
||||
fn apply_paint_bindings(doc: &mut Document, id: NodeId, sets: &[(bool, Color)]) {
|
||||
/// Write bound colours into a node's fills (`true`) or strokes by index, keeping the rest of the
|
||||
/// list and each paint's opacity, visibility and blend mode (only what changed, so unchanged layers
|
||||
/// stay shared). A first-paint binding on an empty list adds the paint.
|
||||
fn apply_paint_bindings(doc: &mut Document, id: NodeId, sets: &[(bool, usize, Color)]) {
|
||||
let Some(n) = doc.node(id) else { return };
|
||||
let (mut fills, mut strokes) = (n.fills.clone(), n.strokes.clone());
|
||||
for (is_fill, c) in sets {
|
||||
for (is_fill, i, c) in sets {
|
||||
let list = if *is_fill { &mut fills } else { &mut strokes };
|
||||
match list.first_mut() {
|
||||
match list.get_mut(*i) {
|
||||
Some(p) => p.kind = PaintKind::Solid { color: *c },
|
||||
None => list.push(Paint::solid(*c)),
|
||||
None if *i == 0 => list.push(Paint::solid(*c)),
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
if doc.node(id).is_some_and(|n| n.fills != fills || n.strokes != strokes)
|
||||
@@ -722,29 +735,6 @@ fn apply_paint_bindings(doc: &mut Document, id: NodeId, sets: &[(bool, Color)])
|
||||
}
|
||||
}
|
||||
|
||||
/// Keep a node's bindings on its effects' fields (`effects/<index>/<field>`) with their effects
|
||||
/// when the list changes: `map` gives an effect's new index, or None when it was removed.
|
||||
pub(crate) fn remap_effect_bindings(n: &mut Node, map: impl Fn(usize) -> Option<usize>) {
|
||||
let moved: Vec<(String, Option<String>)> = n
|
||||
.bound
|
||||
.keys()
|
||||
.filter_map(|k| {
|
||||
let (i, field) = k.strip_prefix("effects/")?.split_once('/')?;
|
||||
let i = i.parse::<usize>().ok()?;
|
||||
Some((k.clone(), map(i).map(|j| format!("effects/{j}/{field}"))))
|
||||
})
|
||||
.collect();
|
||||
let mut kept = Vec::new();
|
||||
for (old, new) in moved {
|
||||
if let Some(var) = n.bound.remove(&old)
|
||||
&& let Some(new) = new
|
||||
{
|
||||
kept.push((new, var));
|
||||
}
|
||||
}
|
||||
n.bound.extend(kept);
|
||||
}
|
||||
|
||||
/// Write bound values into a node's effects (only what changed, so unchanged layers stay shared).
|
||||
fn apply_effect_bindings(doc: &mut Document, id: NodeId, sets: &[(usize, String, VariableValue)]) {
|
||||
let Some(mut effects) = doc.node(id).map(|n| n.effects.clone()) else { return };
|
||||
|
||||
@@ -6,6 +6,7 @@ use std::collections::BTreeSet;
|
||||
use figcraft_doc::*;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::lists::BoundList;
|
||||
use super::{CommandSpec, ok};
|
||||
use crate::params::*;
|
||||
use crate::{Result, Session, bad, spec};
|
||||
@@ -237,14 +238,17 @@ pub fn field_scope(doc: &Document, node: NodeId, field: &str) -> Option<&'static
|
||||
"characters" => "TEXT_CONTENT",
|
||||
"fontFamily" => "FONT_FAMILY",
|
||||
"fontStyle" => "FONT_STYLE",
|
||||
"stroke" | "strokes/0/color" => "STROKE_COLOR",
|
||||
f if f.starts_with("effects/") && f.ends_with("/color") => "EFFECT_COLOR",
|
||||
f if f.starts_with("effects/") => "EFFECT_FLOAT",
|
||||
"fill" | "fills" | "fills/0" | "fills/0/color" => match doc.node(node).map(|n| &n.kind) {
|
||||
Some(NodeKind::Text(_)) => "TEXT_FILL",
|
||||
Some(k) if k.is_frame_like() => "FRAME_FILL",
|
||||
_ => "SHAPE_FILL",
|
||||
f => match super::lists::list_binding(f)? {
|
||||
(BoundList::Strokes, _, _) => "STROKE_COLOR",
|
||||
(BoundList::Effects, _, "color") => "EFFECT_COLOR",
|
||||
(BoundList::Effects, _, _) => "EFFECT_FLOAT",
|
||||
(BoundList::Fills, _, _) => match doc.node(node).map(|n| &n.kind) {
|
||||
Some(NodeKind::Text(_)) => "TEXT_FILL",
|
||||
Some(k) if k.is_frame_like() => "FRAME_FILL",
|
||||
_ => "SHAPE_FILL",
|
||||
},
|
||||
// Layout grid fields take any number variable.
|
||||
(BoundList::Grids, _, _) => return None,
|
||||
},
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user