@open-pencil/scene-graph 0.17.0-beta.1 → 0.17.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (236) hide show
  1. package/dist/arrow-caps.d.ts.map +1 -1
  2. package/dist/arrow-caps.js +2 -5
  3. package/dist/arrow-caps.js.map +1 -1
  4. package/dist/behaviours/index.d.ts +2 -2
  5. package/dist/behaviours/index.js +2 -2
  6. package/dist/behaviours/kinds.d.ts +11 -5
  7. package/dist/behaviours/kinds.d.ts.map +1 -1
  8. package/dist/behaviours/kinds.js +76 -75
  9. package/dist/behaviours/kinds.js.map +1 -1
  10. package/dist/behaviours/layers.d.ts.map +1 -1
  11. package/dist/behaviours/layers.js +3 -2
  12. package/dist/behaviours/layers.js.map +1 -1
  13. package/dist/behaviours/model.d.ts +7 -4
  14. package/dist/behaviours/model.d.ts.map +1 -1
  15. package/dist/behaviours/model.js +16 -9
  16. package/dist/behaviours/model.js.map +1 -1
  17. package/dist/behaviours/schema.d.ts +118 -4
  18. package/dist/behaviours/schema.d.ts.map +1 -1
  19. package/dist/behaviours/schema.js +18 -3
  20. package/dist/behaviours/schema.js.map +1 -1
  21. package/dist/behaviours/spec.d.ts.map +1 -1
  22. package/dist/behaviours/spec.js +3 -2
  23. package/dist/behaviours/spec.js.map +1 -1
  24. package/dist/bindings.js +9 -1
  25. package/dist/bindings.js.map +1 -1
  26. package/dist/checkpoint.d.ts +31 -2
  27. package/dist/checkpoint.d.ts.map +1 -1
  28. package/dist/checkpoint.js +176 -5
  29. package/dist/checkpoint.js.map +1 -1
  30. package/dist/components/index.d.ts +3 -3
  31. package/dist/components/index.js +3 -3
  32. package/dist/components/properties.d.ts +13 -1
  33. package/dist/components/properties.d.ts.map +1 -1
  34. package/dist/components/properties.js +58 -3
  35. package/dist/components/properties.js.map +1 -1
  36. package/dist/components/variants.d.ts +16 -10
  37. package/dist/components/variants.d.ts.map +1 -1
  38. package/dist/components/variants.js +39 -13
  39. package/dist/components/variants.js.map +1 -1
  40. package/dist/connectors/hit.d.ts.map +1 -1
  41. package/dist/connectors/hit.js +1 -7
  42. package/dist/connectors/hit.js.map +1 -1
  43. package/dist/connectors/pieces.d.ts.map +1 -1
  44. package/dist/connectors/pieces.js +2 -5
  45. package/dist/connectors/pieces.js.map +1 -1
  46. package/dist/copy.d.ts.map +1 -1
  47. package/dist/copy.js +9 -15
  48. package/dist/copy.js.map +1 -1
  49. package/dist/css/blend.d.ts +11 -0
  50. package/dist/css/blend.d.ts.map +1 -0
  51. package/dist/css/blend.js +30 -0
  52. package/dist/css/blend.js.map +1 -0
  53. package/dist/css/index.d.ts +2 -1
  54. package/dist/css/index.js +2 -1
  55. package/dist/cubic.d.ts +7 -0
  56. package/dist/cubic.d.ts.map +1 -0
  57. package/dist/cubic.js +17 -0
  58. package/dist/cubic.js.map +1 -0
  59. package/dist/export-format.d.ts +14 -1
  60. package/dist/export-format.d.ts.map +1 -1
  61. package/dist/export-format.js +26 -1
  62. package/dist/export-format.js.map +1 -1
  63. package/dist/fields/text.d.ts +2 -2
  64. package/dist/fields/text.d.ts.map +1 -1
  65. package/dist/fields/text.js +2 -1
  66. package/dist/fields/text.js.map +1 -1
  67. package/dist/figjam/index.d.ts +2 -2
  68. package/dist/figjam/index.js +2 -2
  69. package/dist/figjam/palette.d.ts +13 -2
  70. package/dist/figjam/palette.d.ts.map +1 -1
  71. package/dist/figjam/palette.js +30 -2
  72. package/dist/figjam/palette.js.map +1 -1
  73. package/dist/generated/index.d.ts +2 -2
  74. package/dist/generated/index.js +2 -2
  75. package/dist/generated/shape-with-text.d.ts +7 -2
  76. package/dist/generated/shape-with-text.d.ts.map +1 -1
  77. package/dist/generated/shape-with-text.js +20 -3
  78. package/dist/generated/shape-with-text.js.map +1 -1
  79. package/dist/geometry.d.ts +30 -46
  80. package/dist/geometry.d.ts.map +1 -1
  81. package/dist/geometry.js +63 -131
  82. package/dist/geometry.js.map +1 -1
  83. package/dist/graph.d.ts +1 -0
  84. package/dist/graph.d.ts.map +1 -1
  85. package/dist/graph.js +15 -3
  86. package/dist/graph.js.map +1 -1
  87. package/dist/guides.d.ts +3 -1
  88. package/dist/guides.d.ts.map +1 -1
  89. package/dist/guides.js +5 -1
  90. package/dist/guides.js.map +1 -1
  91. package/dist/hit-test.d.ts.map +1 -1
  92. package/dist/hit-test.js +42 -29
  93. package/dist/hit-test.js.map +1 -1
  94. package/dist/index.d.ts +2 -2
  95. package/dist/instances/fields.d.ts +2 -2
  96. package/dist/instances/fields.d.ts.map +1 -1
  97. package/dist/instances/fields.js +8 -0
  98. package/dist/instances/fields.js.map +1 -1
  99. package/dist/instances/index.d.ts +1 -3
  100. package/dist/instances/index.d.ts.map +1 -1
  101. package/dist/instances/index.js +36 -30
  102. package/dist/instances/index.js.map +1 -1
  103. package/dist/instances/kept-overrides.js +204 -0
  104. package/dist/instances/kept-overrides.js.map +1 -0
  105. package/dist/instances/main-component.d.ts +3 -1
  106. package/dist/instances/main-component.d.ts.map +1 -1
  107. package/dist/instances/main-component.js +10 -2
  108. package/dist/instances/main-component.js.map +1 -1
  109. package/dist/instances/overrides.d.ts +4 -1
  110. package/dist/instances/overrides.d.ts.map +1 -1
  111. package/dist/instances/overrides.js +18 -2
  112. package/dist/instances/overrides.js.map +1 -1
  113. package/dist/instances/swap.js +288 -0
  114. package/dist/instances/swap.js.map +1 -0
  115. package/dist/instances/sync.js +84 -32
  116. package/dist/instances/sync.js.map +1 -1
  117. package/dist/node-defaults.d.ts.map +1 -1
  118. package/dist/node-defaults.js +1 -1
  119. package/dist/node-defaults.js.map +1 -1
  120. package/dist/outline/flatten.d.ts +15 -0
  121. package/dist/outline/flatten.d.ts.map +1 -0
  122. package/dist/outline/flatten.js +189 -0
  123. package/dist/outline/flatten.js.map +1 -0
  124. package/dist/outline/heads.js +34 -0
  125. package/dist/outline/heads.js.map +1 -0
  126. package/dist/outline/hit.d.ts +21 -0
  127. package/dist/outline/hit.d.ts.map +1 -0
  128. package/dist/outline/hit.js +276 -0
  129. package/dist/outline/hit.js.map +1 -0
  130. package/dist/outline/index.d.ts +5 -0
  131. package/dist/outline/index.js +5 -0
  132. package/dist/outline/point.d.ts +16 -0
  133. package/dist/outline/point.d.ts.map +1 -0
  134. package/dist/outline/point.js +42 -0
  135. package/dist/outline/point.js.map +1 -0
  136. package/dist/outline/shapes.d.ts +35 -0
  137. package/dist/outline/shapes.d.ts.map +1 -0
  138. package/dist/outline/shapes.js +181 -0
  139. package/dist/outline/shapes.js.map +1 -0
  140. package/dist/plugin-data/fields.d.ts +2 -1
  141. package/dist/plugin-data/fields.d.ts.map +1 -1
  142. package/dist/plugin-data/fields.js +19 -4
  143. package/dist/plugin-data/fields.js.map +1 -1
  144. package/dist/scaling/tree.js +1 -1
  145. package/dist/shared-styles.d.ts +14 -1
  146. package/dist/shared-styles.d.ts.map +1 -1
  147. package/dist/shared-styles.js +63 -2
  148. package/dist/shared-styles.js.map +1 -1
  149. package/dist/slots/content.d.ts.map +1 -1
  150. package/dist/slots/content.js +2 -0
  151. package/dist/slots/content.js.map +1 -1
  152. package/dist/slots/frames.d.ts +2 -0
  153. package/dist/slots/frames.d.ts.map +1 -1
  154. package/dist/slots/frames.js +7 -2
  155. package/dist/slots/frames.js.map +1 -1
  156. package/dist/text/index.d.ts +2 -2
  157. package/dist/text/index.js +3 -3
  158. package/dist/text/lengths.d.ts +6 -1
  159. package/dist/text/lengths.d.ts.map +1 -1
  160. package/dist/text/lengths.js +10 -1
  161. package/dist/text/lengths.js.map +1 -1
  162. package/dist/text/overflow.d.ts +2 -2
  163. package/dist/text/overflow.js.map +1 -1
  164. package/dist/text/picture.d.ts +1 -2
  165. package/dist/text/picture.d.ts.map +1 -1
  166. package/dist/text/picture.js +1 -4
  167. package/dist/text/picture.js.map +1 -1
  168. package/dist/transfer/index.js +1 -1
  169. package/dist/transfer/nodes.js +9 -5
  170. package/dist/transfer/nodes.js.map +1 -1
  171. package/dist/types.d.ts +27 -1
  172. package/dist/types.d.ts.map +1 -1
  173. package/dist/types.js.map +1 -1
  174. package/dist/variables/bindings.d.ts +28 -5
  175. package/dist/variables/bindings.d.ts.map +1 -1
  176. package/dist/variables/bindings.js +104 -14
  177. package/dist/variables/bindings.js.map +1 -1
  178. package/dist/variables/fields.d.ts +19 -1
  179. package/dist/variables/fields.d.ts.map +1 -1
  180. package/dist/variables/fields.js +36 -1
  181. package/dist/variables/fields.js.map +1 -1
  182. package/dist/variables/index.d.ts +3 -3
  183. package/dist/variables/index.d.ts.map +1 -1
  184. package/dist/variables/index.js +23 -8
  185. package/dist/variables/index.js.map +1 -1
  186. package/package.json +7 -1
  187. package/src/arrow-caps.ts +2 -5
  188. package/src/behaviours/kinds.ts +51 -34
  189. package/src/behaviours/layers.ts +4 -2
  190. package/src/behaviours/model.ts +22 -11
  191. package/src/behaviours/schema.ts +20 -4
  192. package/src/behaviours/spec.ts +8 -2
  193. package/src/bindings.ts +11 -2
  194. package/src/checkpoint.ts +251 -4
  195. package/src/components/properties.ts +92 -2
  196. package/src/components/variants.ts +51 -30
  197. package/src/connectors/hit.ts +1 -12
  198. package/src/connectors/pieces.ts +2 -5
  199. package/src/copy.ts +9 -15
  200. package/src/css/blend.ts +27 -0
  201. package/src/css/index.ts +1 -0
  202. package/src/cubic.ts +14 -0
  203. package/src/export-format.ts +29 -0
  204. package/src/fields/text.ts +3 -1
  205. package/src/figjam/palette.ts +29 -1
  206. package/src/generated/shape-with-text.ts +28 -2
  207. package/src/geometry.ts +78 -198
  208. package/src/graph.ts +27 -3
  209. package/src/guides.ts +5 -0
  210. package/src/hit-test.ts +67 -54
  211. package/src/instances/fields.ts +8 -0
  212. package/src/instances/index.ts +58 -31
  213. package/src/instances/kept-overrides.ts +280 -0
  214. package/src/instances/main-component.ts +12 -0
  215. package/src/instances/overrides.ts +27 -1
  216. package/src/instances/swap.ts +451 -0
  217. package/src/instances/sync.ts +141 -42
  218. package/src/node-defaults.ts +2 -1
  219. package/src/outline/flatten.ts +190 -0
  220. package/src/outline/heads.ts +52 -0
  221. package/src/outline/hit.ts +369 -0
  222. package/src/outline/index.ts +4 -0
  223. package/src/outline/point.ts +59 -0
  224. package/src/outline/shapes.ts +207 -0
  225. package/src/plugin-data/fields.ts +15 -2
  226. package/src/shared-styles.ts +82 -1
  227. package/src/slots/content.ts +2 -0
  228. package/src/slots/frames.ts +9 -7
  229. package/src/text/lengths.ts +10 -0
  230. package/src/text/overflow.ts +2 -2
  231. package/src/text/picture.ts +0 -4
  232. package/src/transfer/nodes.ts +7 -5
  233. package/src/types.ts +28 -0
  234. package/src/variables/bindings.ts +125 -14
  235. package/src/variables/fields.ts +46 -1
  236. package/src/variables/index.ts +35 -2
package/src/arrow-caps.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { cubicPoint } from './cubic'
1
2
  import type { Vector } from './primitives'
2
3
  import type { Stroke, StrokeCap, VectorNetwork } from './types'
3
4
 
@@ -256,11 +257,7 @@ export function trimArrowShafts(
256
257
  type Cubic = [Vector, Vector, Vector, Vector]
257
258
 
258
259
  function cubicAt([a, b, c, d]: Cubic, t: number): Vector {
259
- const u = 1 - t
260
- return {
261
- x: u * u * u * a.x + 3 * u * u * t * b.x + 3 * u * t * t * c.x + t * t * t * d.x,
262
- y: u * u * u * a.y + 3 * u * u * t * b.y + 3 * u * t * t * c.y + t * t * t * d.y
263
- }
260
+ return cubicPoint(a, b, c, d, t)
264
261
  }
265
262
 
266
263
  function lerp(a: Vector, b: Vector, t: number): Vector {
@@ -13,6 +13,7 @@ export const BEHAVIOUR_KINDS = [
13
13
  'slider',
14
14
  'progress',
15
15
  'tabs',
16
+ 'tab',
16
17
  'collapsible',
17
18
  'accordion'
18
19
  ] as const
@@ -21,9 +22,17 @@ export type BehaviourKind = (typeof BEHAVIOUR_KINDS)[number]
21
22
 
22
23
  /**
23
24
  * The interaction states a control can show, each drawn as a value of one variant property:
24
- * at rest, under the pointer, while pressed, focused from the keyboard, and disabled.
25
+ * at rest, under the pointer, while pressed, focused from the keyboard, disabled, and invalid. Only
26
+ * a form control has an invalid state (`hasInvalidState`), shown while its value fails validation.
25
27
  */
26
- export const INTERACTION_STATES = ['rest', 'hover', 'pressed', 'focus', 'disabled'] as const
28
+ export const INTERACTION_STATES = [
29
+ 'rest',
30
+ 'hover',
31
+ 'pressed',
32
+ 'focus',
33
+ 'disabled',
34
+ 'invalid'
35
+ ] as const
27
36
 
28
37
  export type InteractionState = (typeof INTERACTION_STATES)[number]
29
38
 
@@ -52,7 +61,8 @@ export interface BehaviourPartContract {
52
61
  required: boolean
53
62
  /**
54
63
  * Whether the slot holds the control's items, each an instance with its own behaviour (a
55
- * radio, a toggle, a collapsible), which the control turns on and off.
64
+ * radio, a toggle, a collapsible, a tab), which the control turns on and off. A tab list's
65
+ * triggers may also be plain layers, which have no state of their own.
56
66
  */
57
67
  items?: boolean
58
68
  /**
@@ -67,6 +77,11 @@ export interface BehaviourContract {
67
77
  parts: BehaviourPartContract[]
68
78
  }
69
79
 
80
+ const DISABLED: BehaviourValueContract = { id: 'disabled', type: 'boolean', required: false }
81
+
82
+ /** Whether a form control's value fails validation, which code marks with `aria-invalid`. */
83
+ const INVALID: BehaviourValueContract = { id: 'invalid', type: 'boolean', required: false }
84
+
70
85
  /**
71
86
  * A text field or textarea: its text is a text property, and `filled`, when bound, draws the
72
87
  * empty field with its placeholder.
@@ -75,7 +90,8 @@ const TEXT_INPUT: BehaviourContract = {
75
90
  values: [
76
91
  { id: 'value', type: 'text', required: true },
77
92
  { id: 'filled', type: 'boolean', required: false },
78
- { id: 'disabled', type: 'boolean', required: false }
93
+ DISABLED,
94
+ INVALID
79
95
  ],
80
96
  parts: []
81
97
  }
@@ -87,7 +103,7 @@ const TEXT_INPUT: BehaviourContract = {
87
103
  */
88
104
  export const BEHAVIOUR_CONTRACTS: Readonly<Record<BehaviourKind, BehaviourContract>> = {
89
105
  button: {
90
- values: [{ id: 'disabled', type: 'boolean', required: false }],
106
+ values: [DISABLED],
91
107
  parts: []
92
108
  },
93
109
  textField: TEXT_INPUT,
@@ -96,7 +112,8 @@ export const BEHAVIOUR_CONTRACTS: Readonly<Record<BehaviourKind, BehaviourContra
96
112
  values: [
97
113
  { id: 'value', type: 'number', required: false },
98
114
  { id: 'text', type: 'text', required: true },
99
- { id: 'disabled', type: 'boolean', required: false }
115
+ DISABLED,
116
+ INVALID
100
117
  ],
101
118
  parts: [
102
119
  { id: 'increment', required: false },
@@ -104,46 +121,31 @@ export const BEHAVIOUR_CONTRACTS: Readonly<Record<BehaviourKind, BehaviourContra
104
121
  ]
105
122
  },
106
123
  toggle: {
107
- values: [
108
- { id: 'value', type: 'boolean', required: true },
109
- { id: 'disabled', type: 'boolean', required: false }
110
- ],
124
+ values: [{ id: 'value', type: 'boolean', required: true }, DISABLED],
111
125
  parts: []
112
126
  },
113
127
  switch: {
114
- values: [
115
- { id: 'value', type: 'boolean', required: true },
116
- { id: 'disabled', type: 'boolean', required: false }
117
- ],
128
+ values: [{ id: 'value', type: 'boolean', required: true }, DISABLED, INVALID],
118
129
  parts: [{ id: 'thumb', required: false }]
119
130
  },
120
131
  checkbox: {
121
- values: [
122
- { id: 'value', type: 'boolean', required: true },
123
- { id: 'disabled', type: 'boolean', required: false }
124
- ],
132
+ values: [{ id: 'value', type: 'boolean', required: true }, DISABLED, INVALID],
125
133
  parts: [{ id: 'indicator', required: false }]
126
134
  },
127
135
  radio: {
128
- values: [
129
- { id: 'value', type: 'boolean', required: true },
130
- { id: 'disabled', type: 'boolean', required: false }
131
- ],
136
+ values: [{ id: 'value', type: 'boolean', required: true }, DISABLED, INVALID],
132
137
  parts: [{ id: 'indicator', required: false }]
133
138
  },
134
139
  radioGroup: {
135
- values: [{ id: 'disabled', type: 'boolean', required: false }],
140
+ values: [DISABLED, INVALID],
136
141
  parts: [{ id: 'items', required: true, items: true, children: 'item' }]
137
142
  },
138
143
  toggleGroup: {
139
- values: [{ id: 'disabled', type: 'boolean', required: false }],
144
+ values: [DISABLED],
140
145
  parts: [{ id: 'items', required: true, items: true, children: 'item' }]
141
146
  },
142
147
  slider: {
143
- values: [
144
- { id: 'value', type: 'number', required: false },
145
- { id: 'disabled', type: 'boolean', required: false }
146
- ],
148
+ values: [{ id: 'value', type: 'number', required: false }, DISABLED],
147
149
  parts: [
148
150
  { id: 'track', required: true },
149
151
  { id: 'range', required: false },
@@ -153,10 +155,18 @@ export const BEHAVIOUR_CONTRACTS: Readonly<Record<BehaviourKind, BehaviourContra
153
155
  tabs: {
154
156
  values: [{ id: 'value', type: 'choice', required: false }],
155
157
  parts: [
156
- { id: 'list', required: true, children: 'trigger' },
158
+ { id: 'list', required: true, items: true, children: 'trigger' },
157
159
  { id: 'panels', required: false, children: 'content' }
158
160
  ]
159
161
  },
162
+ // A tab list's trigger, after Reka's TabsTrigger: on while its tab is the chosen one.
163
+ tab: {
164
+ values: [
165
+ { id: 'value', type: 'boolean', required: true },
166
+ { id: 'disabled', type: 'boolean', required: false }
167
+ ],
168
+ parts: []
169
+ },
160
170
  progress: {
161
171
  values: [{ id: 'value', type: 'number', required: false }],
162
172
  parts: [
@@ -165,17 +175,14 @@ export const BEHAVIOUR_CONTRACTS: Readonly<Record<BehaviourKind, BehaviourContra
165
175
  ]
166
176
  },
167
177
  collapsible: {
168
- values: [
169
- { id: 'open', type: 'boolean', required: false },
170
- { id: 'disabled', type: 'boolean', required: false }
171
- ],
178
+ values: [{ id: 'open', type: 'boolean', required: false }, DISABLED],
172
179
  parts: [
173
180
  { id: 'trigger', required: true },
174
181
  { id: 'content', required: true }
175
182
  ]
176
183
  },
177
184
  accordion: {
178
- values: [{ id: 'disabled', type: 'boolean', required: false }],
185
+ values: [DISABLED],
179
186
  parts: [{ id: 'items', required: true, items: true, children: 'item' }]
180
187
  }
181
188
  }
@@ -183,3 +190,13 @@ export const BEHAVIOUR_CONTRACTS: Readonly<Record<BehaviourKind, BehaviourContra
183
190
  export function behaviourContract(kind: BehaviourKind): BehaviourContract {
184
191
  return BEHAVIOUR_CONTRACTS[kind]
185
192
  }
193
+
194
+ /** Whether a kind of control can be invalid: a form control, whose contract has `invalid`. */
195
+ export function hasInvalidState(kind: BehaviourKind): boolean {
196
+ return BEHAVIOUR_CONTRACTS[kind].values.some((value) => value.id === 'invalid')
197
+ }
198
+
199
+ /** The interaction states a kind of control can show. */
200
+ export function interactionStatesOf(kind: BehaviourKind): InteractionState[] {
201
+ return INTERACTION_STATES.filter((state) => state !== 'invalid' || hasInvalidState(kind))
202
+ }
@@ -1,11 +1,13 @@
1
1
  import type { SceneGraph } from '../graph'
2
+ import { instanceMainComponent } from '../instances/main-component'
2
3
  import type { SceneNode } from '../types'
3
4
  import { behaviourOwner, readBehaviour } from './model'
4
5
 
5
6
  /** Whether an instance's main component, or its set, has a behaviour. */
6
7
  export function hasBehaviour(graph: SceneGraph, node: SceneNode): boolean {
7
- if (node.type !== 'INSTANCE' || !node.componentId) return false
8
- const component = graph.getNode(node.componentId)
8
+ if (node.type !== 'INSTANCE') return false
9
+ // A copy of a nested instance names the instance it copies, so follow it to the component.
10
+ const component = instanceMainComponent(graph, node)
9
11
  const owner = component && behaviourOwner(graph, component)
10
12
  return !!owner && !!readBehaviour(owner)
11
13
  }
@@ -1,12 +1,13 @@
1
1
  import { mapValues } from 'es-toolkit'
2
2
 
3
+ import { resolvedVariantDefinition } from '../components/variants'
3
4
  import type { SceneGraph } from '../graph'
4
5
  import { readPluginData, withPluginData } from '../plugin-data/field'
5
6
  import { OPEN_PENCIL_PLUGIN_DATA } from '../plugin-data/fields'
6
7
  import type { ComponentPropertyDefinition, SceneNode } from '../types'
7
8
  import {
8
9
  behaviourContract,
9
- INTERACTION_STATES,
10
+ interactionStatesOf,
10
11
  type BehaviourKind,
11
12
  type InteractionState
12
13
  } from './kinds'
@@ -85,17 +86,25 @@ export function withBehaviour(
85
86
  return withPluginData(owner.pluginData, OPEN_PENCIL_PLUGIN_DATA.behaviour, behaviour ?? undefined)
86
87
  }
87
88
 
88
- /** Component properties the behaviour can bind to: the owner's, and a set's variants' own. */
89
+ /**
90
+ * Component properties the behaviour can bind to: the owner's, with a set's variant values and
91
+ * defaults read from its variants, and a set's variants' own.
92
+ */
89
93
  export function behaviourProperties(
90
94
  graph: SceneGraph,
91
95
  owner: SceneNode
92
96
  ): ComponentPropertyDefinition[] {
93
- const variants =
94
- owner.type === 'COMPONENT_SET'
95
- ? owner.childIds.flatMap((id) => graph.getNode(id)?.componentPropertyDefinitions ?? [])
96
- : []
97
+ const isSet = owner.type === 'COMPONENT_SET'
98
+ const own = isSet
99
+ ? owner.componentPropertyDefinitions.map((definition) =>
100
+ resolvedVariantDefinition(graph, owner, definition)
101
+ )
102
+ : owner.componentPropertyDefinitions
103
+ const variants = isSet
104
+ ? owner.childIds.flatMap((id) => graph.getNode(id)?.componentPropertyDefinitions ?? [])
105
+ : []
97
106
  const seen = new Set<string>()
98
- return [...owner.componentPropertyDefinitions, ...variants].filter((definition) => {
107
+ return [...own, ...variants].filter((definition) => {
99
108
  if (seen.has(definition.id)) return false
100
109
  seen.add(definition.id)
101
110
  return true
@@ -142,19 +151,21 @@ const STATE_NAMES: Record<InteractionState, readonly string[]> = {
142
151
  hover: ['hover', 'hovered', 'hovering'],
143
152
  pressed: ['pressed', 'active', 'down', 'pressing'],
144
153
  focus: ['focus', 'focused', 'focus visible', 'focus-visible'],
145
- disabled: ['disabled', 'inactive']
154
+ disabled: ['disabled', 'inactive'],
155
+ invalid: ['invalid', 'error']
146
156
  }
147
157
 
148
158
  /**
149
- * Interaction states for a variant property, each mapped to the value whose name means it, such
150
- * as `Hover` or `Pressed`; states without such a value are left unset.
159
+ * A kind of control's interaction states for a variant property, each mapped to the value whose
160
+ * name means it, such as `Hover` or `Pressed`; states without such a value are left unset.
151
161
  */
152
162
  export function guessInteractionStates(
163
+ kind: BehaviourKind,
153
164
  propertyId: string,
154
165
  values: readonly string[]
155
166
  ): BehaviourInteractionStates {
156
167
  const states: BehaviourInteractionStates = { propertyId }
157
- for (const state of INTERACTION_STATES) {
168
+ for (const state of interactionStatesOf(kind)) {
158
169
  const match = values.find((value) => STATE_NAMES[state].includes(value.trim().toLowerCase()))
159
170
  if (match) states[state] = match
160
171
  }
@@ -1,6 +1,6 @@
1
1
  import * as v from 'valibot'
2
2
 
3
- import { BEHAVIOUR_KINDS, type InteractionState } from './kinds'
3
+ import { BEHAVIOUR_KINDS, hasInvalidState, type InteractionState } from './kinds'
4
4
 
5
5
  const booleanBindingSchema = v.object({
6
6
  propertyId: v.string(),
@@ -33,12 +33,13 @@ export const interactionStateValues = {
33
33
  hover: v.optional(v.string()),
34
34
  pressed: v.optional(v.string()),
35
35
  focus: v.optional(v.string()),
36
- disabled: v.optional(v.string())
36
+ disabled: v.optional(v.string()),
37
+ invalid: v.optional(v.string())
37
38
  } satisfies Record<InteractionState, unknown>
38
39
 
39
40
  const interactionStatesSchema = v.object({ propertyId: v.string(), ...interactionStateValues })
40
41
 
41
- export const behaviourSchema = v.object({
42
+ const storedBehaviourSchema = v.object({
42
43
  kind: v.picklist(BEHAVIOUR_KINDS),
43
44
  /** Boolean values, by value id, bound to component properties. */
44
45
  booleans: v.record(v.string(), booleanBindingSchema),
@@ -51,8 +52,23 @@ export const behaviourSchema = v.object({
51
52
  states: v.optional(interactionStatesSchema)
52
53
  })
53
54
 
55
+ type StoredBehaviour = v.InferOutput<typeof storedBehaviourSchema>
56
+
57
+ /** The behaviour without an invalid state when its kind of control has none. */
58
+ function withoutUnsupportedStates(behaviour: StoredBehaviour): StoredBehaviour {
59
+ if (behaviour.states?.invalid === undefined || hasInvalidState(behaviour.kind)) return behaviour
60
+ const { invalid: _invalid, ...states } = behaviour.states
61
+ return { ...behaviour, states }
62
+ }
63
+
64
+ /**
65
+ * A stored behaviour. An invalid state on a kind of control that has none, such as a button,
66
+ * is dropped as it is read, so nothing draws or exports it; the rest of the behaviour stays.
67
+ */
68
+ export const behaviourSchema = v.pipe(storedBehaviourSchema, v.transform(withoutUnsupportedStates))
69
+
54
70
  /** How a main component behaves as a control, as kept in its plugin data. */
55
- export type Behaviour = v.InferOutput<typeof behaviourSchema>
71
+ export type Behaviour = StoredBehaviour
56
72
  export type BehaviourBooleanBinding = v.InferOutput<typeof booleanBindingSchema>
57
73
  export type BehaviourNumberSettings = v.InferOutput<typeof numberSettingsSchema>
58
74
  export type BehaviourInteractionStates = v.InferOutput<typeof interactionStatesSchema>
@@ -2,7 +2,7 @@ import { omitBy } from 'es-toolkit/object'
2
2
 
3
3
  import type { SceneGraph } from '../graph'
4
4
  import type { ComponentPropertyDefinition, SceneNode } from '../types'
5
- import { behaviourContract } from './kinds'
5
+ import { behaviourContract, hasInvalidState } from './kinds'
6
6
  import { behaviourProperties, emptyBehaviour, guessInteractionStates, textBinding } from './model'
7
7
  import { isNumberRange, type Behaviour, type BehaviourSpec } from './schema'
8
8
 
@@ -90,12 +90,18 @@ function resolveStates(
90
90
  ): void {
91
91
  const name = typeof states === 'string' ? states : states.property
92
92
  const definition = findProperty(properties, name, ['VARIANT'], 'states')
93
- const guessed = guessInteractionStates(definition.id, definition.variantOptions ?? [])
93
+ const guessed = guessInteractionStates(
94
+ behaviour.kind,
95
+ definition.id,
96
+ definition.variantOptions ?? []
97
+ )
94
98
  if (typeof states === 'string') {
95
99
  behaviour.states = guessed
96
100
  return
97
101
  }
98
102
  const { property: _property, ...values } = states
103
+ if (values.invalid !== undefined && !hasInvalidState(behaviour.kind))
104
+ throw new Error(`A ${behaviour.kind} has no invalid state`)
99
105
  behaviour.states = { ...guessed, ...stripUndefined(values) }
100
106
  }
101
107
 
package/src/bindings.ts CHANGED
@@ -1,10 +1,18 @@
1
1
  import { omit } from 'es-toolkit/object'
2
2
 
3
3
  import type { SceneNode } from './types'
4
+ import { effectBindableProperties, effectBindingTarget } from './variables/fields'
5
+
6
+ /** Whether an effect binding names a property its effect still has: a blur binds only radius. */
7
+ function bindsEffectProperty(node: SceneNode, key: string): boolean {
8
+ const target = effectBindingTarget(key)
9
+ const effect = target && node.effects.at(target.index)
10
+ return !effect || effectBindableProperties(effect.type).includes(target.property)
11
+ }
4
12
 
5
13
  export function removeStaleBindings(
6
14
  node: SceneNode,
7
- field: 'fills' | 'strokes',
15
+ field: 'fills' | 'strokes' | 'effects',
8
16
  changes: Partial<SceneNode>
9
17
  ): void {
10
18
  const length = node[field].length
@@ -12,7 +20,8 @@ export function removeStaleBindings(
12
20
  if (key === field) return true
13
21
  if (!key.startsWith(`${field}/`)) return false
14
22
  const index = Number.parseInt(key.split('/')[1] ?? '', 10)
15
- return Number.isNaN(index) || index < 0 || index >= length
23
+ if (Number.isNaN(index) || index < 0 || index >= length) return true
24
+ return field === 'effects' && !bindsEffectProperty(node, key)
16
25
  })
17
26
  if (stale.length === 0) return
18
27
  node.boundVariables = omit(node.boundVariables, stale)