@open-pencil/scene-graph 0.17.0-beta.1 → 0.17.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/arrow-caps.d.ts.map +1 -1
- package/dist/arrow-caps.js +2 -5
- package/dist/arrow-caps.js.map +1 -1
- package/dist/behaviours/index.d.ts +2 -2
- package/dist/behaviours/index.js +2 -2
- package/dist/behaviours/kinds.d.ts +11 -5
- package/dist/behaviours/kinds.d.ts.map +1 -1
- package/dist/behaviours/kinds.js +76 -75
- package/dist/behaviours/kinds.js.map +1 -1
- package/dist/behaviours/layers.d.ts.map +1 -1
- package/dist/behaviours/layers.js +3 -2
- package/dist/behaviours/layers.js.map +1 -1
- package/dist/behaviours/model.d.ts +7 -4
- package/dist/behaviours/model.d.ts.map +1 -1
- package/dist/behaviours/model.js +16 -9
- package/dist/behaviours/model.js.map +1 -1
- package/dist/behaviours/schema.d.ts +118 -4
- package/dist/behaviours/schema.d.ts.map +1 -1
- package/dist/behaviours/schema.js +18 -3
- package/dist/behaviours/schema.js.map +1 -1
- package/dist/behaviours/spec.d.ts.map +1 -1
- package/dist/behaviours/spec.js +3 -2
- package/dist/behaviours/spec.js.map +1 -1
- package/dist/bindings.js +9 -1
- package/dist/bindings.js.map +1 -1
- package/dist/checkpoint.d.ts +31 -2
- package/dist/checkpoint.d.ts.map +1 -1
- package/dist/checkpoint.js +176 -5
- package/dist/checkpoint.js.map +1 -1
- package/dist/components/index.d.ts +3 -3
- package/dist/components/index.js +3 -3
- package/dist/components/properties.d.ts +13 -1
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +58 -3
- package/dist/components/properties.js.map +1 -1
- package/dist/components/variants.d.ts +16 -10
- package/dist/components/variants.d.ts.map +1 -1
- package/dist/components/variants.js +39 -13
- package/dist/components/variants.js.map +1 -1
- package/dist/connectors/hit.d.ts.map +1 -1
- package/dist/connectors/hit.js +1 -7
- package/dist/connectors/hit.js.map +1 -1
- package/dist/connectors/pieces.d.ts.map +1 -1
- package/dist/connectors/pieces.js +2 -5
- package/dist/connectors/pieces.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +9 -15
- package/dist/copy.js.map +1 -1
- package/dist/css/blend.d.ts +11 -0
- package/dist/css/blend.d.ts.map +1 -0
- package/dist/css/blend.js +30 -0
- package/dist/css/blend.js.map +1 -0
- package/dist/css/index.d.ts +2 -1
- package/dist/css/index.js +2 -1
- package/dist/cubic.d.ts +7 -0
- package/dist/cubic.d.ts.map +1 -0
- package/dist/cubic.js +17 -0
- package/dist/cubic.js.map +1 -0
- package/dist/export-format.d.ts +14 -1
- package/dist/export-format.d.ts.map +1 -1
- package/dist/export-format.js +26 -1
- package/dist/export-format.js.map +1 -1
- package/dist/fields/text.d.ts +2 -2
- package/dist/fields/text.d.ts.map +1 -1
- package/dist/fields/text.js +2 -1
- package/dist/fields/text.js.map +1 -1
- package/dist/figjam/index.d.ts +2 -2
- package/dist/figjam/index.js +2 -2
- package/dist/figjam/palette.d.ts +13 -2
- package/dist/figjam/palette.d.ts.map +1 -1
- package/dist/figjam/palette.js +30 -2
- package/dist/figjam/palette.js.map +1 -1
- package/dist/generated/index.d.ts +2 -2
- package/dist/generated/index.js +2 -2
- package/dist/generated/shape-with-text.d.ts +7 -2
- package/dist/generated/shape-with-text.d.ts.map +1 -1
- package/dist/generated/shape-with-text.js +20 -3
- package/dist/generated/shape-with-text.js.map +1 -1
- package/dist/geometry.d.ts +30 -46
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +63 -131
- package/dist/geometry.js.map +1 -1
- package/dist/graph.d.ts +1 -0
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +15 -3
- package/dist/graph.js.map +1 -1
- package/dist/guides.d.ts +3 -1
- package/dist/guides.d.ts.map +1 -1
- package/dist/guides.js +5 -1
- package/dist/guides.js.map +1 -1
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +42 -29
- package/dist/hit-test.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/fields.d.ts +2 -2
- package/dist/instances/fields.d.ts.map +1 -1
- package/dist/instances/fields.js +8 -0
- package/dist/instances/fields.js.map +1 -1
- package/dist/instances/index.d.ts +1 -3
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +36 -30
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/kept-overrides.js +204 -0
- package/dist/instances/kept-overrides.js.map +1 -0
- package/dist/instances/main-component.d.ts +3 -1
- package/dist/instances/main-component.d.ts.map +1 -1
- package/dist/instances/main-component.js +10 -2
- package/dist/instances/main-component.js.map +1 -1
- package/dist/instances/overrides.d.ts +4 -1
- package/dist/instances/overrides.d.ts.map +1 -1
- package/dist/instances/overrides.js +18 -2
- package/dist/instances/overrides.js.map +1 -1
- package/dist/instances/swap.js +288 -0
- package/dist/instances/swap.js.map +1 -0
- package/dist/instances/sync.js +84 -32
- package/dist/instances/sync.js.map +1 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +1 -1
- package/dist/node-defaults.js.map +1 -1
- package/dist/outline/flatten.d.ts +15 -0
- package/dist/outline/flatten.d.ts.map +1 -0
- package/dist/outline/flatten.js +189 -0
- package/dist/outline/flatten.js.map +1 -0
- package/dist/outline/heads.js +34 -0
- package/dist/outline/heads.js.map +1 -0
- package/dist/outline/hit.d.ts +21 -0
- package/dist/outline/hit.d.ts.map +1 -0
- package/dist/outline/hit.js +276 -0
- package/dist/outline/hit.js.map +1 -0
- package/dist/outline/index.d.ts +5 -0
- package/dist/outline/index.js +5 -0
- package/dist/outline/point.d.ts +16 -0
- package/dist/outline/point.d.ts.map +1 -0
- package/dist/outline/point.js +42 -0
- package/dist/outline/point.js.map +1 -0
- package/dist/outline/shapes.d.ts +35 -0
- package/dist/outline/shapes.d.ts.map +1 -0
- package/dist/outline/shapes.js +181 -0
- package/dist/outline/shapes.js.map +1 -0
- package/dist/plugin-data/fields.d.ts +2 -1
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +19 -4
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/scaling/tree.js +1 -1
- package/dist/shared-styles.d.ts +14 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +63 -2
- package/dist/shared-styles.js.map +1 -1
- package/dist/slots/content.d.ts.map +1 -1
- package/dist/slots/content.js +2 -0
- package/dist/slots/content.js.map +1 -1
- package/dist/slots/frames.d.ts +2 -0
- package/dist/slots/frames.d.ts.map +1 -1
- package/dist/slots/frames.js +7 -2
- package/dist/slots/frames.js.map +1 -1
- package/dist/text/index.d.ts +2 -2
- package/dist/text/index.js +3 -3
- package/dist/text/lengths.d.ts +6 -1
- package/dist/text/lengths.d.ts.map +1 -1
- package/dist/text/lengths.js +10 -1
- package/dist/text/lengths.js.map +1 -1
- package/dist/text/overflow.d.ts +2 -2
- package/dist/text/overflow.js.map +1 -1
- package/dist/text/picture.d.ts +1 -2
- package/dist/text/picture.d.ts.map +1 -1
- package/dist/text/picture.js +1 -4
- package/dist/text/picture.js.map +1 -1
- package/dist/transfer/index.js +1 -1
- package/dist/transfer/nodes.js +9 -5
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/types.d.ts +27 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/variables/bindings.d.ts +28 -5
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +104 -14
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/fields.d.ts +19 -1
- package/dist/variables/fields.d.ts.map +1 -1
- package/dist/variables/fields.js +36 -1
- package/dist/variables/fields.js.map +1 -1
- package/dist/variables/index.d.ts +3 -3
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +23 -8
- package/dist/variables/index.js.map +1 -1
- package/package.json +7 -1
- package/src/arrow-caps.ts +2 -5
- package/src/behaviours/kinds.ts +51 -34
- package/src/behaviours/layers.ts +4 -2
- package/src/behaviours/model.ts +22 -11
- package/src/behaviours/schema.ts +20 -4
- package/src/behaviours/spec.ts +8 -2
- package/src/bindings.ts +11 -2
- package/src/checkpoint.ts +251 -4
- package/src/components/properties.ts +92 -2
- package/src/components/variants.ts +51 -30
- package/src/connectors/hit.ts +1 -12
- package/src/connectors/pieces.ts +2 -5
- package/src/copy.ts +9 -15
- package/src/css/blend.ts +27 -0
- package/src/css/index.ts +1 -0
- package/src/cubic.ts +14 -0
- package/src/export-format.ts +29 -0
- package/src/fields/text.ts +3 -1
- package/src/figjam/palette.ts +29 -1
- package/src/generated/shape-with-text.ts +28 -2
- package/src/geometry.ts +78 -198
- package/src/graph.ts +27 -3
- package/src/guides.ts +5 -0
- package/src/hit-test.ts +67 -54
- package/src/instances/fields.ts +8 -0
- package/src/instances/index.ts +58 -31
- package/src/instances/kept-overrides.ts +280 -0
- package/src/instances/main-component.ts +12 -0
- package/src/instances/overrides.ts +27 -1
- package/src/instances/swap.ts +451 -0
- package/src/instances/sync.ts +141 -42
- package/src/node-defaults.ts +2 -1
- package/src/outline/flatten.ts +190 -0
- package/src/outline/heads.ts +52 -0
- package/src/outline/hit.ts +369 -0
- package/src/outline/index.ts +4 -0
- package/src/outline/point.ts +59 -0
- package/src/outline/shapes.ts +207 -0
- package/src/plugin-data/fields.ts +15 -2
- package/src/shared-styles.ts +82 -1
- package/src/slots/content.ts +2 -0
- package/src/slots/frames.ts +9 -7
- package/src/text/lengths.ts +10 -0
- package/src/text/overflow.ts +2 -2
- package/src/text/picture.ts +0 -4
- package/src/transfer/nodes.ts +7 -5
- package/src/types.ts +28 -0
- package/src/variables/bindings.ts +125 -14
- package/src/variables/fields.ts +46 -1
- package/src/variables/index.ts +35 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fields.js","names":[],"sources":["../../src/instances/fields.ts"],"sourcesContent":["import { difference } from 'es-toolkit/array'\n\nimport { CORNER_FIELDS, PAINT_FIELDS, STROKE_FIELDS } from '../fields/appearance'\nimport { SIZE_FIELDS, TRANSFORM_FIELDS } from '../fields/geometry'\nimport { AUTO_LAYOUT_FIELDS, SIZE_CONSTRAINT_FIELDS } from '../fields/layout'\nimport { TEXT_STYLE_FIELDS } from '../fields/text'\nimport { STYLE_LINK_FIELDS } from '../shared-styles'\nimport type { SceneNode } from '../types'\n\nexport const INSTANCE_SYNC_TEXT_PROPS = [\n 'name',\n 'text',\n ...TEXT_STYLE_FIELDS,\n 'textDirection',\n 'textAutoResize',\n 'textTruncation',\n 'maxLines',\n 'leadingTrim',\n 'listSpacing',\n 'hangingList',\n // Styled ranges belong to the characters: an instance that overrides its text keeps them.\n 'styleRuns',\n 'textParagraphs'\n] as const\n\n/** Fields that belong to another one: overriding the owner keeps them too. */\nexport const INSTANCE_SYNC_FIELD_OWNERS: Readonly<Partial<Record<string, string>>> = {\n fillStyleId: 'fills',\n styleRuns: 'text',\n textParagraphs: 'text'\n}\n\n/**\n * Where a component's layer sits and how it is shaped, which Figma refuses to override in an\n * instance (\"This property cannot be overridden in an instance\"), so its copies always follow.\n * A layer in an auto layout flow takes its position from the layout instead.\n */\nexport const INSTANCE_FOLLOWED_FIELDS = [\n ...TRANSFORM_FIELDS,\n 'horizontalConstraint',\n 'verticalConstraint',\n 'layoutPositioning',\n 'counterAxisAlignContent',\n 'isMask',\n 'maskType',\n 'arcData',\n 'pointCount',\n 'starInnerRadius',\n 'shapeWithTextType',\n 'shapeUserHeight',\n 'shapeTruncates'\n] as const\n\n/** Fields a component's layer gives its copies unless an instance overrides them. */\nexport const INSTANCE_SYNC_PROPS: (keyof SceneNode)[] = difference(\n [\n ...SIZE_FIELDS,\n ...SIZE_CONSTRAINT_FIELDS,\n ...PAINT_FIELDS,\n ...STROKE_FIELDS,\n ...CORNER_FIELDS,\n ...AUTO_LAYOUT_FIELDS,\n 'itemReverseZIndex',\n 'gridPosition',\n 'clipsContent',\n 'layoutGrids',\n 'boundVariables',\n 'variableModes',\n // Applied shared styles follow the component unless an instance overrides them.\n ...STYLE_LINK_FIELDS\n ],\n INSTANCE_FOLLOWED_FIELDS\n)\n\n/** How a layer inside an instance sits in its parent and whether it is locked, which its overrides may change. */\nexport const INSTANCE_SYNC_CHILD_PROPS = ['layoutGrow', 'layoutAlignSelf', 'locked'] as const\n\n/** Fields an edit to a layer inside an instance records as that instance's override. */\nexport const INSTANCE_OVERRIDE_FIELDS = [\n ...INSTANCE_SYNC_PROPS,\n ...INSTANCE_SYNC_TEXT_PROPS,\n ...INSTANCE_SYNC_CHILD_PROPS,\n 'visible'\n] as const\n\n/** Fields a component's layer gives the copies of it in every instance. */\nexport const INSTANCE_SYNC_FIELDS = [\n ...INSTANCE_OVERRIDE_FIELDS,\n ...INSTANCE_FOLLOWED_FIELDS\n] as const\n"],"mappings":";;;;;;;AASA,MAAa,2BAA2B;CACtC;CACA;CACA,GAAG;CACH;CACA;CACA;CACA;CACA;CACA;CACA;CAEA;CACA;AACF;;AAGA,MAAa,6BAAwE;CACnF,aAAa;CACb,WAAW;CACX,gBAAgB;AAClB;;;;;;AAOA,MAAa,2BAA2B;CACtC,GAAG;CACH;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;;AAGA,MAAa,sBAA2C,WACtD;CACE,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH;CACA;CACA;CACA;CACA;CACA;CAEA,GAAG;AACL,GACA,wBACF;;AAGA,MAAa,4BAA4B;CAAC;CAAc;CAAmB;AAAQ;;AAGnF,MAAa,2BAA2B;CACtC,GAAG;CACH,GAAG;CACH,GAAG;CACH;AACF;;AAGA,MAAa,uBAAuB,CAClC,GAAG,0BACH,GAAG,wBACL"}
|
|
1
|
+
{"version":3,"file":"fields.js","names":[],"sources":["../../src/instances/fields.ts"],"sourcesContent":["import { difference } from 'es-toolkit/array'\n\nimport { CORNER_FIELDS, PAINT_FIELDS, STROKE_FIELDS } from '../fields/appearance'\nimport { SIZE_FIELDS, TRANSFORM_FIELDS } from '../fields/geometry'\nimport { AUTO_LAYOUT_FIELDS, SIZE_CONSTRAINT_FIELDS } from '../fields/layout'\nimport { TEXT_STYLE_FIELDS } from '../fields/text'\nimport { STYLE_LINK_FIELDS } from '../shared-styles'\nimport type { SceneNode } from '../types'\n\nexport const INSTANCE_SYNC_TEXT_PROPS = [\n 'name',\n 'text',\n ...TEXT_STYLE_FIELDS,\n 'textDirection',\n 'textAutoResize',\n 'textTruncation',\n 'maxLines',\n 'leadingTrim',\n 'listSpacing',\n 'hangingList',\n // Styled ranges belong to the characters: an instance that overrides its text keeps them.\n 'styleRuns',\n 'textParagraphs'\n] as const\n\n/** Fields that belong to another one: overriding the owner keeps them too. */\nexport const INSTANCE_SYNC_FIELD_OWNERS: Readonly<Partial<Record<string, string>>> = {\n fillStyleId: 'fills',\n styleRuns: 'text',\n textParagraphs: 'text'\n}\n\n/**\n * Where a component's layer sits and how it is shaped, which Figma refuses to override in an\n * instance (\"This property cannot be overridden in an instance\"), so its copies always follow.\n * A layer in an auto layout flow takes its position from the layout instead.\n */\nexport const INSTANCE_FOLLOWED_FIELDS = [\n ...TRANSFORM_FIELDS,\n 'horizontalConstraint',\n 'verticalConstraint',\n 'layoutPositioning',\n 'counterAxisAlignContent',\n 'isMask',\n 'maskType',\n 'maskIsOutline',\n 'booleanOperation',\n 'isExposedInstance',\n 'connectorStart',\n 'connectorEnd',\n 'connectorLineType',\n 'connectorStartCap',\n 'connectorEndCap',\n 'arcData',\n 'pointCount',\n 'starInnerRadius',\n 'shapeWithTextType',\n 'shapeUserHeight',\n 'shapeTruncates'\n] as const\n\n/** Fields a component's layer gives its copies unless an instance overrides them. */\nexport const INSTANCE_SYNC_PROPS: (keyof SceneNode)[] = difference(\n [\n ...SIZE_FIELDS,\n ...SIZE_CONSTRAINT_FIELDS,\n ...PAINT_FIELDS,\n ...STROKE_FIELDS,\n ...CORNER_FIELDS,\n ...AUTO_LAYOUT_FIELDS,\n 'itemReverseZIndex',\n 'gridPosition',\n 'clipsContent',\n 'layoutGrids',\n 'boundVariables',\n 'variableModes',\n // Applied shared styles follow the component unless an instance overrides them.\n ...STYLE_LINK_FIELDS\n ],\n INSTANCE_FOLLOWED_FIELDS\n)\n\n/** How a layer inside an instance sits in its parent and whether it is locked, which its overrides may change. */\nexport const INSTANCE_SYNC_CHILD_PROPS = ['layoutGrow', 'layoutAlignSelf', 'locked'] as const\n\n/** Fields an edit to a layer inside an instance records as that instance's override. */\nexport const INSTANCE_OVERRIDE_FIELDS = [\n ...INSTANCE_SYNC_PROPS,\n ...INSTANCE_SYNC_TEXT_PROPS,\n ...INSTANCE_SYNC_CHILD_PROPS,\n 'visible'\n] as const\n\n/** Fields a component's layer gives the copies of it in every instance. */\nexport const INSTANCE_SYNC_FIELDS = [\n ...INSTANCE_OVERRIDE_FIELDS,\n ...INSTANCE_FOLLOWED_FIELDS\n] as const\n"],"mappings":";;;;;;;AASA,MAAa,2BAA2B;CACtC;CACA;CACA,GAAG;CACH;CACA;CACA;CACA;CACA;CACA;CACA;CAEA;CACA;AACF;;AAGA,MAAa,6BAAwE;CACnF,aAAa;CACb,WAAW;CACX,gBAAgB;AAClB;;;;;;AAOA,MAAa,2BAA2B;CACtC,GAAG;CACH;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;;AAGA,MAAa,sBAA2C,WACtD;CACE,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;CACH;CACA;CACA;CACA;CACA;CACA;CAEA,GAAG;AACL,GACA,wBACF;;AAGA,MAAa,4BAA4B;CAAC;CAAc;CAAmB;AAAQ;;AAGnF,MAAa,2BAA2B;CACtC,GAAG;CACH,GAAG;CACH,GAAG;CACH;AACF;;AAGA,MAAa,uBAAuB,CAClC,GAAG,0BACH,GAAG,wBACL"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { NodeCloneMode } from "../copy.js";
|
|
2
2
|
import { canCreateInstance, createsComponentCycle, cycleSafeParent } from "./cycles.js";
|
|
3
|
-
import { instanceMainComponent } from "./main-component.js";
|
|
3
|
+
import { findInstanceAncestor, instanceMainComponent } from "./main-component.js";
|
|
4
4
|
import { INSTANCE_FOLLOWED_FIELDS, INSTANCE_OVERRIDE_FIELDS, INSTANCE_SYNC_FIELDS, INSTANCE_SYNC_PROPS, INSTANCE_SYNC_TEXT_PROPS } from "./fields.js";
|
|
5
5
|
import { SceneGraph } from "../graph.js";
|
|
6
6
|
import { SceneNode } from "../types.js";
|
|
@@ -28,8 +28,6 @@ declare function syncInstance(graph: SceneGraph, instanceId: string): void;
|
|
|
28
28
|
declare function detachInstance(graph: SceneGraph, instanceId: string): void;
|
|
29
29
|
declare function getMainComponent(graph: SceneGraph, instanceId: string): SceneNode | undefined;
|
|
30
30
|
declare function getInstances(graph: SceneGraph, componentId: string): SceneNode[];
|
|
31
|
-
/** Nearest INSTANCE at or above `nodeId` — self, parent, grandparent, etc. */
|
|
32
|
-
declare function findInstanceAncestor(graph: SceneGraph, nodeId: string): SceneNode | undefined;
|
|
33
31
|
/**
|
|
34
32
|
* True when a node field is protected from instance synchronization.
|
|
35
33
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/instances/index.ts"],"mappings":";;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/instances/index.ts"],"mappings":";;;;;;;iBA0CgB,2BAA2B,WAAW,YAAY,QAAQ;iBAM1D,eACd,OAAO,YACP,qBACA,kBACA,YAAW,QAAQ,aAClB;iBAkBa,yBACd,OAAO,YACP,oBACA,qBACA,OAAM;UAmCS;;;;;;EAMf;;iBAgBc,sBACd,OAAO,YACP,oBACA,uBACE,aAAoB;iBAuDR,cAAc,OAAO,YAAY;;;;;iBAQjC,sBAAsB,OAAO,YAAY,cAAc;;iBAOvD,aAAa,OAAO,YAAY;iBAsChC,eAAe,OAAO,YAAY;iBAWlC,iBAAiB,OAAO,YAAY,qBAAqB;iBAMzD,aAAa,OAAO,YAAY,sBAAsB;;;;iBActD,oBAAoB,OAAO,YAAY,gBAAgB;iBAMvD,uBACd,OAAO,YACP,gBACA,QAAQ"}
|
package/dist/instances/index.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { clearInstanceOverrides, getInstanceOverride, hasInstanceOverride as hasInstanceOverride$1, setInstanceOverride } from "./overrides.js";
|
|
2
|
-
import { instanceMainComponent } from "./main-component.js";
|
|
2
|
+
import { findInstanceAncestor, instanceMainComponent } from "./main-component.js";
|
|
3
3
|
import { detachOwnedSlotContent, restoreOwnedSlotContent } from "../slots/frames.js";
|
|
4
4
|
import { keepOwnContentSize } from "./content-size.js";
|
|
5
5
|
import { createInstanceSyncContext, groupCopyRemovals, removeLeftoverCopies } from "./copies.js";
|
|
6
6
|
import { canCreateInstance, createsComponentCycle, cycleSafeParent } from "./cycles.js";
|
|
7
7
|
import { INSTANCE_FOLLOWED_FIELDS, INSTANCE_OVERRIDE_FIELDS, INSTANCE_SYNC_FIELDS, INSTANCE_SYNC_PROPS, INSTANCE_SYNC_TEXT_PROPS } from "./fields.js";
|
|
8
8
|
import { bindingProtection, cloneChildrenWithMapping, copyProp, enclosingInstanceOverrideFields, isProtectedSyncField, sourceInTargetCoordinates, syncBindingFields, syncChildren, updateSyncedProps } from "./sync.js";
|
|
9
|
+
import { followComponentLayers } from "./swap.js";
|
|
9
10
|
//#region src/instances/index.ts
|
|
10
11
|
function copyInstanceComponentProps(component) {
|
|
11
12
|
const props = {};
|
|
@@ -46,27 +47,48 @@ function recordNestedSwap(graph, instance, componentId) {
|
|
|
46
47
|
setInstanceOverride(owner.instanceOverrides, owner.id, instance.id, "componentId", componentId);
|
|
47
48
|
graph.updateNode(owner.id, { instanceOverrides: owner.instanceOverrides });
|
|
48
49
|
}
|
|
50
|
+
function namedAfter(graph, instance, component) {
|
|
51
|
+
const main = instanceMainComponent(graph, instance);
|
|
52
|
+
const set = main?.parentId ? graph.nodes.get(main.parentId) : void 0;
|
|
53
|
+
if (set?.type === "COMPONENT_SET" && component.parentId === set.id) return main;
|
|
54
|
+
return instance.componentId ? graph.nodes.get(instance.componentId) : void 0;
|
|
55
|
+
}
|
|
49
56
|
function swapInstanceComponent(graph, instanceId, componentId, { keepName = false } = {}) {
|
|
50
57
|
const instance = graph.nodes.get(instanceId);
|
|
51
58
|
const component = graph.nodes.get(componentId);
|
|
52
59
|
if (!instance || component?.type !== "COMPONENT" || instance.type !== "INSTANCE") return;
|
|
53
60
|
if (instance.parentId && !canCreateInstance(graph, componentId, instance.parentId)) return;
|
|
54
|
-
const previousComponent = instance
|
|
61
|
+
const previousComponent = namedAfter(graph, instance, component);
|
|
55
62
|
recordNestedSwap(graph, instance, componentId);
|
|
56
|
-
const updates = {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
copyProp(updates, source, key);
|
|
61
|
-
}
|
|
63
|
+
const updates = {
|
|
64
|
+
componentId,
|
|
65
|
+
...ownSyncedProps(graph, instance, sourceInTargetCoordinates(component, instance.componentScale)).updates
|
|
66
|
+
};
|
|
62
67
|
if (!keepName && (!previousComponent || instance.name === previousComponent.name)) updates.name = component.name;
|
|
63
|
-
const childIds = Array.from(instance.childIds);
|
|
64
68
|
const slotContent = detachOwnedSlotContent(graph, instance);
|
|
65
|
-
for (const childId of childIds) graph.deleteNode(childId);
|
|
66
69
|
graph.updateNode(instanceId, updates);
|
|
67
|
-
|
|
70
|
+
followComponentLayers(graph, instance, component, parkedContent(slotContent));
|
|
68
71
|
restoreOwnedSlotContent(graph, instance, slotContent);
|
|
69
72
|
}
|
|
73
|
+
const NOTHING_PARKED = /* @__PURE__ */ new Set();
|
|
74
|
+
function parkedContent(parked) {
|
|
75
|
+
if (parked.slots.length === 0) return NOTHING_PARKED;
|
|
76
|
+
return new Set(parked.slots.flatMap((slot) => slot.content));
|
|
77
|
+
}
|
|
78
|
+
/** What an instance takes from its component, and which fields it keeps as its own. */
|
|
79
|
+
function ownSyncedProps(graph, instance, source) {
|
|
80
|
+
const enclosing = enclosingInstanceOverrideFields(graph, instance);
|
|
81
|
+
enclosing.push(new Set(instance.instanceOverrides.self.keys()));
|
|
82
|
+
const protectedField = bindingProtection(enclosing);
|
|
83
|
+
const updates = {};
|
|
84
|
+
syncBindingFields(instance, source, updates, protectedField);
|
|
85
|
+
const kept = (key) => isProtectedSyncField(instance, key, protectedField);
|
|
86
|
+
for (const key of INSTANCE_SYNC_PROPS) if (key !== "boundVariables" && !kept(key)) copyProp(updates, source, key);
|
|
87
|
+
return {
|
|
88
|
+
updates,
|
|
89
|
+
kept
|
|
90
|
+
};
|
|
91
|
+
}
|
|
70
92
|
const syncingComponentsByGraph = /* @__PURE__ */ new WeakMap();
|
|
71
93
|
function syncInstances(graph, componentId) {
|
|
72
94
|
syncInstancesOf(graph, componentId, getInstances(graph, componentId));
|
|
@@ -99,19 +121,10 @@ function syncInstancesOf(graph, componentId, instances) {
|
|
|
99
121
|
try {
|
|
100
122
|
groupCopyRemovals(graph, (removals) => {
|
|
101
123
|
for (const instance of instances) {
|
|
102
|
-
const enclosing = enclosingInstanceOverrideFields(graph, instance);
|
|
103
|
-
enclosing.push(new Set(instance.instanceOverrides.self.keys()));
|
|
104
|
-
const protectedField = bindingProtection(enclosing);
|
|
105
124
|
const source = sourceInTargetCoordinates(component, instance.componentScale);
|
|
106
|
-
const updates =
|
|
107
|
-
syncBindingFields(instance, source, updates, protectedField);
|
|
108
|
-
for (const key of INSTANCE_SYNC_PROPS) {
|
|
109
|
-
if (key === "boundVariables") continue;
|
|
110
|
-
if (isProtectedSyncField(instance, key, protectedField)) continue;
|
|
111
|
-
copyProp(updates, source, key);
|
|
112
|
-
}
|
|
125
|
+
const { updates, kept } = ownSyncedProps(graph, instance, source);
|
|
113
126
|
keepOwnContentSize(graph, updates, instance, source);
|
|
114
|
-
updateSyncedProps(graph, instance, updates);
|
|
127
|
+
updateSyncedProps(graph, instance, updates, kept);
|
|
115
128
|
const context = createInstanceSyncContext(instance.id, instance.instanceOverrides);
|
|
116
129
|
syncChildren(graph, component.id, instance.id, instance.instanceOverrides, context);
|
|
117
130
|
removeLeftoverCopies(graph, context, removals);
|
|
@@ -144,14 +157,6 @@ function getInstances(graph, componentId) {
|
|
|
144
157
|
}
|
|
145
158
|
return instances;
|
|
146
159
|
}
|
|
147
|
-
/** Nearest INSTANCE at or above `nodeId` — self, parent, grandparent, etc. */
|
|
148
|
-
function findInstanceAncestor(graph, nodeId) {
|
|
149
|
-
let current = graph.nodes.get(nodeId);
|
|
150
|
-
while (current) {
|
|
151
|
-
if (current.type === "INSTANCE") return current;
|
|
152
|
-
current = current.parentId ? graph.nodes.get(current.parentId) : void 0;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
160
|
/**
|
|
156
161
|
* True when a node field is protected from instance synchronization.
|
|
157
162
|
*/
|
|
@@ -165,6 +170,7 @@ function recordInstanceOverride(graph, nodeId, fields) {
|
|
|
165
170
|
if (!instance) return;
|
|
166
171
|
const relevant = [...fields].filter((field) => INSTANCE_OVERRIDE_FIELDS.includes(field));
|
|
167
172
|
if (relevant.length === 0) return;
|
|
173
|
+
graph.willChangeNode(instance);
|
|
168
174
|
for (const field of relevant) setInstanceOverride(instance.instanceOverrides, instance.id, nodeId, field);
|
|
169
175
|
graph.updateNode(instance.id, { instanceOverrides: instance.instanceOverrides });
|
|
170
176
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["hasNodeInstanceOverride"],"sources":["../../src/instances/index.ts"],"sourcesContent":["import type { NodeCloneMode } from '../copy'\nimport type { SceneGraph } from '../graph'\nimport { detachOwnedSlotContent, restoreOwnedSlotContent } from '../slots/frames'\nimport type { SceneNode } from '../types'\nimport { keepOwnContentSize } from './content-size'\nimport { createInstanceSyncContext, groupCopyRemovals, removeLeftoverCopies } from './copies'\nimport { canCreateInstance } from './cycles'\nimport { INSTANCE_OVERRIDE_FIELDS, INSTANCE_SYNC_PROPS } from './fields'\nimport {\n clearInstanceOverrides,\n getInstanceOverride,\n hasInstanceOverride as hasNodeInstanceOverride,\n setInstanceOverride\n} from './overrides'\nimport {\n bindingProtection,\n cloneChildrenWithMapping,\n copyProp,\n enclosingInstanceOverrideFields,\n isProtectedSyncField,\n sourceInTargetCoordinates,\n syncBindingFields,\n syncChildren,\n updateSyncedProps\n} from './sync'\n\nexport { canCreateInstance, createsComponentCycle, cycleSafeParent } from './cycles'\nexport { instanceMainComponent } from './main-component'\nexport {\n INSTANCE_FOLLOWED_FIELDS,\n INSTANCE_OVERRIDE_FIELDS,\n INSTANCE_SYNC_FIELDS,\n INSTANCE_SYNC_PROPS,\n INSTANCE_SYNC_TEXT_PROPS\n} from './fields'\n\nexport function copyInstanceComponentProps(component: SceneNode): Partial<SceneNode> {\n const props: Partial<SceneNode> = {}\n for (const key of INSTANCE_SYNC_PROPS) copyProp(props, component, key)\n return props\n}\n\nexport function createInstance(\n graph: SceneGraph,\n componentId: string,\n parentId: string,\n overrides: Partial<SceneNode> = {}\n): SceneNode | null {\n const component = graph.nodes.get(componentId)\n if (component?.type !== 'COMPONENT' || !canCreateInstance(graph, componentId, parentId))\n return null\n\n const props: Partial<SceneNode> = {\n ...copyInstanceComponentProps(component),\n name: component.name\n }\n\n // The component checked above is the one the instance shows, whatever `overrides` holds.\n const instance = graph.createNode('INSTANCE', parentId, { ...props, ...overrides, componentId })\n\n cloneChildrenWithMapping(graph, component.id, instance.id)\n\n return instance\n}\n\nexport function populateInstanceChildren(\n graph: SceneGraph,\n instanceId: string,\n componentId: string,\n mode: NodeCloneMode = 'deep'\n): void {\n const instance = graph.nodes.get(instanceId)\n const component = graph.nodes.get(componentId)\n if (!instance || !component || instance.type !== 'INSTANCE') return\n cloneChildrenWithMapping(graph, componentId, instanceId, mode)\n}\n\n/**\n * A nested instance's link to the enclosing component's record is its `componentId` when it\n * was populated by cloning. A swap replaces that field, so keep the correspondence as the\n * owner's `sourceComponentId` override and record the swap itself; materialized documents\n * already carry the correspondence and only gain the swap.\n */\nfunction recordNestedSwap(graph: SceneGraph, instance: SceneNode, componentId: string): void {\n const owner = instance.parentId ? findInstanceAncestor(graph, instance.parentId) : undefined\n if (!owner) return\n const existing = getInstanceOverride(\n owner.instanceOverrides,\n owner.id,\n instance.id,\n 'sourceComponentId'\n )\n if (typeof existing !== 'string' && instance.componentId)\n setInstanceOverride(\n owner.instanceOverrides,\n owner.id,\n instance.id,\n 'sourceComponentId',\n instance.componentId\n )\n setInstanceOverride(owner.instanceOverrides, owner.id, instance.id, 'componentId', componentId)\n graph.updateNode(owner.id, { instanceOverrides: owner.instanceOverrides })\n}\n\nexport interface SwapInstanceOptions {\n /**\n * Keep the instance's name even when it is its old component's. A swap otherwise renames such\n * an instance after the new component, as Figma does; a view that only shows another variant,\n * such as preview, keeps names so layer paths stay the same.\n */\n keepName?: boolean\n}\n\nexport function swapInstanceComponent(\n graph: SceneGraph,\n instanceId: string,\n componentId: string,\n { keepName = false }: SwapInstanceOptions = {}\n): void {\n const instance = graph.nodes.get(instanceId)\n const component = graph.nodes.get(componentId)\n if (!instance || component?.type !== 'COMPONENT' || instance.type !== 'INSTANCE') return\n if (instance.parentId && !canCreateInstance(graph, componentId, instance.parentId)) return\n\n const previousComponent = instance.componentId ? graph.nodes.get(instance.componentId) : undefined\n recordNestedSwap(graph, instance, componentId)\n const updates: Partial<SceneNode> = { componentId }\n const source = sourceInTargetCoordinates(component, instance.componentScale)\n for (const key of INSTANCE_SYNC_PROPS) {\n if (hasNodeInstanceOverride(instance.instanceOverrides, instance.id, instance.id, key)) continue\n copyProp(updates, source, key)\n }\n\n if (!keepName && (!previousComponent || instance.name === previousComponent.name))\n updates.name = component.name\n\n const childIds = Array.from(instance.childIds)\n const slotContent = detachOwnedSlotContent(graph, instance)\n for (const childId of childIds) graph.deleteNode(childId)\n graph.updateNode(instanceId, updates)\n cloneChildrenWithMapping(graph, componentId, instanceId)\n restoreOwnedSlotContent(graph, instance, slotContent)\n}\n\nconst syncingComponentsByGraph = new WeakMap<SceneGraph, Set<string>>()\n\nexport function syncInstances(graph: SceneGraph, componentId: string): void {\n syncInstancesOf(graph, componentId, getInstances(graph, componentId))\n}\n\n/**\n * Syncs each component's instances, in order, as one edit: the copies they remove stay\n * restorable together for as long as undo keeps that edit.\n */\nexport function syncInstancesTogether(graph: SceneGraph, componentIds: Iterable<string>): void {\n groupCopyRemovals(graph, () => {\n for (const id of componentIds) syncInstances(graph, id)\n })\n}\n\n/** Syncs one instance from its component, as `syncInstances` does for every instance. */\nexport function syncInstance(graph: SceneGraph, instanceId: string): void {\n const instance = graph.nodes.get(instanceId)\n if (instance?.type !== 'INSTANCE' || !instance.componentId) return\n syncInstancesOf(graph, instance.componentId, [instance])\n}\n\nfunction syncInstancesOf(\n graph: SceneGraph,\n componentId: string,\n instances: Iterable<SceneNode>\n): void {\n const component = graph.nodes.get(componentId)\n if (component?.type !== 'COMPONENT') return\n let syncing = syncingComponentsByGraph.get(graph)\n if (!syncing) {\n syncing = new Set()\n syncingComponentsByGraph.set(graph, syncing)\n }\n if (syncing.has(componentId)) return\n syncing.add(componentId)\n try {\n groupCopyRemovals(graph, (removals) => {\n for (const instance of instances) {\n const enclosing = enclosingInstanceOverrideFields(graph, instance)\n enclosing.push(new Set(instance.instanceOverrides.self.keys()))\n const protectedField = bindingProtection(enclosing)\n const source = sourceInTargetCoordinates(component, instance.componentScale)\n const updates: Partial<SceneNode> = {}\n syncBindingFields(instance, source, updates, protectedField)\n for (const key of INSTANCE_SYNC_PROPS) {\n if (key === 'boundVariables') continue\n if (isProtectedSyncField(instance, key, protectedField)) continue\n copyProp(updates, source, key)\n }\n // An instance that hugs its layers sizes to what it shows, not to the component.\n keepOwnContentSize(graph, updates, instance, source)\n updateSyncedProps(graph, instance, updates)\n const context = createInstanceSyncContext(instance.id, instance.instanceOverrides)\n syncChildren(graph, component.id, instance.id, instance.instanceOverrides, context)\n removeLeftoverCopies(graph, context, removals)\n }\n })\n } finally {\n syncing.delete(componentId)\n }\n}\n\nexport function detachInstance(graph: SceneGraph, instanceId: string): void {\n const node = graph.nodes.get(instanceId)\n if (node?.type !== 'INSTANCE') return\n if (node.componentId) {\n graph.instanceIndex.get(node.componentId)?.delete(instanceId)\n }\n node.type = 'FRAME'\n node.componentId = null\n clearInstanceOverrides(node.instanceOverrides)\n}\n\nexport function getMainComponent(graph: SceneGraph, instanceId: string): SceneNode | undefined {\n const node = graph.nodes.get(instanceId)\n if (!node?.componentId) return undefined\n return graph.nodes.get(node.componentId)\n}\n\nexport function getInstances(graph: SceneGraph, componentId: string): SceneNode[] {\n const ids = graph.instanceIndex.get(componentId)\n if (!ids) return []\n const instances: SceneNode[] = []\n for (const id of ids) {\n const node = graph.nodes.get(id)\n if (node) instances.push(node)\n }\n return instances\n}\n\n/** Nearest INSTANCE at or above `nodeId` — self, parent, grandparent, etc. */\nexport function findInstanceAncestor(graph: SceneGraph, nodeId: string): SceneNode | undefined {\n let current = graph.nodes.get(nodeId)\n while (current) {\n if (current.type === 'INSTANCE') return current\n current = current.parentId ? graph.nodes.get(current.parentId) : undefined\n }\n return undefined\n}\n\n/**\n * True when a node field is protected from instance synchronization.\n */\nexport function hasInstanceOverride(graph: SceneGraph, nodeId: string, field: string): boolean {\n const instance = findInstanceAncestor(graph, nodeId)\n if (!instance) return false\n return hasNodeInstanceOverride(instance.instanceOverrides, instance.id, nodeId, field)\n}\n\nexport function recordInstanceOverride(\n graph: SceneGraph,\n nodeId: string,\n fields: Iterable<string>\n): void {\n const instance = findInstanceAncestor(graph, nodeId)\n if (!instance) return\n\n const relevant = [...fields].filter((field) =>\n (INSTANCE_OVERRIDE_FIELDS as readonly string[]).includes(field)\n )\n\n if (relevant.length === 0) return\n\n for (const field of relevant)\n setInstanceOverride(instance.instanceOverrides, instance.id, nodeId, field)\n graph.updateNode(instance.id, { instanceOverrides: instance.instanceOverrides })\n}\n"],"mappings":";;;;;;;;;AA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AM,SAAS,UAClB,oBAAoB,SAAS,mBAAmB,SAAS,IAAI,QAAQ,KAAK;CAC5E,MAAM,WAAW,SAAS,IAAI,EAAE,mBAAmB,SAAS,kBAAkB,CAAC;AACjF"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["hasNodeInstanceOverride"],"sources":["../../src/instances/index.ts"],"sourcesContent":["import type { NodeCloneMode } from '../copy'\nimport type { SceneGraph } from '../graph'\nimport {\n detachOwnedSlotContent,\n restoreOwnedSlotContent,\n type ParkedSlotContent\n} from '../slots/frames'\nimport type { SceneNode } from '../types'\nimport { keepOwnContentSize } from './content-size'\nimport { createInstanceSyncContext, groupCopyRemovals, removeLeftoverCopies } from './copies'\nimport { canCreateInstance } from './cycles'\nimport { INSTANCE_OVERRIDE_FIELDS, INSTANCE_SYNC_PROPS } from './fields'\nimport { findInstanceAncestor, instanceMainComponent } from './main-component'\nimport {\n clearInstanceOverrides,\n getInstanceOverride,\n hasInstanceOverride as hasNodeInstanceOverride,\n setInstanceOverride\n} from './overrides'\nimport { followComponentLayers } from './swap'\nimport {\n bindingProtection,\n cloneChildrenWithMapping,\n copyProp,\n enclosingInstanceOverrideFields,\n isProtectedSyncField,\n sourceInTargetCoordinates,\n syncBindingFields,\n syncChildren,\n updateSyncedProps\n} from './sync'\n\nexport { canCreateInstance, createsComponentCycle, cycleSafeParent } from './cycles'\nexport { findInstanceAncestor, instanceMainComponent } from './main-component'\nexport {\n INSTANCE_FOLLOWED_FIELDS,\n INSTANCE_OVERRIDE_FIELDS,\n INSTANCE_SYNC_FIELDS,\n INSTANCE_SYNC_PROPS,\n INSTANCE_SYNC_TEXT_PROPS\n} from './fields'\n\nexport function copyInstanceComponentProps(component: SceneNode): Partial<SceneNode> {\n const props: Partial<SceneNode> = {}\n for (const key of INSTANCE_SYNC_PROPS) copyProp(props, component, key)\n return props\n}\n\nexport function createInstance(\n graph: SceneGraph,\n componentId: string,\n parentId: string,\n overrides: Partial<SceneNode> = {}\n): SceneNode | null {\n const component = graph.nodes.get(componentId)\n if (component?.type !== 'COMPONENT' || !canCreateInstance(graph, componentId, parentId))\n return null\n\n const props: Partial<SceneNode> = {\n ...copyInstanceComponentProps(component),\n name: component.name\n }\n\n // The component checked above is the one the instance shows, whatever `overrides` holds.\n const instance = graph.createNode('INSTANCE', parentId, { ...props, ...overrides, componentId })\n\n cloneChildrenWithMapping(graph, component.id, instance.id)\n\n return instance\n}\n\nexport function populateInstanceChildren(\n graph: SceneGraph,\n instanceId: string,\n componentId: string,\n mode: NodeCloneMode = 'deep'\n): void {\n const instance = graph.nodes.get(instanceId)\n const component = graph.nodes.get(componentId)\n if (!instance || !component || instance.type !== 'INSTANCE') return\n cloneChildrenWithMapping(graph, componentId, instanceId, mode)\n}\n\n/**\n * A nested instance's link to the enclosing component's record is its `componentId` when it\n * was populated by cloning. A swap replaces that field, so keep the correspondence as the\n * owner's `sourceComponentId` override and record the swap itself; materialized documents\n * already carry the correspondence and only gain the swap.\n */\nfunction recordNestedSwap(graph: SceneGraph, instance: SceneNode, componentId: string): void {\n const owner = instance.parentId ? findInstanceAncestor(graph, instance.parentId) : undefined\n if (!owner) return\n const existing = getInstanceOverride(\n owner.instanceOverrides,\n owner.id,\n instance.id,\n 'sourceComponentId'\n )\n if (typeof existing !== 'string' && instance.componentId)\n setInstanceOverride(\n owner.instanceOverrides,\n owner.id,\n instance.id,\n 'sourceComponentId',\n instance.componentId\n )\n setInstanceOverride(owner.instanceOverrides, owner.id, instance.id, 'componentId', componentId)\n graph.updateNode(owner.id, { instanceOverrides: owner.instanceOverrides })\n}\n\nexport interface SwapInstanceOptions {\n /**\n * Keep the instance's name even when it is its old component's. A swap otherwise renames such\n * an instance after the new component, as Figma does; a view that only shows another variant,\n * such as preview, keeps names so layer paths stay the same.\n */\n keepName?: boolean\n}\n\nfunction namedAfter(\n graph: SceneGraph,\n instance: SceneNode,\n component: SceneNode\n): SceneNode | undefined {\n const main = instanceMainComponent(graph, instance)\n const set = main?.parentId ? graph.nodes.get(main.parentId) : undefined\n if (set?.type === 'COMPONENT_SET' && component.parentId === set.id) {\n return main\n }\n return instance.componentId ? graph.nodes.get(instance.componentId) : undefined\n}\n\nexport function swapInstanceComponent(\n graph: SceneGraph,\n instanceId: string,\n componentId: string,\n { keepName = false }: SwapInstanceOptions = {}\n): void {\n const instance = graph.nodes.get(instanceId)\n const component = graph.nodes.get(componentId)\n if (!instance || component?.type !== 'COMPONENT' || instance.type !== 'INSTANCE') return\n if (instance.parentId && !canCreateInstance(graph, componentId, instance.parentId)) return\n\n const previousComponent = namedAfter(graph, instance, component)\n recordNestedSwap(graph, instance, componentId)\n const source = sourceInTargetCoordinates(component, instance.componentScale)\n const updates: Partial<SceneNode> = {\n componentId,\n ...ownSyncedProps(graph, instance, source).updates\n }\n\n if (!keepName && (!previousComponent || instance.name === previousComponent.name))\n updates.name = component.name\n\n const slotContent = detachOwnedSlotContent(graph, instance)\n graph.updateNode(instanceId, updates)\n followComponentLayers(graph, instance, component, parkedContent(slotContent))\n restoreOwnedSlotContent(graph, instance, slotContent)\n}\n\nconst NOTHING_PARKED: ReadonlySet<string> = new Set()\n\nfunction parkedContent(parked: ParkedSlotContent): ReadonlySet<string> {\n if (parked.slots.length === 0) {\n return NOTHING_PARKED\n }\n return new Set(parked.slots.flatMap((slot) => slot.content))\n}\n\n/** What an instance takes from its component, and which fields it keeps as its own. */\nfunction ownSyncedProps(\n graph: SceneGraph,\n instance: SceneNode,\n source: SceneNode\n): { updates: Partial<SceneNode>; kept: (field: string) => boolean } {\n const enclosing = enclosingInstanceOverrideFields(graph, instance)\n enclosing.push(new Set(instance.instanceOverrides.self.keys()))\n const protectedField = bindingProtection(enclosing)\n const updates: Partial<SceneNode> = {}\n syncBindingFields(instance, source, updates, protectedField)\n const kept = (key: string) => isProtectedSyncField(instance, key, protectedField)\n for (const key of INSTANCE_SYNC_PROPS) {\n if (key !== 'boundVariables' && !kept(key)) {\n copyProp(updates, source, key)\n }\n }\n return { updates, kept }\n}\n\nconst syncingComponentsByGraph = new WeakMap<SceneGraph, Set<string>>()\n\nexport function syncInstances(graph: SceneGraph, componentId: string): void {\n syncInstancesOf(graph, componentId, getInstances(graph, componentId))\n}\n\n/**\n * Syncs each component's instances, in order, as one edit: the copies they remove stay\n * restorable together for as long as undo keeps that edit.\n */\nexport function syncInstancesTogether(graph: SceneGraph, componentIds: Iterable<string>): void {\n groupCopyRemovals(graph, () => {\n for (const id of componentIds) syncInstances(graph, id)\n })\n}\n\n/** Syncs one instance from its component, as `syncInstances` does for every instance. */\nexport function syncInstance(graph: SceneGraph, instanceId: string): void {\n const instance = graph.nodes.get(instanceId)\n if (instance?.type !== 'INSTANCE' || !instance.componentId) return\n syncInstancesOf(graph, instance.componentId, [instance])\n}\n\nfunction syncInstancesOf(\n graph: SceneGraph,\n componentId: string,\n instances: Iterable<SceneNode>\n): void {\n const component = graph.nodes.get(componentId)\n if (component?.type !== 'COMPONENT') return\n let syncing = syncingComponentsByGraph.get(graph)\n if (!syncing) {\n syncing = new Set()\n syncingComponentsByGraph.set(graph, syncing)\n }\n if (syncing.has(componentId)) return\n syncing.add(componentId)\n try {\n groupCopyRemovals(graph, (removals) => {\n for (const instance of instances) {\n const source = sourceInTargetCoordinates(component, instance.componentScale)\n const { updates, kept } = ownSyncedProps(graph, instance, source)\n // An instance that hugs its layers sizes to what it shows, not to the component.\n keepOwnContentSize(graph, updates, instance, source)\n updateSyncedProps(graph, instance, updates, kept)\n const context = createInstanceSyncContext(instance.id, instance.instanceOverrides)\n syncChildren(graph, component.id, instance.id, instance.instanceOverrides, context)\n removeLeftoverCopies(graph, context, removals)\n }\n })\n } finally {\n syncing.delete(componentId)\n }\n}\n\nexport function detachInstance(graph: SceneGraph, instanceId: string): void {\n const node = graph.nodes.get(instanceId)\n if (node?.type !== 'INSTANCE') return\n if (node.componentId) {\n graph.instanceIndex.get(node.componentId)?.delete(instanceId)\n }\n node.type = 'FRAME'\n node.componentId = null\n clearInstanceOverrides(node.instanceOverrides)\n}\n\nexport function getMainComponent(graph: SceneGraph, instanceId: string): SceneNode | undefined {\n const node = graph.nodes.get(instanceId)\n if (!node?.componentId) return undefined\n return graph.nodes.get(node.componentId)\n}\n\nexport function getInstances(graph: SceneGraph, componentId: string): SceneNode[] {\n const ids = graph.instanceIndex.get(componentId)\n if (!ids) return []\n const instances: SceneNode[] = []\n for (const id of ids) {\n const node = graph.nodes.get(id)\n if (node) instances.push(node)\n }\n return instances\n}\n\n/**\n * True when a node field is protected from instance synchronization.\n */\nexport function hasInstanceOverride(graph: SceneGraph, nodeId: string, field: string): boolean {\n const instance = findInstanceAncestor(graph, nodeId)\n if (!instance) return false\n return hasNodeInstanceOverride(instance.instanceOverrides, instance.id, nodeId, field)\n}\n\nexport function recordInstanceOverride(\n graph: SceneGraph,\n nodeId: string,\n fields: Iterable<string>\n): void {\n const instance = findInstanceAncestor(graph, nodeId)\n if (!instance) return\n\n const relevant = [...fields].filter((field) =>\n (INSTANCE_OVERRIDE_FIELDS as readonly string[]).includes(field)\n )\n\n if (relevant.length === 0) return\n\n graph.willChangeNode(instance)\n for (const field of relevant)\n setInstanceOverride(instance.instanceOverrides, instance.id, nodeId, field)\n graph.updateNode(instance.id, { instanceOverrides: instance.instanceOverrides })\n}\n"],"mappings":";;;;;;;;;;AA0CA,SAAgB,2BAA2B,WAA0C;CACnF,MAAM,QAA4B,CAAC;CACnC,KAAK,MAAM,OAAO,qBAAqB,SAAS,OAAO,WAAW,GAAG;CACrE,OAAO;AACT;AAEA,SAAgB,eACd,OACA,aACA,UACA,YAAgC,CAAC,GACf;CAClB,MAAM,YAAY,MAAM,MAAM,IAAI,WAAW;CAC7C,IAAI,WAAW,SAAS,eAAe,CAAC,kBAAkB,OAAO,aAAa,QAAQ,GACpF,OAAO;CAET,MAAM,QAA4B;EAChC,GAAG,2BAA2B,SAAS;EACvC,MAAM,UAAU;CAClB;CAGA,MAAM,WAAW,MAAM,WAAW,YAAY,UAAU;EAAE,GAAG;EAAO,GAAG;EAAW;CAAY,CAA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{ overriddenAssignmentId, reapplyComponentPropertyAssignments } from "../components/properties.js";
|
|
4
|
+
import { omit } from "es-toolkit/object";
|
|
5
|
+
import { isEqual } from "es-toolkit/predicate";
|
|
6
|
+
//#region src/instances/kept-overrides.ts
|
|
7
|
+
/** Override fields that link a copy to its layer, which a new copy gets from its own layer. */
|
|
8
|
+
const LINK_FIELDS = /* @__PURE__ */ new Set(["sourceComponentId", "componentId"]);
|
|
9
|
+
/**
|
|
10
|
+
* Overrides on layers a switch removed, by switched instance and then by the layer's path from
|
|
11
|
+
* it: each step's name and its order among the same-named layers beside it, the way a switch
|
|
12
|
+
* pairs layers. Figma brings them back when the instance switches to a variant that draws the
|
|
13
|
+
* layer again, though its saved records keep none of them while the layer is gone (desktop
|
|
14
|
+
* 126), so they live with the open document only.
|
|
15
|
+
*/
|
|
16
|
+
const keptByGraph = /* @__PURE__ */ new WeakMap();
|
|
17
|
+
function keptFor(scope, create) {
|
|
18
|
+
let byInstance = keptByGraph.get(scope.graph);
|
|
19
|
+
if (!byInstance && create) {
|
|
20
|
+
byInstance = /* @__PURE__ */ new Map();
|
|
21
|
+
keptByGraph.set(scope.graph, byInstance);
|
|
22
|
+
}
|
|
23
|
+
let byPath = byInstance?.get(scope.instance.id);
|
|
24
|
+
if (!byPath && byInstance && create) {
|
|
25
|
+
byPath = /* @__PURE__ */ new Map();
|
|
26
|
+
byInstance.set(scope.instance.id, byPath);
|
|
27
|
+
}
|
|
28
|
+
return byPath;
|
|
29
|
+
}
|
|
30
|
+
/** A layer's path from the switched instance, by names and their order among namesakes. */
|
|
31
|
+
function layerPath(scope, node) {
|
|
32
|
+
const steps = [];
|
|
33
|
+
let current = node;
|
|
34
|
+
while (current.id !== scope.instance.id) {
|
|
35
|
+
const parent = current.parentId ? scope.graph.getNode(current.parentId) : void 0;
|
|
36
|
+
if (!parent) return void 0;
|
|
37
|
+
let ordinal = 0;
|
|
38
|
+
for (const id of parent.childIds) {
|
|
39
|
+
if (id === current.id) break;
|
|
40
|
+
if (scope.graph.getNode(id)?.name === current.name) ordinal++;
|
|
41
|
+
}
|
|
42
|
+
steps.push(`${current.name}\u0000${ordinal}`);
|
|
43
|
+
current = parent;
|
|
44
|
+
}
|
|
45
|
+
return steps.reverse().join("");
|
|
46
|
+
}
|
|
47
|
+
function snapshot(graph, node, owner, self, fields) {
|
|
48
|
+
const kept = new Map([...fields].filter(([field]) => !LINK_FIELDS.has(field)));
|
|
49
|
+
const swappedTo = fields.has("componentId") && node.type === "INSTANCE" ? instanceMainComponent(graph, node)?.id : void 0;
|
|
50
|
+
if (kept.size === 0 && !swappedTo) return void 0;
|
|
51
|
+
const values = {};
|
|
52
|
+
const bindings = {};
|
|
53
|
+
const assignments = {};
|
|
54
|
+
for (const field of kept.keys()) {
|
|
55
|
+
const binding = overriddenBindingField(field);
|
|
56
|
+
const assigned = overriddenAssignmentId(field);
|
|
57
|
+
if (assigned !== void 0) {
|
|
58
|
+
if (Object.hasOwn(node.componentPropertyAssignments, assigned)) assignments[assigned] = node.componentPropertyAssignments[assigned];
|
|
59
|
+
} else if (binding !== null) bindings[binding] = {
|
|
60
|
+
variableId: node.boundVariables[binding],
|
|
61
|
+
scale: node.variableBindingScales[binding]
|
|
62
|
+
};
|
|
63
|
+
else if (field in node && field !== "boundVariables") Object.assign(values, { [field]: structuredClone(node[field]) });
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
owner,
|
|
67
|
+
self,
|
|
68
|
+
fields: kept,
|
|
69
|
+
values,
|
|
70
|
+
bindings,
|
|
71
|
+
allBindings: kept.has("boundVariables") ? {
|
|
72
|
+
boundVariables: { ...node.boundVariables },
|
|
73
|
+
scales: { ...node.variableBindingScales }
|
|
74
|
+
} : void 0,
|
|
75
|
+
swappedTo,
|
|
76
|
+
assignments
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/** Takes the overrides on a layer off its owners, outer ones and those inside the removed copy. */
|
|
80
|
+
function takeOverrides(scope, node, inner) {
|
|
81
|
+
const taken = [];
|
|
82
|
+
for (const owner of scope.owners) {
|
|
83
|
+
const fields = owner.instanceOverrides.descendants.get(node.id);
|
|
84
|
+
if (!fields) continue;
|
|
85
|
+
scope.willChange(owner);
|
|
86
|
+
owner.instanceOverrides.descendants.delete(node.id);
|
|
87
|
+
const kept = snapshot(scope.graph, node, { id: owner.id }, false, fields);
|
|
88
|
+
if (kept) taken.push(kept);
|
|
89
|
+
}
|
|
90
|
+
for (const owner of inner) {
|
|
91
|
+
const fields = owner.instanceOverrides.descendants.get(node.id);
|
|
92
|
+
const path = layerPath(scope, owner);
|
|
93
|
+
const kept = fields && path ? snapshot(scope.graph, node, { path }, false, fields) : void 0;
|
|
94
|
+
if (kept) taken.push(kept);
|
|
95
|
+
}
|
|
96
|
+
const ownPath = node.type === "INSTANCE" ? layerPath(scope, node) : void 0;
|
|
97
|
+
const own = ownPath ? snapshot(scope.graph, node, { path: ownPath }, true, node.instanceOverrides.self) : void 0;
|
|
98
|
+
if (own) taken.push(own);
|
|
99
|
+
return taken;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Keeps what is overridden on a copy and its layers before a switch removes it. A layer
|
|
103
|
+
* removed with nothing overridden clears what an earlier removal kept for its path, so a value
|
|
104
|
+
* the user reset does not come back.
|
|
105
|
+
*/
|
|
106
|
+
function keepOverrides(scope, copy, inner = []) {
|
|
107
|
+
const path = layerPath(scope, copy);
|
|
108
|
+
const taken = takeOverrides(scope, copy, inner);
|
|
109
|
+
if (path && taken.length > 0) keptFor(scope, true).set(path, taken);
|
|
110
|
+
else if (path) keptFor(scope, false)?.delete(path);
|
|
111
|
+
const owners = copy.type === "INSTANCE" ? [...inner, copy] : inner;
|
|
112
|
+
for (const child of scope.graph.getChildren(copy.id)) keepOverrides(scope, child, owners);
|
|
113
|
+
}
|
|
114
|
+
function ownerOf(scope, ref) {
|
|
115
|
+
if ("id" in ref) return scope.owners.find((owner) => owner.id === ref.id);
|
|
116
|
+
let current = scope.instance;
|
|
117
|
+
for (const step of ref.path.split("")) {
|
|
118
|
+
const [name, ordinal] = step.split("\0");
|
|
119
|
+
current = current ? scope.graph.getChildren(current.id).filter((child) => child.name === name)[Number(ordinal)] : void 0;
|
|
120
|
+
}
|
|
121
|
+
return current?.type === "INSTANCE" ? current : void 0;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Switches a nested instance switched to another variant again first, so what is kept for it
|
|
125
|
+
* and its layers lands on what it then shows. Returns the node as it then stands.
|
|
126
|
+
*/
|
|
127
|
+
function switchAsKept(scope, copy, kept) {
|
|
128
|
+
const swappedTo = kept.find((entry) => entry.swappedTo)?.swappedTo;
|
|
129
|
+
if (swappedTo && copy.type === "INSTANCE" && scope.graph.getNode(swappedTo) && instanceMainComponent(scope.graph, copy)?.id !== swappedTo) scope.graph.swapInstanceComponent(copy.id, swappedTo);
|
|
130
|
+
return scope.graph.getNode(copy.id) ?? copy;
|
|
131
|
+
}
|
|
132
|
+
/** The bindings and unit scales a layer shows with the kept bindings applied over its own. */
|
|
133
|
+
function keptBindings(node, kept) {
|
|
134
|
+
let boundVariables = { ...node.boundVariables };
|
|
135
|
+
let scales = { ...node.variableBindingScales };
|
|
136
|
+
for (const { bindings, allBindings } of kept) {
|
|
137
|
+
if (allBindings) {
|
|
138
|
+
boundVariables = { ...allBindings.boundVariables };
|
|
139
|
+
scales = { ...allBindings.scales };
|
|
140
|
+
}
|
|
141
|
+
for (const [field, { variableId, scale }] of Object.entries(bindings)) {
|
|
142
|
+
boundVariables = variableId ? {
|
|
143
|
+
...boundVariables,
|
|
144
|
+
[field]: variableId
|
|
145
|
+
} : omit(boundVariables, [field]);
|
|
146
|
+
scales = scale !== void 0 ? {
|
|
147
|
+
...scales,
|
|
148
|
+
[field]: scale
|
|
149
|
+
} : omit(scales, [field]);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
boundVariables,
|
|
154
|
+
scales
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function giveOverrides(scope, copy, saved) {
|
|
158
|
+
const resolved = saved.flatMap((entry) => {
|
|
159
|
+
const owner = ownerOf(scope, entry.owner);
|
|
160
|
+
return owner ? [{
|
|
161
|
+
entry,
|
|
162
|
+
owner
|
|
163
|
+
}] : [];
|
|
164
|
+
});
|
|
165
|
+
const kept = resolved.map(({ entry }) => entry);
|
|
166
|
+
const node = switchAsKept(scope, copy, kept);
|
|
167
|
+
const values = {};
|
|
168
|
+
for (const { entry, owner } of resolved) {
|
|
169
|
+
const { self, fields, values: shown } = entry;
|
|
170
|
+
scope.willChange(owner);
|
|
171
|
+
const own = self ? owner.instanceOverrides.self : owner.instanceOverrides.descendants.get(node.id);
|
|
172
|
+
const merged = new Map([...own ?? [], ...fields]);
|
|
173
|
+
if (self) owner.instanceOverrides.self = merged;
|
|
174
|
+
else owner.instanceOverrides.descendants.set(node.id, merged);
|
|
175
|
+
Object.assign(values, structuredClone(shown));
|
|
176
|
+
}
|
|
177
|
+
const { boundVariables, scales } = keptBindings(node, kept);
|
|
178
|
+
if (!isEqual(boundVariables, node.boundVariables)) values.boundVariables = boundVariables;
|
|
179
|
+
if (!isEqual(scales, node.variableBindingScales)) values.variableBindingScales = scales;
|
|
180
|
+
const assignments = Object.assign({}, ...kept.map((entry) => entry.assignments));
|
|
181
|
+
const assigns = Object.keys(assignments).length > 0;
|
|
182
|
+
if (assigns) values.componentPropertyAssignments = {
|
|
183
|
+
...node.componentPropertyAssignments,
|
|
184
|
+
...assignments
|
|
185
|
+
};
|
|
186
|
+
if (Object.keys(values).length > 0) scope.graph.updateNode(node.id, values);
|
|
187
|
+
if (assigns) reapplyComponentPropertyAssignments(scope.graph, node.id);
|
|
188
|
+
}
|
|
189
|
+
/** Gives a copy just placed, and its layers, what an earlier switch kept for their paths. */
|
|
190
|
+
function restoreOverrides(scope, copy) {
|
|
191
|
+
const kept = keptFor(scope, false);
|
|
192
|
+
if (!kept?.size) return;
|
|
193
|
+
const restore = (node) => {
|
|
194
|
+
const path = layerPath(scope, node);
|
|
195
|
+
const found = path ? kept.get(path) : void 0;
|
|
196
|
+
if (found) giveOverrides(scope, node, found);
|
|
197
|
+
for (const child of scope.graph.getChildren(node.id)) restore(child);
|
|
198
|
+
};
|
|
199
|
+
restore(copy);
|
|
200
|
+
}
|
|
201
|
+
//#endregion
|
|
202
|
+
export { keepOverrides, restoreOverrides };
|
|
203
|
+
|
|
204
|
+
//# sourceMappingURL=kept-overrides.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kept-overrides.js","names":[],"sources":["../../src/instances/kept-overrides.ts"],"sourcesContent":["// Overrides a variant switch keeps for the layers it removes, to give back to the next copies.\nimport { omit } from 'es-toolkit/object'\nimport { isEqual } from 'es-toolkit/predicate'\n\nimport {\n overriddenAssignmentId,\n reapplyComponentPropertyAssignments\n} from '../components/properties'\nimport type { SceneGraph } from '../graph'\nimport type { SceneNode } from '../types'\nimport { instanceMainComponent } from './main-component'\nimport { overriddenBindingField } from './overrides'\n\n/** Where a switch runs: the switched instance and the instances enclosing the layers it changes. */\nexport interface OverrideScope {\n graph: SceneGraph\n instance: SceneNode\n owners: readonly SceneNode[]\n /** Reports an owner before its override maps are written into. */\n willChange(owner: SceneNode): void\n}\n\n/**\n * An instance that holds overrides on a layer: one enclosing the switch, by ID, or one inside\n * the removed copy, by its path, since a new copy of it gets a new ID.\n */\ntype OwnerRef = { id: string } | { path: string }\n\n/** What one owner overrides on a layer, with the values the layer showed. */\ninterface KeptOverrides {\n owner: OwnerRef\n /** The owner's own fields, for a nested instance's overrides on itself. */\n self: boolean\n fields: Map<string, unknown>\n values: Partial<SceneNode>\n /** Bindings the overrides claim, with their unit scales; an absent variable claims none. */\n bindings: Record<string, { variableId: string | undefined; scale: number | undefined }>\n /** Set when the overrides claim every binding, as removing a variable records. */\n allBindings?: { boundVariables: SceneNode['boundVariables']; scales: Scales }\n /** The component a nested instance was switched to, which its new copy is switched to again. */\n swappedTo?: string\n /** Property values the overrides claim on a nested instance. */\n assignments: Record<string, string>\n}\n\ntype Scales = SceneNode['variableBindingScales']\n\n/** Override fields that link a copy to its layer, which a new copy gets from its own layer. */\nconst LINK_FIELDS: ReadonlySet<string> = new Set(['sourceComponentId', 'componentId'])\n\n/**\n * Overrides on layers a switch removed, by switched instance and then by the layer's path from\n * it: each step's name and its order among the same-named layers beside it, the way a switch\n * pairs layers. Figma brings them back when the instance switches to a variant that draws the\n * layer again, though its saved records keep none of them while the layer is gone (desktop\n * 126), so they live with the open document only.\n */\nconst keptByGraph = new WeakMap<SceneGraph, Map<string, Map<string, KeptOverrides[]>>>()\n\nfunction keptFor(scope: OverrideScope, create: true): Map<string, KeptOverrides[]>\nfunction keptFor(scope: OverrideScope, create: false): Map<string, KeptOverrides[]> | undefined\nfunction keptFor(scope: OverrideScope, create: boolean) {\n let byInstance = keptByGraph.get(scope.graph)\n if (!byInstance && create) {\n byInstance = new Map()\n keptByGraph.set(scope.graph, byInstance)\n }\n let byPath = byInstance?.get(scope.instance.id)\n if (!byPath && byInstance && create) {\n byPath = new Map()\n byInstance.set(scope.instance.id, byPath)\n }\n return byPath\n}\n\n/** A layer's path from the switched instance, by names and their order among namesakes. */\nfunction layerPath(scope: OverrideScope, node: SceneNode): string | undefined {\n const steps: string[] = []\n let current = node\n while (current.id !== scope.instance.id) {\n const parent: SceneNode | undefined = current.parentId\n ? scope.graph.getNode(current.parentId)\n : undefined\n if (!parent) return undefined\n let ordinal = 0\n for (const id of parent.childIds) {\n if (id === current.id) break\n if (scope.graph.getNode(id)?.name === current.name) ordinal++\n }\n steps.push(`${current.name}\\u0000${ordinal}`)\n current = parent\n }\n return steps.reverse().join('\\u0001')\n}\n\nfunction snapshot(\n graph: SceneGraph,\n node: SceneNode,\n owner: OwnerRef,\n self: boolean,\n fields: ReadonlyMap<string, unknown>\n): KeptOverrides | undefined {\n const kept = new Map([...fields].filter(([field]) => !LINK_FIELDS.has(field)))\n // A swap is kept as the component it shows; the new copy records it again when switched.\n const swappedTo =\n fields.has('componentId') && node.type === 'INSTANCE'\n ? instanceMainComponent(graph, node)?.id\n : undefined\n if (kept.size === 0 && !swappedTo) return undefined\n const values: Partial<SceneNode> = {}\n const bindings: KeptOverrides['bindings'] = {}\n const assignments: Record<string, string> = {}\n for (const field of kept.keys()) {\n const binding = overriddenBindingField(field)\n const assigned = overriddenAssignmentId(field)\n if (assigned !== undefined) {\n if (Object.hasOwn(node.componentPropertyAssignments, assigned))\n assignments[assigned] = node.componentPropertyAssignments[assigned]\n } else if (binding !== null) {\n bindings[binding] = {\n variableId: node.boundVariables[binding],\n scale: node.variableBindingScales[binding]\n }\n } else if (field in node && field !== 'boundVariables') {\n Object.assign(values, { [field]: structuredClone(node[field as keyof SceneNode]) })\n }\n }\n const allBindings = kept.has('boundVariables')\n ? {\n boundVariables: { ...node.boundVariables },\n scales: { ...node.variableBindingScales }\n }\n : undefined\n return { owner, self, fields: kept, values, bindings, allBindings, swappedTo, assignments }\n}\n\n/** Takes the overrides on a layer off its owners, outer ones and those inside the removed copy. */\nfunction takeOverrides(\n scope: OverrideScope,\n node: SceneNode,\n inner: readonly SceneNode[]\n): KeptOverrides[] {\n const taken: KeptOverrides[] = []\n for (const owner of scope.owners) {\n const fields = owner.instanceOverrides.descendants.get(node.id)\n if (!fields) continue\n scope.willChange(owner)\n owner.instanceOverrides.descendants.delete(node.id)\n const kept = snapshot(scope.graph, node, { id: owner.id }, false, fields)\n if (kept) taken.push(kept)\n }\n for (const owner of inner) {\n const fields = owner.instanceOverrides.descendants.get(node.id)\n const path = layerPath(scope, owner)\n const kept = fields && path ? snapshot(scope.graph, node, { path }, false, fields) : undefined\n if (kept) taken.push(kept)\n }\n const ownPath = node.type === 'INSTANCE' ? layerPath(scope, node) : undefined\n const own = ownPath\n ? snapshot(scope.graph, node, { path: ownPath }, true, node.instanceOverrides.self)\n : undefined\n if (own) taken.push(own)\n return taken\n}\n\n/**\n * Keeps what is overridden on a copy and its layers before a switch removes it. A layer\n * removed with nothing overridden clears what an earlier removal kept for its path, so a value\n * the user reset does not come back.\n */\nexport function keepOverrides(\n scope: OverrideScope,\n copy: SceneNode,\n inner: readonly SceneNode[] = []\n): void {\n const path = layerPath(scope, copy)\n const taken = takeOverrides(scope, copy, inner)\n if (path && taken.length > 0) keptFor(scope, true).set(path, taken)\n else if (path) keptFor(scope, false)?.delete(path)\n const owners = copy.type === 'INSTANCE' ? [...inner, copy] : inner\n for (const child of scope.graph.getChildren(copy.id)) keepOverrides(scope, child, owners)\n}\n\nfunction ownerOf(scope: OverrideScope, ref: OwnerRef): SceneNode | undefined {\n if ('id' in ref) return scope.owners.find((owner) => owner.id === ref.id)\n let current: SceneNode | undefined = scope.instance\n for (const step of ref.path.split('\\u0001')) {\n const [name, ordinal] = step.split('\\u0000')\n current = current\n ? scope.graph.getChildren(current.id).filter((child) => child.name === name)[Number(ordinal)]\n : undefined\n }\n return current?.type === 'INSTANCE' ? current : undefined\n}\n\n/**\n * Switches a nested instance switched to another variant again first, so what is kept for it\n * and its layers lands on what it then shows. Returns the node as it then stands.\n */\nfunction switchAsKept(scope: OverrideScope, copy: SceneNode, kept: readonly KeptOverrides[]) {\n const swappedTo = kept.find((entry) => entry.swappedTo)?.swappedTo\n if (\n swappedTo &&\n copy.type === 'INSTANCE' &&\n scope.graph.getNode(swappedTo) &&\n instanceMainComponent(scope.graph, copy)?.id !== swappedTo\n ) {\n scope.graph.swapInstanceComponent(copy.id, swappedTo)\n }\n return scope.graph.getNode(copy.id) ?? copy\n}\n\n/** The bindings and unit scales a layer shows with the kept bindings applied over its own. */\nfunction keptBindings(node: SceneNode, kept: readonly KeptOverrides[]) {\n let boundVariables: Record<string, string> = { ...node.boundVariables }\n let scales: Scales = { ...node.variableBindingScales }\n for (const { bindings, allBindings } of kept) {\n if (allBindings) {\n boundVariables = { ...allBindings.boundVariables }\n scales = { ...allBindings.scales }\n }\n for (const [field, { variableId, scale }] of Object.entries(bindings)) {\n boundVariables = variableId\n ? { ...boundVariables, [field]: variableId }\n : omit(boundVariables, [field])\n scales = scale !== undefined ? { ...scales, [field]: scale } : omit(scales, [field])\n }\n }\n return { boundVariables, scales }\n}\n\nfunction giveOverrides(scope: OverrideScope, copy: SceneNode, saved: readonly KeptOverrides[]) {\n // What an owner no longer standing around the copy overrode is not given back at all.\n const resolved = saved.flatMap((entry) => {\n const owner = ownerOf(scope, entry.owner)\n return owner ? [{ entry, owner }] : []\n })\n const kept = resolved.map(({ entry }) => entry)\n const node = switchAsKept(scope, copy, kept)\n const values: Partial<SceneNode> = {}\n for (const { entry, owner } of resolved) {\n const { self, fields, values: shown } = entry\n scope.willChange(owner)\n const own = self\n ? owner.instanceOverrides.self\n : owner.instanceOverrides.descendants.get(node.id)\n const merged = new Map([...(own ?? []), ...fields])\n if (self) owner.instanceOverrides.self = merged\n else owner.instanceOverrides.descendants.set(node.id, merged)\n Object.assign(values, structuredClone(shown))\n }\n const { boundVariables, scales } = keptBindings(node, kept)\n if (!isEqual(boundVariables, node.boundVariables)) values.boundVariables = boundVariables\n if (!isEqual(scales, node.variableBindingScales)) values.variableBindingScales = scales\n const assignments: Record<string, string> = Object.assign(\n {},\n ...kept.map((entry) => entry.assignments)\n )\n const assigns = Object.keys(assignments).length > 0\n if (assigns) {\n values.componentPropertyAssignments = { ...node.componentPropertyAssignments, ...assignments }\n }\n if (Object.keys(values).length > 0) scope.graph.updateNode(node.id, values)\n // The values a nested instance is given show in its layers, as a matched copy's do.\n if (assigns) reapplyComponentPropertyAssignments(scope.graph, node.id)\n}\n\n/** Gives a copy just placed, and its layers, what an earlier switch kept for their paths. */\nexport function restoreOverrides(scope: OverrideScope, copy: SceneNode): void {\n const kept = keptFor(scope, false)\n if (!kept?.size) return\n const restore = (node: SceneNode) => {\n const path = layerPath(scope, node)\n const found = path ? kept.get(path) : undefined\n // Kept, not taken: undoing this switch and switching again gives them back once more.\n if (found) giveOverrides(scope, node, found)\n for (const child of scope.graph.getChildren(node.id)) restore(child)\n }\n restore(copy)\n}\n"],"mappings":";;;;;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,KAAK;EAC3C,KAAK,MAAM,SAAS,MAAM,MAAM,YAAY,KAAK,EAAE,GAAG,QAAQ,KAAK;CACrE;CACA,QAAQ,IAAI;AACd"}
|
|
@@ -6,6 +6,8 @@ import { SceneNode } from "../types.js";
|
|
|
6
6
|
* instance it was cloned from, so follow those links to the component.
|
|
7
7
|
*/
|
|
8
8
|
declare function instanceMainComponent(graph: SceneGraph, instance: SceneNode): SceneNode | undefined;
|
|
9
|
+
/** Nearest INSTANCE at or above `nodeId` — self, parent, grandparent, etc. */
|
|
10
|
+
declare function findInstanceAncestor(graph: SceneGraph, nodeId: string): SceneNode | undefined;
|
|
9
11
|
//#endregion
|
|
10
|
-
export { instanceMainComponent };
|
|
12
|
+
export { findInstanceAncestor, instanceMainComponent };
|
|
11
13
|
//# sourceMappingURL=main-component.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main-component.d.ts","names":[],"sources":["../../src/instances/main-component.ts"],"mappings":";;;;;;;iBASgB,sBACd,OAAO,YACP,UAAU,YACT"}
|
|
1
|
+
{"version":3,"file":"main-component.d.ts","names":[],"sources":["../../src/instances/main-component.ts"],"mappings":";;;;;;;iBASgB,sBACd,OAAO,YACP,UAAU,YACT;;iBAmDa,qBAAqB,OAAO,YAAY,iBAAiB"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { overriddenBindingField } from "./overrides.js";
|
|
2
1
|
import { paintBindingTarget } from "../variables/fields.js";
|
|
2
|
+
import { overriddenBindingField } from "./overrides.js";
|
|
3
3
|
//#region src/instances/main-component.ts
|
|
4
4
|
/**
|
|
5
5
|
* The main component an instance shows. An instance nested in another instance points at the
|
|
@@ -36,7 +36,15 @@ function sourceWithEffectiveFill(graph, source, target) {
|
|
|
36
36
|
variableBindingScales: fillBindings(source.variableBindingScales, effective.variableBindingScales)
|
|
37
37
|
};
|
|
38
38
|
}
|
|
39
|
+
/** Nearest INSTANCE at or above `nodeId` — self, parent, grandparent, etc. */
|
|
40
|
+
function findInstanceAncestor(graph, nodeId) {
|
|
41
|
+
let current = graph.nodes.get(nodeId);
|
|
42
|
+
while (current) {
|
|
43
|
+
if (current.type === "INSTANCE") return current;
|
|
44
|
+
current = current.parentId ? graph.nodes.get(current.parentId) : void 0;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
39
47
|
//#endregion
|
|
40
|
-
export { instanceMainComponent, ownsSelfFillField, sourceWithEffectiveFill };
|
|
48
|
+
export { findInstanceAncestor, instanceMainComponent, ownsSelfFillField, sourceWithEffectiveFill };
|
|
41
49
|
|
|
42
50
|
//# sourceMappingURL=main-component.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main-component.js","names":[],"sources":["../../src/instances/main-component.ts"],"sourcesContent":["import type { SceneGraph } from '../graph'\nimport type { SceneNode } from '../types'\nimport { paintBindingTarget } from '../variables/fields'\nimport { overriddenBindingField } from './overrides'\n\n/**\n * The main component an instance shows. An instance nested in another instance points at the\n * instance it was cloned from, so follow those links to the component.\n */\nexport function instanceMainComponent(\n graph: SceneGraph,\n instance: SceneNode\n): SceneNode | undefined {\n const seen = new Set<string>()\n let current = instance.componentId ? graph.getNode(instance.componentId) : undefined\n while (current?.type === 'INSTANCE' && current.componentId && !seen.has(current.id)) {\n seen.add(current.id)\n current = graph.getNode(current.componentId)\n }\n return current?.type === 'COMPONENT' ? current : undefined\n}\n\n/** Root paint claims also protect a nested copy when its enclosing component syncs it. */\nexport function ownsSelfFillField(target: SceneNode, field: string): boolean {\n const own = target.instanceOverrides.self\n if (field === 'fills' || field === 'fillStyleId') return own.has(field) || own.has('fills')\n // A paint the layer set itself owns its bindings too, bound or not.\n const binding = overriddenBindingField(field)\n return (\n binding !== null &&\n paintBindingTarget(binding)?.list === 'fills' &&\n (own.has(field) || own.has('boundVariables') || own.has('fills'))\n )\n}\n\n/** A swapped nested root takes its fill from what it shows, not its template's old component. */\nexport function sourceWithEffectiveFill(\n graph: SceneGraph,\n source: SceneNode,\n target: SceneNode\n): SceneNode {\n if (source.type !== 'INSTANCE' || target.type !== 'INSTANCE') return source\n const original = instanceMainComponent(graph, source)\n const effective = instanceMainComponent(graph, target)\n if (!original || !effective || original.id === effective.id) return source\n const fillBindings = <T>(from: Record<string, T>, to: Record<string, T>): Record<string, T> =>\n Object.fromEntries([\n ...Object.entries(from).filter(([key]) => !key.startsWith('fills/')),\n ...Object.entries(to).filter(([key]) => key.startsWith('fills/'))\n ])\n return {\n ...source,\n fills: effective.fills,\n fillStyleId: effective.fillStyleId,\n boundVariables: fillBindings(source.boundVariables, effective.boundVariables),\n variableBindingScales: fillBindings(\n source.variableBindingScales,\n effective.variableBindingScales\n )\n }\n}\n"],"mappings":";;;;;;;AASA,SAAgB,sBACd,OACA,UACuB;CACvB,MAAM,uBAAO,IAAI,IAAY;CAC7B,IAAI,UAAU,SAAS,cAAc,MAAM,QAAQ,SAAS,WAAW,IAAI,KAAA;CAC3E,OAAO,SAAS,SAAS,cAAc,QAAQ,eAAe,CAAC,KAAK,IAAI,QAAQ,EAAE,GAAG;EACnF,KAAK,IAAI,QAAQ,EAAE;EACnB,UAAU,MAAM,QAAQ,QAAQ,WAAW;CAC7C;CACA,OAAO,SAAS,SAAS,cAAc,UAAU,KAAA;AACnD;;AAGA,SAAgB,kBAAkB,QAAmB,OAAwB;CAC3E,MAAM,MAAM,OAAO,kBAAkB;CACrC,IAAI,UAAU,WAAW,UAAU,eAAe,OAAO,IAAI,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO;CAE1F,MAAM,UAAU,uBAAuB,KAAK;CAC5C,OACE,YAAY,QACZ,mBAAmB,OAAO,CAAC,EAAE,SAAS,YACrC,IAAI,IAAI,KAAK,KAAK,IAAI,IAAI,gBAAgB,KAAK,IAAI,IAAI,OAAO;AAEnE;;AAGA,SAAgB,wBACd,OACA,QACA,QACW;CACX,IAAI,OAAO,SAAS,cAAc,OAAO,SAAS,YAAY,OAAO;CACrE,MAAM,WAAW,sBAAsB,OAAO,MAAM;CACpD,MAAM,YAAY,sBAAsB,OAAO,MAAM;CACrD,IAAI,CAAC,YAAY,CAAC,aAAa,SAAS,OAAO,UAAU,IAAI,OAAO;CACpE,MAAM,gBAAmB,MAAyB,OAChD,OAAO,YAAY,CACjB,GAAG,OAAO,QAAQ,IAAI,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,WAAW,QAAQ,CAAC,GACnE,GAAG,OAAO,QAAQ,EAAE,CAAC,CAAC,QAAQ,CAAC,SAAS,IAAI,WAAW,QAAQ,CAAC,CAClE,CAAC;CACH,OAAO;EACL,GAAG;EACH,OAAO,UAAU;EACjB,aAAa,UAAU;EACvB,gBAAgB,aAAa,OAAO,gBAAgB,UAAU,cAAc;EAC5E,uBAAuB,aACrB,OAAO,uBACP,UAAU,qBACZ;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"main-component.js","names":[],"sources":["../../src/instances/main-component.ts"],"sourcesContent":["import type { SceneGraph } from '../graph'\nimport type { SceneNode } from '../types'\nimport { paintBindingTarget } from '../variables/fields'\nimport { overriddenBindingField } from './overrides'\n\n/**\n * The main component an instance shows. An instance nested in another instance points at the\n * instance it was cloned from, so follow those links to the component.\n */\nexport function instanceMainComponent(\n graph: SceneGraph,\n instance: SceneNode\n): SceneNode | undefined {\n const seen = new Set<string>()\n let current = instance.componentId ? graph.getNode(instance.componentId) : undefined\n while (current?.type === 'INSTANCE' && current.componentId && !seen.has(current.id)) {\n seen.add(current.id)\n current = graph.getNode(current.componentId)\n }\n return current?.type === 'COMPONENT' ? current : undefined\n}\n\n/** Root paint claims also protect a nested copy when its enclosing component syncs it. */\nexport function ownsSelfFillField(target: SceneNode, field: string): boolean {\n const own = target.instanceOverrides.self\n if (field === 'fills' || field === 'fillStyleId') return own.has(field) || own.has('fills')\n // A paint the layer set itself owns its bindings too, bound or not.\n const binding = overriddenBindingField(field)\n return (\n binding !== null &&\n paintBindingTarget(binding)?.list === 'fills' &&\n (own.has(field) || own.has('boundVariables') || own.has('fills'))\n )\n}\n\n/** A swapped nested root takes its fill from what it shows, not its template's old component. */\nexport function sourceWithEffectiveFill(\n graph: SceneGraph,\n source: SceneNode,\n target: SceneNode\n): SceneNode {\n if (source.type !== 'INSTANCE' || target.type !== 'INSTANCE') return source\n const original = instanceMainComponent(graph, source)\n const effective = instanceMainComponent(graph, target)\n if (!original || !effective || original.id === effective.id) return source\n const fillBindings = <T>(from: Record<string, T>, to: Record<string, T>): Record<string, T> =>\n Object.fromEntries([\n ...Object.entries(from).filter(([key]) => !key.startsWith('fills/')),\n ...Object.entries(to).filter(([key]) => key.startsWith('fills/'))\n ])\n return {\n ...source,\n fills: effective.fills,\n fillStyleId: effective.fillStyleId,\n boundVariables: fillBindings(source.boundVariables, effective.boundVariables),\n variableBindingScales: fillBindings(\n source.variableBindingScales,\n effective.variableBindingScales\n )\n }\n}\n\n/** Nearest INSTANCE at or above `nodeId` — self, parent, grandparent, etc. */\nexport function findInstanceAncestor(graph: SceneGraph, nodeId: string): SceneNode | undefined {\n let current = graph.nodes.get(nodeId)\n while (current) {\n if (current.type === 'INSTANCE') {\n return current\n }\n current = current.parentId ? graph.nodes.get(current.parentId) : undefined\n }\n return undefined\n}\n"],"mappings":";;;;;;;AASA,SAAgB,sBACd,OACA,UACuB;CACvB,MAAM,uBAAO,IAAI,IAAY;CAC7B,IAAI,UAAU,SAAS,cAAc,MAAM,QAAQ,SAAS,WAAW,IAAI,KAAA;CAC3E,OAAO,SAAS,SAAS,cAAc,QAAQ,eAAe,CAAC,KAAK,IAAI,QAAQ,EAAE,GAAG;EACnF,KAAK,IAAI,QAAQ,EAAE;EACnB,UAAU,MAAM,QAAQ,QAAQ,WAAW;CAC7C;CACA,OAAO,SAAS,SAAS,cAAc,UAAU,KAAA;AACnD;;AAGA,SAAgB,kBAAkB,QAAmB,OAAwB;CAC3E,MAAM,MAAM,OAAO,kBAAkB;CACrC,IAAI,UAAU,WAAW,UAAU,eAAe,OAAO,IAAI,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO;CAE1F,MAAM,UAAU,uBAAuB,KAAK;CAC5C,OACE,YAAY,QACZ,mBAAmB,OAAO,CAAC,EAAE,SAAS,YACrC,IAAI,IAAI,KAAK,KAAK,IAAI,IAAI,gBAAgB,KAAK,IAAI,IAAI,OAAO;AAEnE;;AAGA,SAAgB,wBACd,OACA,QACA,QACW;CACX,IAAI,OAAO,SAAS,cAAc,OAAO,SAAS,YAAY,OAAO;CACrE,MAAM,WAAW,sBAAsB,OAAO,MAAM;CACpD,MAAM,YAAY,sBAAsB,OAAO,MAAM;CACrD,IAAI,CAAC,YAAY,CAAC,aAAa,SAAS,OAAO,UAAU,IAAI,OAAO;CACpE,MAAM,gBAAmB,MAAyB,OAChD,OAAO,YAAY,CACjB,GAAG,OAAO,QAAQ,IAAI,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,WAAW,QAAQ,CAAC,GACnE,GAAG,OAAO,QAAQ,EAAE,CAAC,CAAC,QAAQ,CAAC,SAAS,IAAI,WAAW,QAAQ,CAAC,CAClE,CAAC;CACH,OAAO;EACL,GAAG;EACH,OAAO,UAAU;EACjB,aAAa,UAAU;EACvB,gBAAgB,aAAa,OAAO,gBAAgB,UAAU,cAAc;EAC5E,uBAAuB,aACrB,OAAO,uBACP,UAAU,qBACZ;CACF;AACF;;AAGA,SAAgB,qBAAqB,OAAmB,QAAuC;CAC7F,IAAI,UAAU,MAAM,MAAM,IAAI,MAAM;CACpC,OAAO,SAAS;EACd,IAAI,QAAQ,SAAS,YACnB,OAAO;EAET,UAAU,QAAQ,WAAW,MAAM,MAAM,IAAI,QAAQ,QAAQ,IAAI,KAAA;CACnE;AAEF"}
|
|
@@ -26,7 +26,10 @@ declare function cloneInstanceOverrideState(state: InstanceOverrideState): Insta
|
|
|
26
26
|
interface InstanceOverrideReferenceMapper {
|
|
27
27
|
node: (id: string) => string;
|
|
28
28
|
variable: (id: string) => string;
|
|
29
|
+
collection?: (id: string) => string | undefined;
|
|
29
30
|
}
|
|
31
|
+
declare function variableModeOverrideField(collectionId: string): InstanceOverrideField;
|
|
32
|
+
declare function variableModeOverrideCollection(field: InstanceOverrideField): string | undefined;
|
|
30
33
|
/** Remap runtime identities only; ordinary strings and source-format payloads stay opaque. */
|
|
31
34
|
declare function remapInstanceOverrideState(state: InstanceOverrideState, references: InstanceOverrideReferenceMapper): InstanceOverrideState;
|
|
32
35
|
declare function getInstanceOverride(state: InstanceOverrideState, instanceId: string, nodeId: string, field: InstanceOverrideField): unknown;
|
|
@@ -44,5 +47,5 @@ declare function deleteInstanceOverride(state: InstanceOverrideState, instanceId
|
|
|
44
47
|
declare function clearInstanceOverrides(state: InstanceOverrideState): void;
|
|
45
48
|
declare function forEachInstanceOverride(state: InstanceOverrideState, callback: (nodeId: string, field: InstanceOverrideField, value: unknown) => void): void;
|
|
46
49
|
//#endregion
|
|
47
|
-
export { InstanceOverrideField, InstanceOverrideReferenceMapper, InstanceOverrideState, SerializedInstanceOverrideState, SerializedOverrideValue, bindingOverrideField, clearInstanceOverrides, cloneInstanceOverrideState, copiedComponentLayerId, createInstanceOverrideState, deleteInstanceOverride, deserializeInstanceOverrideState, forEachInstanceOverride, getInstanceOverride, hasInstanceOverride, overriddenBindingField, remapInstanceOverrideState, serializeInstanceOverrideState, setInstanceOverride };
|
|
50
|
+
export { InstanceOverrideField, InstanceOverrideReferenceMapper, InstanceOverrideState, SerializedInstanceOverrideState, SerializedOverrideValue, bindingOverrideField, clearInstanceOverrides, cloneInstanceOverrideState, copiedComponentLayerId, createInstanceOverrideState, deleteInstanceOverride, deserializeInstanceOverrideState, forEachInstanceOverride, getInstanceOverride, hasInstanceOverride, overriddenBindingField, remapInstanceOverrideState, serializeInstanceOverrideState, setInstanceOverride, variableModeOverrideCollection, variableModeOverrideField };
|
|
48
51
|
//# sourceMappingURL=overrides.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"overrides.d.ts","names":[],"sources":["../../src/instances/overrides.ts"],"mappings":";KAAY;UAEK;EACf,MAAM,IAAI;EACV,aAAa,YAAY,IAAI;;;;;;iBASf,qBAAqB,gBAAgB;;iBAKrC,uBAAuB,OAAO;iBAM9B,+BAA+B;UAI9B;EACf;EACA;;UAGe;EACf,MAAM,OAAO,uBAAuB;EACpC,aAAa,eAAe,OAAO,uBAAuB;;iBAiC5C,+BACd,OAAO,wBACN;iBAUa,iCAAiC,iBAAiB;iBAgBlD,2BAA2B,OAAO,wBAAwB;UAYzD;EACf,OAAO;EACP,WAAW
|
|
1
|
+
{"version":3,"file":"overrides.d.ts","names":[],"sources":["../../src/instances/overrides.ts"],"mappings":";KAAY;UAEK;EACf,MAAM,IAAI;EACV,aAAa,YAAY,IAAI;;;;;;iBASf,qBAAqB,gBAAgB;;iBAKrC,uBAAuB,OAAO;iBAM9B,+BAA+B;UAI9B;EACf;EACA;;UAGe;EACf,MAAM,OAAO,uBAAuB;EACpC,aAAa,eAAe,OAAO,uBAAuB;;iBAiC5C,+BACd,OAAO,wBACN;iBAUa,iCAAiC,iBAAiB;iBAgBlD,2BAA2B,OAAO,wBAAwB;UAYzD;EACf,OAAO;EACP,WAAW;EACX,cAAc;;iBAKA,0BAA0B,uBAAuB;iBAIjD,+BAA+B,OAAO;;iBAOtC,2BACd,OAAO,uBACP,YAAY,kCACX;iBA+Ba,oBACd,OAAO,uBACP,oBACA,gBACA,OAAO;;;;;iBASO,uBACd,OAAO,uBACP,oBACA;EAAQ;EAAY;;iBAMN,oBACd,OAAO,uBACP,oBACA,gBACA,OAAO;iBAMO,oBACd,OAAO,uBACP,oBACA,gBACA,OAAO,uBACP;iBAWc,uBACd,OAAO,uBACP,oBACA,gBACA,OAAO;iBAQO,uBAAuB,OAAO;iBAK9B,wBACd,OAAO,uBACP,WAAW,gBAAgB,OAAO,uBAAuB"}
|
|
@@ -79,6 +79,13 @@ function cloneInstanceOverrideState(state) {
|
|
|
79
79
|
descendants: new Map([...state.descendants].map(([id, fields]) => [id, new Map([...fields].map(([field, value]) => [field, structuredClone(value)]))]))
|
|
80
80
|
};
|
|
81
81
|
}
|
|
82
|
+
const VARIABLE_MODE_FIELD_PREFIX = "variableModes/";
|
|
83
|
+
function variableModeOverrideField(collectionId) {
|
|
84
|
+
return `${VARIABLE_MODE_FIELD_PREFIX}${collectionId}`;
|
|
85
|
+
}
|
|
86
|
+
function variableModeOverrideCollection(field) {
|
|
87
|
+
return field.startsWith(VARIABLE_MODE_FIELD_PREFIX) ? field.slice(14) : void 0;
|
|
88
|
+
}
|
|
82
89
|
/** Remap runtime identities only; ordinary strings and source-format payloads stay opaque. */
|
|
83
90
|
function remapInstanceOverrideState(state, references) {
|
|
84
91
|
const remapValue = (field, value) => {
|
|
@@ -87,7 +94,16 @@ function remapInstanceOverrideState(state, references) {
|
|
|
87
94
|
if (overriddenBindingField(field) !== null) return references.variable(value);
|
|
88
95
|
return value;
|
|
89
96
|
};
|
|
90
|
-
const
|
|
97
|
+
const remapField = (field) => {
|
|
98
|
+
const collection = variableModeOverrideCollection(field);
|
|
99
|
+
if (collection === void 0 || !references.collection) return field;
|
|
100
|
+
const mapped = references.collection(collection);
|
|
101
|
+
return mapped === void 0 ? void 0 : variableModeOverrideField(mapped);
|
|
102
|
+
};
|
|
103
|
+
const fields = (entries) => new Map([...entries].flatMap(([field, value]) => {
|
|
104
|
+
const mapped = remapField(field);
|
|
105
|
+
return mapped === void 0 ? [] : [[mapped, remapValue(field, value)]];
|
|
106
|
+
}));
|
|
91
107
|
const descendants = /* @__PURE__ */ new Map();
|
|
92
108
|
for (const [id, entries] of state.descendants) {
|
|
93
109
|
const mapped = references.node(id);
|
|
@@ -137,6 +153,6 @@ function forEachInstanceOverride(state, callback) {
|
|
|
137
153
|
for (const [nodeId, fields] of state.descendants) for (const [field, value] of fields) callback(nodeId, field, value);
|
|
138
154
|
}
|
|
139
155
|
//#endregion
|
|
140
|
-
export { bindingOverrideField, clearInstanceOverrides, cloneInstanceOverrideState, copiedComponentLayerId, createInstanceOverrideState, deleteInstanceOverride, deserializeInstanceOverrideState, forEachInstanceOverride, getInstanceOverride, hasInstanceOverride, overriddenBindingField, remapInstanceOverrideState, serializeInstanceOverrideState, setInstanceOverride };
|
|
156
|
+
export { bindingOverrideField, clearInstanceOverrides, cloneInstanceOverrideState, copiedComponentLayerId, createInstanceOverrideState, deleteInstanceOverride, deserializeInstanceOverrideState, forEachInstanceOverride, getInstanceOverride, hasInstanceOverride, overriddenBindingField, remapInstanceOverrideState, serializeInstanceOverrideState, setInstanceOverride, variableModeOverrideCollection, variableModeOverrideField };
|
|
141
157
|
|
|
142
158
|
//# sourceMappingURL=overrides.js.map
|