@open-pencil/scene-graph 0.17.0-beta.1 → 0.17.0-beta.3
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/asset-keys.d.ts +11 -1
- package/dist/asset-keys.d.ts.map +1 -1
- package/dist/asset-keys.js +16 -1
- package/dist/asset-keys.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 +211 -10
- 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 +18 -1
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +66 -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/constants.d.ts +6 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +17 -1
- package/dist/constants.js.map +1 -1
- package/dist/constraints.d.ts +25 -0
- package/dist/constraints.d.ts.map +1 -0
- package/dist/constraints.js +51 -0
- package/dist/constraints.js.map +1 -0
- package/dist/coordinate.d.ts +64 -1
- package/dist/coordinate.d.ts.map +1 -1
- package/dist/coordinate.js +217 -1
- package/dist/coordinate.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +17 -16
- 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 +4 -2
- package/dist/css/index.js +4 -2
- package/dist/css/text.d.ts +28 -1
- package/dist/css/text.d.ts.map +1 -1
- package/dist/css/text.js +84 -17
- package/dist/css/text.js.map +1 -1
- package/dist/css/transform.d.ts +34 -0
- package/dist/css/transform.d.ts.map +1 -0
- package/dist/css/transform.js +320 -0
- package/dist/css/transform.js.map +1 -0
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +11 -1
- package/dist/css/values.js.map +1 -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 +37 -46
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +80 -137
- package/dist/geometry.js.map +1 -1
- package/dist/graph.d.ts +49 -12
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +125 -27
- package/dist/graph.js.map +1 -1
- package/dist/group-bounds.d.ts +14 -4
- package/dist/group-bounds.d.ts.map +1 -1
- package/dist/group-bounds.js +76 -11
- package/dist/group-bounds.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 +5 -0
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +57 -38
- package/dist/hit-test.js.map +1 -1
- package/dist/images.d.ts +27 -3
- package/dist/images.d.ts.map +1 -1
- package/dist/images.js +57 -3
- package/dist/images.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 +39 -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/placement.js +55 -12
- package/dist/instances/placement.js.map +1 -1
- package/dist/instances/swap.js +291 -0
- package/dist/instances/swap.js.map +1 -0
- package/dist/instances/sync.js +112 -39
- package/dist/instances/sync.js.map +1 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +2 -1
- package/dist/node-defaults.js.map +1 -1
- package/dist/order-keys.d.ts +3 -1
- package/dist/order-keys.d.ts.map +1 -1
- package/dist/order-keys.js +6 -1
- package/dist/order-keys.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 +4 -1
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +21 -4
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/resize.d.ts +22 -11
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +227 -93
- package/dist/resize.js.map +1 -1
- package/dist/scaling/index.d.ts +3 -2
- package/dist/scaling/index.js +3 -2
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +5 -1
- package/dist/scaling/node.js.map +1 -1
- package/dist/scaling/snapshot.d.ts +25 -0
- package/dist/scaling/snapshot.d.ts.map +1 -0
- package/dist/scaling/snapshot.js +39 -0
- package/dist/scaling/snapshot.js.map +1 -0
- package/dist/scaling/tree.d.ts +24 -2
- package/dist/scaling/tree.d.ts.map +1 -1
- package/dist/scaling/tree.js +38 -15
- package/dist/scaling/tree.js.map +1 -1
- package/dist/shaders/model.d.ts.map +1 -1
- package/dist/shaders/model.js +2 -1
- package/dist/shaders/model.js.map +1 -1
- package/dist/shared-styles.d.ts +16 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +69 -2
- package/dist/shared-styles.js.map +1 -1
- package/dist/slots/authoring.d.ts +3 -2
- package/dist/slots/authoring.d.ts.map +1 -1
- package/dist/slots/authoring.js +5 -3
- package/dist/slots/authoring.js.map +1 -1
- package/dist/slots/content.d.ts.map +1 -1
- package/dist/slots/content.js +6 -1
- 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/direction.d.ts +3 -1
- package/dist/text/direction.d.ts.map +1 -1
- package/dist/text/direction.js +5 -1
- package/dist/text/direction.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/apply.d.ts.map +1 -1
- package/dist/transfer/apply.js +1 -6
- package/dist/transfer/apply.js.map +1 -1
- package/dist/transfer/copied.d.ts +35 -3
- package/dist/transfer/copied.d.ts.map +1 -1
- package/dist/transfer/copied.js +44 -13
- package/dist/transfer/copied.js.map +1 -1
- package/dist/transfer/dependencies.d.ts +38 -0
- package/dist/transfer/dependencies.d.ts.map +1 -0
- package/dist/transfer/dependencies.js +100 -0
- package/dist/transfer/dependencies.js.map +1 -0
- package/dist/transfer/history.js +0 -1
- package/dist/transfer/history.js.map +1 -1
- package/dist/transfer/index.d.ts +5 -3
- package/dist/transfer/index.js +5 -3
- package/dist/transfer/nodes.d.ts.map +1 -1
- package/dist/transfer/nodes.js +16 -19
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/transfer/plan.d.ts +0 -1
- package/dist/transfer/plan.js +3 -5
- package/dist/transfer/plan.js.map +1 -1
- package/dist/transfer/remove.js +5 -2
- package/dist/transfer/remove.js.map +1 -1
- package/dist/transfer/resources.d.ts +21 -0
- package/dist/transfer/resources.d.ts.map +1 -0
- package/dist/transfer/resources.js +38 -0
- package/dist/transfer/resources.js.map +1 -0
- package/dist/transfer/variables.d.ts +8 -1
- package/dist/transfer/variables.d.ts.map +1 -1
- package/dist/transfer/variables.js +24 -1
- package/dist/transfer/variables.js.map +1 -1
- package/dist/types.d.ts +73 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/undo.d.ts +28 -3
- package/dist/undo.d.ts.map +1 -1
- package/dist/undo.js +24 -7
- package/dist/undo.js.map +1 -1
- package/dist/variables/bindings.d.ts +37 -11
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +122 -74
- 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/formula/check.d.ts +1 -1
- package/dist/variables/formula/index.d.ts +2 -2
- package/dist/variables/formula/index.js +1 -1
- package/dist/variables/formula/index.js.map +1 -1
- package/dist/variables/formula/print.d.ts +1 -1
- package/dist/variables/formula/signatures.js +1 -1
- package/dist/variables/index.d.ts +7 -7
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +57 -26
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/paints.d.ts +57 -0
- package/dist/variables/paints.d.ts.map +1 -0
- package/dist/variables/paints.js +231 -0
- package/dist/variables/paints.js.map +1 -0
- package/dist/variables/resolution.d.ts +14 -11
- package/dist/variables/resolution.d.ts.map +1 -1
- package/dist/variables/resolution.js +36 -17
- package/dist/variables/resolution.js.map +1 -1
- package/dist/variables/subtree.js +17 -0
- package/dist/variables/subtree.js.map +1 -0
- package/dist/vector-network.d.ts.map +1 -1
- package/dist/vector-network.js +2 -8
- package/dist/vector-network.js.map +1 -1
- package/package.json +14 -1
- package/src/arrow-caps.ts +2 -5
- package/src/asset-keys.ts +20 -0
- 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 +319 -17
- package/src/components/properties.ts +101 -2
- package/src/components/variants.ts +51 -30
- package/src/connectors/hit.ts +1 -12
- package/src/connectors/pieces.ts +2 -5
- package/src/constants.ts +17 -0
- package/src/constraints.ts +60 -0
- package/src/coordinate.ts +220 -0
- package/src/copy.ts +18 -19
- package/src/css/blend.ts +27 -0
- package/src/css/index.ts +18 -1
- package/src/css/text.ts +85 -17
- package/src/css/transform.ts +266 -0
- package/src/css/values.ts +8 -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 +97 -206
- package/src/graph.ts +155 -45
- package/src/group-bounds.ts +84 -17
- package/src/guides.ts +5 -0
- package/src/hit-test.ts +93 -64
- package/src/images.ts +71 -3
- package/src/instances/fields.ts +8 -0
- package/src/instances/index.ts +62 -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/placement.ts +63 -10
- package/src/instances/swap.ts +456 -0
- package/src/instances/sync.ts +176 -50
- package/src/node-defaults.ts +3 -1
- package/src/order-keys.ts +6 -0
- 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 +21 -2
- package/src/resize.ts +267 -119
- package/src/scaling/index.ts +9 -1
- package/src/scaling/node.ts +9 -1
- package/src/scaling/snapshot.ts +51 -0
- package/src/scaling/tree.ts +63 -19
- package/src/shaders/model.ts +2 -3
- package/src/shared-styles.ts +90 -1
- package/src/slots/authoring.ts +5 -3
- package/src/slots/content.ts +6 -1
- package/src/slots/frames.ts +9 -7
- package/src/text/direction.ts +5 -0
- package/src/text/lengths.ts +10 -0
- package/src/text/overflow.ts +2 -2
- package/src/text/picture.ts +0 -4
- package/src/transfer/apply.ts +1 -13
- package/src/transfer/copied.ts +83 -15
- package/src/transfer/dependencies.ts +126 -0
- package/src/transfer/history.ts +0 -6
- package/src/transfer/index.ts +20 -2
- package/src/transfer/nodes.ts +17 -31
- package/src/transfer/plan.ts +3 -11
- package/src/transfer/remove.ts +5 -2
- package/src/transfer/resources.ts +54 -0
- package/src/transfer/variables.ts +34 -0
- package/src/types.ts +83 -1
- package/src/undo.ts +48 -10
- package/src/variables/bindings.ts +152 -88
- package/src/variables/fields.ts +46 -1
- package/src/variables/formula/index.ts +3 -1
- package/src/variables/index.ts +90 -39
- package/src/variables/paints.ts +296 -0
- package/src/variables/resolution.ts +39 -28
- package/src/variables/subtree.ts +15 -0
- package/src/vector-network.ts +4 -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,17 @@ 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 {
|
|
14
|
+
effectBindingTarget,
|
|
15
|
+
isNumericVariableBindingField,
|
|
16
|
+
paintBindingTarget
|
|
17
|
+
} from '../variables/fields'
|
|
18
|
+
import {
|
|
19
|
+
applyBoundPaintColorsIn,
|
|
20
|
+
copyAppliedBoundPaints,
|
|
21
|
+
keepCopyBoundPaints
|
|
22
|
+
} from '../variables/paints'
|
|
10
23
|
import { scaleVariableBindingUnits } from '../variables/units'
|
|
11
24
|
import { applyEnclosingAssignments, assignedFieldValues, propertyDrivenFields } from './assignments'
|
|
12
25
|
import { keepOwnContentSize, remeasureChangedText, type SyncedPlacement } from './content-size'
|
|
@@ -27,7 +40,7 @@ import {
|
|
|
27
40
|
setInstanceOverride,
|
|
28
41
|
type InstanceOverrideState
|
|
29
42
|
} from './overrides'
|
|
30
|
-
import { PLACEMENT_FIELDS, placedByConstraints } from './placement'
|
|
43
|
+
import { fitCopiedGroup, PLACEMENT_FIELDS, placedByConstraints } from './placement'
|
|
31
44
|
import { followVectorGeometry } from './vector-geometry'
|
|
32
45
|
|
|
33
46
|
function setSceneProp<K extends keyof SceneNode>(
|
|
@@ -62,6 +75,8 @@ export function copyProp(
|
|
|
62
75
|
setSceneProp(target, key, source.gridPosition ? { ...source.gridPosition } : null)
|
|
63
76
|
} else if (key === 'targetAspectRatio') {
|
|
64
77
|
setSceneProp(target, key, source.targetAspectRatio ? { ...source.targetAspectRatio } : null)
|
|
78
|
+
} else if (key === 'booleanOperation') {
|
|
79
|
+
setSceneProp(target, key, source.booleanOperation ?? 'UNION')
|
|
65
80
|
} else {
|
|
66
81
|
const value = source[key]
|
|
67
82
|
setSceneProp(target, key, Array.isArray(value) ? structuredClone(value) : value)
|
|
@@ -76,7 +91,19 @@ export function bindingProtection(scopes: ReadonlySet<string>[]): (field: string
|
|
|
76
91
|
if (field !== 'boundVariables' || !perField) fields.add(field)
|
|
77
92
|
}
|
|
78
93
|
}
|
|
79
|
-
return (field) =>
|
|
94
|
+
return (field) => {
|
|
95
|
+
if (fields.has(field)) return true
|
|
96
|
+
// Paints and effects an instance set itself own their bindings too, bound or not.
|
|
97
|
+
const binding = overriddenBindingField(field)
|
|
98
|
+
if (binding === null) return false
|
|
99
|
+
const paint = paintBindingTarget(binding)
|
|
100
|
+
if (paint) return fields.has(paint.list)
|
|
101
|
+
return fields.has('effects') && effectBindingTarget(binding) !== null
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function isUnbound(node: SceneNode): boolean {
|
|
106
|
+
return isEmptyObject(node.boundVariables) && isEmptyObject(node.variableBindingScales)
|
|
80
107
|
}
|
|
81
108
|
|
|
82
109
|
export function syncBindingFields(
|
|
@@ -85,7 +112,9 @@ export function syncBindingFields(
|
|
|
85
112
|
updates: Partial<SceneNode>,
|
|
86
113
|
protectedField: (field: string) => boolean
|
|
87
114
|
): void {
|
|
88
|
-
if (protectedField('boundVariables'))
|
|
115
|
+
if (protectedField('boundVariables') || (isUnbound(source) && isUnbound(target))) {
|
|
116
|
+
return
|
|
117
|
+
}
|
|
89
118
|
const bindings = { ...source.boundVariables }
|
|
90
119
|
const scales = { ...source.variableBindingScales }
|
|
91
120
|
for (const field of new Set([
|
|
@@ -126,6 +155,15 @@ export function sourceInTargetCoordinates(source: SceneNode, targetScale: number
|
|
|
126
155
|
}
|
|
127
156
|
}
|
|
128
157
|
|
|
158
|
+
const SYNCED_FIELDS = INSTANCE_SYNC_FIELDS.filter((key) => key !== 'boundVariables').map((key) => ({
|
|
159
|
+
key,
|
|
160
|
+
owner: INSTANCE_SYNC_FIELD_OWNERS[key],
|
|
161
|
+
placement: PLACEMENT_FIELDS.has(key),
|
|
162
|
+
position: key === 'x' || key === 'y'
|
|
163
|
+
}))
|
|
164
|
+
|
|
165
|
+
export const NOTHING_PROTECTED = (_field: string): boolean => false
|
|
166
|
+
|
|
129
167
|
interface SyncedFieldsInput {
|
|
130
168
|
compParent: SceneNode
|
|
131
169
|
instParent: SceneNode
|
|
@@ -137,6 +175,7 @@ interface SyncedFieldsInput {
|
|
|
137
175
|
|
|
138
176
|
/** The fields a copy takes from its component's layer, placed in the copy's parent. */
|
|
139
177
|
function copySyncedFields(
|
|
178
|
+
graph: SceneGraph,
|
|
140
179
|
updates: Partial<SceneNode>,
|
|
141
180
|
copy: SceneNode,
|
|
142
181
|
source: SceneNode,
|
|
@@ -146,16 +185,16 @@ function copySyncedFields(
|
|
|
146
185
|
const placedByLayout = instParent.layoutMode !== 'NONE' && source.layoutPositioning !== 'ABSOLUTE'
|
|
147
186
|
const placed = placedByLayout
|
|
148
187
|
? source
|
|
149
|
-
: 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
|
|
188
|
+
: placedByConstraints(graph, source, compParent, instParent, factor)
|
|
189
|
+
for (const { key, owner, placement, position } of SYNCED_FIELDS) {
|
|
190
|
+
if ((placedByLayout && position) || skip(key) || (owner && skip(owner))) {
|
|
191
|
+
continue
|
|
192
|
+
}
|
|
193
|
+
const from = placement ? placed : source
|
|
157
194
|
// Most fields already match: comparing first spares cloning them for nothing.
|
|
158
|
-
if (isEqual(copy[key], from[key]))
|
|
195
|
+
if (copy[key] === from[key] || isEqual(copy[key], from[key])) {
|
|
196
|
+
continue
|
|
197
|
+
}
|
|
159
198
|
copyProp(updates, from, key)
|
|
160
199
|
}
|
|
161
200
|
// New paints bring their style link, unless the copy keeps a style of its own.
|
|
@@ -169,18 +208,67 @@ function copySyncedFields(
|
|
|
169
208
|
export function updateSyncedProps(
|
|
170
209
|
graph: SceneGraph,
|
|
171
210
|
target: SceneNode,
|
|
172
|
-
updates: Partial<SceneNode
|
|
211
|
+
updates: Partial<SceneNode>,
|
|
212
|
+
/** A field the copy keeps as its own, which the sync leaves as it is. */
|
|
213
|
+
kept: (field: string) => boolean = () => false
|
|
214
|
+
): void {
|
|
215
|
+
resolveBoundNumbersInPlace(graph, target, updates, kept)
|
|
216
|
+
if (updates.fills || updates.strokes || updates.boundVariables) {
|
|
217
|
+
applySyncedModes(graph, target, updates)
|
|
218
|
+
keepCopyBoundPaints(graph, target, updates)
|
|
219
|
+
}
|
|
220
|
+
let changed: Partial<SceneNode> | undefined
|
|
221
|
+
for (const key of Object.keys(updates)) {
|
|
222
|
+
const current = target[key as keyof SceneNode]
|
|
223
|
+
const value = updates[key as keyof SceneNode]
|
|
224
|
+
if (current !== value && !isEqual(current, value)) {
|
|
225
|
+
changed ??= {}
|
|
226
|
+
Reflect.set(changed, key, value)
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (!changed) {
|
|
230
|
+
return
|
|
231
|
+
}
|
|
232
|
+
if (changed.fills && updates.fillStyleId !== undefined) {
|
|
233
|
+
changed.fillStyleId = updates.fillStyleId
|
|
234
|
+
}
|
|
235
|
+
graph.updateNode(target.id, changed)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* A bound number the sync writes takes the value it resolves to where the copy sits, not the
|
|
240
|
+
* one its component holds: an instance that sets another mode, or sits in one that does, shows
|
|
241
|
+
* that mode's value, as Figma draws it. A field the sync leaves alone, such as one an instance
|
|
242
|
+
* overrides, keeps its own value.
|
|
243
|
+
*/
|
|
244
|
+
function resolveBoundNumbersInPlace(
|
|
245
|
+
graph: SceneGraph,
|
|
246
|
+
target: SceneNode,
|
|
247
|
+
updates: Partial<SceneNode>,
|
|
248
|
+
kept: (field: string) => boolean
|
|
173
249
|
): void {
|
|
174
|
-
const
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
250
|
+
const bound = updates.boundVariables ?? target.boundVariables
|
|
251
|
+
// The fields the sync writes, and the ones it binds anew, which keep an equal value only in
|
|
252
|
+
// the component's mode.
|
|
253
|
+
const rebound = updates.boundVariables
|
|
254
|
+
? Object.keys(bound).filter((field) => bound[field] !== target.boundVariables[field])
|
|
255
|
+
: []
|
|
256
|
+
const synced = uniq([...Object.keys(updates), ...rebound]).filter(
|
|
257
|
+
(field) => isNumericVariableBindingField(field) && bound[field] && !kept(field)
|
|
258
|
+
)
|
|
259
|
+
if (synced.length === 0) return
|
|
260
|
+
applySyncedModes(graph, target, updates)
|
|
261
|
+
const resolved = resolvedNumericBindings(graph, { ...target, ...updates })
|
|
262
|
+
Object.assign(updates, pick(resolved, synced as (keyof SceneNode)[]))
|
|
181
263
|
}
|
|
182
264
|
|
|
183
|
-
|
|
265
|
+
/** The modes the copy takes in this sync decide what its bindings resolve to. */
|
|
266
|
+
function applySyncedModes(graph: SceneGraph, target: SceneNode, updates: Partial<SceneNode>): void {
|
|
267
|
+
if (updates.variableModes && !isEqual(updates.variableModes, target.variableModes))
|
|
268
|
+
graph.updateNode(target.id, { variableModes: updates.variableModes })
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export function cloneChildInCoordinates(
|
|
184
272
|
graph: SceneGraph,
|
|
185
273
|
src: SceneNode,
|
|
186
274
|
sourceParent: SceneNode,
|
|
@@ -189,10 +277,12 @@ function cloneChildInCoordinates(
|
|
|
189
277
|
): SceneNode {
|
|
190
278
|
const componentScale =
|
|
191
279
|
(src.componentScale * targetParent.componentScale) / sourceParent.componentScale
|
|
192
|
-
|
|
280
|
+
const clone = graph.createNode(src.type, targetParent.id, {
|
|
193
281
|
...cloneNodeProps(sourceInTargetCoordinates(src, componentScale), src.id, mode),
|
|
194
282
|
componentScale
|
|
195
283
|
})
|
|
284
|
+
copyAppliedBoundPaints(src, clone)
|
|
285
|
+
return clone
|
|
196
286
|
}
|
|
197
287
|
|
|
198
288
|
export function cloneChildrenWithMapping(
|
|
@@ -239,6 +329,9 @@ function childBindingProtection(
|
|
|
239
329
|
) {
|
|
240
330
|
const fields = enclosingInstanceOverrideFields(graph, child)
|
|
241
331
|
fields.push(new Set(overrides.descendants.get(child.id)?.keys()))
|
|
332
|
+
if (child.instanceOverrides.self.size === 0 && fields.every((scope) => scope.size === 0)) {
|
|
333
|
+
return NOTHING_PROTECTED
|
|
334
|
+
}
|
|
242
335
|
const protectedField = bindingProtection(fields)
|
|
243
336
|
return (field: string) => protectedField(field) || ownsSelfFillField(child, field)
|
|
244
337
|
}
|
|
@@ -333,6 +426,53 @@ function sortInstanceChildren(
|
|
|
333
426
|
})
|
|
334
427
|
}
|
|
335
428
|
|
|
429
|
+
export interface MatchedCopy {
|
|
430
|
+
compParent: SceneNode
|
|
431
|
+
instParent: SceneNode
|
|
432
|
+
compChild: SceneNode
|
|
433
|
+
instChild: SceneNode
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
export function syncCopy(
|
|
437
|
+
graph: SceneGraph,
|
|
438
|
+
{ compParent, instParent, compChild, instChild }: MatchedCopy,
|
|
439
|
+
protectedField: (field: string) => boolean,
|
|
440
|
+
linkedLayerId?: string
|
|
441
|
+
): boolean {
|
|
442
|
+
const driven = propertyDrivenFields(graph, instChild, compChild)
|
|
443
|
+
const kept =
|
|
444
|
+
driven.size === 0 && protectedField === NOTHING_PROTECTED
|
|
445
|
+
? NOTHING_PROTECTED
|
|
446
|
+
: (key: string) => driven.has(key) || isProtectedSyncField(instChild, key, protectedField)
|
|
447
|
+
const componentScale =
|
|
448
|
+
(compChild.componentScale * instParent.componentScale) / compParent.componentScale
|
|
449
|
+
const effectiveSource = sourceWithEffectiveFill(graph, compChild, instChild)
|
|
450
|
+
const source = sourceInTargetCoordinates(effectiveSource, componentScale)
|
|
451
|
+
// Which properties a layer serves is the component's to say; a slot or exposed layer
|
|
452
|
+
// created on the component becomes one in every instance.
|
|
453
|
+
const updates: Partial<SceneNode> = {}
|
|
454
|
+
if (instChild.componentScale !== componentScale) {
|
|
455
|
+
updates.componentScale = componentScale
|
|
456
|
+
}
|
|
457
|
+
if (!isEqual(instChild.componentPropertyReferences, compChild.componentPropertyReferences)) {
|
|
458
|
+
updates.componentPropertyReferences = structuredClone(compChild.componentPropertyReferences)
|
|
459
|
+
}
|
|
460
|
+
if (linkedLayerId !== undefined) {
|
|
461
|
+
updates.componentId = linkedLayerId
|
|
462
|
+
}
|
|
463
|
+
syncBindingFields(instChild, source, updates, protectedField)
|
|
464
|
+
const placement = copySyncedFields(graph, updates, instChild, source, {
|
|
465
|
+
compParent,
|
|
466
|
+
instParent,
|
|
467
|
+
factor: componentScale / compChild.componentScale,
|
|
468
|
+
skip: kept
|
|
469
|
+
})
|
|
470
|
+
Object.assign(updates, assignedFieldValues(graph, instChild, compChild))
|
|
471
|
+
keepOwnContentSize(graph, updates, instChild, source, placement)
|
|
472
|
+
updateSyncedProps(graph, instChild, updates, kept)
|
|
473
|
+
return placement.placed !== source
|
|
474
|
+
}
|
|
475
|
+
|
|
336
476
|
/** True when syncing `compParentId` into `instParentId` would form a cycle. */
|
|
337
477
|
function isCyclicSync(graph: SceneGraph, compParentId: string, instParentId: string): boolean {
|
|
338
478
|
return compParentId === instParentId || graph.isDescendant(instParentId, compParentId)
|
|
@@ -394,6 +534,7 @@ function placeMissingChildren(
|
|
|
394
534
|
cloneChildrenWithMapping(graph, compChildId, clone.id)
|
|
395
535
|
}
|
|
396
536
|
applyEnclosingAssignments(graph, clone)
|
|
537
|
+
applyBoundPaintColorsIn(graph, clone.id)
|
|
397
538
|
instChildMap.set(compChildId, clone)
|
|
398
539
|
usedInstChildIds.add(clone.id)
|
|
399
540
|
context.matched.add(clone.id)
|
|
@@ -409,13 +550,13 @@ export function syncChildren(
|
|
|
409
550
|
instParentId: string,
|
|
410
551
|
overrides: InstanceOverrideState,
|
|
411
552
|
context: InstanceSyncContext = createInstanceSyncContext(instParentId, overrides)
|
|
412
|
-
):
|
|
553
|
+
): boolean {
|
|
413
554
|
// An instance inside its own component would clone the component into itself without end.
|
|
414
|
-
if (isCyclicSync(graph, compParentId, instParentId)) return
|
|
555
|
+
if (isCyclicSync(graph, compParentId, instParentId)) return false
|
|
415
556
|
|
|
416
557
|
const compParent = graph.nodes.get(compParentId)
|
|
417
558
|
const instParent = graph.nodes.get(instParentId)
|
|
418
|
-
if (!compParent || !instParent) return
|
|
559
|
+
if (!compParent || !instParent) return false
|
|
419
560
|
|
|
420
561
|
const instChildMap = new Map<string, SceneNode>()
|
|
421
562
|
const usedInstChildIds = new Set<string>()
|
|
@@ -446,34 +587,16 @@ export function syncChildren(
|
|
|
446
587
|
placeMissingChildren(graph, compParent, instParent, instChildMap, usedInstChildIds, context)
|
|
447
588
|
|
|
448
589
|
// Pass 4: Synchronize properties and recurse
|
|
590
|
+
let constrained = false
|
|
449
591
|
for (const compChildId of compParent.childIds) {
|
|
450
592
|
const compChild = graph.nodes.get(compChildId)
|
|
451
593
|
const instChild = instChildMap.get(compChildId)
|
|
452
594
|
if (!compChild || !instChild) continue
|
|
453
595
|
|
|
454
|
-
const
|
|
455
|
-
const
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
const effectiveSource = sourceWithEffectiveFill(graph, compChild, instChild)
|
|
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)
|
|
476
|
-
|
|
596
|
+
const match = { compParent, instParent, compChild, instChild }
|
|
597
|
+
const protection = childBindingProtection(graph, instChild, overrides)
|
|
598
|
+
// True when its constraints placed the copy elsewhere than the component's layer.
|
|
599
|
+
if (syncCopy(graph, match, protection)) constrained = true
|
|
477
600
|
if (
|
|
478
601
|
compChild.childIds.length > 0 &&
|
|
479
602
|
!hasNodeInstanceOverride(overrides, instParentId, instChild.id, 'componentId') &&
|
|
@@ -481,10 +604,13 @@ export function syncChildren(
|
|
|
481
604
|
// its bindings are not synced.
|
|
482
605
|
!ownsSlotContent(graph, instChild, slotPropertyId(compChild))
|
|
483
606
|
) {
|
|
484
|
-
syncChildren(graph, compChildId, instChild.id, overrides, context)
|
|
607
|
+
if (syncChildren(graph, compChildId, instChild.id, overrides, context)) constrained = true
|
|
485
608
|
}
|
|
486
609
|
}
|
|
487
610
|
|
|
488
611
|
// Pass 5: Sort instance children to match component child order
|
|
489
612
|
sortInstanceChildren(graph, instParent, instParentId, compParent.childIds, overrides)
|
|
613
|
+
// A group fits layers its constraints moved; else it keeps the component's box, as a boolean must.
|
|
614
|
+
if (constrained) fitCopiedGroup(graph, instParentId)
|
|
615
|
+
return constrained
|
|
490
616
|
}
|
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,
|
|
@@ -204,6 +205,7 @@ export function createDefaultNode(
|
|
|
204
205
|
sharedSymbolVersion: null,
|
|
205
206
|
publishedVersion: null,
|
|
206
207
|
librarySource: null,
|
|
208
|
+
libraryLayerId: null,
|
|
207
209
|
isPublishable: false,
|
|
208
210
|
isSymbolPublishable: false,
|
|
209
211
|
isExposedInstance: false,
|
package/src/order-keys.ts
CHANGED
|
@@ -9,6 +9,12 @@ const ORDER_KEY_MAX = 126 // '~'
|
|
|
9
9
|
const ORDER_KEY_MID = 'O'
|
|
10
10
|
const INDEX_KEY_BASE = ORDER_KEY_MAX - ORDER_KEY_MIN + 1
|
|
11
11
|
|
|
12
|
+
/** Sorts order keys as Figma does: by plain string comparison. */
|
|
13
|
+
export function compareOrderKeys(a: string, b: string): number {
|
|
14
|
+
if (a === b) return 0
|
|
15
|
+
return a < b ? -1 : 1
|
|
16
|
+
}
|
|
17
|
+
|
|
12
18
|
/** The order key for position `index` of a freshly numbered sibling list. */
|
|
13
19
|
export function fractionalPosition(index: number): string {
|
|
14
20
|
const numTildes = Math.floor(index / INDEX_KEY_BASE)
|
|
@@ -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
|
+
}
|