1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//! # `canvist_scene`
//!
//! The scene model behind canvist's canvas: the design-tool sibling of
//! [`canvist_model`]. Where a canvist document is a tree of blocks, a scene
//! is a tree of nodes: pages, frames, groups, shapes, vector paths, images,
//! components, instances, and text frames that each hold a full canvist
//! text document.
//!
//! - A [`SceneSchema`] declares which node types exist. Several types can
//! share one built-in [`NodeKind`], so a plugin's `sticky` can be a frame
//! and its `star` a polygon, and [`NodeKind::Custom`] types draw their own
//! geometry.
//! - A [`Scene`] stores [`Node`]s by stable [`NodeId`]. Each node has a type,
//! a parent, a [`FracKey`] ordering it among its siblings, and [`Props`]:
//! typed core properties plus custom values.
//! - [`Step`]s are the only way to change a scene. Each is invertible and
//! maps one-to-one onto a Yjs operation, so undo, collaboration, and
//! rendering share one pipeline, exactly as in the text model.
//! - A [`Transaction`] applies steps atomically and returns a
//! [`SceneCommit`] naming the touched nodes, which is what caches use to
//! update only what changed.
//!
//! # Example
//!
//! ```
//! use std::sync::Arc;
//!
//! use canvist_model::BlockSpec;
//! use canvist_model::MarkSet;
//! use canvist_model::Point;
//! use canvist_model::Schema;
//! use canvist_scene::Color;
//! use canvist_scene::NodeId;
//! use canvist_scene::Paint;
//! use canvist_scene::Prop;
//! use canvist_scene::Props;
//! use canvist_scene::Scene;
//! use canvist_scene::SceneSchema;
//! use kurbo::Affine;
//! use kurbo::Vec2;
//!
//! let text = Arc::new(Schema::builder().block(BlockSpec::text("paragraph")).build()?);
//! let schema = Arc::new(SceneSchema::standard(text)?);
//! let mut scene = Scene::new(schema, 1)?;
//! let page = scene.pages()[0];
//!
//! let mut transaction = scene.transaction();
//! let card = transaction.insert(page, None, "frame", Props {
//! size: Vec2::new(320.0, 200.0),
//! fills: Arc::new([Paint::solid(Color::WHITE)]),
//! ..Props::default()
//! })?;
//! let title = transaction.insert(card, None, "text", Props {
//! transform: Affine::translate((16.0, 16.0)),
//! size: Vec2::new(288.0, 0.0),
//! ..Props::default()
//! })?;
//! transaction.text(title, |text| {
//! let block = text.doc().first_text_block().expect("text frames start with a paragraph");
//! text.insert_text(Point::new(block, 0), "Hello, canvas", MarkSet::empty())
//! })?;
//! transaction.set(card, Prop::Opacity(0.9))?;
//! let commit = transaction.commit();
//!
//! assert_eq!(scene.text(title).map(|doc| doc.plain_text()).as_deref(), Some("Hello, canvas"));
//!
//! // Every commit carries its own undo.
//! scene.apply_steps(commit.undo_steps())?;
//! assert!(!scene.contains(card));
//! assert_eq!(scene.children(page), &[] as &[NodeId]);
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! # The Yjs shape
//!
//! `canvist_collab`'s scene binding stores a scene as one Yjs document:
//!
//! - The root `Y.Map` named `"nodes"` maps each node id (`client.seq` in base
//! 36) to a node `Y.Map`. The tree is flat: nodes point at their parents.
//! - A node map holds `"type"` (the type name), `"pos"` (one atomic value
//! holding the parent, the key, and the data that breaks move cycles),
//! `"props"` (a `Y.Map` from property name to its JSON value, one
//! last-writer-wins register each), and, for text frames, `"text"`: a
//! `Y.Array` of blocks in `canvist_collab`'s document shape.
//!
//! Each step is one operation on that shape: [`Step::InsertNodes`] and
//! [`Step::RemoveNodes`] set and delete node maps, [`Step::Move`] sets
//! `"pos"`, [`Step::SetProp`] sets one key of `"props"`, and [`Step::Text`]
//! is the text binding's own translation inside `"text"`.
//!
//! # Properties
//!
//! Properties are named in JSON and Yjs by [`PropKey`]'s names: `name`,
//! `visible`, `locked`, `transform` (`[a, b, c, d, e, f]`, kurbo's affine
//! coefficients), `size` (`[w, h]`), `opacity`, `blend` (CSS blend mode
//! names), `fills` and `strokes` (lists of paints such as
//! `{ "type": "solid", "color": "#3b2cf5ff" }`), `stroke`, `effects`,
//! `radius` (`[tl, tr, br, bl]`), `clip`, `constraints`, `layout`,
//! `layoutChild`, and `shape` (for example
//! `{ "type": "path", "d": "M0 0L10 10" }`). Custom values use `c:<name>`
//! and instance overrides `o:<component node id>:<property>`.
pub use SceneError;
pub use NodeId;
pub use NodeIdGenerator;
pub use ParseNodeIdError;
pub use SCENE_TYPE;
pub use SCENE_VERSION;
pub use props_to_json;
pub use FracKey;
pub use KeyError;
pub use kurbo;
pub use Align;
pub use AutoLayout;
pub use Constraint;
pub use Constraints;
pub use Direction;
pub use LayoutChild;
pub use Padding;
pub use Sizing;
pub use Node;
pub use NodeRecord;
pub use BlendMode;
pub use Color;
pub use Effect;
pub use EffectKind;
pub use GradientStop;
pub use ImageFit;
pub use Paint;
pub use PaintKind;
pub use Shadow;
pub use StrokeAlign;
pub use StrokeCap;
pub use StrokeJoin;
pub use StrokeStyle;
pub use Prop;
pub use PropKey;
pub use Props;
pub use Scene;
pub use GeometryFn;
pub use NodeKind;
pub use NodeSpec;
pub use NodeTypeId;
pub use SceneSchema;
pub use SceneSchemaBuilder;
pub use SceneSchemaError;
pub use BoolOp;
pub use FillRule;
pub use Shape;
pub use TextSizing;
pub use Applied;
pub use Step;
pub use SceneCommit;
pub use Transaction;