@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.
- package/dist/arrow-caps.d.ts.map +1 -1
- package/dist/arrow-caps.js +2 -5
- package/dist/arrow-caps.js.map +1 -1
- package/dist/behaviours/index.d.ts +2 -2
- package/dist/behaviours/index.js +2 -2
- package/dist/behaviours/kinds.d.ts +11 -5
- package/dist/behaviours/kinds.d.ts.map +1 -1
- package/dist/behaviours/kinds.js +76 -75
- package/dist/behaviours/kinds.js.map +1 -1
- package/dist/behaviours/layers.d.ts.map +1 -1
- package/dist/behaviours/layers.js +3 -2
- package/dist/behaviours/layers.js.map +1 -1
- package/dist/behaviours/model.d.ts +7 -4
- package/dist/behaviours/model.d.ts.map +1 -1
- package/dist/behaviours/model.js +16 -9
- package/dist/behaviours/model.js.map +1 -1
- package/dist/behaviours/schema.d.ts +118 -4
- package/dist/behaviours/schema.d.ts.map +1 -1
- package/dist/behaviours/schema.js +18 -3
- package/dist/behaviours/schema.js.map +1 -1
- package/dist/behaviours/spec.d.ts.map +1 -1
- package/dist/behaviours/spec.js +3 -2
- package/dist/behaviours/spec.js.map +1 -1
- package/dist/bindings.js +9 -1
- package/dist/bindings.js.map +1 -1
- package/dist/checkpoint.d.ts +31 -2
- package/dist/checkpoint.d.ts.map +1 -1
- package/dist/checkpoint.js +176 -5
- package/dist/checkpoint.js.map +1 -1
- package/dist/components/index.d.ts +3 -3
- package/dist/components/index.js +3 -3
- package/dist/components/properties.d.ts +13 -1
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +58 -3
- package/dist/components/properties.js.map +1 -1
- package/dist/components/variants.d.ts +16 -10
- package/dist/components/variants.d.ts.map +1 -1
- package/dist/components/variants.js +39 -13
- package/dist/components/variants.js.map +1 -1
- package/dist/connectors/hit.d.ts.map +1 -1
- package/dist/connectors/hit.js +1 -7
- package/dist/connectors/hit.js.map +1 -1
- package/dist/connectors/pieces.d.ts.map +1 -1
- package/dist/connectors/pieces.js +2 -5
- package/dist/connectors/pieces.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +9 -15
- package/dist/copy.js.map +1 -1
- package/dist/css/blend.d.ts +11 -0
- package/dist/css/blend.d.ts.map +1 -0
- package/dist/css/blend.js +30 -0
- package/dist/css/blend.js.map +1 -0
- package/dist/css/index.d.ts +2 -1
- package/dist/css/index.js +2 -1
- package/dist/cubic.d.ts +7 -0
- package/dist/cubic.d.ts.map +1 -0
- package/dist/cubic.js +17 -0
- package/dist/cubic.js.map +1 -0
- package/dist/export-format.d.ts +14 -1
- package/dist/export-format.d.ts.map +1 -1
- package/dist/export-format.js +26 -1
- package/dist/export-format.js.map +1 -1
- package/dist/fields/text.d.ts +2 -2
- package/dist/fields/text.d.ts.map +1 -1
- package/dist/fields/text.js +2 -1
- package/dist/fields/text.js.map +1 -1
- package/dist/figjam/index.d.ts +2 -2
- package/dist/figjam/index.js +2 -2
- package/dist/figjam/palette.d.ts +13 -2
- package/dist/figjam/palette.d.ts.map +1 -1
- package/dist/figjam/palette.js +30 -2
- package/dist/figjam/palette.js.map +1 -1
- package/dist/generated/index.d.ts +2 -2
- package/dist/generated/index.js +2 -2
- package/dist/generated/shape-with-text.d.ts +7 -2
- package/dist/generated/shape-with-text.d.ts.map +1 -1
- package/dist/generated/shape-with-text.js +20 -3
- package/dist/generated/shape-with-text.js.map +1 -1
- package/dist/geometry.d.ts +30 -46
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +63 -131
- package/dist/geometry.js.map +1 -1
- package/dist/graph.d.ts +1 -0
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +15 -3
- package/dist/graph.js.map +1 -1
- package/dist/guides.d.ts +3 -1
- package/dist/guides.d.ts.map +1 -1
- package/dist/guides.js +5 -1
- package/dist/guides.js.map +1 -1
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +42 -29
- package/dist/hit-test.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/fields.d.ts +2 -2
- package/dist/instances/fields.d.ts.map +1 -1
- package/dist/instances/fields.js +8 -0
- package/dist/instances/fields.js.map +1 -1
- package/dist/instances/index.d.ts +1 -3
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +36 -30
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/kept-overrides.js +204 -0
- package/dist/instances/kept-overrides.js.map +1 -0
- package/dist/instances/main-component.d.ts +3 -1
- package/dist/instances/main-component.d.ts.map +1 -1
- package/dist/instances/main-component.js +10 -2
- package/dist/instances/main-component.js.map +1 -1
- package/dist/instances/overrides.d.ts +4 -1
- package/dist/instances/overrides.d.ts.map +1 -1
- package/dist/instances/overrides.js +18 -2
- package/dist/instances/overrides.js.map +1 -1
- package/dist/instances/swap.js +288 -0
- package/dist/instances/swap.js.map +1 -0
- package/dist/instances/sync.js +84 -32
- package/dist/instances/sync.js.map +1 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +1 -1
- package/dist/node-defaults.js.map +1 -1
- package/dist/outline/flatten.d.ts +15 -0
- package/dist/outline/flatten.d.ts.map +1 -0
- package/dist/outline/flatten.js +189 -0
- package/dist/outline/flatten.js.map +1 -0
- package/dist/outline/heads.js +34 -0
- package/dist/outline/heads.js.map +1 -0
- package/dist/outline/hit.d.ts +21 -0
- package/dist/outline/hit.d.ts.map +1 -0
- package/dist/outline/hit.js +276 -0
- package/dist/outline/hit.js.map +1 -0
- package/dist/outline/index.d.ts +5 -0
- package/dist/outline/index.js +5 -0
- package/dist/outline/point.d.ts +16 -0
- package/dist/outline/point.d.ts.map +1 -0
- package/dist/outline/point.js +42 -0
- package/dist/outline/point.js.map +1 -0
- package/dist/outline/shapes.d.ts +35 -0
- package/dist/outline/shapes.d.ts.map +1 -0
- package/dist/outline/shapes.js +181 -0
- package/dist/outline/shapes.js.map +1 -0
- package/dist/plugin-data/fields.d.ts +2 -1
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +19 -4
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/scaling/tree.js +1 -1
- package/dist/shared-styles.d.ts +14 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +63 -2
- package/dist/shared-styles.js.map +1 -1
- package/dist/slots/content.d.ts.map +1 -1
- package/dist/slots/content.js +2 -0
- package/dist/slots/content.js.map +1 -1
- package/dist/slots/frames.d.ts +2 -0
- package/dist/slots/frames.d.ts.map +1 -1
- package/dist/slots/frames.js +7 -2
- package/dist/slots/frames.js.map +1 -1
- package/dist/text/index.d.ts +2 -2
- package/dist/text/index.js +3 -3
- package/dist/text/lengths.d.ts +6 -1
- package/dist/text/lengths.d.ts.map +1 -1
- package/dist/text/lengths.js +10 -1
- package/dist/text/lengths.js.map +1 -1
- package/dist/text/overflow.d.ts +2 -2
- package/dist/text/overflow.js.map +1 -1
- package/dist/text/picture.d.ts +1 -2
- package/dist/text/picture.d.ts.map +1 -1
- package/dist/text/picture.js +1 -4
- package/dist/text/picture.js.map +1 -1
- package/dist/transfer/index.js +1 -1
- package/dist/transfer/nodes.js +9 -5
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/types.d.ts +27 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/variables/bindings.d.ts +28 -5
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +104 -14
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/fields.d.ts +19 -1
- package/dist/variables/fields.d.ts.map +1 -1
- package/dist/variables/fields.js +36 -1
- package/dist/variables/fields.js.map +1 -1
- package/dist/variables/index.d.ts +3 -3
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +23 -8
- package/dist/variables/index.js.map +1 -1
- package/package.json +7 -1
- package/src/arrow-caps.ts +2 -5
- package/src/behaviours/kinds.ts +51 -34
- package/src/behaviours/layers.ts +4 -2
- package/src/behaviours/model.ts +22 -11
- package/src/behaviours/schema.ts +20 -4
- package/src/behaviours/spec.ts +8 -2
- package/src/bindings.ts +11 -2
- package/src/checkpoint.ts +251 -4
- package/src/components/properties.ts +92 -2
- package/src/components/variants.ts +51 -30
- package/src/connectors/hit.ts +1 -12
- package/src/connectors/pieces.ts +2 -5
- package/src/copy.ts +9 -15
- package/src/css/blend.ts +27 -0
- package/src/css/index.ts +1 -0
- package/src/cubic.ts +14 -0
- package/src/export-format.ts +29 -0
- package/src/fields/text.ts +3 -1
- package/src/figjam/palette.ts +29 -1
- package/src/generated/shape-with-text.ts +28 -2
- package/src/geometry.ts +78 -198
- package/src/graph.ts +27 -3
- package/src/guides.ts +5 -0
- package/src/hit-test.ts +67 -54
- package/src/instances/fields.ts +8 -0
- package/src/instances/index.ts +58 -31
- package/src/instances/kept-overrides.ts +280 -0
- package/src/instances/main-component.ts +12 -0
- package/src/instances/overrides.ts +27 -1
- package/src/instances/swap.ts +451 -0
- package/src/instances/sync.ts +141 -42
- package/src/node-defaults.ts +2 -1
- package/src/outline/flatten.ts +190 -0
- package/src/outline/heads.ts +52 -0
- package/src/outline/hit.ts +369 -0
- package/src/outline/index.ts +4 -0
- package/src/outline/point.ts +59 -0
- package/src/outline/shapes.ts +207 -0
- package/src/plugin-data/fields.ts +15 -2
- package/src/shared-styles.ts +82 -1
- package/src/slots/content.ts +2 -0
- package/src/slots/frames.ts +9 -7
- package/src/text/lengths.ts +10 -0
- package/src/text/overflow.ts +2 -2
- package/src/text/picture.ts +0 -4
- package/src/transfer/nodes.ts +7 -5
- package/src/types.ts +28 -0
- package/src/variables/bindings.ts +125 -14
- package/src/variables/fields.ts +46 -1
- package/src/variables/index.ts +35 -2
package/src/instances/sync.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// Child cloning and property synchronization shared by instance creation, swap, and sync.
|
|
2
|
-
import {
|
|
2
|
+
import { uniq } from 'es-toolkit/array'
|
|
3
|
+
import { pick } from 'es-toolkit/object'
|
|
4
|
+
import { isEmptyObject, isEqual } from 'es-toolkit/predicate'
|
|
3
5
|
|
|
4
6
|
import { cloneNodeProps, copyEffects, copyFills, copyStrokes, copyStyleRuns } from '../copy'
|
|
5
7
|
import type { NodeCloneMode } from '../copy'
|
|
@@ -7,6 +9,8 @@ import type { SceneGraph } from '../graph'
|
|
|
7
9
|
import { scaleNodeChanges } from '../scaling/node'
|
|
8
10
|
import { ownsSlotContent, slotPropertyId } from '../slots/frames'
|
|
9
11
|
import type { SceneNode } from '../types'
|
|
12
|
+
import { resolvedNumericBindings } from '../variables/bindings'
|
|
13
|
+
import { effectBindingTarget, isNumericVariableBindingField } from '../variables/fields'
|
|
10
14
|
import { scaleVariableBindingUnits } from '../variables/units'
|
|
11
15
|
import { applyEnclosingAssignments, assignedFieldValues, propertyDrivenFields } from './assignments'
|
|
12
16
|
import { keepOwnContentSize, remeasureChangedText, type SyncedPlacement } from './content-size'
|
|
@@ -62,6 +66,8 @@ export function copyProp(
|
|
|
62
66
|
setSceneProp(target, key, source.gridPosition ? { ...source.gridPosition } : null)
|
|
63
67
|
} else if (key === 'targetAspectRatio') {
|
|
64
68
|
setSceneProp(target, key, source.targetAspectRatio ? { ...source.targetAspectRatio } : null)
|
|
69
|
+
} else if (key === 'booleanOperation') {
|
|
70
|
+
setSceneProp(target, key, source.booleanOperation ?? 'UNION')
|
|
65
71
|
} else {
|
|
66
72
|
const value = source[key]
|
|
67
73
|
setSceneProp(target, key, Array.isArray(value) ? structuredClone(value) : value)
|
|
@@ -76,7 +82,16 @@ export function bindingProtection(scopes: ReadonlySet<string>[]): (field: string
|
|
|
76
82
|
if (field !== 'boundVariables' || !perField) fields.add(field)
|
|
77
83
|
}
|
|
78
84
|
}
|
|
79
|
-
return (field) =>
|
|
85
|
+
return (field) => {
|
|
86
|
+
if (fields.has(field)) return true
|
|
87
|
+
// Effects an instance set itself own their bindings too, bound or not, as its fills do.
|
|
88
|
+
const binding = overriddenBindingField(field)
|
|
89
|
+
return binding !== null && fields.has('effects') && effectBindingTarget(binding) !== null
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function isUnbound(node: SceneNode): boolean {
|
|
94
|
+
return isEmptyObject(node.boundVariables) && isEmptyObject(node.variableBindingScales)
|
|
80
95
|
}
|
|
81
96
|
|
|
82
97
|
export function syncBindingFields(
|
|
@@ -85,7 +100,9 @@ export function syncBindingFields(
|
|
|
85
100
|
updates: Partial<SceneNode>,
|
|
86
101
|
protectedField: (field: string) => boolean
|
|
87
102
|
): void {
|
|
88
|
-
if (protectedField('boundVariables'))
|
|
103
|
+
if (protectedField('boundVariables') || (isUnbound(source) && isUnbound(target))) {
|
|
104
|
+
return
|
|
105
|
+
}
|
|
89
106
|
const bindings = { ...source.boundVariables }
|
|
90
107
|
const scales = { ...source.variableBindingScales }
|
|
91
108
|
for (const field of new Set([
|
|
@@ -126,6 +143,15 @@ export function sourceInTargetCoordinates(source: SceneNode, targetScale: number
|
|
|
126
143
|
}
|
|
127
144
|
}
|
|
128
145
|
|
|
146
|
+
const SYNCED_FIELDS = INSTANCE_SYNC_FIELDS.filter((key) => key !== 'boundVariables').map((key) => ({
|
|
147
|
+
key,
|
|
148
|
+
owner: INSTANCE_SYNC_FIELD_OWNERS[key],
|
|
149
|
+
placement: PLACEMENT_FIELDS.has(key),
|
|
150
|
+
position: key === 'x' || key === 'y'
|
|
151
|
+
}))
|
|
152
|
+
|
|
153
|
+
export const NOTHING_PROTECTED = (_field: string): boolean => false
|
|
154
|
+
|
|
129
155
|
interface SyncedFieldsInput {
|
|
130
156
|
compParent: SceneNode
|
|
131
157
|
instParent: SceneNode
|
|
@@ -147,15 +173,15 @@ function copySyncedFields(
|
|
|
147
173
|
const placed = placedByLayout
|
|
148
174
|
? source
|
|
149
175
|
: placedByConstraints(source, compParent, instParent, factor)
|
|
150
|
-
for (const key of
|
|
151
|
-
if (key
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
const
|
|
155
|
-
if (owner && skip(owner)) continue
|
|
156
|
-
const from = PLACEMENT_FIELDS.has(key) ? placed : source
|
|
176
|
+
for (const { key, owner, placement, position } of SYNCED_FIELDS) {
|
|
177
|
+
if ((placedByLayout && position) || skip(key) || (owner && skip(owner))) {
|
|
178
|
+
continue
|
|
179
|
+
}
|
|
180
|
+
const from = placement ? placed : source
|
|
157
181
|
// Most fields already match: comparing first spares cloning them for nothing.
|
|
158
|
-
if (isEqual(copy[key], from[key]))
|
|
182
|
+
if (copy[key] === from[key] || isEqual(copy[key], from[key])) {
|
|
183
|
+
continue
|
|
184
|
+
}
|
|
159
185
|
copyProp(updates, from, key)
|
|
160
186
|
}
|
|
161
187
|
// New paints bring their style link, unless the copy keeps a style of its own.
|
|
@@ -169,18 +195,59 @@ function copySyncedFields(
|
|
|
169
195
|
export function updateSyncedProps(
|
|
170
196
|
graph: SceneGraph,
|
|
171
197
|
target: SceneNode,
|
|
172
|
-
updates: Partial<SceneNode
|
|
198
|
+
updates: Partial<SceneNode>,
|
|
199
|
+
/** A field the copy keeps as its own, which the sync leaves as it is. */
|
|
200
|
+
kept: (field: string) => boolean = () => false
|
|
173
201
|
): void {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
202
|
+
resolveBoundNumbersInPlace(graph, target, updates, kept)
|
|
203
|
+
let changed: Partial<SceneNode> | undefined
|
|
204
|
+
for (const key of Object.keys(updates)) {
|
|
205
|
+
const current = target[key as keyof SceneNode]
|
|
206
|
+
const value = updates[key as keyof SceneNode]
|
|
207
|
+
if (current !== value && !isEqual(current, value)) {
|
|
208
|
+
changed ??= {}
|
|
209
|
+
Reflect.set(changed, key, value)
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (!changed) {
|
|
213
|
+
return
|
|
214
|
+
}
|
|
215
|
+
if (changed.fills && updates.fillStyleId !== undefined) {
|
|
216
|
+
changed.fillStyleId = updates.fillStyleId
|
|
217
|
+
}
|
|
218
|
+
graph.updateNode(target.id, changed)
|
|
181
219
|
}
|
|
182
220
|
|
|
183
|
-
|
|
221
|
+
/**
|
|
222
|
+
* A bound number the sync writes takes the value it resolves to where the copy sits, not the
|
|
223
|
+
* one its component holds: an instance that sets another mode, or sits in one that does, shows
|
|
224
|
+
* that mode's value, as Figma draws it. A field the sync leaves alone, such as one an instance
|
|
225
|
+
* overrides, keeps its own value.
|
|
226
|
+
*/
|
|
227
|
+
function resolveBoundNumbersInPlace(
|
|
228
|
+
graph: SceneGraph,
|
|
229
|
+
target: SceneNode,
|
|
230
|
+
updates: Partial<SceneNode>,
|
|
231
|
+
kept: (field: string) => boolean
|
|
232
|
+
): void {
|
|
233
|
+
const bound = updates.boundVariables ?? target.boundVariables
|
|
234
|
+
// The fields the sync writes, and the ones it binds anew, which keep an equal value only in
|
|
235
|
+
// the component's mode.
|
|
236
|
+
const rebound = updates.boundVariables
|
|
237
|
+
? Object.keys(bound).filter((field) => bound[field] !== target.boundVariables[field])
|
|
238
|
+
: []
|
|
239
|
+
const synced = uniq([...Object.keys(updates), ...rebound]).filter(
|
|
240
|
+
(field) => isNumericVariableBindingField(field) && bound[field] && !kept(field)
|
|
241
|
+
)
|
|
242
|
+
if (synced.length === 0) return
|
|
243
|
+
// The modes the copy takes in this sync decide what its bindings resolve to.
|
|
244
|
+
if (updates.variableModes && !isEqual(updates.variableModes, target.variableModes))
|
|
245
|
+
graph.updateNode(target.id, { variableModes: updates.variableModes })
|
|
246
|
+
const resolved = resolvedNumericBindings(graph, { ...target, ...updates })
|
|
247
|
+
Object.assign(updates, pick(resolved, synced as (keyof SceneNode)[]))
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function cloneChildInCoordinates(
|
|
184
251
|
graph: SceneGraph,
|
|
185
252
|
src: SceneNode,
|
|
186
253
|
sourceParent: SceneNode,
|
|
@@ -239,6 +306,9 @@ function childBindingProtection(
|
|
|
239
306
|
) {
|
|
240
307
|
const fields = enclosingInstanceOverrideFields(graph, child)
|
|
241
308
|
fields.push(new Set(overrides.descendants.get(child.id)?.keys()))
|
|
309
|
+
if (child.instanceOverrides.self.size === 0 && fields.every((scope) => scope.size === 0)) {
|
|
310
|
+
return NOTHING_PROTECTED
|
|
311
|
+
}
|
|
242
312
|
const protectedField = bindingProtection(fields)
|
|
243
313
|
return (field: string) => protectedField(field) || ownsSelfFillField(child, field)
|
|
244
314
|
}
|
|
@@ -333,6 +403,52 @@ function sortInstanceChildren(
|
|
|
333
403
|
})
|
|
334
404
|
}
|
|
335
405
|
|
|
406
|
+
export interface MatchedCopy {
|
|
407
|
+
compParent: SceneNode
|
|
408
|
+
instParent: SceneNode
|
|
409
|
+
compChild: SceneNode
|
|
410
|
+
instChild: SceneNode
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
export function syncCopy(
|
|
414
|
+
graph: SceneGraph,
|
|
415
|
+
{ compParent, instParent, compChild, instChild }: MatchedCopy,
|
|
416
|
+
protectedField: (field: string) => boolean,
|
|
417
|
+
linkedLayerId?: string
|
|
418
|
+
): void {
|
|
419
|
+
const driven = propertyDrivenFields(graph, instChild, compChild)
|
|
420
|
+
const kept =
|
|
421
|
+
driven.size === 0 && protectedField === NOTHING_PROTECTED
|
|
422
|
+
? NOTHING_PROTECTED
|
|
423
|
+
: (key: string) => driven.has(key) || isProtectedSyncField(instChild, key, protectedField)
|
|
424
|
+
const componentScale =
|
|
425
|
+
(compChild.componentScale * instParent.componentScale) / compParent.componentScale
|
|
426
|
+
const effectiveSource = sourceWithEffectiveFill(graph, compChild, instChild)
|
|
427
|
+
const source = sourceInTargetCoordinates(effectiveSource, componentScale)
|
|
428
|
+
// Which properties a layer serves is the component's to say; a slot or exposed layer
|
|
429
|
+
// created on the component becomes one in every instance.
|
|
430
|
+
const updates: Partial<SceneNode> = {}
|
|
431
|
+
if (instChild.componentScale !== componentScale) {
|
|
432
|
+
updates.componentScale = componentScale
|
|
433
|
+
}
|
|
434
|
+
if (!isEqual(instChild.componentPropertyReferences, compChild.componentPropertyReferences)) {
|
|
435
|
+
updates.componentPropertyReferences = structuredClone(compChild.componentPropertyReferences)
|
|
436
|
+
}
|
|
437
|
+
if (linkedLayerId !== undefined) {
|
|
438
|
+
updates.componentId = linkedLayerId
|
|
439
|
+
}
|
|
440
|
+
syncBindingFields(instChild, source, updates, protectedField)
|
|
441
|
+
const placement = copySyncedFields(updates, instChild, source, {
|
|
442
|
+
compParent,
|
|
443
|
+
instParent,
|
|
444
|
+
factor: componentScale / compChild.componentScale,
|
|
445
|
+
skip: kept
|
|
446
|
+
})
|
|
447
|
+
Object.assign(updates, assignedFieldValues(graph, instChild, compChild))
|
|
448
|
+
keepOwnContentSize(graph, updates, instChild, source, placement)
|
|
449
|
+
updateSyncedProps(graph, instChild, updates, kept)
|
|
450
|
+
}
|
|
451
|
+
|
|
336
452
|
/** True when syncing `compParentId` into `instParentId` would form a cycle. */
|
|
337
453
|
function isCyclicSync(graph: SceneGraph, compParentId: string, instParentId: string): boolean {
|
|
338
454
|
return compParentId === instParentId || graph.isDescendant(instParentId, compParentId)
|
|
@@ -451,28 +567,11 @@ export function syncChildren(
|
|
|
451
567
|
const instChild = instChildMap.get(compChildId)
|
|
452
568
|
if (!compChild || !instChild) continue
|
|
453
569
|
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
(
|
|
458
|
-
|
|
459
|
-
const source = sourceInTargetCoordinates(effectiveSource, componentScale)
|
|
460
|
-
// Which properties a layer serves is the component's to say; a slot or exposed layer
|
|
461
|
-
// created on the component becomes one in every instance.
|
|
462
|
-
const updates: Partial<SceneNode> = {
|
|
463
|
-
componentScale,
|
|
464
|
-
componentPropertyReferences: structuredClone(compChild.componentPropertyReferences)
|
|
465
|
-
}
|
|
466
|
-
syncBindingFields(instChild, source, updates, protectedField)
|
|
467
|
-
const placement = copySyncedFields(updates, instChild, source, {
|
|
468
|
-
compParent,
|
|
469
|
-
instParent,
|
|
470
|
-
factor: componentScale / compChild.componentScale,
|
|
471
|
-
skip: (key) => driven.has(key) || isProtectedSyncField(instChild, key, protectedField)
|
|
472
|
-
})
|
|
473
|
-
Object.assign(updates, assignedFieldValues(graph, instChild, compChild))
|
|
474
|
-
keepOwnContentSize(graph, updates, instChild, source, placement)
|
|
475
|
-
updateSyncedProps(graph, instChild, updates)
|
|
570
|
+
syncCopy(
|
|
571
|
+
graph,
|
|
572
|
+
{ compParent, instParent, compChild, instChild },
|
|
573
|
+
childBindingProtection(graph, instChild, overrides)
|
|
574
|
+
)
|
|
476
575
|
|
|
477
576
|
if (
|
|
478
577
|
compChild.childIds.length > 0 &&
|
package/src/node-defaults.ts
CHANGED
|
@@ -187,7 +187,8 @@ export function createDefaultNode(
|
|
|
187
187
|
textTruncation: 'DISABLED',
|
|
188
188
|
autoRename: true,
|
|
189
189
|
pointCount: 5,
|
|
190
|
-
|
|
190
|
+
// Figma's: the golden-ratio star, whose inner points line up with its outer ones.
|
|
191
|
+
starInnerRadius: (3 - Math.sqrt(5)) / 2,
|
|
191
192
|
shapeWithTextType: 'SQUARE',
|
|
192
193
|
shapeUserHeight: 0,
|
|
193
194
|
shapeTruncates: false,
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { cubicPoint } from '../cubic'
|
|
2
|
+
import { GeometryCommand, readGeometryBlob } from '../geometry'
|
|
3
|
+
import type { PolygonOutlineCommand } from '../polygon'
|
|
4
|
+
import type { Vector } from '../primitives'
|
|
5
|
+
import type { VectorNetwork } from '../types'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Keeps a huge curve at a tiny tolerance from flattening into an unbounded number of points, while
|
|
9
|
+
* leaving room for the largest arcs at the maximum zoom: a 100,000-unit circle at 25,600% needs
|
|
10
|
+
* about 12,000 steps to stay within the click pad.
|
|
11
|
+
*/
|
|
12
|
+
const MAX_CURVE_STEPS = 65_536
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Steps that keep a cubic's polyline within `tolerance` of it: equal steps in t stray at most
|
|
16
|
+
* M / (8n²) from a curve whose second derivative is at most M, and for a cubic
|
|
17
|
+
* M ≤ 6 · max(|p0 − 2p1 + p2|, |p1 − 2p2 + p3|).
|
|
18
|
+
*/
|
|
19
|
+
function curveSteps(p0: Vector, p1: Vector, p2: Vector, p3: Vector, tolerance: number): number {
|
|
20
|
+
const bend = Math.max(
|
|
21
|
+
Math.hypot(p0.x - 2 * p1.x + p2.x, p0.y - 2 * p1.y + p2.y),
|
|
22
|
+
Math.hypot(p1.x - 2 * p2.x + p3.x, p1.y - 2 * p2.y + p3.y)
|
|
23
|
+
)
|
|
24
|
+
const steps = Math.ceil(Math.sqrt((6 * bend) / (8 * tolerance)))
|
|
25
|
+
return Math.min(MAX_CURVE_STEPS, Math.max(1, steps))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Points along a cubic after its start, within `tolerance` of it, ending on `p3`. */
|
|
29
|
+
function cubicPoints(p0: Vector, p1: Vector, p2: Vector, p3: Vector, tolerance: number): Vector[] {
|
|
30
|
+
const steps = curveSteps(p0, p1, p2, p3, tolerance)
|
|
31
|
+
const points: Vector[] = []
|
|
32
|
+
for (let step = 1; step <= steps; step++) points.push(cubicPoint(p0, p1, p2, p3, step / steps))
|
|
33
|
+
return points
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Points along a vector network segment, from its start to its end, in network coordinates. */
|
|
37
|
+
export function segmentPoints(
|
|
38
|
+
network: VectorNetwork,
|
|
39
|
+
index: number,
|
|
40
|
+
tolerance: number
|
|
41
|
+
): Vector[] | null {
|
|
42
|
+
// `at` counts a negative index from the end and truncates a fraction; a loop naming either
|
|
43
|
+
// refers to no segment.
|
|
44
|
+
if (!Number.isInteger(index) || index < 0) return null
|
|
45
|
+
const segment = network.segments.at(index)
|
|
46
|
+
if (!segment || segment.start < 0 || segment.end < 0) return null
|
|
47
|
+
const p0 = network.vertices.at(segment.start)
|
|
48
|
+
const p3 = network.vertices.at(segment.end)
|
|
49
|
+
if (!p0 || !p3) return null
|
|
50
|
+
const { tangentStart: a, tangentEnd: b } = segment
|
|
51
|
+
if (a.x === 0 && a.y === 0 && b.x === 0 && b.y === 0) return [p0, p3]
|
|
52
|
+
const p1 = { x: p0.x + a.x, y: p0.y + a.y }
|
|
53
|
+
const p2 = { x: p3.x + b.x, y: p3.y + b.y }
|
|
54
|
+
return [p0, ...cubicPoints(p0, p1, p2, p3, tolerance)]
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* A loop's points in the order it is walked. Segments in a loop can be stored either way round,
|
|
59
|
+
* so each is followed from the vertex the previous one ended on.
|
|
60
|
+
*/
|
|
61
|
+
export function loopPoints(network: VectorNetwork, loop: number[], tolerance: number): Vector[] {
|
|
62
|
+
const { segments } = network
|
|
63
|
+
const first = segments.at(loop[0] ?? -1)
|
|
64
|
+
const second = segments.at(loop[1] ?? -1)
|
|
65
|
+
if (!first) return []
|
|
66
|
+
// Walk the first segment toward the vertex it shares with the next one.
|
|
67
|
+
const firstForward = !second || first.end === second.start || first.end === second.end
|
|
68
|
+
let current = firstForward ? first.start : first.end
|
|
69
|
+
const points: Vector[] = []
|
|
70
|
+
for (const index of loop) {
|
|
71
|
+
const segment = segments.at(index)
|
|
72
|
+
const along = segmentPoints(network, index, tolerance)
|
|
73
|
+
if (!segment || !along) continue
|
|
74
|
+
const forward = segment.start === current
|
|
75
|
+
points.push(...(forward ? along : along.toReversed()))
|
|
76
|
+
current = forward ? segment.end : segment.start
|
|
77
|
+
}
|
|
78
|
+
return points
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The network's segments joined into runs, a run continuing while each segment starts where the
|
|
83
|
+
* last ended, as a path draws them one after another.
|
|
84
|
+
*/
|
|
85
|
+
export function segmentRuns(network: VectorNetwork, tolerance: number): Vector[][] {
|
|
86
|
+
const runs: Vector[][] = []
|
|
87
|
+
let run: Vector[] = []
|
|
88
|
+
let end = -1
|
|
89
|
+
for (const [index, segment] of network.segments.entries()) {
|
|
90
|
+
const along = segmentPoints(network, index, tolerance)
|
|
91
|
+
if (!along) continue
|
|
92
|
+
if (segment.start === end && run.length > 0) run.push(...along.slice(1))
|
|
93
|
+
else {
|
|
94
|
+
if (run.length > 1) runs.push(run)
|
|
95
|
+
run = along
|
|
96
|
+
}
|
|
97
|
+
end = segment.end
|
|
98
|
+
}
|
|
99
|
+
if (run.length > 1) runs.push(run)
|
|
100
|
+
return runs
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The contours of a Figma geometry blob, curves flattened to within `tolerance`. */
|
|
104
|
+
export function blobContours(blob: Uint8Array, tolerance: number): Vector[][] {
|
|
105
|
+
const contours: Vector[][] = []
|
|
106
|
+
let contour: Vector[] = []
|
|
107
|
+
const flush = () => {
|
|
108
|
+
if (contour.length > 1) contours.push(contour)
|
|
109
|
+
contour = []
|
|
110
|
+
}
|
|
111
|
+
readGeometryBlob(blob, (command, c) => {
|
|
112
|
+
if (command === GeometryCommand.CLOSE) return flush()
|
|
113
|
+
if (command === GeometryCommand.MOVE) {
|
|
114
|
+
flush()
|
|
115
|
+
contour.push({ x: c[0], y: c[1] })
|
|
116
|
+
return
|
|
117
|
+
}
|
|
118
|
+
const end = { x: c[c.length - 2], y: c[c.length - 1] }
|
|
119
|
+
const last = contour.at(-1)
|
|
120
|
+
if (!last || command === GeometryCommand.LINE) {
|
|
121
|
+
contour.push(end)
|
|
122
|
+
} else if (command === GeometryCommand.QUAD) {
|
|
123
|
+
// A quadratic as the cubic with the same curve.
|
|
124
|
+
const q = { x: c[0], y: c[1] }
|
|
125
|
+
const c1 = { x: last.x + (2 / 3) * (q.x - last.x), y: last.y + (2 / 3) * (q.y - last.y) }
|
|
126
|
+
const c2 = { x: end.x + (2 / 3) * (q.x - end.x), y: end.y + (2 / 3) * (q.y - end.y) }
|
|
127
|
+
contour.push(...cubicPoints(last, c1, c2, end, tolerance))
|
|
128
|
+
} else {
|
|
129
|
+
contour.push(...cubicPoints(last, { x: c[0], y: c[1] }, { x: c[2], y: c[3] }, end, tolerance))
|
|
130
|
+
}
|
|
131
|
+
})
|
|
132
|
+
flush()
|
|
133
|
+
return contours
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** The contours of polygon outline commands, curves flattened to within `tolerance`. */
|
|
137
|
+
export function commandContours(
|
|
138
|
+
commands: readonly PolygonOutlineCommand[],
|
|
139
|
+
tolerance: number
|
|
140
|
+
): Vector[][] {
|
|
141
|
+
const contours: Vector[][] = []
|
|
142
|
+
let contour: Vector[] = []
|
|
143
|
+
for (const command of commands) {
|
|
144
|
+
if (command.type === 'Z' || command.type === 'M') {
|
|
145
|
+
if (contour.length > 1) contours.push(contour)
|
|
146
|
+
contour = command.type === 'M' ? [{ x: command.x, y: command.y }] : []
|
|
147
|
+
continue
|
|
148
|
+
}
|
|
149
|
+
const last = contour.at(-1) ?? { x: 0, y: 0 }
|
|
150
|
+
if (command.type !== 'C') contour.push({ x: command.x, y: command.y })
|
|
151
|
+
else
|
|
152
|
+
contour.push(
|
|
153
|
+
...cubicPoints(
|
|
154
|
+
last,
|
|
155
|
+
{ x: command.x1, y: command.y1 },
|
|
156
|
+
{ x: command.x2, y: command.y2 },
|
|
157
|
+
{ x: command.x, y: command.y },
|
|
158
|
+
tolerance
|
|
159
|
+
)
|
|
160
|
+
)
|
|
161
|
+
}
|
|
162
|
+
if (contour.length > 1) contours.push(contour)
|
|
163
|
+
return contours
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Points around an ellipse arc from `start` to `end` radians, at most `tolerance` off it. */
|
|
167
|
+
export function arcPoints(
|
|
168
|
+
cx: number,
|
|
169
|
+
cy: number,
|
|
170
|
+
rx: number,
|
|
171
|
+
ry: number,
|
|
172
|
+
start: number,
|
|
173
|
+
end: number,
|
|
174
|
+
tolerance: number
|
|
175
|
+
): Vector[] {
|
|
176
|
+
const radius = Math.max(rx, ry)
|
|
177
|
+
// A chord over angle θ strays r(1 − cos(θ/2)) from the arc.
|
|
178
|
+
const step =
|
|
179
|
+
radius > tolerance ? 2 * Math.acos(Math.max(-1, 1 - tolerance / radius)) : Math.PI / 2
|
|
180
|
+
const steps = Math.min(
|
|
181
|
+
MAX_CURVE_STEPS,
|
|
182
|
+
Math.max(1, Math.ceil(Math.abs(end - start) / Math.max(step, 1e-3)))
|
|
183
|
+
)
|
|
184
|
+
const points: Vector[] = []
|
|
185
|
+
for (let i = 0; i <= steps; i++) {
|
|
186
|
+
const angle = start + ((end - start) * i) / steps
|
|
187
|
+
points.push({ x: cx + rx * Math.cos(angle), y: cy + ry * Math.sin(angle) })
|
|
188
|
+
}
|
|
189
|
+
return points
|
|
190
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import {
|
|
2
|
+
arrowLinesSegments,
|
|
3
|
+
circleCapRadius,
|
|
4
|
+
collectArrowEndpoints,
|
|
5
|
+
diamondCapPoints,
|
|
6
|
+
equilateralArrowPoints,
|
|
7
|
+
triangleCapPoints
|
|
8
|
+
} from '../arrow-caps'
|
|
9
|
+
import type { Vector } from '../primitives'
|
|
10
|
+
import type { StrokeCap, VectorNetwork } from '../types'
|
|
11
|
+
import { containsByWinding, distanceToLines, distanceToSegment } from './point'
|
|
12
|
+
|
|
13
|
+
/** The outline of each filled head with corners, at an end pointing along `angle`. */
|
|
14
|
+
const HEAD_OUTLINES: Record<
|
|
15
|
+
'ARROW_EQUILATERAL' | 'TRIANGLE_FILLED' | 'DIAMOND_FILLED',
|
|
16
|
+
(x: number, y: number, angle: number, weight: number) => Vector[]
|
|
17
|
+
> = {
|
|
18
|
+
ARROW_EQUILATERAL: equilateralArrowPoints,
|
|
19
|
+
TRIANGLE_FILLED: triangleCapPoints,
|
|
20
|
+
DIAMOND_FILLED: diamondCapPoints
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Whether the point lands on an arrow head drawn at the open ends of `network`, in its own
|
|
25
|
+
* coordinates, within `pad`: inside a filled head or near its outline, or near a line arrow's
|
|
26
|
+
* wings, which are stroked at the line's weight.
|
|
27
|
+
*/
|
|
28
|
+
export function hitsArrowHeads(
|
|
29
|
+
network: VectorNetwork,
|
|
30
|
+
cap: StrokeCap,
|
|
31
|
+
weight: number,
|
|
32
|
+
point: Vector,
|
|
33
|
+
pad: number
|
|
34
|
+
): boolean {
|
|
35
|
+
for (const { x, y, angle, cap: head } of collectArrowEndpoints(network, cap)) {
|
|
36
|
+
if (head === 'CIRCLE_FILLED') {
|
|
37
|
+
if (Math.hypot(point.x - x, point.y - y) <= circleCapRadius(weight) + pad) return true
|
|
38
|
+
continue
|
|
39
|
+
}
|
|
40
|
+
if (head === 'ARROW_LINES') {
|
|
41
|
+
const wings = arrowLinesSegments(x, y, angle, weight)
|
|
42
|
+
if (wings.some((wing) => distanceToSegment(point, wing.from, wing.to) <= weight / 2 + pad)) {
|
|
43
|
+
return true
|
|
44
|
+
}
|
|
45
|
+
continue
|
|
46
|
+
}
|
|
47
|
+
const outline = HEAD_OUTLINES[head](x, y, angle, weight)
|
|
48
|
+
if (containsByWinding([outline], 'NONZERO', point)) return true
|
|
49
|
+
if (distanceToLines([[...outline, outline[0]]], point) <= pad) return true
|
|
50
|
+
}
|
|
51
|
+
return false
|
|
52
|
+
}
|