@open-pencil/scene-graph 0.17.0-beta.3 → 0.17.0-beta.5
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/arrays.d.ts +17 -0
- package/dist/arrays.d.ts.map +1 -0
- package/dist/arrays.js +38 -0
- package/dist/arrays.js.map +1 -0
- package/dist/buffered-events.js +1 -1
- package/dist/buffered-events.js.map +1 -1
- package/dist/clipping.d.ts +7 -1
- package/dist/clipping.d.ts.map +1 -1
- package/dist/clipping.js +23 -19
- package/dist/clipping.js.map +1 -1
- package/dist/components/index.d.ts +2 -2
- package/dist/components/index.js +2 -2
- package/dist/components/properties.d.ts +1 -3
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +2 -9
- package/dist/components/properties.js.map +1 -1
- package/dist/corners.d.ts +22 -0
- package/dist/corners.d.ts.map +1 -0
- package/dist/corners.js +52 -0
- package/dist/corners.js.map +1 -0
- package/dist/css/grid.d.ts +4 -1
- package/dist/css/grid.d.ts.map +1 -1
- package/dist/css/grid.js +16 -7
- package/dist/css/grid.js.map +1 -1
- package/dist/css/index.d.ts +2 -2
- package/dist/css/index.js +2 -2
- package/dist/css/values.d.ts +6 -1
- package/dist/css/values.d.ts.map +1 -1
- package/dist/font-style.d.ts +2 -1
- package/dist/font-style.d.ts.map +1 -1
- package/dist/font-style.js +31 -6
- package/dist/font-style.js.map +1 -1
- package/dist/geometry.d.ts +68 -3
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +155 -5
- package/dist/geometry.js.map +1 -1
- package/dist/gradient.d.ts +9 -2
- package/dist/gradient.d.ts.map +1 -1
- package/dist/gradient.js +29 -1
- package/dist/gradient.js.map +1 -1
- package/dist/graph.d.ts +4 -0
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +30 -4
- package/dist/graph.js.map +1 -1
- package/dist/group-bounds.d.ts.map +1 -1
- package/dist/group-bounds.js +4 -3
- package/dist/group-bounds.js.map +1 -1
- package/dist/guides.d.ts +11 -2
- package/dist/guides.d.ts.map +1 -1
- package/dist/guides.js +30 -1
- package/dist/guides.js.map +1 -1
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +57 -5
- package/dist/hit-test.js.map +1 -1
- package/dist/icons/fit.d.ts +17 -0
- package/dist/icons/fit.d.ts.map +1 -0
- package/dist/icons/fit.js +21 -0
- package/dist/icons/fit.js.map +1 -0
- package/dist/icons/glyph.d.ts.map +1 -1
- package/dist/icons/glyph.js +5 -4
- package/dist/icons/glyph.js.map +1 -1
- package/dist/icons/index.d.ts +2 -1
- package/dist/icons/index.js +2 -1
- package/dist/images.d.ts +14 -4
- package/dist/images.d.ts.map +1 -1
- package/dist/images.js +45 -5
- package/dist/images.js.map +1 -1
- package/dist/instances/assignments.js +39 -6
- package/dist/instances/assignments.js.map +1 -1
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +39 -2
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/kept-overrides.js +2 -2
- package/dist/instances/kept-overrides.js.map +1 -1
- package/dist/instances/matching.js +68 -0
- package/dist/instances/matching.js.map +1 -0
- package/dist/instances/overrides.d.ts +4 -1
- package/dist/instances/overrides.d.ts.map +1 -1
- package/dist/instances/overrides.js +13 -1
- package/dist/instances/overrides.js.map +1 -1
- package/dist/instances/protection.js +67 -0
- package/dist/instances/protection.js.map +1 -0
- package/dist/instances/swap.js +7 -5
- package/dist/instances/swap.js.map +1 -1
- package/dist/instances/sync.js +10 -116
- package/dist/instances/sync.js.map +1 -1
- package/dist/layout/guides.d.ts +17 -1
- package/dist/layout/guides.d.ts.map +1 -1
- package/dist/layout/guides.js +31 -11
- package/dist/layout/guides.js.map +1 -1
- package/dist/layout/index.d.ts +4 -2
- package/dist/layout/index.js +4 -2
- package/dist/layout/limits.d.ts +40 -0
- package/dist/layout/limits.d.ts.map +1 -0
- package/dist/layout/limits.js +54 -0
- package/dist/layout/limits.js.map +1 -0
- package/dist/layout/sizing.d.ts.map +1 -1
- package/dist/layout/sizing.js +0 -1
- package/dist/layout/sizing.js.map +1 -1
- package/dist/layout/spacing.d.ts +10 -0
- package/dist/layout/spacing.d.ts.map +1 -0
- package/dist/layout/spacing.js +12 -0
- package/dist/layout/spacing.js.map +1 -0
- package/dist/masks.d.ts +28 -0
- package/dist/masks.d.ts.map +1 -0
- package/dist/masks.js +53 -0
- package/dist/masks.js.map +1 -0
- package/dist/matrix.d.ts +2 -1
- package/dist/matrix.d.ts.map +1 -1
- package/dist/matrix.js +14 -1
- package/dist/matrix.js.map +1 -1
- package/dist/node-defaults.d.ts +9 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +16 -1
- package/dist/node-defaults.js.map +1 -1
- package/dist/outline/flatten.d.ts.map +1 -1
- package/dist/outline/flatten.js +24 -17
- package/dist/outline/flatten.js.map +1 -1
- package/dist/outline/hit.d.ts +2 -8
- package/dist/outline/hit.d.ts.map +1 -1
- package/dist/outline/hit.js +16 -25
- package/dist/outline/hit.js.map +1 -1
- package/dist/outline/index.d.ts +3 -3
- package/dist/outline/index.js +3 -3
- package/dist/outline/shapes.d.ts +3 -1
- package/dist/outline/shapes.d.ts.map +1 -1
- package/dist/outline/shapes.js +18 -39
- package/dist/outline/shapes.js.map +1 -1
- package/dist/paints/index.d.ts +3 -1
- package/dist/paints/index.js +3 -1
- package/dist/paints/pattern.d.ts +13 -0
- package/dist/paints/pattern.d.ts.map +1 -0
- package/dist/paints/pattern.js +47 -0
- package/dist/paints/pattern.js.map +1 -0
- package/dist/paints/shows.d.ts +11 -0
- package/dist/paints/shows.d.ts.map +1 -0
- package/dist/paints/shows.js +16 -0
- package/dist/paints/shows.js.map +1 -0
- package/dist/paths/arc.d.ts +32 -0
- package/dist/paths/arc.d.ts.map +1 -0
- package/dist/paths/arc.js +49 -0
- package/dist/paths/arc.js.map +1 -0
- package/dist/paths/index.d.ts +5 -0
- package/dist/paths/index.js +5 -0
- package/dist/paths/network.d.ts +43 -0
- package/dist/paths/network.d.ts.map +1 -0
- package/dist/paths/network.js +249 -0
- package/dist/paths/network.js.map +1 -0
- package/dist/paths/sink.d.ts +20 -0
- package/dist/paths/sink.d.ts.map +1 -0
- package/dist/paths/sink.js +32 -0
- package/dist/paths/sink.js.map +1 -0
- package/dist/paths/smooth-corners.d.ts +25 -0
- package/dist/paths/smooth-corners.d.ts.map +1 -0
- package/dist/paths/smooth-corners.js +196 -0
- package/dist/paths/smooth-corners.js.map +1 -0
- package/dist/plugin-data/fields.d.ts +4 -0
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +2 -0
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/polygon.d.ts +1 -3
- package/dist/polygon.d.ts.map +1 -1
- package/dist/polygon.js +1 -16
- package/dist/polygon.js.map +1 -1
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +5 -5
- package/dist/resize.js.map +1 -1
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +25 -3
- package/dist/scaling/node.js.map +1 -1
- package/dist/text/auto-resize.d.ts +15 -0
- package/dist/text/auto-resize.d.ts.map +1 -0
- package/dist/text/auto-resize.js +23 -0
- package/dist/text/auto-resize.js.map +1 -0
- package/dist/text/index.d.ts +4 -3
- package/dist/text/index.js +4 -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 +14 -5
- package/dist/text/overflow.d.ts.map +1 -1
- package/dist/text/overflow.js +24 -6
- package/dist/text/overflow.js.map +1 -1
- package/dist/text/path.js +10 -0
- package/dist/text/path.js.map +1 -0
- package/dist/text/picture.js +5 -5
- package/dist/text/picture.js.map +1 -1
- package/dist/transfer/dependencies.d.ts.map +1 -1
- package/dist/transfer/dependencies.js +3 -2
- package/dist/transfer/dependencies.js.map +1 -1
- package/dist/transfer/nodes.js +2 -1
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/types.d.ts +21 -8
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/variables/index.d.ts +4 -3
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +4 -3
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/lookup.d.ts +20 -0
- package/dist/variables/lookup.d.ts.map +1 -0
- package/dist/variables/lookup.js +34 -0
- package/dist/variables/lookup.js.map +1 -0
- package/dist/variables/paints.d.ts +35 -4
- package/dist/variables/paints.d.ts.map +1 -1
- package/dist/variables/paints.js +67 -16
- package/dist/variables/paints.js.map +1 -1
- package/dist/variables/resolution.d.ts +3 -1
- package/dist/variables/resolution.d.ts.map +1 -1
- package/dist/variables/resolution.js +8 -3
- package/dist/variables/resolution.js.map +1 -1
- package/dist/variables/subtree.js +1 -1
- package/dist/variables/subtree.js.map +1 -1
- package/dist/vector-network.d.ts.map +1 -1
- package/dist/vector-network.js +12 -76
- package/dist/vector-network.js.map +1 -1
- package/package.json +25 -1
- package/src/arrays.ts +37 -0
- package/src/buffered-events.ts +2 -1
- package/src/clipping.ts +11 -8
- package/src/components/properties.ts +1 -13
- package/src/corners.ts +93 -0
- package/src/css/grid.ts +21 -7
- package/src/css/index.ts +1 -1
- package/src/font-style.ts +38 -4
- package/src/geometry.ts +219 -6
- package/src/gradient.ts +23 -1
- package/src/graph.ts +22 -4
- package/src/group-bounds.ts +7 -3
- package/src/guides.ts +26 -1
- package/src/hit-test.ts +68 -10
- package/src/icons/fit.ts +24 -0
- package/src/icons/glyph.ts +5 -4
- package/src/icons/index.ts +1 -0
- package/src/images.ts +67 -7
- package/src/instances/assignments.ts +65 -7
- package/src/instances/index.ts +53 -4
- package/src/instances/kept-overrides.ts +2 -5
- package/src/instances/matching.ts +101 -0
- package/src/instances/overrides.ts +18 -0
- package/src/instances/protection.ts +103 -0
- package/src/instances/swap.ts +11 -9
- package/src/instances/sync.ts +11 -184
- package/src/layout/guides.ts +42 -9
- package/src/layout/index.ts +2 -0
- package/src/layout/limits.ts +64 -0
- package/src/layout/sizing.ts +0 -2
- package/src/layout/spacing.ts +11 -0
- package/src/masks.ts +51 -0
- package/src/matrix.ts +15 -2
- package/src/node-defaults.ts +23 -3
- package/src/outline/flatten.ts +30 -18
- package/src/outline/hit.ts +16 -33
- package/src/outline/index.ts +2 -2
- package/src/outline/shapes.ts +19 -36
- package/src/paints/index.ts +2 -0
- package/src/paints/pattern.ts +45 -0
- package/src/paints/shows.ts +13 -0
- package/src/paths/arc.ts +59 -0
- package/src/paths/index.ts +4 -0
- package/src/paths/network.ts +334 -0
- package/src/paths/sink.ts +50 -0
- package/src/paths/smooth-corners.ts +356 -0
- package/src/plugin-data/fields.ts +2 -0
- package/src/polygon.ts +0 -17
- package/src/resize.ts +6 -7
- package/src/scaling/node.ts +29 -3
- package/src/text/auto-resize.ts +25 -0
- package/src/text/index.ts +1 -0
- package/src/text/lengths.ts +11 -0
- package/src/text/overflow.ts +32 -6
- package/src/text/path.ts +13 -0
- package/src/text/picture.ts +5 -5
- package/src/transfer/dependencies.ts +3 -2
- package/src/transfer/nodes.ts +2 -1
- package/src/types.ts +25 -8
- package/src/variables/index.ts +1 -0
- package/src/variables/lookup.ts +31 -0
- package/src/variables/paints.ts +101 -20
- package/src/variables/resolution.ts +14 -2
- package/src/variables/subtree.ts +1 -1
- package/src/vector-network.ts +17 -138
package/dist/instances/swap.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { hasInstanceOverride, overriddenBindingField, setInstanceOverride } from "./overrides.js";
|
|
1
|
+
import { hasInstanceOverride, overriddenAssignmentId, overriddenBindingField, setInstanceOverride } from "./overrides.js";
|
|
2
|
+
import { appendAll } from "../arrays.js";
|
|
2
3
|
import { ownsSelfFillField } from "./main-component.js";
|
|
3
4
|
import { ownsSlotContent, slotPropertyId } from "../slots/frames.js";
|
|
4
5
|
import { applyBoundPaintColorsIn, refreshBoundPaintsIn } from "../variables/paints.js";
|
|
5
|
-
import {
|
|
6
|
+
import { NOTHING_PROTECTED, bindingProtection } from "./protection.js";
|
|
7
|
+
import { reapplyComponentPropertyAssignments } from "../components/properties.js";
|
|
6
8
|
import { applyEnclosingAssignments } from "./assignments.js";
|
|
7
9
|
import { keepOverrides, restoreOverrides } from "./kept-overrides.js";
|
|
8
|
-
import {
|
|
10
|
+
import { cloneChildInCoordinates, cloneChildrenWithMapping, syncCopy } from "./sync.js";
|
|
9
11
|
import { isEqual } from "es-toolkit/predicate";
|
|
10
12
|
//#region src/instances/swap.ts
|
|
11
13
|
const REPLACED_LAYER_FIELDS = [
|
|
@@ -74,7 +76,7 @@ function takeCopy(byLayer, name, ordinal) {
|
|
|
74
76
|
function untakenCopies(byLayer) {
|
|
75
77
|
const untaken = [];
|
|
76
78
|
for (const namesakes of byLayer.linked.values()) for (const copy of namesakes) if (copy) untaken.push(copy);
|
|
77
|
-
for (const namesakes of byLayer.unlinked.values()) untaken
|
|
79
|
+
for (const namesakes of byLayer.unlinked.values()) appendAll(untaken, namesakes);
|
|
78
80
|
return untaken;
|
|
79
81
|
}
|
|
80
82
|
function changeOwner(swap, owner) {
|
|
@@ -189,7 +191,7 @@ function matchCopies(swap, compParent, instParent) {
|
|
|
189
191
|
if (!copy || copy.type === layer.type) return copy;
|
|
190
192
|
retyped.set(index, copy);
|
|
191
193
|
});
|
|
192
|
-
stale
|
|
194
|
+
appendAll(stale, untakenCopies(byLayer));
|
|
193
195
|
return {
|
|
194
196
|
matched,
|
|
195
197
|
stale,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"swap.js","names":[],"sources":["../../src/instances/swap.ts"],"sourcesContent":["import { isEqual } from 'es-toolkit/predicate'\n\nimport {\n overriddenAssignmentId,\n reapplyComponentPropertyAssignments\n} from '../components/properties'\nimport type { SceneGraph } from '../graph'\nimport { ownsSlotContent, slotPropertyId } from '../slots/frames'\nimport type { SceneNode } from '../types'\nimport { applyBoundPaintColorsIn, refreshBoundPaintsIn } from '../variables/paints'\nimport { applyEnclosingAssignments } from './assignments'\nimport { keepOverrides, restoreOverrides, type OverrideScope } from './kept-overrides'\nimport { ownsSelfFillField } from './main-component'\nimport { hasInstanceOverride, overriddenBindingField, setInstanceOverride } from './overrides'\nimport {\n bindingProtection,\n cloneChildInCoordinates,\n cloneChildrenWithMapping,\n NOTHING_PROTECTED,\n syncCopy,\n type MatchedCopy\n} from './sync'\n\nconst REPLACED_LAYER_FIELDS = ['textPathData', 'textPathBox', 'exportSettings'] as const\n\ninterface CopiesByLayer {\n linked: Map<string, (SceneNode | undefined)[]>\n unlinked: Map<string, SceneNode[]>\n}\n\ninterface Swap {\n graph: SceneGraph\n instance: SceneNode\n owners: SceneNode[]\n changedOwners: Set<SceneNode>\n parked: ReadonlySet<string>\n namesakeOrdinals: Map<string, number>\n}\n\nfunction sourceLayer(swap: Swap, copy: SceneNode): SceneNode | undefined {\n for (let index = swap.owners.length - 1; index >= 0; index--) {\n const fields = swap.owners[index].instanceOverrides.descendants.get(copy.id)\n const linked = fields?.get('sourceComponentId')\n if (typeof linked === 'string') {\n return swap.graph.getNode(linked)\n }\n }\n const linked = copy.componentId ? swap.graph.getNode(copy.componentId) : undefined\n return linked?.type === 'COMPONENT' ? undefined : linked\n}\n\nfunction namesakeOrdinal(swap: Swap, layer: SceneNode): number {\n const known = swap.namesakeOrdinals.get(layer.id)\n if (known !== undefined) {\n return known\n }\n const parent = layer.parentId ? swap.graph.getNode(layer.parentId) : undefined\n const seen = new Map<string, number>()\n for (const id of parent?.childIds ?? []) {\n const sibling = swap.graph.getNode(id)\n if (sibling) {\n const ordinal = seen.get(sibling.name) ?? 0\n seen.set(sibling.name, ordinal + 1)\n swap.namesakeOrdinals.set(id, ordinal)\n }\n }\n return swap.namesakeOrdinals.get(layer.id) ?? 0\n}\n\nfunction addLinkedCopy(\n byLayer: CopiesByLayer,\n copy: SceneNode,\n source: SceneNode,\n ordinal: number\n): boolean {\n const namesakes = byLayer.linked.get(source.name) ?? []\n byLayer.linked.set(source.name, namesakes)\n if (namesakes[ordinal] !== undefined) {\n return false\n }\n namesakes[ordinal] = copy\n return true\n}\n\nfunction addUnlinkedCopy(byLayer: CopiesByLayer, copy: SceneNode): void {\n const namesakes = byLayer.unlinked.get(copy.name)\n if (namesakes) {\n namesakes.push(copy)\n } else {\n byLayer.unlinked.set(copy.name, [copy])\n }\n}\n\nfunction copiesByLayer(swap: Swap, parent: SceneNode, stale: SceneNode[]): CopiesByLayer {\n const byLayer: CopiesByLayer = { linked: new Map(), unlinked: new Map() }\n for (const id of parent.childIds) {\n const copy = swap.graph.getNode(id)\n if (!copy || swap.parked.has(id)) {\n continue\n }\n const source = sourceLayer(swap, copy)\n if (!source) {\n addUnlinkedCopy(byLayer, copy)\n } else if (!addLinkedCopy(byLayer, copy, source, namesakeOrdinal(swap, source))) {\n stale.push(copy)\n }\n }\n return byLayer\n}\n\nfunction takeCopy(byLayer: CopiesByLayer, name: string, ordinal: number): SceneNode | undefined {\n const namesakes = byLayer.linked.get(name)\n const linked = namesakes?.[ordinal]\n if (namesakes && linked) {\n namesakes[ordinal] = undefined\n return linked\n }\n return byLayer.unlinked.get(name)?.shift()\n}\n\nfunction untakenCopies(byLayer: CopiesByLayer): SceneNode[] {\n const untaken: SceneNode[] = []\n for (const namesakes of byLayer.linked.values()) {\n for (const copy of namesakes) {\n if (copy) {\n untaken.push(copy)\n }\n }\n }\n for (const namesakes of byLayer.unlinked.values()) {\n untaken.push(...namesakes)\n }\n return untaken\n}\n\nfunction changeOwner(swap: Swap, owner: SceneNode): void {\n if (!swap.changedOwners.has(owner)) {\n swap.graph.willChangeNode(owner)\n swap.changedOwners.add(owner)\n }\n}\n\nfunction forgetOverrides(swap: Swap, node: SceneNode): void {\n for (const owner of swap.owners) {\n if (owner.instanceOverrides.descendants.has(node.id)) {\n changeOwner(swap, owner)\n owner.instanceOverrides.descendants.delete(node.id)\n }\n }\n for (const child of swap.graph.getChildren(node.id)) {\n forgetOverrides(swap, child)\n }\n}\n\nfunction removeCopy(swap: Swap, copy: SceneNode): void {\n if (swap.owners.some((owner) => owner.instanceOverrides.descendants.size > 0)) {\n forgetOverrides(swap, copy)\n }\n swap.graph.deleteNode(copy.id)\n}\n\n/** The switch as the kept overrides see it. */\nfunction overrideScope(swap: Swap): OverrideScope {\n return {\n graph: swap.graph,\n instance: swap.instance,\n owners: swap.owners,\n willChange: (owner) => changeOwner(swap, owner)\n }\n}\n\n/** Removes a copy the new variant has no layer for, keeping its overrides for a later switch. */\nfunction removeStaleCopy(swap: Swap, copy: SceneNode): void {\n keepOverrides(overrideScope(swap), copy)\n removeCopy(swap, copy)\n}\n\nfunction isSwappedCopy(swap: Swap, copy: SceneNode): boolean {\n return swap.owners.some((owner) =>\n hasInstanceOverride(owner.instanceOverrides, owner.id, copy.id, 'componentId')\n )\n}\n\nfunction relinkSourceLayer(swap: Swap, copy: SceneNode, layer: SceneNode): void {\n for (const owner of swap.owners) {\n const linked = owner.instanceOverrides.descendants.get(copy.id)\n if (linked?.has('sourceComponentId')) {\n changeOwner(swap, owner)\n setInstanceOverride(owner.instanceOverrides, owner.id, copy.id, 'sourceComponentId', layer.id)\n }\n }\n}\n\nfunction linkCopy(\n swap: Swap,\n copy: SceneNode,\n layer: SceneNode,\n swapped: boolean\n): string | undefined {\n relinkSourceLayer(swap, copy, layer)\n return swapped || copy.componentId === layer.id ? undefined : layer.id\n}\n\nfunction copyProtection(\n swap: Swap,\n copy: SceneNode,\n previous: SceneNode | undefined\n): (field: string) => boolean {\n const scopes: ReadonlySet<string>[] = []\n for (const owner of swap.owners) {\n const fields = owner.instanceOverrides.descendants.get(copy.id)\n if (fields) {\n scopes.push(new Set(fields.keys()))\n }\n }\n const ownsField = (field: string) =>\n ownsSelfFillField(copy, field) ||\n (previous !== undefined && divergesFromLayer(copy, previous, field))\n if (scopes.length === 0) {\n return copy.instanceOverrides.self.size === 0 ? NOTHING_PROTECTED : ownsField\n }\n const protectedField = bindingProtection(scopes)\n return (field) => protectedField(field) || ownsField(field)\n}\n\nfunction divergesFromLayer(copy: SceneNode, layer: SceneNode, field: string): boolean {\n const own = copy.instanceOverrides.self\n const binding = overriddenBindingField(field)\n if (binding !== null) {\n return (\n (own.has(field) || own.has('boundVariables')) &&\n !isEqual(copy.boundVariables[binding], layer.boundVariables[binding])\n )\n }\n if (field === 'boundVariables' || !own.has(field)) {\n return false\n }\n const key = field as keyof SceneNode\n return !isEqual(copy[key], layer[key])\n}\n\nfunction followReplacedLayerFields(graph: SceneGraph, copy: SceneNode, layer: SceneNode): void {\n const changes: Partial<SceneNode> = {}\n for (const key of REPLACED_LAYER_FIELDS) {\n if (copy[key] !== layer[key] && !isEqual(copy[key], layer[key])) {\n Object.assign(changes, { [key]: structuredClone(layer[key]) })\n }\n }\n if (Object.keys(changes).length > 0) {\n graph.updateNode(copy.id, changes)\n }\n}\n\nfunction userAssignedIds(swap: Swap, copy: SceneNode): Set<string> {\n const ids = new Set<string>()\n for (const owner of swap.owners) {\n for (const field of owner.instanceOverrides.descendants.get(copy.id)?.keys() ?? []) {\n const propertyId = overriddenAssignmentId(field)\n if (propertyId !== undefined) {\n ids.add(propertyId)\n }\n }\n }\n return ids\n}\n\nfunction followAssignments(swap: Swap, copy: SceneNode, layer: SceneNode): boolean {\n if (isEqual(copy.componentPropertyAssignments, layer.componentPropertyAssignments)) {\n return false\n }\n const userAssigned = userAssignedIds(swap, copy)\n const assignments = { ...layer.componentPropertyAssignments }\n for (const [propertyId, value] of Object.entries(copy.componentPropertyAssignments)) {\n if (userAssigned.has(propertyId)) {\n assignments[propertyId] = value\n }\n }\n if (isEqual(assignments, copy.componentPropertyAssignments)) {\n return false\n }\n swap.graph.updateNode(copy.id, { componentPropertyAssignments: assignments })\n return true\n}\n\nfunction cloneLayer(\n swap: Swap,\n compParent: SceneNode,\n instParent: SceneNode,\n layer: SceneNode\n): SceneNode {\n const clone = cloneChildInCoordinates(swap.graph, layer, compParent, instParent)\n if (layer.childIds.length > 0) {\n cloneChildrenWithMapping(swap.graph, layer.id, clone.id)\n }\n // A new copy shows its variables where it sits; overrides restored after this keep theirs.\n applyBoundPaintColorsIn(swap.graph, clone.id)\n return clone\n}\n\nfunction followInside(swap: Swap, layer: SceneNode, copy: SceneNode): void {\n const owner = copy.type === 'INSTANCE'\n if (owner) {\n swap.owners.push(copy)\n }\n followLayers(swap, layer, copy)\n if (owner) {\n swap.owners.pop()\n }\n}\n\nfunction matchCopies(swap: Swap, compParent: SceneNode, instParent: SceneNode) {\n const stale: SceneNode[] = []\n /** Copies whose layer the new variant draws as another type, by that layer's index. */\n const retyped = new Map<number, SceneNode>()\n const byLayer = copiesByLayer(swap, instParent, stale)\n const ordinals = new Map<string, number>()\n const matched = compParent.childIds.map((layerId, index) => {\n const layer = swap.graph.getNode(layerId)\n if (!layer) {\n return undefined\n }\n const ordinal = ordinals.get(layer.name) ?? 0\n ordinals.set(layer.name, ordinal + 1)\n const copy = takeCopy(byLayer, layer.name, ordinal)\n if (!copy || copy.type === layer.type) {\n return copy\n }\n retyped.set(index, copy)\n return undefined\n })\n stale.push(...untakenCopies(byLayer))\n return { matched, stale, retyped }\n}\n\nfunction standsInOrderFor(swap: Swap, copy: SceneNode, layer: SceneNode, index: number): boolean {\n if (copy.type !== layer.type || swap.parked.has(copy.id)) {\n return false\n }\n const source = sourceLayer(swap, copy)\n if (!source) {\n return copy.name === layer.name\n }\n const sourceParent = source.parentId ? swap.graph.getNode(source.parentId) : undefined\n return source.name === layer.name && sourceParent?.childIds[index] === source.id\n}\n\nfunction hasCopiesInOrder(swap: Swap, compParent: SceneNode, instParent: SceneNode): boolean {\n const layerIds = compParent.childIds\n const copyIds = instParent.childIds\n if (layerIds.length !== copyIds.length) {\n return false\n }\n for (let index = 0; index < layerIds.length; index++) {\n const layer = swap.graph.getNode(layerIds[index])\n const copy = swap.graph.getNode(copyIds[index])\n if (!layer || !copy || !standsInOrderFor(swap, copy, layer, index)) {\n return false\n }\n }\n return true\n}\n\nfunction followCopiesInOrder(swap: Swap, compParent: SceneNode, instParent: SceneNode): void {\n compParent.childIds.forEach((layerId, index) => {\n const compChild = swap.graph.getNode(layerId)\n const instChild = swap.graph.getNode(instParent.childIds[index])\n if (compChild && instChild) {\n followCopy(swap, { compParent, instParent, compChild, instChild })\n }\n })\n}\n\nfunction followLayers(swap: Swap, compParent: SceneNode, instParent: SceneNode): void {\n if (hasCopiesInOrder(swap, compParent, instParent)) {\n followCopiesInOrder(swap, compParent, instParent)\n return\n }\n const { graph } = swap\n const { matched, stale, retyped } = matchCopies(swap, compParent, instParent)\n // Paths are read while the old copies still stand in their old order. A copy the new variant\n // draws as another type gives its overrides to the new copy the same way, so the layers in it\n // keep theirs too, as Figma keeps a rectangle's opacity on the ellipse of the same name.\n for (const copy of [...stale, ...retyped.values()]) {\n removeStaleCopy(swap, copy)\n }\n compParent.childIds.forEach((layerId, index) => {\n const layer = graph.getNode(layerId)\n if (!layer) {\n return\n }\n const copy = matched[index] ?? cloneLayer(swap, compParent, instParent, layer)\n if (instParent.childIds[index] !== copy.id) {\n graph.insertChildAt(copy.id, instParent.id, index)\n }\n if (copy === matched[index]) {\n followCopy(swap, { compParent, instParent, compChild: layer, instChild: copy })\n } else {\n // A new copy takes the values the instances around it assign, as one a sync adds does.\n applyEnclosingAssignments(graph, copy)\n restoreOverrides(overrideScope(swap), copy)\n // A mode restored on the copy changes what its unclaimed bound paints resolve to.\n refreshBoundPaintsIn(graph, copy.id)\n }\n })\n}\n\nfunction followCopy(swap: Swap, match: MatchedCopy): void {\n const { compChild: layer, instChild: copy } = match\n const swapped = isSwappedCopy(swap, copy)\n const previous = sourceLayer(swap, copy)\n const protection = copyProtection(swap, copy, previous)\n const linkedLayerId = linkCopy(swap, copy, layer, swapped)\n syncCopy(swap.graph, match, protection, linkedLayerId)\n if (previous !== layer) {\n followReplacedLayerFields(swap.graph, copy, layer)\n }\n if (swapped || ownsSlotContent(swap.graph, copy, slotPropertyId(layer))) {\n return\n }\n const reassigned = copy.type === 'INSTANCE' && followAssignments(swap, copy, layer)\n followInside(swap, layer, copy)\n if (reassigned) {\n reapplyComponentPropertyAssignments(swap.graph, copy.id)\n }\n}\n\nfunction enclosingInstances(graph: SceneGraph, instance: SceneNode): SceneNode[] {\n const owners: SceneNode[] = []\n graph.closest(instance.id, (node) => {\n if (node.type === 'INSTANCE') {\n owners.push(node)\n }\n return false\n })\n return owners.reverse()\n}\n\nexport function followComponentLayers(\n graph: SceneGraph,\n instance: SceneNode,\n component: SceneNode,\n parked: ReadonlySet<string>\n): void {\n const swap: Swap = {\n graph,\n instance,\n owners: enclosingInstances(graph, instance),\n changedOwners: new Set(),\n parked,\n namesakeOrdinals: new Map()\n }\n followLayers(swap, component, instance)\n for (const owner of swap.changedOwners) {\n graph.updateNode(owner.id, { instanceOverrides: owner.instanceOverrides })\n }\n}\n"],"mappings":";;;;;;;;;;AAuBA,MAAM,wBAAwB;CAAC;CAAgB;CAAe;AAAgB;AAgB9E,SAAS,YAAY,MAAY,MAAwC;CACvE,KAAK,IAAI,QAAQ,KAAK,OAAO,SAAS,GAAG,SAAS,GAAG,SAAS;EAE5D,MAAM,SADS,KAAK,OAAO,MAAM,CAAC,kBAAkB,YAAY,IAAI,KAAK,EACrD,CAAC,EAAE,IAAI,mBAAmB;EAC9C,IAAI,OAAO,WAAW,UACpB,OAAO,KAAK,MAAM,QAAQ,MAAM;CAEpC;CACA,MAAM,SAAS,KAAK,cAAc,KAAK,MAAM,QAAQ,KAAK,WAAW,IAAI,KAAA;CACzE,OAAO,QAAQ,SAAS,cAAc,KAAA,IAAY;AACpD;AAEA,SAAS,gBAAgB,MAAY,OAA0B;CAC7D,MAAM,QAAQ,KAAK,iBAAiB,IAAI,MAAM,EAAE;CAChD,IAAI,UAAU,KAAA,GACZ,OAAO;CAET,MAAM,SAAS,MAAM,WAAW,KAAK,MAAM,QAAQ,MAAM,QAAQ,IAAI,KAAA;CACrE,MAAM,uBAAO,IAAI,IAAoB;CACrC,KAAK,MAAM,MAAM,QAAQ,YAAY,CA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C1C,+BAAe,IAAI,IAAI;EACvB;EACA,kCAAkB,IAAI,IAAI;CAC5B;CACA,aAAa,MAAM,WAAW,QAAQ;CACtC,KAAK,MAAM,SAAS,KAAK,eACvB,MAAM,WAAW,MAAM,IAAI,EAAE,mBAAmB,MAAM,kBAAkB,CAAC;AAE7E"}
|
|
1
|
+
{"version":3,"file":"swap.js","names":[],"sources":["../../src/instances/swap.ts"],"sourcesContent":["import { isEqual } from 'es-toolkit/predicate'\n\nimport { appendAll } from '../arrays'\nimport { reapplyComponentPropertyAssignments } from '../components/properties'\nimport type { SceneGraph } from '../graph'\nimport { ownsSlotContent, slotPropertyId } from '../slots/frames'\nimport type { SceneNode } from '../types'\nimport { applyBoundPaintColorsIn, refreshBoundPaintsIn } from '../variables/paints'\nimport { applyEnclosingAssignments } from './assignments'\nimport { keepOverrides, restoreOverrides, type OverrideScope } from './kept-overrides'\nimport { ownsSelfFillField } from './main-component'\nimport {\n hasInstanceOverride,\n overriddenAssignmentId,\n overriddenBindingField,\n setInstanceOverride\n} from './overrides'\nimport { bindingProtection, NOTHING_PROTECTED } from './protection'\nimport {\n cloneChildInCoordinates,\n cloneChildrenWithMapping,\n syncCopy,\n type MatchedCopy\n} from './sync'\n\nconst REPLACED_LAYER_FIELDS = ['textPathData', 'textPathBox', 'exportSettings'] as const\n\ninterface CopiesByLayer {\n linked: Map<string, (SceneNode | undefined)[]>\n unlinked: Map<string, SceneNode[]>\n}\n\ninterface Swap {\n graph: SceneGraph\n instance: SceneNode\n owners: SceneNode[]\n changedOwners: Set<SceneNode>\n parked: ReadonlySet<string>\n namesakeOrdinals: Map<string, number>\n}\n\nfunction sourceLayer(swap: Swap, copy: SceneNode): SceneNode | undefined {\n for (let index = swap.owners.length - 1; index >= 0; index--) {\n const fields = swap.owners[index].instanceOverrides.descendants.get(copy.id)\n const linked = fields?.get('sourceComponentId')\n if (typeof linked === 'string') {\n return swap.graph.getNode(linked)\n }\n }\n const linked = copy.componentId ? swap.graph.getNode(copy.componentId) : undefined\n return linked?.type === 'COMPONENT' ? undefined : linked\n}\n\nfunction namesakeOrdinal(swap: Swap, layer: SceneNode): number {\n const known = swap.namesakeOrdinals.get(layer.id)\n if (known !== undefined) {\n return known\n }\n const parent = layer.parentId ? swap.graph.getNode(layer.parentId) : undefined\n const seen = new Map<string, number>()\n for (const id of parent?.childIds ?? []) {\n const sibling = swap.graph.getNode(id)\n if (sibling) {\n const ordinal = seen.get(sibling.name) ?? 0\n seen.set(sibling.name, ordinal + 1)\n swap.namesakeOrdinals.set(id, ordinal)\n }\n }\n return swap.namesakeOrdinals.get(layer.id) ?? 0\n}\n\nfunction addLinkedCopy(\n byLayer: CopiesByLayer,\n copy: SceneNode,\n source: SceneNode,\n ordinal: number\n): boolean {\n const namesakes = byLayer.linked.get(source.name) ?? []\n byLayer.linked.set(source.name, namesakes)\n if (namesakes[ordinal] !== undefined) {\n return false\n }\n namesakes[ordinal] = copy\n return true\n}\n\nfunction addUnlinkedCopy(byLayer: CopiesByLayer, copy: SceneNode): void {\n const namesakes = byLayer.unlinked.get(copy.name)\n if (namesakes) {\n namesakes.push(copy)\n } else {\n byLayer.unlinked.set(copy.name, [copy])\n }\n}\n\nfunction copiesByLayer(swap: Swap, parent: SceneNode, stale: SceneNode[]): CopiesByLayer {\n const byLayer: CopiesByLayer = { linked: new Map(), unlinked: new Map() }\n for (const id of parent.childIds) {\n const copy = swap.graph.getNode(id)\n if (!copy || swap.parked.has(id)) {\n continue\n }\n const source = sourceLayer(swap, copy)\n if (!source) {\n addUnlinkedCopy(byLayer, copy)\n } else if (!addLinkedCopy(byLayer, copy, source, namesakeOrdinal(swap, source))) {\n stale.push(copy)\n }\n }\n return byLayer\n}\n\nfunction takeCopy(byLayer: CopiesByLayer, name: string, ordinal: number): SceneNode | undefined {\n const namesakes = byLayer.linked.get(name)\n const linked = namesakes?.[ordinal]\n if (namesakes && linked) {\n namesakes[ordinal] = undefined\n return linked\n }\n return byLayer.unlinked.get(name)?.shift()\n}\n\nfunction untakenCopies(byLayer: CopiesByLayer): SceneNode[] {\n const untaken: SceneNode[] = []\n for (const namesakes of byLayer.linked.values()) {\n for (const copy of namesakes) {\n if (copy) {\n untaken.push(copy)\n }\n }\n }\n for (const namesakes of byLayer.unlinked.values()) {\n appendAll(untaken, namesakes)\n }\n return untaken\n}\n\nfunction changeOwner(swap: Swap, owner: SceneNode): void {\n if (!swap.changedOwners.has(owner)) {\n swap.graph.willChangeNode(owner)\n swap.changedOwners.add(owner)\n }\n}\n\nfunction forgetOverrides(swap: Swap, node: SceneNode): void {\n for (const owner of swap.owners) {\n if (owner.instanceOverrides.descendants.has(node.id)) {\n changeOwner(swap, owner)\n owner.instanceOverrides.descendants.delete(node.id)\n }\n }\n for (const child of swap.graph.getChildren(node.id)) {\n forgetOverrides(swap, child)\n }\n}\n\nfunction removeCopy(swap: Swap, copy: SceneNode): void {\n if (swap.owners.some((owner) => owner.instanceOverrides.descendants.size > 0)) {\n forgetOverrides(swap, copy)\n }\n swap.graph.deleteNode(copy.id)\n}\n\n/** The switch as the kept overrides see it. */\nfunction overrideScope(swap: Swap): OverrideScope {\n return {\n graph: swap.graph,\n instance: swap.instance,\n owners: swap.owners,\n willChange: (owner) => changeOwner(swap, owner)\n }\n}\n\n/** Removes a copy the new variant has no layer for, keeping its overrides for a later switch. */\nfunction removeStaleCopy(swap: Swap, copy: SceneNode): void {\n keepOverrides(overrideScope(swap), copy)\n removeCopy(swap, copy)\n}\n\nfunction isSwappedCopy(swap: Swap, copy: SceneNode): boolean {\n return swap.owners.some((owner) =>\n hasInstanceOverride(owner.instanceOverrides, owner.id, copy.id, 'componentId')\n )\n}\n\nfunction relinkSourceLayer(swap: Swap, copy: SceneNode, layer: SceneNode): void {\n for (const owner of swap.owners) {\n const linked = owner.instanceOverrides.descendants.get(copy.id)\n if (linked?.has('sourceComponentId')) {\n changeOwner(swap, owner)\n setInstanceOverride(owner.instanceOverrides, owner.id, copy.id, 'sourceComponentId', layer.id)\n }\n }\n}\n\nfunction linkCopy(\n swap: Swap,\n copy: SceneNode,\n layer: SceneNode,\n swapped: boolean\n): string | undefined {\n relinkSourceLayer(swap, copy, layer)\n return swapped || copy.componentId === layer.id ? undefined : layer.id\n}\n\nfunction copyProtection(\n swap: Swap,\n copy: SceneNode,\n previous: SceneNode | undefined\n): (field: string) => boolean {\n const scopes: ReadonlySet<string>[] = []\n for (const owner of swap.owners) {\n const fields = owner.instanceOverrides.descendants.get(copy.id)\n if (fields) {\n scopes.push(new Set(fields.keys()))\n }\n }\n const ownsField = (field: string) =>\n ownsSelfFillField(copy, field) ||\n (previous !== undefined && divergesFromLayer(copy, previous, field))\n if (scopes.length === 0) {\n return copy.instanceOverrides.self.size === 0 ? NOTHING_PROTECTED : ownsField\n }\n const protectedField = bindingProtection(scopes)\n return (field) => protectedField(field) || ownsField(field)\n}\n\nfunction divergesFromLayer(copy: SceneNode, layer: SceneNode, field: string): boolean {\n const own = copy.instanceOverrides.self\n const binding = overriddenBindingField(field)\n if (binding !== null) {\n return (\n (own.has(field) || own.has('boundVariables')) &&\n !isEqual(copy.boundVariables[binding], layer.boundVariables[binding])\n )\n }\n if (field === 'boundVariables' || !own.has(field)) {\n return false\n }\n const key = field as keyof SceneNode\n return !isEqual(copy[key], layer[key])\n}\n\nfunction followReplacedLayerFields(graph: SceneGraph, copy: SceneNode, layer: SceneNode): void {\n const changes: Partial<SceneNode> = {}\n for (const key of REPLACED_LAYER_FIELDS) {\n if (copy[key] !== layer[key] && !isEqual(copy[key], layer[key])) {\n Object.assign(changes, { [key]: structuredClone(layer[key]) })\n }\n }\n if (Object.keys(changes).length > 0) {\n graph.updateNode(copy.id, changes)\n }\n}\n\nfunction userAssignedIds(swap: Swap, copy: SceneNode): Set<string> {\n const ids = new Set<string>()\n for (const owner of swap.owners) {\n for (const field of owner.instanceOverrides.descendants.get(copy.id)?.keys() ?? []) {\n const propertyId = overriddenAssignmentId(field)\n if (propertyId !== undefined) {\n ids.add(propertyId)\n }\n }\n }\n return ids\n}\n\nfunction followAssignments(swap: Swap, copy: SceneNode, layer: SceneNode): boolean {\n if (isEqual(copy.componentPropertyAssignments, layer.componentPropertyAssignments)) {\n return false\n }\n const userAssigned = userAssignedIds(swap, copy)\n const assignments = { ...layer.componentPropertyAssignments }\n for (const [propertyId, value] of Object.entries(copy.componentPropertyAssignments)) {\n if (userAssigned.has(propertyId)) {\n assignments[propertyId] = value\n }\n }\n if (isEqual(assignments, copy.componentPropertyAssignments)) {\n return false\n }\n swap.graph.updateNode(copy.id, { componentPropertyAssignments: assignments })\n return true\n}\n\nfunction cloneLayer(\n swap: Swap,\n compParent: SceneNode,\n instParent: SceneNode,\n layer: SceneNode\n): SceneNode {\n const clone = cloneChildInCoordinates(swap.graph, layer, compParent, instParent)\n if (layer.childIds.length > 0) {\n cloneChildrenWithMapping(swap.graph, layer.id, clone.id)\n }\n // A new copy shows its variables where it sits; overrides restored after this keep theirs.\n applyBoundPaintColorsIn(swap.graph, clone.id)\n return clone\n}\n\nfunction followInside(swap: Swap, layer: SceneNode, copy: SceneNode): void {\n const owner = copy.type === 'INSTANCE'\n if (owner) {\n swap.owners.push(copy)\n }\n followLayers(swap, layer, copy)\n if (owner) {\n swap.owners.pop()\n }\n}\n\nfunction matchCopies(swap: Swap, compParent: SceneNode, instParent: SceneNode) {\n const stale: SceneNode[] = []\n /** Copies whose layer the new variant draws as another type, by that layer's index. */\n const retyped = new Map<number, SceneNode>()\n const byLayer = copiesByLayer(swap, instParent, stale)\n const ordinals = new Map<string, number>()\n const matched = compParent.childIds.map((layerId, index) => {\n const layer = swap.graph.getNode(layerId)\n if (!layer) {\n return undefined\n }\n const ordinal = ordinals.get(layer.name) ?? 0\n ordinals.set(layer.name, ordinal + 1)\n const copy = takeCopy(byLayer, layer.name, ordinal)\n if (!copy || copy.type === layer.type) {\n return copy\n }\n retyped.set(index, copy)\n return undefined\n })\n appendAll(stale, untakenCopies(byLayer))\n return { matched, stale, retyped }\n}\n\nfunction standsInOrderFor(swap: Swap, copy: SceneNode, layer: SceneNode, index: number): boolean {\n if (copy.type !== layer.type || swap.parked.has(copy.id)) {\n return false\n }\n const source = sourceLayer(swap, copy)\n if (!source) {\n return copy.name === layer.name\n }\n const sourceParent = source.parentId ? swap.graph.getNode(source.parentId) : undefined\n return source.name === layer.name && sourceParent?.childIds[index] === source.id\n}\n\nfunction hasCopiesInOrder(swap: Swap, compParent: SceneNode, instParent: SceneNode): boolean {\n const layerIds = compParent.childIds\n const copyIds = instParent.childIds\n if (layerIds.length !== copyIds.length) {\n return false\n }\n for (let index = 0; index < layerIds.length; index++) {\n const layer = swap.graph.getNode(layerIds[index])\n const copy = swap.graph.getNode(copyIds[index])\n if (!layer || !copy || !standsInOrderFor(swap, copy, layer, index)) {\n return false\n }\n }\n return true\n}\n\nfunction followCopiesInOrder(swap: Swap, compParent: SceneNode, instParent: SceneNode): void {\n compParent.childIds.forEach((layerId, index) => {\n const compChild = swap.graph.getNode(layerId)\n const instChild = swap.graph.getNode(instParent.childIds[index])\n if (compChild && instChild) {\n followCopy(swap, { compParent, instParent, compChild, instChild })\n }\n })\n}\n\nfunction followLayers(swap: Swap, compParent: SceneNode, instParent: SceneNode): void {\n if (hasCopiesInOrder(swap, compParent, instParent)) {\n followCopiesInOrder(swap, compParent, instParent)\n return\n }\n const { graph } = swap\n const { matched, stale, retyped } = matchCopies(swap, compParent, instParent)\n // Paths are read while the old copies still stand in their old order. A copy the new variant\n // draws as another type gives its overrides to the new copy the same way, so the layers in it\n // keep theirs too, as Figma keeps a rectangle's opacity on the ellipse of the same name.\n for (const copy of [...stale, ...retyped.values()]) {\n removeStaleCopy(swap, copy)\n }\n compParent.childIds.forEach((layerId, index) => {\n const layer = graph.getNode(layerId)\n if (!layer) {\n return\n }\n const copy = matched[index] ?? cloneLayer(swap, compParent, instParent, layer)\n if (instParent.childIds[index] !== copy.id) {\n graph.insertChildAt(copy.id, instParent.id, index)\n }\n if (copy === matched[index]) {\n followCopy(swap, { compParent, instParent, compChild: layer, instChild: copy })\n } else {\n // A new copy takes the values the instances around it assign, as one a sync adds does.\n applyEnclosingAssignments(graph, copy)\n restoreOverrides(overrideScope(swap), copy)\n // A mode restored on the copy changes what its unclaimed bound paints resolve to.\n refreshBoundPaintsIn(graph, copy.id)\n }\n })\n}\n\nfunction followCopy(swap: Swap, match: MatchedCopy): void {\n const { compChild: layer, instChild: copy } = match\n const swapped = isSwappedCopy(swap, copy)\n const previous = sourceLayer(swap, copy)\n const protection = copyProtection(swap, copy, previous)\n const linkedLayerId = linkCopy(swap, copy, layer, swapped)\n syncCopy(swap.graph, match, protection, linkedLayerId)\n if (previous !== layer) {\n followReplacedLayerFields(swap.graph, copy, layer)\n }\n if (swapped || ownsSlotContent(swap.graph, copy, slotPropertyId(layer))) {\n return\n }\n const reassigned = copy.type === 'INSTANCE' && followAssignments(swap, copy, layer)\n followInside(swap, layer, copy)\n if (reassigned) {\n reapplyComponentPropertyAssignments(swap.graph, copy.id)\n }\n}\n\nfunction enclosingInstances(graph: SceneGraph, instance: SceneNode): SceneNode[] {\n const owners: SceneNode[] = []\n graph.closest(instance.id, (node) => {\n if (node.type === 'INSTANCE') {\n owners.push(node)\n }\n return false\n })\n return owners.reverse()\n}\n\nexport function followComponentLayers(\n graph: SceneGraph,\n instance: SceneNode,\n component: SceneNode,\n parked: ReadonlySet<string>\n): void {\n const swap: Swap = {\n graph,\n instance,\n owners: enclosingInstances(graph, instance),\n changedOwners: new Set(),\n parked,\n namesakeOrdinals: new Map()\n }\n followLayers(swap, component, instance)\n for (const owner of swap.changedOwners) {\n graph.updateNode(owner.id, { instanceOverrides: owner.instanceOverrides })\n }\n}\n"],"mappings":";;;;;;;;;;;;AAyBA,MAAM,wBAAwB;CAAC;CAAgB;CAAe;AAAgB;AAgB9E,SAAS,YAAY,MAAY,MAAwC;CACvE,KAAK,IAAI,QAAQ,KAAK,OAAO,SAAS,GAAG,SAAS,GAAG,SAAS;EAE5D,MAAM,SADS,KAAK,OAAO,MAAM,CAAC,kBAAkB,Y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KAAK,OAAO,MAAM,eAAe,KAAK,CAAC,GACpE;CAEF,MAAM,aAAa,KAAK,SAAS,cAAc,kBAAkB,MAAM,MAAM,KAAK;CAClF,aAAa,MAAM,OAAO,IAAI;CAC9B,IAAI,YACF,oCAAoC,KAAK,OAAO,KAAK,EAAE;AAE3D;AAEA,SAAS,mBAAmB,OAAmB,UAAkC;CAC/E,MAAM,SAAsB,CAAC;CAC7B,MAAM,QAAQ,SAAS,KAAK,SAAS;EACnC,IAAI,KAAK,SAAS,YAChB,OAAO,KAAK,IAAI;EAElB,OAAO;CACT,CAAC;CACD,OAAO,OAAO,QAAQ;AACxB;AAEA,SAAgB,sBACd,OACA,UACA,WACA,QACM;CACN,MAAM,OAAa;EACjB;EACA;EACA,QAAQ,mBAAmB,OAAO,QAAQ;EAC1C,+BAAe,IAAI,IAAI;EACvB;EACA,kCAAkB,IAAI,IAAI;CAC5B;CACA,aAAa,MAAM,WAAW,QAAQ;CACtC,KAAK,MAAM,SAAS,KAAK,eACvB,MAAM,WAAW,MAAM,IAAI,EAAE,mBAAmB,MAAM,kBAAkB,CAAC;AAE7E"}
|
package/dist/instances/sync.js
CHANGED
|
@@ -1,20 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { isNumericVariableBindingField } from "../variables/fields.js";
|
|
2
|
+
import { copiedComponentLayerId, hasInstanceOverride } from "./overrides.js";
|
|
3
3
|
import { cloneNodeProps, copyEffects, copyFills, copyStrokes, copyStyleRuns } from "../copy.js";
|
|
4
|
-
import {
|
|
4
|
+
import { sourceWithEffectiveFill } from "./main-component.js";
|
|
5
5
|
import { ownsSlotContent, slotPropertyId } from "../slots/frames.js";
|
|
6
6
|
import { applyBoundPaintColorsIn, copyAppliedBoundPaints, keepCopyBoundPaints } from "../variables/paints.js";
|
|
7
7
|
import { keepOwnContentSize, remeasureChangedText } from "./content-size.js";
|
|
8
8
|
import { createInstanceSyncContext, hasUnplacedCopiesBelow, restoreRemovedCopy, unplacedCopy } from "./copies.js";
|
|
9
9
|
import { INSTANCE_SYNC_FIELDS, INSTANCE_SYNC_FIELD_OWNERS } from "./fields.js";
|
|
10
|
+
import { NOTHING_PROTECTED, childBindingProtection, isProtectedSyncField, syncBindingFields } from "./protection.js";
|
|
10
11
|
import { applyEnclosingAssignments, assignedFieldValues, propertyDrivenFields } from "./assignments.js";
|
|
11
12
|
import { scaleNodeChanges } from "../scaling/node.js";
|
|
12
13
|
import { resolvedNumericBindings } from "../variables/bindings.js";
|
|
13
14
|
import { scaleVariableBindingUnits } from "../variables/units.js";
|
|
15
|
+
import { matchFallbackChildren, matchLinkedChildren } from "./matching.js";
|
|
14
16
|
import { PLACEMENT_FIELDS, fitCopiedGroup, placedByConstraints } from "./placement.js";
|
|
15
17
|
import { followVectorGeometry } from "./vector-geometry.js";
|
|
16
18
|
import { pick } from "es-toolkit/object";
|
|
17
|
-
import {
|
|
19
|
+
import { isEqual } from "es-toolkit/predicate";
|
|
18
20
|
import { uniq } from "es-toolkit/array";
|
|
19
21
|
//#region src/instances/sync.ts
|
|
20
22
|
function setSceneProp(target, key, value) {
|
|
@@ -37,45 +39,6 @@ function copyProp(target, source, key) {
|
|
|
37
39
|
setSceneProp(target, key, Array.isArray(value) ? structuredClone(value) : value);
|
|
38
40
|
}
|
|
39
41
|
}
|
|
40
|
-
function bindingProtection(scopes) {
|
|
41
|
-
const fields = /* @__PURE__ */ new Set();
|
|
42
|
-
for (const scope of scopes) {
|
|
43
|
-
const perField = [...scope].some((field) => overriddenBindingField(field) !== null);
|
|
44
|
-
for (const field of scope) if (field !== "boundVariables" || !perField) fields.add(field);
|
|
45
|
-
}
|
|
46
|
-
return (field) => {
|
|
47
|
-
if (fields.has(field)) return true;
|
|
48
|
-
const binding = overriddenBindingField(field);
|
|
49
|
-
if (binding === null) return false;
|
|
50
|
-
const paint = paintBindingTarget(binding);
|
|
51
|
-
if (paint) return fields.has(paint.list);
|
|
52
|
-
return fields.has("effects") && effectBindingTarget(binding) !== null;
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
function isUnbound(node) {
|
|
56
|
-
return isEmptyObject(node.boundVariables) && isEmptyObject(node.variableBindingScales);
|
|
57
|
-
}
|
|
58
|
-
function syncBindingFields(target, source, updates, protectedField) {
|
|
59
|
-
if (protectedField("boundVariables") || isUnbound(source) && isUnbound(target)) return;
|
|
60
|
-
const bindings = { ...source.boundVariables };
|
|
61
|
-
const scales = { ...source.variableBindingScales };
|
|
62
|
-
for (const field of /* @__PURE__ */ new Set([
|
|
63
|
-
...Object.keys(source.boundVariables),
|
|
64
|
-
...Object.keys(target.boundVariables),
|
|
65
|
-
...Object.keys(target.variableBindingScales)
|
|
66
|
-
])) {
|
|
67
|
-
if (!protectedField(bindingOverrideField(field))) continue;
|
|
68
|
-
if (!Object.hasOwn(target.boundVariables, field)) Reflect.deleteProperty(bindings, field);
|
|
69
|
-
else bindings[field] = target.boundVariables[field];
|
|
70
|
-
if (target.variableBindingScales[field] === void 0) Reflect.deleteProperty(scales, field);
|
|
71
|
-
else scales[field] = target.variableBindingScales[field];
|
|
72
|
-
}
|
|
73
|
-
updates.boundVariables = bindings;
|
|
74
|
-
updates.variableBindingScales = scales;
|
|
75
|
-
}
|
|
76
|
-
function isProtectedSyncField(node, key, protectedField) {
|
|
77
|
-
return protectedField(key) || key in node.boundVariables && (protectedField("boundVariables") || protectedField(bindingOverrideField(key)));
|
|
78
|
-
}
|
|
79
42
|
function sourceInTargetCoordinates(source, targetScale) {
|
|
80
43
|
const factor = targetScale / source.componentScale;
|
|
81
44
|
if (!Number.isFinite(factor) || factor <= 0) throw new Error("Invalid component coordinate scale");
|
|
@@ -92,7 +55,6 @@ const SYNCED_FIELDS = INSTANCE_SYNC_FIELDS.filter((key) => key !== "boundVariabl
|
|
|
92
55
|
placement: PLACEMENT_FIELDS.has(key),
|
|
93
56
|
position: key === "x" || key === "y"
|
|
94
57
|
}));
|
|
95
|
-
const NOTHING_PROTECTED = (_field) => false;
|
|
96
58
|
/** The fields a copy takes from its component's layer, placed in the copy's parent. */
|
|
97
59
|
function copySyncedFields(graph, updates, copy, source, { compParent, instParent, factor, skip }) {
|
|
98
60
|
const placedByLayout = instParent.layoutMode !== "NONE" && source.layoutPositioning !== "ABSOLUTE";
|
|
@@ -174,63 +136,6 @@ function cloneChildrenWithMapping(graph, sourceParentId, destParentId, mode = "d
|
|
|
174
136
|
if (src.childIds.length > 0) cloneChildrenWithMapping(graph, childId, clone.id, mode);
|
|
175
137
|
}
|
|
176
138
|
}
|
|
177
|
-
function enclosingInstanceOverrideFields(graph, node) {
|
|
178
|
-
const fields = [];
|
|
179
|
-
let parent = node.parentId ? graph.nodes.get(node.parentId) : void 0;
|
|
180
|
-
while (parent) {
|
|
181
|
-
if (parent.type === "INSTANCE") fields.push(new Set(parent.instanceOverrides.descendants.get(node.id)?.keys()));
|
|
182
|
-
parent = parent.parentId ? graph.nodes.get(parent.parentId) : void 0;
|
|
183
|
-
}
|
|
184
|
-
return fields;
|
|
185
|
-
}
|
|
186
|
-
function childBindingProtection(graph, child, overrides) {
|
|
187
|
-
const fields = enclosingInstanceOverrideFields(graph, child);
|
|
188
|
-
fields.push(new Set(overrides.descendants.get(child.id)?.keys()));
|
|
189
|
-
if (child.instanceOverrides.self.size === 0 && fields.every((scope) => scope.size === 0)) return NOTHING_PROTECTED;
|
|
190
|
-
const protectedField = bindingProtection(fields);
|
|
191
|
-
return (field) => protectedField(field) || ownsSelfFillField(child, field);
|
|
192
|
-
}
|
|
193
|
-
function linkMatchedChild(overrides, instParentId, instChild, compChildId) {
|
|
194
|
-
if (instChild.type === "INSTANCE") setInstanceOverride(overrides, instParentId, instChild.id, "sourceComponentId", compChildId);
|
|
195
|
-
else instChild.componentId = compChildId;
|
|
196
|
-
}
|
|
197
|
-
function matchFallbackChildren(graph, compParent, instParent, instParentId, overrides, instChildMap, usedInstChildIds) {
|
|
198
|
-
const fallbackByType = /* @__PURE__ */ new Map();
|
|
199
|
-
const remainingCandidateCount = /* @__PURE__ */ new Map();
|
|
200
|
-
const unmatchedComponentCount = /* @__PURE__ */ new Map();
|
|
201
|
-
for (const compChildId of compParent.childIds) {
|
|
202
|
-
if (instChildMap.has(compChildId)) continue;
|
|
203
|
-
const child = graph.nodes.get(compChildId);
|
|
204
|
-
if (child) unmatchedComponentCount.set(child.type, (unmatchedComponentCount.get(child.type) ?? 0) + 1);
|
|
205
|
-
}
|
|
206
|
-
for (const childId of instParent.childIds) {
|
|
207
|
-
const child = graph.nodes.get(childId);
|
|
208
|
-
if (!child || usedInstChildIds.has(child.id)) continue;
|
|
209
|
-
remainingCandidateCount.set(child.type, (remainingCandidateCount.get(child.type) ?? 0) + 1);
|
|
210
|
-
let byName = fallbackByType.get(child.type);
|
|
211
|
-
if (!byName) {
|
|
212
|
-
byName = /* @__PURE__ */ new Map();
|
|
213
|
-
fallbackByType.set(child.type, byName);
|
|
214
|
-
}
|
|
215
|
-
const queue = byName.get(child.name);
|
|
216
|
-
if (queue) queue.push(child);
|
|
217
|
-
else byName.set(child.name, [child]);
|
|
218
|
-
}
|
|
219
|
-
for (const compChildId of compParent.childIds) {
|
|
220
|
-
if (instChildMap.has(compChildId)) continue;
|
|
221
|
-
const compChild = graph.nodes.get(compChildId);
|
|
222
|
-
if (!compChild) continue;
|
|
223
|
-
const candidatesByName = fallbackByType.get(compChild.type);
|
|
224
|
-
const candidateCount = remainingCandidateCount.get(compChild.type) ?? 0;
|
|
225
|
-
if (candidateCount > (unmatchedComponentCount.get(compChild.type) ?? 0)) continue;
|
|
226
|
-
const match = candidatesByName?.get(compChild.name)?.shift();
|
|
227
|
-
if (!match) continue;
|
|
228
|
-
remainingCandidateCount.set(compChild.type, candidateCount - 1);
|
|
229
|
-
instChildMap.set(compChildId, match);
|
|
230
|
-
usedInstChildIds.add(match.id);
|
|
231
|
-
linkMatchedChild(overrides, instParentId, match, compChildId);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
139
|
function sortInstanceChildren(graph, instParent, instParentId, compChildOrder, overrides) {
|
|
235
140
|
const orderMap = /* @__PURE__ */ new Map();
|
|
236
141
|
for (let i = 0; i < compChildOrder.length; i++) orderMap.set(compChildOrder[i], i);
|
|
@@ -271,18 +176,6 @@ function syncCopy(graph, { compParent, instParent, compChild, instChild }, prote
|
|
|
271
176
|
function isCyclicSync(graph, compParentId, instParentId) {
|
|
272
177
|
return compParentId === instParentId || graph.isDescendant(instParentId, compParentId);
|
|
273
178
|
}
|
|
274
|
-
/** Children already linked to a component child by sourceComponentId or componentId. */
|
|
275
|
-
function matchLinkedChildren(graph, instParent, instParentId, overrides, compChildIdSet, instChildMap, usedInstChildIds) {
|
|
276
|
-
for (const childId of instParent.childIds) {
|
|
277
|
-
const child = graph.nodes.get(childId);
|
|
278
|
-
if (!child) continue;
|
|
279
|
-
const mappedComponentId = copiedComponentLayerId(overrides, instParentId, child);
|
|
280
|
-
if (mappedComponentId && compChildIdSet.has(mappedComponentId)) {
|
|
281
|
-
instChildMap.set(mappedComponentId, child);
|
|
282
|
-
usedInstChildIds.add(child.id);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
179
|
/**
|
|
287
180
|
* Pass 3 of a sync: bring along the copy of a layer moved here inside the component, bring back
|
|
288
181
|
* one the sync removed when its layer returns, or clone a layer the instance has no copy of. A
|
|
@@ -320,8 +213,9 @@ function syncChildren(graph, compParentId, instParentId, overrides, context = cr
|
|
|
320
213
|
if (!compParent || !instParent) return false;
|
|
321
214
|
const instChildMap = /* @__PURE__ */ new Map();
|
|
322
215
|
const usedInstChildIds = /* @__PURE__ */ new Set();
|
|
323
|
-
|
|
324
|
-
|
|
216
|
+
const compChildIdSet = new Set(compParent.childIds);
|
|
217
|
+
matchLinkedChildren(graph, instParent, instParentId, overrides, compChildIdSet, instChildMap, usedInstChildIds);
|
|
218
|
+
matchFallbackChildren(graph, compParent, instParent, instParentId, context, instChildMap, usedInstChildIds);
|
|
325
219
|
placeMissingChildren(graph, compParent, instParent, instChildMap, usedInstChildIds, context);
|
|
326
220
|
let constrained = false;
|
|
327
221
|
for (const compChildId of compParent.childIds) {
|
|
@@ -343,6 +237,6 @@ function syncChildren(graph, compParentId, instParentId, overrides, context = cr
|
|
|
343
237
|
return constrained;
|
|
344
238
|
}
|
|
345
239
|
//#endregion
|
|
346
|
-
export {
|
|
240
|
+
export { cloneChildInCoordinates, cloneChildrenWithMapping, copyProp, sourceInTargetCoordinates, syncChildren, syncCopy, updateSyncedProps };
|
|
347
241
|
|
|
348
242
|
//# sourceMappingURL=sync.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sync.js","names":["hasNodeInstanceOverride"],"sources":["../../src/instances/sync.ts"],"sourcesContent":["// Child cloning and property synchronization shared by instance creation, swap, and sync.\nimport { uniq } from 'es-toolkit/array'\nimport { pick } from 'es-toolkit/object'\nimport { isEmptyObject, isEqual } from 'es-toolkit/predicate'\n\nimport { cloneNodeProps, copyEffects, copyFills, copyStrokes, copyStyleRuns } from '../copy'\nimport type { NodeCloneMode } from '../copy'\nimport type { SceneGraph } from '../graph'\nimport { scaleNodeChanges } from '../scaling/node'\nimport { ownsSlotContent, slotPropertyId } from '../slots/frames'\nimport type { SceneNode } from '../types'\nimport { resolvedNumericBindings } from '../variables/bindings'\nimport {\n effectBindingTarget,\n isNumericVariableBindingField,\n paintBindingTarget\n} from '../variables/fields'\nimport {\n applyBoundPaintColorsIn,\n copyAppliedBoundPaints,\n keepCopyBoundPaints\n} from '../variables/paints'\nimport { scaleVariableBindingUnits } from '../variables/units'\nimport { applyEnclosingAssignments, assignedFieldValues, propertyDrivenFields } from './assignments'\nimport { keepOwnContentSize, remeasureChangedText, type SyncedPlacement } from './content-size'\nimport {\n createInstanceSyncContext,\n hasUnplacedCopiesBelow,\n restoreRemovedCopy,\n unplacedCopy,\n type InstanceSyncContext\n} from './copies'\nimport { INSTANCE_SYNC_FIELD_OWNERS, INSTANCE_SYNC_FIELDS } from './fields'\nimport { ownsSelfFillField, sourceWithEffectiveFill } from './main-component'\nimport {\n bindingOverrideField,\n copiedComponentLayerId,\n hasInstanceOverride as hasNodeInstanceOverride,\n overriddenBindingField,\n setInstanceOverride,\n type InstanceOverrideState\n} from './overrides'\nimport { fitCopiedGroup, PLACEMENT_FIELDS, placedByConstraints } from './placement'\nimport { followVectorGeometry } from './vector-geometry'\n\nfunction setSceneProp<K extends keyof SceneNode>(\n target: Partial<SceneNode>,\n key: K,\n value: SceneNode[K]\n): void {\n target[key] = value\n}\n\nexport function copyProp(\n target: Partial<SceneNode> | SceneNode,\n source: SceneNode,\n key: keyof SceneNode\n): void {\n if (key === 'fills') {\n setSceneProp(target, key, copyFills(source.fills))\n } else if (key === 'strokes') {\n setSceneProp(target, key, copyStrokes(source.strokes))\n } else if (key === 'effects') {\n setSceneProp(target, key, copyEffects(source.effects))\n } else if (key === 'styleRuns') {\n setSceneProp(target, key, copyStyleRuns(source.styleRuns))\n } else if (key === 'boundVariables') {\n // Shallow copy the binding map — values are variable IDs (strings), not objects\n setSceneProp(target, key, { ...source.boundVariables })\n setSceneProp(target, 'variableBindingScales', { ...source.variableBindingScales })\n } else if (key === 'variableModes') {\n setSceneProp(target, key, { ...source.variableModes })\n } else if (key === 'gridPosition') {\n // Shallow copy the grid position object — all fields are primitives\n setSceneProp(target, key, source.gridPosition ? { ...source.gridPosition } : null)\n } else if (key === 'targetAspectRatio') {\n setSceneProp(target, key, source.targetAspectRatio ? { ...source.targetAspectRatio } : null)\n } else if (key === 'booleanOperation') {\n setSceneProp(target, key, source.booleanOperation ?? 'UNION')\n } else {\n const value = source[key]\n setSceneProp(target, key, Array.isArray(value) ? structuredClone(value) : value)\n }\n}\n\nexport function bindingProtection(scopes: ReadonlySet<string>[]): (field: string) => boolean {\n const fields = new Set<string>()\n for (const scope of scopes) {\n const perField = [...scope].some((field) => overriddenBindingField(field) !== null)\n for (const field of scope) {\n if (field !== 'boundVariables' || !perField) fields.add(field)\n }\n }\n return (field) => {\n if (fields.has(field)) return true\n // Paints and effects an instance set itself own their bindings too, bound or not.\n const binding = overriddenBindingField(field)\n if (binding === null) return false\n const paint = paintBindingTarget(binding)\n if (paint) return fields.has(paint.list)\n return fields.has('effects') && effectBindingTarget(binding) !== null\n }\n}\n\nfunction isUnbound(node: SceneNode): boolean {\n return isEmptyObject(node.boundVariables) && isEmptyObject(node.variableBindingScales)\n}\n\nexport function syncBindingFields(\n target: SceneNode,\n source: SceneNode,\n updates: Partial<SceneNode>,\n protectedField: (field: string) => boolean\n): void {\n if (protectedField('boundVariables') || (isUnbound(source) && isUnbound(target))) {\n return\n }\n const bindings = { ...source.boundVariables }\n const scales = { ...source.variableBindingScales }\n for (const field of new Set([\n ...Object.keys(source.boundVariables),\n ...Object.keys(target.boundVariables),\n ...Object.keys(target.variableBindingScales)\n ])) {\n if (!protectedField(bindingOverrideField(field))) continue\n if (!Object.hasOwn(target.boundVariables, field)) Reflect.deleteProperty(bindings, field)\n else bindings[field] = target.boundVariables[field]\n if (target.variableBindingScales[field] === undefined) Reflect.deleteProperty(scales, field)\n else scales[field] = target.variableBindingScales[field]\n }\n updates.boundVariables = bindings\n updates.variableBindingScales = scales\n}\n\nexport function isProtectedSyncField(\n node: SceneNode,\n key: string,\n protectedField: (field: string) => boolean\n): boolean {\n return (\n protectedField(key) ||\n (key in node.boundVariables &&\n (protectedField('boundVariables') || protectedField(bindingOverrideField(key))))\n )\n}\n\nexport function sourceInTargetCoordinates(source: SceneNode, targetScale: number): SceneNode {\n const factor = targetScale / source.componentScale\n if (!Number.isFinite(factor) || factor <= 0) throw new Error('Invalid component coordinate scale')\n if (factor === 1) return source\n return {\n ...source,\n ...scaleNodeChanges(source, factor, true),\n ...scaleVariableBindingUnits(source, factor)\n }\n}\n\nconst SYNCED_FIELDS = INSTANCE_SYNC_FIELDS.filter((key) => key !== 'boundVariables').map((key) => ({\n key,\n owner: INSTANCE_SYNC_FIELD_OWNERS[key],\n placement: PLACEMENT_FIELDS.has(key),\n position: key === 'x' || key === 'y'\n}))\n\nexport const NOTHING_PROTECTED = (_field: string): boolean => false\n\ninterface SyncedFieldsInput {\n compParent: SceneNode\n instParent: SceneNode\n /** The component's coordinates in the copy's. */\n factor: number\n /** Fields the copy keeps: overridden, bound, or driven by a property. */\n skip: (key: string) => boolean\n}\n\n/** The fields a copy takes from its component's layer, placed in the copy's parent. */\nfunction copySyncedFields(\n graph: SceneGraph,\n updates: Partial<SceneNode>,\n copy: SceneNode,\n source: SceneNode,\n { compParent, instParent, factor, skip }: SyncedFieldsInput\n): SyncedPlacement {\n // In an auto layout flow the layout places the copy, as it placed the component's layer.\n const placedByLayout = instParent.layoutMode !== 'NONE' && source.layoutPositioning !== 'ABSOLUTE'\n const placed = placedByLayout\n ? source\n : placedByConstraints(graph, source, compParent, instParent, factor)\n for (const { key, owner, placement, position } of SYNCED_FIELDS) {\n if ((placedByLayout && position) || skip(key) || (owner && skip(owner))) {\n continue\n }\n const from = placement ? placed : source\n // Most fields already match: comparing first spares cloning them for nothing.\n if (copy[key] === from[key] || isEqual(copy[key], from[key])) {\n continue\n }\n copyProp(updates, from, key)\n }\n // New paints bring their style link, unless the copy keeps a style of its own.\n if (updates.fills)\n updates.fillStyleId = skip('fillStyleId') ? copy.fillStyleId : source.fillStyleId\n followVectorGeometry(updates, copy, source)\n remeasureChangedText(updates, copy)\n return { placed, x: !placedByLayout && !skip('x'), y: !placedByLayout && !skip('y') }\n}\n\nexport function updateSyncedProps(\n graph: SceneGraph,\n target: SceneNode,\n updates: Partial<SceneNode>,\n /** A field the copy keeps as its own, which the sync leaves as it is. */\n kept: (field: string) => boolean = () => false\n): void {\n resolveBoundNumbersInPlace(graph, target, updates, kept)\n if (updates.fills || updates.strokes || updates.boundVariables) {\n applySyncedModes(graph, target, updates)\n keepCopyBoundPaints(graph, target, updates)\n }\n let changed: Partial<SceneNode> | undefined\n for (const key of Object.keys(updates)) {\n const current = target[key as keyof SceneNode]\n const value = updates[key as keyof SceneNode]\n if (current !== value && !isEqual(current, value)) {\n changed ??= {}\n Reflect.set(changed, key, value)\n }\n }\n if (!changed) {\n return\n }\n if (changed.fills && updates.fillStyleId !== undefined) {\n changed.fillStyleId = updates.fillStyleId\n }\n graph.updateNode(target.id, changed)\n}\n\n/**\n * A bound number the sync writes takes the value it resolves to where the copy sits, not the\n * one its component holds: an instance that sets another mode, or sits in one that does, shows\n * that mode's value, as Figma draws it. A field the sync leaves alone, such as one an instance\n * overrides, keeps its own value.\n */\nfunction resolveBoundNumbersInPlace(\n graph: SceneGraph,\n target: SceneNode,\n updates: Partial<SceneNode>,\n kept: (field: string) => boolean\n): void {\n const bound = updates.boundVariables ?? target.boundVariables\n // The fields the sync writes, and the ones it binds anew, which keep an equal value only in\n // the component's mode.\n const rebound = updates.boundVariables\n ? Object.keys(bound).filter((field) => bound[field] !== target.boundVariables[field])\n : []\n const synced = uniq([...Object.keys(updates), ...rebound]).filter(\n (field) => isNumericVariableBindingField(field) && bound[field] && !kept(field)\n )\n if (synced.length === 0) return\n applySyncedModes(graph, target, updates)\n const resolved = resolvedNumericBindings(graph, { ...target, ...updates })\n Object.assign(updates, pick(resolved, synced as (keyof SceneNode)[]))\n}\n\n/** The modes the copy takes in this sync decide what its bindings resolve to. */\nfunction applySyncedModes(graph: SceneGraph, target: SceneNode, updates: Partial<SceneNode>): void {\n if (updates.variableModes && !isEqual(updates.variableModes, target.variableModes))\n graph.updateNode(target.id, { variableModes: updates.variableModes })\n}\n\nexport function cloneChildInCoordinates(\n graph: SceneGraph,\n src: SceneNode,\n sourceParent: SceneNode,\n targetParent: SceneNode,\n mode: NodeCloneMode = 'deep'\n): SceneNode {\n const componentScale =\n (src.componentScale * targetParent.componentScale) / sourceParent.componentScale\n const clone = graph.createNode(src.type, targetParent.id, {\n ...cloneNodeProps(sourceInTargetCoordinates(src, componentScale), src.id, mode),\n componentScale\n })\n copyAppliedBoundPaints(src, clone)\n return clone\n}\n\nexport function cloneChildrenWithMapping(\n graph: SceneGraph,\n sourceParentId: string,\n destParentId: string,\n mode: NodeCloneMode = 'deep'\n): void {\n // A cyclic component, such as an instance of an ancestor, would clone itself without end.\n if (sourceParentId === destParentId || graph.isDescendant(destParentId, sourceParentId)) return\n\n const sourceParent = graph.nodes.get(sourceParentId)\n const targetParent = graph.nodes.get(destParentId)\n if (!sourceParent || !targetParent) return\n\n for (const childId of sourceParent.childIds) {\n const src = graph.nodes.get(childId)\n if (!src) continue\n\n const clone = cloneChildInCoordinates(graph, src, sourceParent, targetParent, mode)\n\n if (src.childIds.length > 0) {\n cloneChildrenWithMapping(graph, childId, clone.id, mode)\n }\n }\n}\n\nexport function enclosingInstanceOverrideFields(graph: SceneGraph, node: SceneNode): Set<string>[] {\n const fields: Set<string>[] = []\n let parent = node.parentId ? graph.nodes.get(node.parentId) : undefined\n while (parent) {\n if (parent.type === 'INSTANCE') {\n fields.push(new Set(parent.instanceOverrides.descendants.get(node.id)?.keys()))\n }\n parent = parent.parentId ? graph.nodes.get(parent.parentId) : undefined\n }\n return fields\n}\n\nfunction childBindingProtection(\n graph: SceneGraph,\n child: SceneNode,\n overrides: InstanceOverrideState\n) {\n const fields = enclosingInstanceOverrideFields(graph, child)\n fields.push(new Set(overrides.descendants.get(child.id)?.keys()))\n if (child.instanceOverrides.self.size === 0 && fields.every((scope) => scope.size === 0)) {\n return NOTHING_PROTECTED\n }\n const protectedField = bindingProtection(fields)\n return (field: string) => protectedField(field) || ownsSelfFillField(child, field)\n}\n\nfunction linkMatchedChild(\n overrides: InstanceOverrideState,\n instParentId: string,\n instChild: SceneNode,\n compChildId: string\n): void {\n if (instChild.type === 'INSTANCE') {\n setInstanceOverride(overrides, instParentId, instChild.id, 'sourceComponentId', compChildId)\n } else {\n instChild.componentId = compChildId\n }\n}\n\nfunction matchFallbackChildren(\n graph: SceneGraph,\n compParent: SceneNode,\n instParent: SceneNode,\n instParentId: string,\n overrides: InstanceOverrideState,\n instChildMap: Map<string, SceneNode>,\n usedInstChildIds: Set<string>\n): void {\n const fallbackByType = new Map<SceneNode['type'], Map<string, SceneNode[]>>()\n const remainingCandidateCount = new Map<SceneNode['type'], number>()\n const unmatchedComponentCount = new Map<SceneNode['type'], number>()\n for (const compChildId of compParent.childIds) {\n if (instChildMap.has(compChildId)) continue\n const child = graph.nodes.get(compChildId)\n if (child) {\n unmatchedComponentCount.set(child.type, (unmatchedComponentCount.get(child.type) ?? 0) + 1)\n }\n }\n for (const childId of instParent.childIds) {\n const child = graph.nodes.get(childId)\n if (!child || usedInstChildIds.has(child.id)) continue\n remainingCandidateCount.set(child.type, (remainingCandidateCount.get(child.type) ?? 0) + 1)\n let byName = fallbackByType.get(child.type)\n if (!byName) {\n byName = new Map()\n fallbackByType.set(child.type, byName)\n }\n const queue = byName.get(child.name)\n if (queue) queue.push(child)\n else byName.set(child.name, [child])\n }\n\n // Match by name and type with FIFO queues to preserve sibling order in linear time.\n for (const compChildId of compParent.childIds) {\n if (instChildMap.has(compChildId)) continue\n const compChild = graph.nodes.get(compChildId)\n if (!compChild) continue\n const candidatesByName = fallbackByType.get(compChild.type)\n const candidateCount = remainingCandidateCount.get(compChild.type) ?? 0\n if (candidateCount > (unmatchedComponentCount.get(compChild.type) ?? 0)) continue\n const match = candidatesByName?.get(compChild.name)?.shift()\n if (!match) continue\n remainingCandidateCount.set(compChild.type, candidateCount - 1)\n instChildMap.set(compChildId, match)\n usedInstChildIds.add(match.id)\n linkMatchedChild(overrides, instParentId, match, compChildId)\n }\n}\n\nfunction sortInstanceChildren(\n graph: SceneGraph,\n instParent: SceneNode,\n instParentId: string,\n compChildOrder: string[],\n overrides: InstanceOverrideState\n): void {\n const orderMap = new Map<string, number>()\n for (let i = 0; i < compChildOrder.length; i++) orderMap.set(compChildOrder[i], i)\n\n const ranks = new Map<string, number>()\n for (let index = 0; index < instParent.childIds.length; index++) {\n const childId = instParent.childIds[index]\n const node = graph.nodes.get(childId)\n const mapped = node && copiedComponentLayerId(overrides, instParentId, node)\n const componentIndex = mapped ? orderMap.get(mapped) : undefined\n ranks.set(childId, componentIndex ?? compChildOrder.length + index)\n }\n const sorted = instParent.childIds.toSorted(\n (left, right) => (ranks.get(left) ?? 0) - (ranks.get(right) ?? 0)\n )\n // Move through the graph so the reorder is reported, as collaboration syncs it.\n sorted.forEach((childId, index) => {\n if (instParent.childIds[index] !== childId) graph.insertChildAt(childId, instParentId, index)\n })\n}\n\nexport interface MatchedCopy {\n compParent: SceneNode\n instParent: SceneNode\n compChild: SceneNode\n instChild: SceneNode\n}\n\nexport function syncCopy(\n graph: SceneGraph,\n { compParent, instParent, compChild, instChild }: MatchedCopy,\n protectedField: (field: string) => boolean,\n linkedLayerId?: string\n): boolean {\n const driven = propertyDrivenFields(graph, instChild, compChild)\n const kept =\n driven.size === 0 && protectedField === NOTHING_PROTECTED\n ? NOTHING_PROTECTED\n : (key: string) => driven.has(key) || isProtectedSyncField(instChild, key, protectedField)\n const componentScale =\n (compChild.componentScale * instParent.componentScale) / compParent.componentScale\n const effectiveSource = sourceWithEffectiveFill(graph, compChild, instChild)\n const source = sourceInTargetCoordinates(effectiveSource, componentScale)\n // Which properties a layer serves is the component's to say; a slot or exposed layer\n // created on the component becomes one in every instance.\n const updates: Partial<SceneNode> = {}\n if (instChild.componentScale !== componentScale) {\n updates.componentScale = componentScale\n }\n if (!isEqual(instChild.componentPropertyReferences, compChild.componentPropertyReferences)) {\n updates.componentPropertyReferences = structuredClone(compChild.componentPropertyReferences)\n }\n if (linkedLayerId !== undefined) {\n updates.componentId = linkedLayerId\n }\n syncBindingFields(instChild, source, updates, protectedField)\n const placement = copySyncedFields(graph, updates, instChild, source, {\n compParent,\n instParent,\n factor: componentScale / compChild.componentScale,\n skip: kept\n })\n Object.assign(updates, assignedFieldValues(graph, instChild, compChild))\n keepOwnContentSize(graph, updates, instChild, source, placement)\n updateSyncedProps(graph, instChild, updates, kept)\n return placement.placed !== source\n}\n\n/** True when syncing `compParentId` into `instParentId` would form a cycle. */\nfunction isCyclicSync(graph: SceneGraph, compParentId: string, instParentId: string): boolean {\n return compParentId === instParentId || graph.isDescendant(instParentId, compParentId)\n}\n\n/** Children already linked to a component child by sourceComponentId or componentId. */\nfunction matchLinkedChildren(\n graph: SceneGraph,\n instParent: SceneNode,\n instParentId: string,\n overrides: InstanceOverrideState,\n compChildIdSet: ReadonlySet<string>,\n instChildMap: Map<string, SceneNode>,\n usedInstChildIds: Set<string>\n): void {\n for (const childId of instParent.childIds) {\n const child = graph.nodes.get(childId)\n if (!child) continue\n const mappedComponentId = copiedComponentLayerId(overrides, instParentId, child)\n if (mappedComponentId && compChildIdSet.has(mappedComponentId)) {\n instChildMap.set(mappedComponentId, child)\n usedInstChildIds.add(child.id)\n }\n }\n}\n\n/**\n * Pass 3 of a sync: bring along the copy of a layer moved here inside the component, bring back\n * one the sync removed when its layer returns, or clone a layer the instance has no copy of. A\n * clone takes its own subtree unless copies the instance has belong in it, which the recursion\n * moves in instead. Children left unmatched are the sync's to remove once it finishes.\n */\nfunction placeMissingChildren(\n graph: SceneGraph,\n compParent: SceneNode,\n instParent: SceneNode,\n instChildMap: Map<string, SceneNode>,\n usedInstChildIds: Set<string>,\n context: InstanceSyncContext\n): void {\n for (const child of instChildMap.values()) context.matched.add(child.id)\n\n for (const compChildId of compParent.childIds) {\n if (instChildMap.has(compChildId)) continue\n const moved =\n unplacedCopy(graph, context, compChildId, instParent.id) ??\n restoreRemovedCopy(graph, context, compChildId, instParent.id)\n if (moved) {\n graph.insertChildAt(moved.id, instParent.id, instParent.childIds.length)\n instChildMap.set(compChildId, moved)\n usedInstChildIds.add(moved.id)\n context.matched.add(moved.id)\n continue\n }\n const src = graph.nodes.get(compChildId)\n if (!src) continue\n const clone = cloneChildInCoordinates(graph, src, compParent, instParent)\n if (src.childIds.length > 0 && !hasUnplacedCopiesBelow(graph, context, compChildId)) {\n cloneChildrenWithMapping(graph, compChildId, clone.id)\n }\n applyEnclosingAssignments(graph, clone)\n applyBoundPaintColorsIn(graph, clone.id)\n instChildMap.set(compChildId, clone)\n usedInstChildIds.add(clone.id)\n context.matched.add(clone.id)\n }\n for (const childId of instParent.childIds) {\n if (!usedInstChildIds.has(childId)) context.leftovers.add(childId)\n }\n}\n\nexport function syncChildren(\n graph: SceneGraph,\n compParentId: string,\n instParentId: string,\n overrides: InstanceOverrideState,\n context: InstanceSyncContext = createInstanceSyncContext(instParentId, overrides)\n): boolean {\n // An instance inside its own component would clone the component into itself without end.\n if (isCyclicSync(graph, compParentId, instParentId)) return false\n\n const compParent = graph.nodes.get(compParentId)\n const instParent = graph.nodes.get(instParentId)\n if (!compParent || !instParent) return false\n\n const instChildMap = new Map<string, SceneNode>()\n const usedInstChildIds = new Set<string>()\n const compChildIdSet = new Set(compParent.childIds)\n\n // Pass 1: Direct matching via sourceComponentId or componentId\n matchLinkedChildren(\n graph,\n instParent,\n instParentId,\n overrides,\n compChildIdSet,\n instChildMap,\n usedInstChildIds\n )\n\n // Pass 2: Fallback matching for unmatched children (e.g. from Figma imports)\n matchFallbackChildren(\n graph,\n compParent,\n instParent,\n instParentId,\n overrides,\n instChildMap,\n usedInstChildIds\n )\n\n placeMissingChildren(graph, compParent, instParent, instChildMap, usedInstChildIds, context)\n\n // Pass 4: Synchronize properties and recurse\n let constrained = false\n for (const compChildId of compParent.childIds) {\n const compChild = graph.nodes.get(compChildId)\n const instChild = instChildMap.get(compChildId)\n if (!compChild || !instChild) continue\n\n const match = { compParent, instParent, compChild, instChild }\n const protection = childBindingProtection(graph, instChild, overrides)\n // True when its constraints placed the copy elsewhere than the component's layer.\n if (syncCopy(graph, match, protection)) constrained = true\n if (\n compChild.childIds.length > 0 &&\n !hasNodeInstanceOverride(overrides, instParentId, instChild.id, 'componentId') &&\n // The component's frame is the authority on which slot this is; instance copies of\n // its bindings are not synced.\n !ownsSlotContent(graph, instChild, slotPropertyId(compChild))\n ) {\n if (syncChildren(graph, compChildId, instChild.id, overrides, context)) constrained = true\n }\n }\n\n // Pass 5: Sort instance children to match component child order\n sortInstanceChildren(graph, instParent, instParentId, compParent.childIds, overrides)\n // A group fits layers its constraints moved; else it ke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AEA,SAAgB,aACd,OACA,cACA,cACA,WACA,UAA+B,0BAA0B,cAAc,SAAS,GACvE;CAET,IAAI,aAAa,OAAO,cAAc,YAAY,GAAG,OAAO;CAE5D,MAAM,aAAa,MAAM,MAAM,IAAI,YAAY;CAC/C,MAAM,aAAa,MAAM,MAAM,IAAI,YAAY;CAC/C,IAAI,CAAC,cAAc,CAAC,YAAY,OAAO;CAEvC,MAAM,+BAAe,IAAI,IAAuB;CAChD,MAAM,mCAAmB,IAAI,IAAY;CAIzC,oBACE,OACA,YACA,cACA,WACA,IARyB,IAAI,WAAW,QAQ3B,GACb,cACA,gBACF;CAGA,sBACE,OACA,YACA,YACA,cACA,WACA,cACA,gBACF;CAEA,qBAAqB,OAAO,YAAY,YAAY,cAAc,kBAAkB,OAAO;CAG3F,IAAI,cAAc;CAClB,KAAK,MAAM,eAAe,WAAW,UAAU;EAC7C,MAAM,YAAY,MAAM,MAAM,IAAI,WAAW;EAC7C,MAAM,YAAY,aAAa,IAAI,WAAW;EAC9C,IAAI,CAAC,aAAa,CAAC,WAAW;EAK9B,IAAI,SAAS,OAAO;GAHJ;GAAY;GAAY;GAAW;EAG3B,GAFL,uBAAuB,OAAO,WAAW,SAExB,CAAC,GAAG,cAAc;EACtD,IACE,UAAU,SAAS,SAAS,KAC5B,CAACA,oBAAwB,WAAW,cAAc,UAAU,IAAI,aAAa,KAG7E,CAAC,gBAAgB,OAAO,WAAW,eAAe,SAAS,CAAC,GAExD;OAAA,aAAa,OAAO,aAAa,UAAU,IAAI,WAAW,OAAO,GAAG,cAAc;EAAA;CAE1F;CAGA,qBAAqB,OAAO,YAAY,cAAc,WAAW,UAAU,SAAS;CAEpF,IAAI,aAAa,eAAe,OAAO,YAAY;CACnD,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"sync.js","names":["hasNodeInstanceOverride"],"sources":["../../src/instances/sync.ts"],"sourcesContent":["// Child cloning and property synchronization shared by instance creation, swap, and sync.\nimport { uniq } from 'es-toolkit/array'\nimport { pick } from 'es-toolkit/object'\nimport { isEqual } from 'es-toolkit/predicate'\n\nimport { cloneNodeProps, copyEffects, copyFills, copyStrokes, copyStyleRuns } from '../copy'\nimport type { NodeCloneMode } from '../copy'\nimport type { SceneGraph } from '../graph'\nimport { scaleNodeChanges } from '../scaling/node'\nimport { ownsSlotContent, slotPropertyId } from '../slots/frames'\nimport type { SceneNode } from '../types'\nimport { resolvedNumericBindings } from '../variables/bindings'\nimport { isNumericVariableBindingField } from '../variables/fields'\nimport {\n applyBoundPaintColorsIn,\n copyAppliedBoundPaints,\n keepCopyBoundPaints\n} from '../variables/paints'\nimport { scaleVariableBindingUnits } from '../variables/units'\nimport { applyEnclosingAssignments, assignedFieldValues, propertyDrivenFields } from './assignments'\nimport { keepOwnContentSize, remeasureChangedText, type SyncedPlacement } from './content-size'\nimport {\n createInstanceSyncContext,\n hasUnplacedCopiesBelow,\n restoreRemovedCopy,\n unplacedCopy,\n type InstanceSyncContext\n} from './copies'\nimport { INSTANCE_SYNC_FIELD_OWNERS, INSTANCE_SYNC_FIELDS } from './fields'\nimport { sourceWithEffectiveFill } from './main-component'\nimport { matchFallbackChildren, matchLinkedChildren } from './matching'\nimport {\n copiedComponentLayerId,\n hasInstanceOverride as hasNodeInstanceOverride,\n type InstanceOverrideState\n} from './overrides'\nimport { fitCopiedGroup, PLACEMENT_FIELDS, placedByConstraints } from './placement'\nimport {\n childBindingProtection,\n isProtectedSyncField,\n NOTHING_PROTECTED,\n syncBindingFields\n} from './protection'\nimport { followVectorGeometry } from './vector-geometry'\n\nfunction setSceneProp<K extends keyof SceneNode>(\n target: Partial<SceneNode>,\n key: K,\n value: SceneNode[K]\n): void {\n target[key] = value\n}\n\nexport function copyProp(\n target: Partial<SceneNode> | SceneNode,\n source: SceneNode,\n key: keyof SceneNode\n): void {\n if (key === 'fills') {\n setSceneProp(target, key, copyFills(source.fills))\n } else if (key === 'strokes') {\n setSceneProp(target, key, copyStrokes(source.strokes))\n } else if (key === 'effects') {\n setSceneProp(target, key, copyEffects(source.effects))\n } else if (key === 'styleRuns') {\n setSceneProp(target, key, copyStyleRuns(source.styleRuns))\n } else if (key === 'boundVariables') {\n // Shallow copy the binding map — values are variable IDs (strings), not objects\n setSceneProp(target, key, { ...source.boundVariables })\n setSceneProp(target, 'variableBindingScales', { ...source.variableBindingScales })\n } else if (key === 'variableModes') {\n setSceneProp(target, key, { ...source.variableModes })\n } else if (key === 'gridPosition') {\n // Shallow copy the grid position object — all fields are primitives\n setSceneProp(target, key, source.gridPosition ? { ...source.gridPosition } : null)\n } else if (key === 'targetAspectRatio') {\n setSceneProp(target, key, source.targetAspectRatio ? { ...source.targetAspectRatio } : null)\n } else if (key === 'booleanOperation') {\n setSceneProp(target, key, source.booleanOperation ?? 'UNION')\n } else {\n const value = source[key]\n setSceneProp(target, key, Array.isArray(value) ? structuredClone(value) : value)\n }\n}\n\nexport function sourceInTargetCoordinates(source: SceneNode, targetScale: number): SceneNode {\n const factor = targetScale / source.componentScale\n if (!Number.isFinite(factor) || factor <= 0) throw new Error('Invalid component coordinate scale')\n if (factor === 1) return source\n return {\n ...source,\n ...scaleNodeChanges(source, factor, true),\n ...scaleVariableBindingUnits(source, factor)\n }\n}\n\nconst SYNCED_FIELDS = INSTANCE_SYNC_FIELDS.filter((key) => key !== 'boundVariables').map((key) => ({\n key,\n owner: INSTANCE_SYNC_FIELD_OWNERS[key],\n placement: PLACEMENT_FIELDS.has(key),\n position: key === 'x' || key === 'y'\n}))\n\ninterface SyncedFieldsInput {\n compParent: SceneNode\n instParent: SceneNode\n /** The component's coordinates in the copy's. */\n factor: number\n /** Fields the copy keeps: overridden, bound, or driven by a property. */\n skip: (key: string) => boolean\n}\n\n/** The fields a copy takes from its component's layer, placed in the copy's parent. */\nfunction copySyncedFields(\n graph: SceneGraph,\n updates: Partial<SceneNode>,\n copy: SceneNode,\n source: SceneNode,\n { compParent, instParent, factor, skip }: SyncedFieldsInput\n): SyncedPlacement {\n // In an auto layout flow the layout places the copy, as it placed the component's layer.\n const placedByLayout = instParent.layoutMode !== 'NONE' && source.layoutPositioning !== 'ABSOLUTE'\n const placed = placedByLayout\n ? source\n : placedByConstraints(graph, source, compParent, instParent, factor)\n for (const { key, owner, placement, position } of SYNCED_FIELDS) {\n if ((placedByLayout && position) || skip(key) || (owner && skip(owner))) {\n continue\n }\n const from = placement ? placed : source\n // Most fields already match: comparing first spares cloning them for nothing.\n if (copy[key] === from[key] || isEqual(copy[key], from[key])) {\n continue\n }\n copyProp(updates, from, key)\n }\n // New paints bring their style link, unless the copy keeps a style of its own.\n if (updates.fills)\n updates.fillStyleId = skip('fillStyleId') ? copy.fillStyleId : source.fillStyleId\n followVectorGeometry(updates, copy, source)\n remeasureChangedText(updates, copy)\n return { placed, x: !placedByLayout && !skip('x'), y: !placedByLayout && !skip('y') }\n}\n\nexport function updateSyncedProps(\n graph: SceneGraph,\n target: SceneNode,\n updates: Partial<SceneNode>,\n /** A field the copy keeps as its own, which the sync leaves as it is. */\n kept: (field: string) => boolean = () => false\n): void {\n resolveBoundNumbersInPlace(graph, target, updates, kept)\n if (updates.fills || updates.strokes || updates.boundVariables) {\n applySyncedModes(graph, target, updates)\n keepCopyBoundPaints(graph, target, updates)\n }\n let changed: Partial<SceneNode> | undefined\n for (const key of Object.keys(updates)) {\n const current = target[key as keyof SceneNode]\n const value = updates[key as keyof SceneNode]\n if (current !== value && !isEqual(current, value)) {\n changed ??= {}\n Reflect.set(changed, key, value)\n }\n }\n if (!changed) {\n return\n }\n if (changed.fills && updates.fillStyleId !== undefined) {\n changed.fillStyleId = updates.fillStyleId\n }\n graph.updateNode(target.id, changed)\n}\n\n/**\n * A bound number the sync writes takes the value it resolves to where the copy sits, not the\n * one its component holds: an instance that sets another mode, or sits in one that does, shows\n * that mode's value, as Figma draws it. A field the sync leaves alone, such as one an instance\n * overrides, keeps its own value.\n */\nfunction resolveBoundNumbersInPlace(\n graph: SceneGraph,\n target: SceneNode,\n updates: Partial<SceneNode>,\n kept: (field: string) => boolean\n): void {\n const bound = updates.boundVariables ?? target.boundVariables\n // The fields the sync writes, and the ones it binds anew, which keep an equal value only in\n // the component's mode.\n const rebound = updates.boundVariables\n ? Object.keys(bound).filter((field) => bound[field] !== target.boundVariables[field])\n : []\n const synced = uniq([...Object.keys(updates), ...rebound]).filter(\n (field) => isNumericVariableBindingField(field) && bound[field] && !kept(field)\n )\n if (synced.length === 0) return\n applySyncedModes(graph, target, updates)\n const resolved = resolvedNumericBindings(graph, { ...target, ...updates })\n Object.assign(updates, pick(resolved, synced as (keyof SceneNode)[]))\n}\n\n/** The modes the copy takes in this sync decide what its bindings resolve to. */\nfunction applySyncedModes(graph: SceneGraph, target: SceneNode, updates: Partial<SceneNode>): void {\n if (updates.variableModes && !isEqual(updates.variableModes, target.variableModes))\n graph.updateNode(target.id, { variableModes: updates.variableModes })\n}\n\nexport function cloneChildInCoordinates(\n graph: SceneGraph,\n src: SceneNode,\n sourceParent: SceneNode,\n targetParent: SceneNode,\n mode: NodeCloneMode = 'deep'\n): SceneNode {\n const componentScale =\n (src.componentScale * targetParent.componentScale) / sourceParent.componentScale\n const clone = graph.createNode(src.type, targetParent.id, {\n ...cloneNodeProps(sourceInTargetCoordinates(src, componentScale), src.id, mode),\n componentScale\n })\n copyAppliedBoundPaints(src, clone)\n return clone\n}\n\nexport function cloneChildrenWithMapping(\n graph: SceneGraph,\n sourceParentId: string,\n destParentId: string,\n mode: NodeCloneMode = 'deep'\n): void {\n // A cyclic component, such as an instance of an ancestor, would clone itself without end.\n if (sourceParentId === destParentId || graph.isDescendant(destParentId, sourceParentId)) return\n\n const sourceParent = graph.nodes.get(sourceParentId)\n const targetParent = graph.nodes.get(destParentId)\n if (!sourceParent || !targetParent) return\n\n for (const childId of sourceParent.childIds) {\n const src = graph.nodes.get(childId)\n if (!src) continue\n\n const clone = cloneChildInCoordinates(graph, src, sourceParent, targetParent, mode)\n\n if (src.childIds.length > 0) {\n cloneChildrenWithMapping(graph, childId, clone.id, mode)\n }\n }\n}\n\nfunction sortInstanceChildren(\n graph: SceneGraph,\n instParent: SceneNode,\n instParentId: string,\n compChildOrder: string[],\n overrides: InstanceOverrideState\n): void {\n const orderMap = new Map<string, number>()\n for (let i = 0; i < compChildOrder.length; i++) orderMap.set(compChildOrder[i], i)\n\n const ranks = new Map<string, number>()\n for (let index = 0; index < instParent.childIds.length; index++) {\n const childId = instParent.childIds[index]\n const node = graph.nodes.get(childId)\n const mapped = node && copiedComponentLayerId(overrides, instParentId, node)\n const componentIndex = mapped ? orderMap.get(mapped) : undefined\n ranks.set(childId, componentIndex ?? compChildOrder.length + index)\n }\n const sorted = instParent.childIds.toSorted(\n (left, right) => (ranks.get(left) ?? 0) - (ranks.get(right) ?? 0)\n )\n // Move through the graph so the reorder is reported, as collaboration syncs it.\n sorted.forEach((childId, index) => {\n if (instParent.childIds[index] !== childId) graph.insertChildAt(childId, instParentId, index)\n })\n}\n\nexport interface MatchedCopy {\n compParent: SceneNode\n instParent: SceneNode\n compChild: SceneNode\n instChild: SceneNode\n}\n\nexport function syncCopy(\n graph: SceneGraph,\n { compParent, instParent, compChild, instChild }: MatchedCopy,\n protectedField: (field: string) => boolean,\n linkedLayerId?: string\n): boolean {\n const driven = propertyDrivenFields(graph, instChild, compChild)\n const kept =\n driven.size === 0 && protectedField === NOTHING_PROTECTED\n ? NOTHING_PROTECTED\n : (key: string) => driven.has(key) || isProtectedSyncField(instChild, key, protectedField)\n const componentScale =\n (compChild.componentScale * instParent.componentScale) / compParent.componentScale\n const effectiveSource = sourceWithEffectiveFill(graph, compChild, instChild)\n const source = sourceInTargetCoordinates(effectiveSource, componentScale)\n // Which properties a layer serves is the component's to say; a slot or exposed layer\n // created on the component becomes one in every instance.\n const updates: Partial<SceneNode> = {}\n if (instChild.componentScale !== componentScale) {\n updates.componentScale = componentScale\n }\n if (!isEqual(instChild.componentPropertyReferences, compChild.componentPropertyReferences)) {\n updates.componentPropertyReferences = structuredClone(compChild.componentPropertyReferences)\n }\n if (linkedLayerId !== undefined) {\n updates.componentId = linkedLayerId\n }\n syncBindingFields(instChild, source, updates, protectedField)\n const placement = copySyncedFields(graph, updates, instChild, source, {\n compParent,\n instParent,\n factor: componentScale / compChild.componentScale,\n skip: kept\n })\n Object.assign(updates, assignedFieldValues(graph, instChild, compChild))\n keepOwnContentSize(graph, updates, instChild, source, placement)\n updateSyncedProps(graph, instChild, updates, kept)\n return placement.placed !== source\n}\n\n/** True when syncing `compParentId` into `instParentId` would form a cycle. */\nfunction isCyclicSync(graph: SceneGraph, compParentId: string, instParentId: string): boolean {\n return compParentId === instParentId || graph.isDescendant(instParentId, compParentId)\n}\n\n/**\n * Pass 3 of a sync: bring along the copy of a layer moved here inside the component, bring back\n * one the sync removed when its layer returns, or clone a layer the instance has no copy of. A\n * clone takes its own subtree unless copies the instance has belong in it, which the recursion\n * moves in instead. Children left unmatched are the sync's to remove once it finishes.\n */\nfunction placeMissingChildren(\n graph: SceneGraph,\n compParent: SceneNode,\n instParent: SceneNode,\n instChildMap: Map<string, SceneNode>,\n usedInstChildIds: Set<string>,\n context: InstanceSyncContext\n): void {\n for (const child of instChildMap.values()) context.matched.add(child.id)\n\n for (const compChildId of compParent.childIds) {\n if (instChildMap.has(compChildId)) continue\n const moved =\n unplacedCopy(graph, context, compChildId, instParent.id) ??\n restoreRemovedCopy(graph, context, compChildId, instParent.id)\n if (moved) {\n graph.insertChildAt(moved.id, instParent.id, instParent.childIds.length)\n instChildMap.set(compChildId, moved)\n usedInstChildIds.add(moved.id)\n context.matched.add(moved.id)\n continue\n }\n const src = graph.nodes.get(compChildId)\n if (!src) continue\n const clone = cloneChildInCoordinates(graph, src, compParent, instParent)\n if (src.childIds.length > 0 && !hasUnplacedCopiesBelow(graph, context, compChildId)) {\n cloneChildrenWithMapping(graph, compChildId, clone.id)\n }\n applyEnclosingAssignments(graph, clone)\n applyBoundPaintColorsIn(graph, clone.id)\n instChildMap.set(compChildId, clone)\n usedInstChildIds.add(clone.id)\n context.matched.add(clone.id)\n }\n for (const childId of instParent.childIds) {\n if (!usedInstChildIds.has(childId)) context.leftovers.add(childId)\n }\n}\n\nexport function syncChildren(\n graph: SceneGraph,\n compParentId: string,\n instParentId: string,\n overrides: InstanceOverrideState,\n context: InstanceSyncContext = createInstanceSyncContext(instParentId, overrides)\n): boolean {\n // An instance inside its own component would clone the component into itself without end.\n if (isCyclicSync(graph, compParentId, instParentId)) return false\n\n const compParent = graph.nodes.get(compParentId)\n const instParent = graph.nodes.get(instParentId)\n if (!compParent || !instParent) return false\n\n const instChildMap = new Map<string, SceneNode>()\n const usedInstChildIds = new Set<string>()\n const compChildIdSet = new Set(compParent.childIds)\n\n // Pass 1: Direct matching via sourceComponentId or componentId\n matchLinkedChildren(\n graph,\n instParent,\n instParentId,\n overrides,\n compChildIdSet,\n instChildMap,\n usedInstChildIds\n )\n\n // Pass 2: Fallback matching for unmatched children (e.g. from Figma imports)\n matchFallbackChildren(\n graph,\n compParent,\n instParent,\n instParentId,\n context,\n instChildMap,\n usedInstChildIds\n )\n\n placeMissingChildren(graph, compParent, instParent, instChildMap, usedInstChildIds, context)\n\n // Pass 4: Synchronize properties and recurse\n let constrained = false\n for (const compChildId of compParent.childIds) {\n const compChild = graph.nodes.get(compChildId)\n const instChild = instChildMap.get(compChildId)\n if (!compChild || !instChild) continue\n\n const match = { compParent, instParent, compChild, instChild }\n const protection = childBindingProtection(graph, instChild, overrides)\n // True when its constraints placed the copy elsewhere than the component's layer.\n if (syncCopy(graph, match, protection)) constrained = true\n if (\n compChild.childIds.length > 0 &&\n !hasNodeInstanceOverride(overrides, instParentId, instChild.id, 'componentId') &&\n // The component's frame is the authority on which slot this is; instance copies of\n // its bindings are not synced.\n !ownsSlotContent(graph, instChild, slotPropertyId(compChild))\n ) {\n if (syncChildren(graph, compChildId, instChild.id, overrides, context)) constrained = true\n }\n }\n\n // Pass 5: Sort instance children to match component child order\n sortInstanceChildren(graph, instParent, instParentId, compParent.childIds, overrides)\n // A group fits layers its constraints moved; else it keeps the component's box, as a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A,gBACA,cACA,gBACF;CAGA,sBACE,OACA,YACA,YACA,cACA,SACA,cACA,gBACF;CAEA,qBAAqB,OAAO,YAAY,YAAY,cAAc,kBAAkB,OAAO;CAG3F,IAAI,cAAc;CAClB,KAAK,MAAM,eAAe,WAAW,UAAU;EAC7C,MAAM,YAAY,MAAM,MAAM,IAAI,WAAW;EAC7C,MAAM,YAAY,aAAa,IAAI,WAAW;EAC9C,IAAI,CAAC,aAAa,CAAC,WAAW;EAK9B,IAAI,SAAS,OAAO;GAHJ;GAAY;GAAY;GAAW;EAG3B,GAFL,uBAAuB,OAAO,WAAW,SAExB,CAAC,GAAG,cAAc;EACtD,IACE,UAAU,SAAS,SAAS,KAC5B,CAACA,oBAAwB,WAAW,cAAc,UAAU,IAAI,aAAa,KAG7E,CAAC,gBAAgB,OAAO,WAAW,eAAe,SAAS,CAAC,GAExD;OAAA,aAAa,OAAO,aAAa,UAAU,IAAI,WAAW,OAAO,GAAG,cAAc;EAAA;CAE1F;CAGA,qBAAqB,OAAO,YAAY,cAAc,WAAW,UAAU,SAAS;CAEpF,IAAI,aAAa,eAAe,OAAO,YAAY;CACnD,OAAO;AACT"}
|
package/dist/layout/guides.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
import { Color } from "../primitives/index.js";
|
|
1
2
|
import { LayoutGrid, SceneNode } from "../types.js";
|
|
2
3
|
//#region src/layout/guides.d.ts
|
|
3
4
|
interface LayoutGuideLine {
|
|
4
5
|
axis: 'x' | 'y';
|
|
5
6
|
position: number;
|
|
6
7
|
}
|
|
8
|
+
/** The most lines one guide draws or snaps to on an axis. */
|
|
9
|
+
declare const MAX_LAYOUT_GUIDE_LINES = 10000;
|
|
7
10
|
/** Figma's Auto count, as its archive stores it: as many sections as fit. */
|
|
8
11
|
declare const AUTO_LAYOUT_GRID_COUNT = 2147483647;
|
|
9
12
|
/** Whether a guide's count is Auto: the archive's value, or the plugin API's `Infinity` or `null`. */
|
|
@@ -13,6 +16,19 @@ declare function isAutoGridCount(count: number | null | undefined): boolean;
|
|
|
13
16
|
* edit's `count` wins over the count the file had. Auto is {@link AUTO_LAYOUT_GRID_COUNT}.
|
|
14
17
|
*/
|
|
15
18
|
declare function layoutGridCount(grid: LayoutGrid): number;
|
|
19
|
+
/** Figma's color for a new guide: red at 10%. */
|
|
20
|
+
declare const DEFAULT_LAYOUT_GRID_COLOR: Readonly<Color>;
|
|
21
|
+
/** A guide's pattern in Figma's plugin names, from either shape: the archive's `STRIPES` along an `axis`. */
|
|
22
|
+
declare function layoutGridPattern(grid: Pick<LayoutGrid, 'pattern' | 'axis'>): 'COLUMNS' | 'ROWS' | 'GRID';
|
|
23
|
+
/**
|
|
24
|
+
* A guide's alignment, from either shape. One set without any stretches, as the plugin API and
|
|
25
|
+
* export have read it; a file's guide always has one, as Figma drops a guide without it.
|
|
26
|
+
*/
|
|
27
|
+
declare function layoutGridAlignment(grid: Pick<LayoutGrid, 'alignment' | 'type'>): 'MIN' | 'CENTER' | 'MAX' | 'STRETCH';
|
|
28
|
+
/** Whether a file's guide record names an alignment; Figma drops one that does not (live). */
|
|
29
|
+
declare function hasLayoutGridAlignment(grid: Partial<Pick<LayoutGrid, 'alignment' | 'type'>>): boolean;
|
|
30
|
+
/** A guide's color, Figma's new-guide red for one set without any. */
|
|
31
|
+
declare function layoutGridColor(grid: Pick<LayoutGrid, 'color'>): Color;
|
|
16
32
|
interface LayoutGuideSection {
|
|
17
33
|
axis: 'x' | 'y';
|
|
18
34
|
start: number;
|
|
@@ -21,5 +37,5 @@ interface LayoutGuideSection {
|
|
|
21
37
|
declare function layoutGuideSections(node: Pick<SceneNode, 'width' | 'height'>, grid: LayoutGrid): LayoutGuideSection[];
|
|
22
38
|
declare function layoutGuideLines(node: Pick<SceneNode, 'width' | 'height'>, grid: LayoutGrid): LayoutGuideLine[];
|
|
23
39
|
//#endregion
|
|
24
|
-
export { AUTO_LAYOUT_GRID_COUNT, LayoutGuideLine, LayoutGuideSection, isAutoGridCount, layoutGridCount, layoutGuideLines, layoutGuideSections };
|
|
40
|
+
export { AUTO_LAYOUT_GRID_COUNT, DEFAULT_LAYOUT_GRID_COLOR, LayoutGuideLine, LayoutGuideSection, MAX_LAYOUT_GUIDE_LINES, hasLayoutGridAlignment, isAutoGridCount, layoutGridAlignment, layoutGridColor, layoutGridCount, layoutGridPattern, layoutGuideLines, layoutGuideSections };
|
|
25
41
|
//# sourceMappingURL=guides.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guides.d.ts","names":[],"sources":["../../src/layout/guides.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"guides.d.ts","names":[],"sources":["../../src/layout/guides.ts"],"mappings":";;;UAGiB;EACf;EACA;;;cAIW;;cAGA;;iBAGG,gBAAgB;;;;;iBAQhB,gBAAgB,MAAM;;cAMzB,2BAA2B,SAAS;;iBAQjC,kBACd,MAAM,KAAK;;;;;iBAYG,oBACd,MAAM,KAAK;;iBAMG,uBACd,MAAM,QAAQ,KAAK;;iBAML,gBAAgB,MAAM,KAAK,uBAAuB;UA4CjD;EACf;EACA;EACA;;iBAGc,oBACd,MAAM,KAAK,gCACX,MAAM,aACL;iBAea,iBAAiB,MAAM,KAAK,gCAAgC,MAAM,aAAU"}
|
package/dist/layout/guides.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
//#region src/layout/guides.ts
|
|
2
|
+
/** The most lines one guide draws or snaps to on an axis. */
|
|
2
3
|
const MAX_LAYOUT_GUIDE_LINES = 1e4;
|
|
3
4
|
/** Figma's Auto count, as its archive stores it: as many sections as fit. */
|
|
4
5
|
const AUTO_LAYOUT_GRID_COUNT = 2147483647;
|
|
@@ -14,16 +15,35 @@ function layoutGridCount(grid) {
|
|
|
14
15
|
const count = grid.count === void 0 ? grid.numSections : grid.count;
|
|
15
16
|
return isAutoGridCount(count) ? AUTO_LAYOUT_GRID_COUNT : count ?? 1;
|
|
16
17
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
/** Figma's color for a new guide: red at 10%. */
|
|
19
|
+
const DEFAULT_LAYOUT_GRID_COLOR = Object.freeze({
|
|
20
|
+
r: 1,
|
|
21
|
+
g: 0,
|
|
22
|
+
b: 0,
|
|
23
|
+
a: .1
|
|
24
|
+
});
|
|
25
|
+
/** A guide's pattern in Figma's plugin names, from either shape: the archive's `STRIPES` along an `axis`. */
|
|
26
|
+
function layoutGridPattern(grid) {
|
|
27
|
+
if (grid.pattern === "GRID" || grid.pattern === "ROWS" || grid.pattern === "COLUMNS") return grid.pattern;
|
|
28
|
+
return grid.axis === "Y" ? "ROWS" : "COLUMNS";
|
|
21
29
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
30
|
+
/**
|
|
31
|
+
* A guide's alignment, from either shape. One set without any stretches, as the plugin API and
|
|
32
|
+
* export have read it; a file's guide always has one, as Figma drops a guide without it.
|
|
33
|
+
*/
|
|
34
|
+
function layoutGridAlignment(grid) {
|
|
35
|
+
return grid.alignment ?? grid.type ?? "STRETCH";
|
|
36
|
+
}
|
|
37
|
+
/** Whether a file's guide record names an alignment; Figma drops one that does not (live). */
|
|
38
|
+
function hasLayoutGridAlignment(grid) {
|
|
39
|
+
return grid.type !== void 0 || grid.alignment !== void 0;
|
|
40
|
+
}
|
|
41
|
+
/** A guide's color, Figma's new-guide red for one set without any. */
|
|
42
|
+
function layoutGridColor(grid) {
|
|
43
|
+
return { ...grid.color ?? DEFAULT_LAYOUT_GRID_COLOR };
|
|
26
44
|
}
|
|
45
|
+
const pattern = layoutGridPattern;
|
|
46
|
+
const alignment = layoutGridAlignment;
|
|
27
47
|
/**
|
|
28
48
|
* How many sections a guide draws. Auto fits as many fixed sections as the frame holds, counting
|
|
29
49
|
* the offset once from the aligned edge, as Figma desktop 126 draws them.
|
|
@@ -79,11 +99,11 @@ function layoutGuideLines(node, grid) {
|
|
|
79
99
|
if (gridPattern === "GRID") {
|
|
80
100
|
const size = grid.sectionSize ?? 0;
|
|
81
101
|
if (size <= 0) return lines;
|
|
82
|
-
for (let index = 0, x = grid.offset ?? 0; x <= node.width && index <
|
|
102
|
+
for (let index = 0, x = grid.offset ?? 0; x <= node.width && index < 1e4; index++, x += size) lines.push({
|
|
83
103
|
axis: "x",
|
|
84
104
|
position: x
|
|
85
105
|
});
|
|
86
|
-
for (let index = 0, y = grid.offset ?? 0; y <= node.height && index <
|
|
106
|
+
for (let index = 0, y = grid.offset ?? 0; y <= node.height && index < 1e4; index++, y += size) lines.push({
|
|
87
107
|
axis: "y",
|
|
88
108
|
position: y
|
|
89
109
|
});
|
|
@@ -100,6 +120,6 @@ function layoutGuideLines(node, grid) {
|
|
|
100
120
|
return lines;
|
|
101
121
|
}
|
|
102
122
|
//#endregion
|
|
103
|
-
export { AUTO_LAYOUT_GRID_COUNT, isAutoGridCount, layoutGridCount, layoutGuideLines, layoutGuideSections };
|
|
123
|
+
export { AUTO_LAYOUT_GRID_COUNT, DEFAULT_LAYOUT_GRID_COLOR, MAX_LAYOUT_GUIDE_LINES, hasLayoutGridAlignment, isAutoGridCount, layoutGridAlignment, layoutGridColor, layoutGridCount, layoutGridPattern, layoutGuideLines, layoutGuideSections };
|
|
104
124
|
|
|
105
125
|
//# sourceMappingURL=guides.js.map
|