@moondraft/import 0.0.0-stage → 0.2.0
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/CHANGELOG.md +36 -0
- package/LICENSE +34 -0
- package/README.md +36 -2
- package/SOURCE.md +394 -0
- package/dist/adapter/convert.d.ts +144 -0
- package/dist/adapter/convert.js +2811 -0
- package/dist/adapter/figma-descriptions.d.ts +7 -0
- package/dist/adapter/figma-descriptions.js +34 -0
- package/dist/adapter/figma-grids.d.ts +12 -0
- package/dist/adapter/figma-grids.js +175 -0
- package/dist/adapter/figma-handoff.d.ts +12 -0
- package/dist/adapter/figma-handoff.js +167 -0
- package/dist/adapter/figma-hyperlinks.d.ts +13 -0
- package/dist/adapter/figma-hyperlinks.js +77 -0
- package/dist/adapter/figma.d.ts +12 -0
- package/dist/adapter/figma.js +40 -0
- package/dist/adapter/file-components.d.ts +20 -0
- package/dist/adapter/file-components.js +352 -0
- package/dist/adapter/file-resources.d.ts +29 -0
- package/dist/adapter/file-resources.js +851 -0
- package/dist/adapter/image-crop.d.ts +18 -0
- package/dist/adapter/image-crop.js +30 -0
- package/dist/adapter/index.d.ts +6 -0
- package/dist/adapter/index.js +9 -0
- package/dist/adapter/link.d.ts +49 -0
- package/dist/adapter/link.js +892 -0
- package/dist/adapter/payload.d.ts +31 -0
- package/dist/adapter/payload.js +78 -0
- package/dist/adapter/shapes.d.ts +31 -0
- package/dist/adapter/shapes.js +75 -0
- package/dist/adapter/variables.d.ts +45 -0
- package/dist/adapter/variables.js +106 -0
- package/dist/figma-file.d.ts +38 -0
- package/dist/figma-file.js +97 -0
- package/dist/figma-worker.d.ts +11 -0
- package/dist/figma-worker.js +12 -0
- package/dist/figma.d.ts +26 -0
- package/dist/figma.js +230 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +50 -0
- package/dist/report.d.ts +42 -0
- package/dist/report.js +142 -0
- package/dist/serialize.d.ts +7 -0
- package/dist/serialize.js +79 -0
- package/dist/svg.d.ts +33 -0
- package/dist/svg.js +44 -0
- package/dist/syntax.d.ts +8 -0
- package/dist/syntax.js +19 -0
- package/dist/taco/figma-import/clipboard.d.ts +25 -0
- package/dist/taco/figma-import/clipboard.js +87 -0
- package/dist/taco/figma-import/clipboardSize.d.ts +31 -0
- package/dist/taco/figma-import/clipboardSize.js +107 -0
- package/dist/taco/figma-import/container/compression.d.ts +9 -0
- package/dist/taco/figma-import/container/compression.js +174 -0
- package/dist/taco/figma-import/container/error.d.ts +15 -0
- package/dist/taco/figma-import/container/error.js +19 -0
- package/dist/taco/figma-import/container/kiwiContainer.d.ts +41 -0
- package/dist/taco/figma-import/container/kiwiContainer.js +61 -0
- package/dist/taco/figma-import/container/limits.d.ts +22 -0
- package/dist/taco/figma-import/container/limits.js +22 -0
- package/dist/taco/figma-import/container/zip.d.ts +14 -0
- package/dist/taco/figma-import/container/zip.js +154 -0
- package/dist/taco/figma-import/figFile.d.ts +34 -0
- package/dist/taco/figma-import/figFile.js +91 -0
- package/dist/taco/figma-import/figFileDocument.d.ts +25 -0
- package/dist/taco/figma-import/figFileDocument.js +100 -0
- package/dist/taco/figma-import/import/blobs.d.ts +10 -0
- package/dist/taco/figma-import/import/blobs.js +23 -0
- package/dist/taco/figma-import/import/carry.d.ts +24 -0
- package/dist/taco/figma-import/import/carry.js +66 -0
- package/dist/taco/figma-import/import/componentProps.d.ts +90 -0
- package/dist/taco/figma-import/import/componentProps.js +329 -0
- package/dist/taco/figma-import/import/componentSets.d.ts +85 -0
- package/dist/taco/figma-import/import/componentSets.js +647 -0
- package/dist/taco/figma-import/import/converters/effects.d.ts +31 -0
- package/dist/taco/figma-import/import/converters/effects.js +127 -0
- package/dist/taco/figma-import/import/converters/ellipse.d.ts +7 -0
- package/dist/taco/figma-import/import/converters/ellipse.js +54 -0
- package/dist/taco/figma-import/import/converters/exportSettings.d.ts +4 -0
- package/dist/taco/figma-import/import/converters/exportSettings.js +49 -0
- package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
- package/dist/taco/figma-import/import/converters/frame.js +63 -0
- package/dist/taco/figma-import/import/converters/gradient.d.ts +74 -0
- package/dist/taco/figma-import/import/converters/gradient.js +108 -0
- package/dist/taco/figma-import/import/converters/group.d.ts +19 -0
- package/dist/taco/figma-import/import/converters/group.js +30 -0
- package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
- package/dist/taco/figma-import/import/converters/image.js +94 -0
- package/dist/taco/figma-import/import/converters/layout.d.ts +33 -0
- package/dist/taco/figma-import/import/converters/layout.js +187 -0
- package/dist/taco/figma-import/import/converters/line.d.ts +11 -0
- package/dist/taco/figma-import/import/converters/line.js +40 -0
- package/dist/taco/figma-import/import/converters/rectangle.d.ts +3 -0
- package/dist/taco/figma-import/import/converters/rectangle.js +11 -0
- package/dist/taco/figma-import/import/converters/section.d.ts +8 -0
- package/dist/taco/figma-import/import/converters/section.js +29 -0
- package/dist/taco/figma-import/import/converters/shared.d.ts +71 -0
- package/dist/taco/figma-import/import/converters/shared.js +372 -0
- package/dist/taco/figma-import/import/converters/text.d.ts +54 -0
- package/dist/taco/figma-import/import/converters/text.js +707 -0
- package/dist/taco/figma-import/import/converters/vector.d.ts +105 -0
- package/dist/taco/figma-import/import/converters/vector.js +609 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +114 -0
- package/dist/taco/figma-import/import/converters/vectorNetwork.js +503 -0
- package/dist/taco/figma-import/import/fields.d.ts +15 -0
- package/dist/taco/figma-import/import/fields.js +29 -0
- package/dist/taco/figma-import/import/instances.d.ts +184 -0
- package/dist/taco/figma-import/import/instances.js +1530 -0
- package/dist/taco/figma-import/import/nestingCycles.d.ts +86 -0
- package/dist/taco/figma-import/import/nestingCycles.js +488 -0
- package/dist/taco/figma-import/import/slots.d.ts +56 -0
- package/dist/taco/figma-import/import/slots.js +96 -0
- package/dist/taco/figma-import/import/styles.d.ts +48 -0
- package/dist/taco/figma-import/import/styles.js +48 -0
- package/dist/taco/figma-import/import/tree.d.ts +66 -0
- package/dist/taco/figma-import/import/tree.js +194 -0
- package/dist/taco/figma-import/import/types.d.ts +232 -0
- package/dist/taco/figma-import/import/types.js +33 -0
- package/dist/taco/figma-import/import/variables.d.ts +137 -0
- package/dist/taco/figma-import/import/variables.js +769 -0
- package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
- package/dist/taco/figma-import/import/variantTrees.js +520 -0
- package/dist/taco/figma-import/import/walk.d.ts +207 -0
- package/dist/taco/figma-import/import/walk.js +1464 -0
- package/dist/taco/figma-import/index.d.ts +22 -0
- package/dist/taco/figma-import/index.js +13 -0
- package/dist/taco/figma-import/kiwi/byteReader.d.ts +37 -0
- package/dist/taco/figma-import/kiwi/byteReader.js +151 -0
- package/dist/taco/figma-import/kiwi/error.d.ts +14 -0
- package/dist/taco/figma-import/kiwi/error.js +29 -0
- package/dist/taco/figma-import/kiwi/limits.d.ts +18 -0
- package/dist/taco/figma-import/kiwi/limits.js +18 -0
- package/dist/taco/figma-import/kiwi/message.d.ts +12 -0
- package/dist/taco/figma-import/kiwi/message.js +147 -0
- package/dist/taco/figma-import/kiwi/schema.d.ts +42 -0
- package/dist/taco/figma-import/kiwi/schema.js +206 -0
- package/dist/taco/figma-import/kiwi/types.d.ts +46 -0
- package/dist/taco/figma-import/kiwi/types.js +18 -0
- package/dist/taco/model/agent-hidden-fields.d.ts +20 -0
- package/dist/taco/model/agent-hidden-fields.js +29 -0
- package/dist/taco/model/asset-users.d.ts +23 -0
- package/dist/taco/model/asset-users.js +128 -0
- package/dist/taco/model/auto-layout-lines.d.ts +32 -0
- package/dist/taco/model/auto-layout-lines.js +70 -0
- package/dist/taco/model/codec.d.ts +444 -0
- package/dist/taco/model/codec.js +5665 -0
- package/dist/taco/model/color.d.ts +11 -0
- package/dist/taco/model/color.js +61 -0
- package/dist/taco/model/comment-operations.d.ts +102 -0
- package/dist/taco/model/comment-operations.js +227 -0
- package/dist/taco/model/component-evaluation.d.ts +96 -0
- package/dist/taco/model/component-evaluation.js +555 -0
- package/dist/taco/model/component-expressions.d.ts +242 -0
- package/dist/taco/model/component-expressions.js +1082 -0
- package/dist/taco/model/component-properties.d.ts +111 -0
- package/dist/taco/model/component-properties.js +339 -0
- package/dist/taco/model/component-samples.d.ts +83 -0
- package/dist/taco/model/component-samples.js +290 -0
- package/dist/taco/model/component-validity.d.ts +85 -0
- package/dist/taco/model/component-validity.js +397 -0
- package/dist/taco/model/components.d.ts +155 -0
- package/dist/taco/model/components.js +505 -0
- package/dist/taco/model/connector-ends.d.ts +20 -0
- package/dist/taco/model/connector-ends.js +188 -0
- package/dist/taco/model/connector-geometry.d.ts +74 -0
- package/dist/taco/model/connector-geometry.js +293 -0
- package/dist/taco/model/corner-radii.d.ts +26 -0
- package/dist/taco/model/corner-radii.js +47 -0
- package/dist/taco/model/derivations.d.ts +81 -0
- package/dist/taco/model/derivations.js +151 -0
- package/dist/taco/model/export-settings.d.ts +53 -0
- package/dist/taco/model/export-settings.js +156 -0
- package/dist/taco/model/expressions/ast.d.ts +153 -0
- package/dist/taco/model/expressions/ast.js +6 -0
- package/dist/taco/model/expressions/cache.d.ts +23 -0
- package/dist/taco/model/expressions/cache.js +43 -0
- package/dist/taco/model/expressions/check.d.ts +72 -0
- package/dist/taco/model/expressions/check.js +517 -0
- package/dist/taco/model/expressions/completion.d.ts +61 -0
- package/dist/taco/model/expressions/completion.js +421 -0
- package/dist/taco/model/expressions/evaluate.d.ts +32 -0
- package/dist/taco/model/expressions/evaluate.js +519 -0
- package/dist/taco/model/expressions/forms.d.ts +84 -0
- package/dist/taco/model/expressions/forms.js +200 -0
- package/dist/taco/model/expressions/index.d.ts +14 -0
- package/dist/taco/model/expressions/index.js +17 -0
- package/dist/taco/model/expressions/lexer.d.ts +42 -0
- package/dist/taco/model/expressions/lexer.js +213 -0
- package/dist/taco/model/expressions/library.d.ts +33 -0
- package/dist/taco/model/expressions/library.js +117 -0
- package/dist/taco/model/expressions/names.d.ts +43 -0
- package/dist/taco/model/expressions/names.js +99 -0
- package/dist/taco/model/expressions/parser.d.ts +15 -0
- package/dist/taco/model/expressions/parser.js +438 -0
- package/dist/taco/model/expressions/rename.d.ts +34 -0
- package/dist/taco/model/expressions/rename.js +46 -0
- package/dist/taco/model/expressions/source.d.ts +15 -0
- package/dist/taco/model/expressions/source.js +161 -0
- package/dist/taco/model/expressions/types.d.ts +119 -0
- package/dist/taco/model/expressions/types.js +102 -0
- package/dist/taco/model/expressions/values.d.ts +97 -0
- package/dist/taco/model/expressions/values.js +160 -0
- package/dist/taco/model/geometry-limits.d.ts +16 -0
- package/dist/taco/model/geometry-limits.js +67 -0
- package/dist/taco/model/geometry.d.ts +47 -0
- package/dist/taco/model/geometry.js +193 -0
- package/dist/taco/model/group-bounds.d.ts +38 -0
- package/dist/taco/model/group-bounds.js +144 -0
- package/dist/taco/model/id-table.d.ts +37 -0
- package/dist/taco/model/id-table.js +80 -0
- package/dist/taco/model/index.d.ts +25 -0
- package/dist/taco/model/index.js +27 -0
- package/dist/taco/model/instance-resolver.d.ts +376 -0
- package/dist/taco/model/instance-resolver.js +1778 -0
- package/dist/taco/model/layer-syntax.d.ts +340 -0
- package/dist/taco/model/layer-syntax.js +3506 -0
- package/dist/taco/model/layout/constraints.d.ts +84 -0
- package/dist/taco/model/layout/constraints.js +445 -0
- package/dist/taco/model/layout/frame-layout.d.ts +46 -0
- package/dist/taco/model/layout/frame-layout.js +618 -0
- package/dist/taco/model/layout/node-sizing.d.ts +163 -0
- package/dist/taco/model/layout/node-sizing.js +486 -0
- package/dist/taco/model/legacy-connector-caps.d.ts +15 -0
- package/dist/taco/model/legacy-connector-caps.js +70 -0
- package/dist/taco/model/linear-call.d.ts +48 -0
- package/dist/taco/model/linear-call.js +58 -0
- package/dist/taco/model/new-nodes.d.ts +145 -0
- package/dist/taco/model/new-nodes.js +277 -0
- package/dist/taco/model/node-builder.d.ts +449 -0
- package/dist/taco/model/node-builder.js +2294 -0
- package/dist/taco/model/node-table-watch.d.ts +36 -0
- package/dist/taco/model/node-table-watch.js +63 -0
- package/dist/taco/model/node-transform.d.ts +39 -0
- package/dist/taco/model/node-transform.js +79 -0
- package/dist/taco/model/node-users-index.d.ts +10 -0
- package/dist/taco/model/node-users-index.js +82 -0
- package/dist/taco/model/operation-references.d.ts +80 -0
- package/dist/taco/model/operation-references.js +399 -0
- package/dist/taco/model/operations.d.ts +610 -0
- package/dist/taco/model/operations.js +2882 -0
- package/dist/taco/model/overlay-table.d.ts +71 -0
- package/dist/taco/model/overlay-table.js +171 -0
- package/dist/taco/model/override-remap.d.ts +69 -0
- package/dist/taco/model/override-remap.js +230 -0
- package/dist/taco/model/parent-table.d.ts +53 -0
- package/dist/taco/model/parent-table.js +193 -0
- package/dist/taco/model/problem-collector.d.ts +27 -0
- package/dist/taco/model/problem-collector.js +101 -0
- package/dist/taco/model/sha256.d.ts +10 -0
- package/dist/taco/model/sha256.js +79 -0
- package/dist/taco/model/shape-geometry.d.ts +96 -0
- package/dist/taco/model/shape-geometry.js +368 -0
- package/dist/taco/model/shared-node-table.d.ts +97 -0
- package/dist/taco/model/shared-node-table.js +546 -0
- package/dist/taco/model/slots.d.ts +113 -0
- package/dist/taco/model/slots.js +281 -0
- package/dist/taco/model/stroke-settings.d.ts +37 -0
- package/dist/taco/model/stroke-settings.js +90 -0
- package/dist/taco/model/svg-path-import.d.ts +89 -0
- package/dist/taco/model/svg-path-import.js +219 -0
- package/dist/taco/model/text-measurement.d.ts +142 -0
- package/dist/taco/model/text-measurement.js +846 -0
- package/dist/taco/model/text-reads.d.ts +66 -0
- package/dist/taco/model/text-reads.js +128 -0
- package/dist/taco/model/text-sizer.d.ts +358 -0
- package/dist/taco/model/text-sizer.js +996 -0
- package/dist/taco/model/theme-users.d.ts +59 -0
- package/dist/taco/model/theme-users.js +312 -0
- package/dist/taco/model/thumbnail.d.ts +15 -0
- package/dist/taco/model/thumbnail.js +34 -0
- package/dist/taco/model/types.d.ts +2369 -0
- package/dist/taco/model/types.js +366 -0
- package/dist/taco/model/unknown-property.d.ts +5 -0
- package/dist/taco/model/unknown-property.js +7 -0
- package/dist/taco/model/validated-documents.d.ts +22 -0
- package/dist/taco/model/validated-documents.js +22 -0
- package/dist/taco/model/variable-modes.d.ts +61 -0
- package/dist/taco/model/variable-modes.js +438 -0
- package/dist/taco/model/variable-references.d.ts +54 -0
- package/dist/taco/model/variable-references.js +183 -0
- package/dist/taco/model/variables.d.ts +24 -0
- package/dist/taco/model/variables.js +151 -0
- package/dist/taco/model/vector-network.d.ts +34 -0
- package/dist/taco/model/vector-network.js +411 -0
- package/dist/taco/svg-import/import-svg.d.ts +36 -0
- package/dist/taco/svg-import/import-svg.js +729 -0
- package/dist/taco/svg-import/index.d.ts +3 -0
- package/dist/taco/svg-import/index.js +3 -0
- package/dist/taco/svg-import/limits/limits.d.ts +79 -0
- package/dist/taco/svg-import/limits/limits.js +160 -0
- package/dist/taco/svg-import/paint/gradients.d.ts +64 -0
- package/dist/taco/svg-import/paint/gradients.js +429 -0
- package/dist/taco/svg-import/paint/paint.d.ts +32 -0
- package/dist/taco/svg-import/paint/paint.js +162 -0
- package/dist/taco/svg-import/paths/paths.d.ts +58 -0
- package/dist/taco/svg-import/paths/paths.js +205 -0
- package/dist/taco/svg-import/public-types.d.ts +279 -0
- package/dist/taco/svg-import/public-types.js +56 -0
- package/dist/taco/svg-import/report/report.d.ts +44 -0
- package/dist/taco/svg-import/report/report.js +79 -0
- package/dist/taco/svg-import/reuse/reuse.d.ts +99 -0
- package/dist/taco/svg-import/reuse/reuse.js +840 -0
- package/dist/taco/svg-import/shapes/shapes.d.ts +35 -0
- package/dist/taco/svg-import/shapes/shapes.js +386 -0
- package/dist/taco/svg-import/structure/effects.d.ts +26 -0
- package/dist/taco/svg-import/structure/effects.js +618 -0
- package/dist/taco/svg-import/structure/image.d.ts +24 -0
- package/dist/taco/svg-import/structure/image.js +495 -0
- package/dist/taco/svg-import/structure/walk.d.ts +85 -0
- package/dist/taco/svg-import/structure/walk.js +544 -0
- package/dist/taco/svg-import/style/color.d.ts +40 -0
- package/dist/taco/svg-import/style/color.js +275 -0
- package/dist/taco/svg-import/style/computed.d.ts +214 -0
- package/dist/taco/svg-import/style/computed.js +1534 -0
- package/dist/taco/svg-import/style/css.d.ts +127 -0
- package/dist/taco/svg-import/style/css.js +541 -0
- package/dist/taco/svg-import/style/media.d.ts +25 -0
- package/dist/taco/svg-import/style/media.js +307 -0
- package/dist/taco/svg-import/style/properties.d.ts +43 -0
- package/dist/taco/svg-import/style/properties.js +135 -0
- package/dist/taco/svg-import/style/selector-reach.d.ts +30 -0
- package/dist/taco/svg-import/style/selector-reach.js +169 -0
- package/dist/taco/svg-import/style/selector-validity.d.ts +14 -0
- package/dist/taco/svg-import/style/selector-validity.js +307 -0
- package/dist/taco/svg-import/text/text.d.ts +29 -0
- package/dist/taco/svg-import/text/text.js +545 -0
- package/dist/taco/svg-import/transform/matrix.d.ts +111 -0
- package/dist/taco/svg-import/transform/matrix.js +229 -0
- package/dist/taco/svg-import/tree/element-data.d.ts +52 -0
- package/dist/taco/svg-import/tree/element-data.js +155 -0
- package/dist/taco/svg-import/tree/elements.d.ts +36 -0
- package/dist/taco/svg-import/tree/elements.js +121 -0
- package/dist/taco/svg-import/units/length.d.ts +43 -0
- package/dist/taco/svg-import/units/length.js +125 -0
- package/dist/taco/svg-import/units/viewbox.d.ts +35 -0
- package/dist/taco/svg-import/units/viewbox.js +77 -0
- package/package.json +55 -4
|
@@ -0,0 +1,546 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node tables that share their entries with the tables they were made from,
|
|
3
|
+
* so that an edit of k nodes in a document of n nodes costs O(k), not O(n).
|
|
4
|
+
*
|
|
5
|
+
* Every table made from one first table belongs to one family. The family
|
|
6
|
+
* keeps one plain object, `live`, that holds the entries of one table of the
|
|
7
|
+
* family, its head. Each table is a Proxy whose target is `live`:
|
|
8
|
+
*
|
|
9
|
+
* - the head has no read traps, so reading an entry, `in`, `Object.hasOwn`
|
|
10
|
+
* and listing its entries go straight to the plain object;
|
|
11
|
+
* - every other table keeps the entries in which it differs from the table
|
|
12
|
+
* one step nearer the head (`differences`, Baker's "rerooting" of a
|
|
13
|
+
* persistent array). Reading an entry walks those steps; listing the
|
|
14
|
+
* entries first makes the table the head, which moves only the entries
|
|
15
|
+
* that differ on the way.
|
|
16
|
+
*
|
|
17
|
+
* A table is written as a draft (`sharedTableDraft`), which is made the head
|
|
18
|
+
* and written in place; the table it was made from keeps the entries the
|
|
19
|
+
* draft overwrites. Sealing it (`sealSharedTable`) closes it to writes: a
|
|
20
|
+
* write returns false, which throws a `TypeError` in strict code, as for a
|
|
21
|
+
* frozen table. A table is a Proxy over an object that changes, so
|
|
22
|
+
* `Object.isFrozen` does not report it as frozen and `Object.freeze` throws;
|
|
23
|
+
* `isFrozenNodeMap` reports a sealed one as frozen. `structuredClone` and
|
|
24
|
+
* `postMessage` cannot copy a Proxy: pass `plainNodeTable` of it across such
|
|
25
|
+
* a boundary.
|
|
26
|
+
*
|
|
27
|
+
* Listing order is the order of a plain object made by the same writes. To
|
|
28
|
+
* keep it, a draft cannot remove an entry of the table it was made from (the
|
|
29
|
+
* write returns false and throws in strict code): a table some entries are
|
|
30
|
+
* removed from starts a new family (`sharedTableCopy`). Within a family,
|
|
31
|
+
* entries are then only added, at the end, so making a table the head
|
|
32
|
+
* removes the entries added last and adds others at the end in the order
|
|
33
|
+
* they were first added.
|
|
34
|
+
*
|
|
35
|
+
* Cost rule. Reading a table walks the steps between it and the head, and
|
|
36
|
+
* making it the head writes the entries that differ on the way, so the cost
|
|
37
|
+
* of a table is the distance to the head. Two rules keep that distance, and
|
|
38
|
+
* so every read, bounded by what was written:
|
|
39
|
+
*
|
|
40
|
+
* - a read walks at most `MAX_READ_STEPS` steps for free; longer reads count
|
|
41
|
+
* their steps, and the table is made the head once they have walked as
|
|
42
|
+
* much as the move costs (`countLongRead`), so reads that take turns
|
|
43
|
+
* between two tables cost O(the writes between them) in all, not per read;
|
|
44
|
+
* - two tables read alongside each other are kept at most `MAX_KIN_STEPS`
|
|
45
|
+
* steps apart by their callers: a laid-out view is made again from a draft
|
|
46
|
+
* of its document when the view it is made from has drifted further
|
|
47
|
+
* (`sharedTableDistance`, used by `sharedStart` in laid-out.ts). Documents
|
|
48
|
+
* far apart are compared by the entries written between them
|
|
49
|
+
* (`sharedTableDifferences`), never read entry by entry by turns.
|
|
50
|
+
*/
|
|
51
|
+
import { copyTable, putEntry } from "./id-table.js";
|
|
52
|
+
/**
|
|
53
|
+
* Marks an entry a table does not have. A registered symbol, the same in
|
|
54
|
+
* every copy of this module, as the tables are (see `versions`).
|
|
55
|
+
*/
|
|
56
|
+
const ABSENT = Symbol.for("@taco-design/model absent node table entry");
|
|
57
|
+
/**
|
|
58
|
+
* Steps a read may walk from a table to the head without counting towards
|
|
59
|
+
* making the table the head. Longer reads add their steps to the table's
|
|
60
|
+
* `stepsRead`, and the table is made the head once that count passes the
|
|
61
|
+
* entries the move would write (see `slotOf`).
|
|
62
|
+
*/
|
|
63
|
+
const MAX_READ_STEPS = 16;
|
|
64
|
+
/**
|
|
65
|
+
* Steps two tables read alongside each other may be apart, such as a
|
|
66
|
+
* laid-out view and its document (see the cost rule above).
|
|
67
|
+
*/
|
|
68
|
+
export const MAX_KIN_STEPS = 64;
|
|
69
|
+
/**
|
|
70
|
+
* Counts of the work the tables do, for tests that check what an edit costs.
|
|
71
|
+
* `wholeCopies`: tables copied entry by entry to start a family.
|
|
72
|
+
* `entriesWritten`: entries a draft wrote. `entriesMoved`: entries written
|
|
73
|
+
* into a family's plain object to make another table the head.
|
|
74
|
+
*/
|
|
75
|
+
export const sharedNodeTableStats = { wholeCopies: 0, entriesWritten: 0, entriesMoved: 0 };
|
|
76
|
+
const listingWatch = (() => {
|
|
77
|
+
const key = Symbol.for("@taco-design/model shared node table listings");
|
|
78
|
+
const holder = globalThis;
|
|
79
|
+
return holder[key] ??= { listed: null, heads: new Set() };
|
|
80
|
+
})();
|
|
81
|
+
/** Starts calling `listed` with the entries of every pass over a whole shared table. Throws when a watch is on. */
|
|
82
|
+
export function watchSharedTableListings(listed) {
|
|
83
|
+
if (listingWatch.listed !== null)
|
|
84
|
+
throw new Error("The shared node tables are already watched.");
|
|
85
|
+
listingWatch.listed = listed;
|
|
86
|
+
resetHeadTraps();
|
|
87
|
+
}
|
|
88
|
+
/** Ends `watchSharedTableListings`, taking its trap off every head that has it. */
|
|
89
|
+
export function unwatchSharedTableListings() {
|
|
90
|
+
listingWatch.listed = null;
|
|
91
|
+
resetHeadTraps();
|
|
92
|
+
}
|
|
93
|
+
/** Sets the traps of the heads in `listingWatch.heads` again, and forgets the tables gone or no longer the head. */
|
|
94
|
+
function resetHeadTraps() {
|
|
95
|
+
const heads = [...listingWatch.heads];
|
|
96
|
+
listingWatch.heads.clear();
|
|
97
|
+
for (const reference of heads) {
|
|
98
|
+
const version = reference.deref();
|
|
99
|
+
if (version === undefined || version.family.head !== version)
|
|
100
|
+
continue;
|
|
101
|
+
// While watching, `setTraps` lists the table again itself.
|
|
102
|
+
setTraps(version);
|
|
103
|
+
if (listingWatch.listed === null)
|
|
104
|
+
listingWatch.heads.add(reference);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/** Reports a pass over `entries` entries of a whole table to `watchSharedTableListings`. */
|
|
108
|
+
export function noteListing(entries) {
|
|
109
|
+
listingWatch.listed?.(entries);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The family table behind each table and each view of one
|
|
113
|
+
* (`shareTableIdentity`). Kept on `globalThis`, so that two copies of this
|
|
114
|
+
* module loaded in one program (a bundle and the package, say) know each
|
|
115
|
+
* other's tables: a table one made is written, sealed and listed by the other.
|
|
116
|
+
*/
|
|
117
|
+
const versions = (() => {
|
|
118
|
+
const key = Symbol.for("@taco-design/model shared node tables");
|
|
119
|
+
const holder = globalThis;
|
|
120
|
+
return holder[key] ??= new WeakMap();
|
|
121
|
+
})();
|
|
122
|
+
function ownSlot(live, id) {
|
|
123
|
+
const value = live[id];
|
|
124
|
+
if (value !== undefined && typeof value !== "function" && value !== Object.prototype)
|
|
125
|
+
return value;
|
|
126
|
+
return Object.hasOwn(live, id) ? value : ABSENT;
|
|
127
|
+
}
|
|
128
|
+
/** The entry of `version` for `id`, or `ABSENT`. */
|
|
129
|
+
function slotOf(version, id) {
|
|
130
|
+
const { family } = version;
|
|
131
|
+
let step = version;
|
|
132
|
+
let steps = 0;
|
|
133
|
+
while (step !== family.head) {
|
|
134
|
+
const differences = step.differences;
|
|
135
|
+
if (differences.has(id)) {
|
|
136
|
+
// Read before counting: making the table the head turns this map around.
|
|
137
|
+
const slot = differences.get(id);
|
|
138
|
+
if (steps > MAX_READ_STEPS)
|
|
139
|
+
countLongRead(version, steps);
|
|
140
|
+
return slot;
|
|
141
|
+
}
|
|
142
|
+
step = step.next;
|
|
143
|
+
steps += 1;
|
|
144
|
+
}
|
|
145
|
+
if (steps > MAX_READ_STEPS)
|
|
146
|
+
countLongRead(version, steps);
|
|
147
|
+
return ownSlot(family.live, id);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Counts a read of `version` that walked `steps` steps, and makes `version`
|
|
151
|
+
* the head once the steps its reads walked pass what that move costs (the
|
|
152
|
+
* steps to the head and the entries that differ on the way). Moving at the
|
|
153
|
+
* first long read instead would let reads that take turns between two
|
|
154
|
+
* tables far apart move every differing entry on every read; this way moving
|
|
155
|
+
* costs at most about what the reads already cost. The cost is counted again
|
|
156
|
+
* only each time `stepsRead` has doubled, and each count stops once it
|
|
157
|
+
* passes `stepsRead`, so the counts cost O(the steps read) in all.
|
|
158
|
+
*/
|
|
159
|
+
function countLongRead(version, steps) {
|
|
160
|
+
version.stepsRead += steps;
|
|
161
|
+
if (version.stepsRead < version.recountAt)
|
|
162
|
+
return;
|
|
163
|
+
version.recountAt = 2 * version.stepsRead;
|
|
164
|
+
let moveCost = 0;
|
|
165
|
+
for (let step = version; step !== version.family.head; step = step.next) {
|
|
166
|
+
moveCost += step.differences.size + 1;
|
|
167
|
+
if (moveCost > version.stepsRead)
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
makeHead(version);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* The steps between two shared tables of one family, or `limit + 1` when
|
|
174
|
+
* they are more than `limit` steps apart or not in one family. Costs at most
|
|
175
|
+
* `limit` steps.
|
|
176
|
+
*/
|
|
177
|
+
export function sharedTableDistance(first, second, limit) {
|
|
178
|
+
let left = versions.get(first);
|
|
179
|
+
let right = versions.get(second);
|
|
180
|
+
if (left === undefined || right === undefined || left.family !== right.family)
|
|
181
|
+
return limit + 1;
|
|
182
|
+
let steps = 0;
|
|
183
|
+
while (left !== right) {
|
|
184
|
+
if (steps > limit)
|
|
185
|
+
return limit + 1;
|
|
186
|
+
// Step up from the deeper table, towards the table both came from.
|
|
187
|
+
if (left.depth >= right.depth)
|
|
188
|
+
left = left.base;
|
|
189
|
+
else
|
|
190
|
+
right = right.base;
|
|
191
|
+
steps += 1;
|
|
192
|
+
if (left === undefined || right === undefined)
|
|
193
|
+
return limit + 1;
|
|
194
|
+
}
|
|
195
|
+
return steps;
|
|
196
|
+
}
|
|
197
|
+
function writeLive(live, id, slot) {
|
|
198
|
+
if (slot === ABSENT)
|
|
199
|
+
delete live[id];
|
|
200
|
+
else
|
|
201
|
+
putEntry(live, id, slot);
|
|
202
|
+
}
|
|
203
|
+
/** The traps every table that is not the head reads through. */
|
|
204
|
+
function readTraps(version) {
|
|
205
|
+
return {
|
|
206
|
+
get: (_target, key, receiver) => {
|
|
207
|
+
if (typeof key !== "string")
|
|
208
|
+
return undefined;
|
|
209
|
+
const slot = slotOf(version, key);
|
|
210
|
+
return slot === ABSENT ? Reflect.get(Object.prototype, key, receiver) : slot;
|
|
211
|
+
},
|
|
212
|
+
has: (_target, key) => (typeof key === "string" && (slotOf(version, key) !== ABSENT || key in Object.prototype)),
|
|
213
|
+
getOwnPropertyDescriptor: (_target, key) => {
|
|
214
|
+
if (typeof key !== "string")
|
|
215
|
+
return undefined;
|
|
216
|
+
const slot = slotOf(version, key);
|
|
217
|
+
return slot === ABSENT ? undefined : { value: slot, writable: true, enumerable: true, configurable: true };
|
|
218
|
+
},
|
|
219
|
+
ownKeys: () => {
|
|
220
|
+
makeHead(version);
|
|
221
|
+
const keys = Reflect.ownKeys(version.family.live);
|
|
222
|
+
noteListing(keys.length);
|
|
223
|
+
return keys;
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Lists the head's keys, counting the pass (`watchSharedTableListings`): the
|
|
229
|
+
* head's only trap while passes are counted.
|
|
230
|
+
*/
|
|
231
|
+
function countedHeadKeys(target) {
|
|
232
|
+
const keys = Reflect.ownKeys(target);
|
|
233
|
+
noteListing(keys.length);
|
|
234
|
+
return keys;
|
|
235
|
+
}
|
|
236
|
+
/** Sets the traps of `version`: none to read the head, the read traps for any other table. */
|
|
237
|
+
function setTraps(version) {
|
|
238
|
+
const { handler } = version;
|
|
239
|
+
let traps;
|
|
240
|
+
if (version.family.head !== version) {
|
|
241
|
+
traps = readTraps(version);
|
|
242
|
+
}
|
|
243
|
+
else if (listingWatch.listed !== null) {
|
|
244
|
+
traps = { ownKeys: countedHeadKeys };
|
|
245
|
+
listingWatch.heads.add(new WeakRef(version));
|
|
246
|
+
}
|
|
247
|
+
// A trap set to undefined counts as no trap. Setting it, instead of
|
|
248
|
+
// deleting it, keeps the handler a fast-mode object, whose traps V8 looks
|
|
249
|
+
// up quickly on every read.
|
|
250
|
+
handler.get = traps?.get;
|
|
251
|
+
handler.has = traps?.has;
|
|
252
|
+
handler.getOwnPropertyDescriptor = traps?.getOwnPropertyDescriptor;
|
|
253
|
+
handler.ownKeys = traps?.ownKeys;
|
|
254
|
+
}
|
|
255
|
+
function write(version, key, slot) {
|
|
256
|
+
if (version.sealed || typeof key !== "string")
|
|
257
|
+
return false;
|
|
258
|
+
makeHead(version);
|
|
259
|
+
const { base, family } = version;
|
|
260
|
+
if (base !== undefined) {
|
|
261
|
+
const kept = base.differences;
|
|
262
|
+
if (!kept.has(key)) {
|
|
263
|
+
// An entry the draft added can be removed again; one of its base
|
|
264
|
+
// cannot (see the listing order above).
|
|
265
|
+
const before = ownSlot(family.live, key);
|
|
266
|
+
if (slot === ABSENT && before !== ABSENT)
|
|
267
|
+
return false;
|
|
268
|
+
kept.set(key, before);
|
|
269
|
+
}
|
|
270
|
+
else if (slot === ABSENT) {
|
|
271
|
+
if (kept.get(key) !== ABSENT)
|
|
272
|
+
return false;
|
|
273
|
+
// The draft removes an entry it added: the base no longer differs in
|
|
274
|
+
// it, so it is dropped from `kept` too. Left there, adding it again
|
|
275
|
+
// later would put it back at its first place, not at the end, when the
|
|
276
|
+
// draft is next made the head.
|
|
277
|
+
kept.delete(key);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
writeLive(family.live, key, slot);
|
|
281
|
+
sharedNodeTableStats.entriesWritten += 1;
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
/** A new table of `family` (a new family when undefined) over `live`, made the head. */
|
|
285
|
+
function newVersion(family, live, base) {
|
|
286
|
+
const handler = {
|
|
287
|
+
get: undefined,
|
|
288
|
+
has: undefined,
|
|
289
|
+
getOwnPropertyDescriptor: undefined,
|
|
290
|
+
ownKeys: undefined,
|
|
291
|
+
set: (_target, key, value) => write(version, key, value),
|
|
292
|
+
defineProperty: (_target, key, descriptor) => "value" in descriptor && write(version, key, descriptor.value),
|
|
293
|
+
deleteProperty: (_target, key) => write(version, key, ABSENT),
|
|
294
|
+
// `Object.freeze` cannot close a table whose target others write: it
|
|
295
|
+
// throws instead of closing the family's plain object.
|
|
296
|
+
preventExtensions: () => false,
|
|
297
|
+
};
|
|
298
|
+
const proxy = new Proxy(live, handler);
|
|
299
|
+
const version = {
|
|
300
|
+
family: family ?? { live, head: undefined },
|
|
301
|
+
proxy,
|
|
302
|
+
handler,
|
|
303
|
+
sealed: false,
|
|
304
|
+
base,
|
|
305
|
+
next: undefined,
|
|
306
|
+
differences: undefined,
|
|
307
|
+
stepsRead: 0,
|
|
308
|
+
recountAt: 0,
|
|
309
|
+
depth: base === undefined ? 0 : base.depth + 1,
|
|
310
|
+
};
|
|
311
|
+
versions.set(proxy, version);
|
|
312
|
+
const previous = family?.head;
|
|
313
|
+
version.family.head = version;
|
|
314
|
+
if (previous !== undefined) {
|
|
315
|
+
// The new table reads as the head it replaces, which now differs from it
|
|
316
|
+
// in nothing.
|
|
317
|
+
previous.next = version;
|
|
318
|
+
previous.differences = new Map();
|
|
319
|
+
setTraps(previous);
|
|
320
|
+
}
|
|
321
|
+
setTraps(version);
|
|
322
|
+
return version;
|
|
323
|
+
}
|
|
324
|
+
/** Makes `version` the head of its family. Costs the entries that differ on the way. */
|
|
325
|
+
function makeHead(version) {
|
|
326
|
+
const { family } = version;
|
|
327
|
+
if (family.head === version)
|
|
328
|
+
return;
|
|
329
|
+
const path = [];
|
|
330
|
+
for (let step = version; step !== family.head; step = step.next)
|
|
331
|
+
path.push(step);
|
|
332
|
+
const { live } = family;
|
|
333
|
+
for (let index = path.length - 1; index >= 0; index -= 1) {
|
|
334
|
+
const child = path[index];
|
|
335
|
+
const parent = family.head;
|
|
336
|
+
// The parent differs from the child in the same entries: the map is
|
|
337
|
+
// turned around in place, each entry taking the value the head had.
|
|
338
|
+
const differences = child.differences;
|
|
339
|
+
for (const [id, slot] of differences) {
|
|
340
|
+
differences.set(id, ownSlot(live, id));
|
|
341
|
+
writeLive(live, id, slot);
|
|
342
|
+
}
|
|
343
|
+
sharedNodeTableStats.entriesMoved += differences.size;
|
|
344
|
+
parent.next = child;
|
|
345
|
+
parent.differences = differences;
|
|
346
|
+
child.next = undefined;
|
|
347
|
+
child.differences = undefined;
|
|
348
|
+
child.stepsRead = 0;
|
|
349
|
+
child.recountAt = 0;
|
|
350
|
+
family.head = child;
|
|
351
|
+
setTraps(parent);
|
|
352
|
+
setTraps(child);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* A table to write into that reads as `table` until it is written: what
|
|
357
|
+
* `copyTable(table)` gives. When `table` is a shared table, the draft shares
|
|
358
|
+
* its entries and costs as much as its writes; a draft of it not sealed yet
|
|
359
|
+
* is sealed first, and is then closed to writes. Any other table is copied
|
|
360
|
+
* once, entry by entry, to start a new family (`sharedNodeTableStats.wholeCopies`).
|
|
361
|
+
*/
|
|
362
|
+
export function sharedTableDraft(table) {
|
|
363
|
+
const from = versions.get(table);
|
|
364
|
+
if (from === undefined)
|
|
365
|
+
return sharedTableCopy(table);
|
|
366
|
+
from.sealed = true;
|
|
367
|
+
makeHead(from);
|
|
368
|
+
return newVersion(from.family, from.family.live, from).proxy;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* A table to write into that reads as `table`, starting a new family with a
|
|
372
|
+
* copy of every entry (`sharedNodeTableStats.wholeCopies`): for an edit that
|
|
373
|
+
* removes entries, which a draft cannot. Entries can be removed from it until
|
|
374
|
+
* it is sealed. A draft of `table` not sealed yet is sealed first.
|
|
375
|
+
*/
|
|
376
|
+
export function sharedTableCopy(table) {
|
|
377
|
+
const from = versions.get(table);
|
|
378
|
+
let copy;
|
|
379
|
+
if (from === undefined) {
|
|
380
|
+
copy = copyTable(table);
|
|
381
|
+
}
|
|
382
|
+
else {
|
|
383
|
+
from.sealed = true;
|
|
384
|
+
makeHead(from);
|
|
385
|
+
copy = copyTable(from.family.live);
|
|
386
|
+
}
|
|
387
|
+
if (listingWatch.listed !== null)
|
|
388
|
+
noteListing(Object.keys(copy).length);
|
|
389
|
+
sharedNodeTableStats.wholeCopies += 1;
|
|
390
|
+
return newVersion(undefined, copy, undefined).proxy;
|
|
391
|
+
}
|
|
392
|
+
/** Closes `table`, a draft from `sharedTableDraft` or a view of one, to writes. Returns it. */
|
|
393
|
+
export function sealSharedTable(table) {
|
|
394
|
+
const version = versions.get(table);
|
|
395
|
+
if (version === undefined)
|
|
396
|
+
throw new TypeError("sealSharedTable needs a table that sharedTableDraft made");
|
|
397
|
+
version.sealed = true;
|
|
398
|
+
return table;
|
|
399
|
+
}
|
|
400
|
+
/** Whether `table` is a table `sharedTableDraft` made, or a view of one. */
|
|
401
|
+
export function isSharedNodeTable(table) {
|
|
402
|
+
return versions.has(table);
|
|
403
|
+
}
|
|
404
|
+
/** Whether `table` is a shared table that `sealSharedTable` has closed. */
|
|
405
|
+
export function isSealedSharedTable(table) {
|
|
406
|
+
return versions.get(table)?.sealed === true;
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Makes `view`, a Proxy over `table` (a table `sharedTableDraft` made), stand
|
|
410
|
+
* for `table`: sealing it seals `table`, and a draft of it shares its entries.
|
|
411
|
+
*/
|
|
412
|
+
export function shareTableIdentity(view, table) {
|
|
413
|
+
const version = versions.get(table);
|
|
414
|
+
if (version === undefined)
|
|
415
|
+
throw new TypeError("shareTableIdentity needs a table that sharedTableDraft made");
|
|
416
|
+
versions.set(view, version);
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Makes the frozen plain table `table` stand for a sealed shared table with
|
|
420
|
+
* the same entries, copied now, so that the first draft of it shares its
|
|
421
|
+
* entries instead of copying them all (`sharedTableDraft`). `table` stays a
|
|
422
|
+
* plain frozen object, read as before. For a document opened in a store:
|
|
423
|
+
* the copy is made when it opens, not on its first edit. Does nothing for a
|
|
424
|
+
* shared table or a table that is not frozen.
|
|
425
|
+
*/
|
|
426
|
+
export function shareFrozenTable(table) {
|
|
427
|
+
if (versions.has(table) || !Object.isFrozen(table))
|
|
428
|
+
return;
|
|
429
|
+
const shared = sharedTableCopy(table);
|
|
430
|
+
sealSharedTable(shared);
|
|
431
|
+
shareTableIdentity(table, shared);
|
|
432
|
+
frozenStandIns.add(table);
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Frozen plain tables `shareFrozenTable` made stand for a shared table: read as they are, as plain tables.
|
|
436
|
+
* Kept on `globalThis`, as the tables are (see `versions`).
|
|
437
|
+
*/
|
|
438
|
+
const frozenStandIns = (() => {
|
|
439
|
+
const key = Symbol.for("@taco-design/model frozen node tables standing for shared ones");
|
|
440
|
+
const holder = globalThis;
|
|
441
|
+
return holder[key] ??= new WeakSet();
|
|
442
|
+
})();
|
|
443
|
+
/**
|
|
444
|
+
* The entries in which two tables of one family may differ, each key with
|
|
445
|
+
* its entry in `previous` and in `current` (undefined where a table has
|
|
446
|
+
* none). The keys are every key written on the way from one table to the
|
|
447
|
+
* other: they hold every key whose entries differ, and may hold keys whose
|
|
448
|
+
* entries are the same again. Undefined when `previous` and `current` are not
|
|
449
|
+
* shared tables of one family.
|
|
450
|
+
*
|
|
451
|
+
* Costs the steps from each table to the head, the steps between the two
|
|
452
|
+
* tables and twice the entries written on the way, not the size of the
|
|
453
|
+
* tables: `previous` is made the head to read its entries, then `current`.
|
|
454
|
+
*/
|
|
455
|
+
export function sharedTableDifferences(previous, current) {
|
|
456
|
+
const from = versions.get(previous);
|
|
457
|
+
const to = versions.get(current);
|
|
458
|
+
if (from === undefined || to === undefined || from.family !== to.family)
|
|
459
|
+
return undefined;
|
|
460
|
+
const keys = [];
|
|
461
|
+
if (from !== to) {
|
|
462
|
+
const seen = new Set();
|
|
463
|
+
const addKeys = (differences) => {
|
|
464
|
+
for (const key of differences.keys()) {
|
|
465
|
+
if (!seen.has(key)) {
|
|
466
|
+
seen.add(key);
|
|
467
|
+
keys.push(key);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
// Both walks go towards the head and meet at the first table on both.
|
|
472
|
+
const onPathFromPrevious = new Set();
|
|
473
|
+
for (let step = from; step !== undefined; step = step.next)
|
|
474
|
+
onPathFromPrevious.add(step);
|
|
475
|
+
let meeting = to;
|
|
476
|
+
while (!onPathFromPrevious.has(meeting)) {
|
|
477
|
+
addKeys(meeting.differences);
|
|
478
|
+
meeting = meeting.next;
|
|
479
|
+
}
|
|
480
|
+
for (let step = from; step !== meeting; step = step.next)
|
|
481
|
+
addKeys(step.differences);
|
|
482
|
+
}
|
|
483
|
+
const { live } = from.family;
|
|
484
|
+
const entryAt = (key) => {
|
|
485
|
+
const slot = ownSlot(live, key);
|
|
486
|
+
return slot === ABSENT ? undefined : slot;
|
|
487
|
+
};
|
|
488
|
+
makeHead(from);
|
|
489
|
+
const before = keys.map(entryAt);
|
|
490
|
+
makeHead(to);
|
|
491
|
+
const result = new Map();
|
|
492
|
+
keys.forEach((key, index) => result.set(key, [before[index], entryAt(key)]));
|
|
493
|
+
return result;
|
|
494
|
+
}
|
|
495
|
+
/** The shared table behind `table`, made the head of its family, or undefined. */
|
|
496
|
+
function headOf(table) {
|
|
497
|
+
if (frozenStandIns.has(table))
|
|
498
|
+
return undefined;
|
|
499
|
+
const version = versions.get(table);
|
|
500
|
+
if (version !== undefined)
|
|
501
|
+
makeHead(version);
|
|
502
|
+
return version;
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* A plain frozen copy of `table` when it is a shared table, for
|
|
506
|
+
* `structuredClone`, `postMessage` and other code that copies plain objects
|
|
507
|
+
* only; `table` itself otherwise.
|
|
508
|
+
*/
|
|
509
|
+
export function plainNodeTable(table) {
|
|
510
|
+
const version = headOf(table);
|
|
511
|
+
if (version === undefined)
|
|
512
|
+
return table;
|
|
513
|
+
const copy = copyTable(version.family.live);
|
|
514
|
+
if (listingWatch.listed !== null)
|
|
515
|
+
noteListing(Object.keys(copy).length);
|
|
516
|
+
return Object.freeze(copy);
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* The keys of `table`, as `Object.keys` lists them. For a shared table they
|
|
520
|
+
* are read from its family's plain object, at the cost of a plain table, not
|
|
521
|
+
* one entry at a time through the Proxy, which costs about three times as
|
|
522
|
+
* much.
|
|
523
|
+
*/
|
|
524
|
+
export function tableKeys(table) {
|
|
525
|
+
const version = headOf(table);
|
|
526
|
+
const keys = Object.keys(version === undefined ? table : version.family.live);
|
|
527
|
+
if (version !== undefined || frozenStandIns.has(table))
|
|
528
|
+
noteListing(keys.length);
|
|
529
|
+
return keys;
|
|
530
|
+
}
|
|
531
|
+
/** The values of `table`, as `Object.values` lists them (see `tableKeys`). */
|
|
532
|
+
export function tableValues(table) {
|
|
533
|
+
const version = headOf(table);
|
|
534
|
+
const values = Object.values(version === undefined ? table : version.family.live);
|
|
535
|
+
if (version !== undefined || frozenStandIns.has(table))
|
|
536
|
+
noteListing(values.length);
|
|
537
|
+
return values;
|
|
538
|
+
}
|
|
539
|
+
/** The entries of `table`, as `Object.entries` lists them (see `tableKeys`). */
|
|
540
|
+
export function tableEntries(table) {
|
|
541
|
+
const version = headOf(table);
|
|
542
|
+
const entries = Object.entries(version === undefined ? table : version.family.live);
|
|
543
|
+
if (version !== undefined || frozenStandIns.has(table))
|
|
544
|
+
noteListing(entries.length);
|
|
545
|
+
return entries;
|
|
546
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { type ComponentId, type ComponentPropertyId, type DesignDocument, type InstanceNode, type InstanceSlots, type LayerOverride, type NodeId, type SceneNode } from "./types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* The slot whose content holds a stored node: the instance that lists the
|
|
4
|
+
* slot host, the nested instance path key (`""` for the instance's own
|
|
5
|
+
* slot), the slot property ID, and the host's ID.
|
|
6
|
+
* @since 0.37.0
|
|
7
|
+
*/
|
|
8
|
+
export interface SlotOwner {
|
|
9
|
+
readonly instanceId: NodeId;
|
|
10
|
+
readonly pathKey: string;
|
|
11
|
+
readonly propertyId: ComponentPropertyId;
|
|
12
|
+
readonly hostId: NodeId;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The innermost slot whose content holds `id`: the parent table is walked up
|
|
16
|
+
* from `id` (the node itself included, so a host gives its own slot) to the
|
|
17
|
+
* first slot host. Undefined when `id` is in no slot's content. Costs one
|
|
18
|
+
* step per level, and the answers are kept per frozen node record, so the
|
|
19
|
+
* nodes passed on the way are answered at once later.
|
|
20
|
+
* @since 0.37.0
|
|
21
|
+
*/
|
|
22
|
+
export declare function slotOwnerOf(document: DesignDocument, id: NodeId): SlotOwner | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Every slot whose content holds `id`, innermost first: the owner of `id`,
|
|
25
|
+
* then the owner of that owner's instance, and so on up. Content of a slot
|
|
26
|
+
* inside the content of another slot changes both.
|
|
27
|
+
* @since 0.37.0
|
|
28
|
+
*/
|
|
29
|
+
export declare function slotOwnersOf(document: DesignDocument, id: NodeId): readonly SlotOwner[];
|
|
30
|
+
/**
|
|
31
|
+
* Whether any component of `document` has a slot property, so that slot
|
|
32
|
+
* content can exist. Reads the component definitions, never the layers, and
|
|
33
|
+
* is kept per frozen components record: code that follows slot owners on every
|
|
34
|
+
* edit asks this first, so a document without slots pays nothing for it.
|
|
35
|
+
* @since 0.37.0
|
|
36
|
+
*/
|
|
37
|
+
export declare function documentHasSlots(document: DesignDocument): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* The slots whose content changes when the document becomes `document` with
|
|
40
|
+
* the nodes `changedNodeIds` written, inserted or removed (a store's
|
|
41
|
+
* `ChangeSet.changedNodeIds`, or `ApplyOperationsResult.changes`): for each
|
|
42
|
+
* of those nodes still in `document`, every owner up the chain
|
|
43
|
+
* (`slotOwnersOf`). A removed node's parent counts for it, as its children
|
|
44
|
+
* changed. Each owner is listed once, innermost first per node, at one
|
|
45
|
+
* parent walk per node.
|
|
46
|
+
*
|
|
47
|
+
* Use this one, with the store's change set, to version slot content (such
|
|
48
|
+
* as an instance's content version when slots are resolved): undo and redo
|
|
49
|
+
* move the store to a stored revision, and `changedSlotOwners` of that
|
|
50
|
+
* revision names the edit that first made it, not the move.
|
|
51
|
+
* @since 0.37.0
|
|
52
|
+
*/
|
|
53
|
+
export declare function slotOwnersOfChanges(document: DesignDocument, changedNodeIds: Iterable<NodeId>): readonly SlotOwner[];
|
|
54
|
+
/**
|
|
55
|
+
* The slots whose content the edit that made `document` changed
|
|
56
|
+
* (`slotOwnersOfChanges` with that edit's changed nodes), kept for the node
|
|
57
|
+
* record. Empty for a document no edit made. `applyOperations` records the
|
|
58
|
+
* changed nodes, as it records parents, and the owners are found on the
|
|
59
|
+
* first call. A document reached by undo or redo is one an earlier edit
|
|
60
|
+
* made, so this names that edit's slots; use `slotOwnersOfChanges` with the
|
|
61
|
+
* store's change set to follow every move of the store.
|
|
62
|
+
* @since 0.37.0
|
|
63
|
+
*/
|
|
64
|
+
export declare function changedSlotOwners(document: DesignDocument): readonly SlotOwner[];
|
|
65
|
+
/**
|
|
66
|
+
* The slot properties of `componentId` that at least one frame of its tree
|
|
67
|
+
* binds (`FrameNode.slot`). A binding to a property that is missing or not a
|
|
68
|
+
* slot does not count. Walks the component's own tree (content of nested
|
|
69
|
+
* instances' slot hosts included) once per frozen node record, or once per
|
|
70
|
+
* `memo` for a record that can still change.
|
|
71
|
+
* @since 0.37.0
|
|
72
|
+
*/
|
|
73
|
+
export declare function boundSlotPropertyIds(document: DesignDocument, componentId: ComponentId, memo?: Map<ComponentId, ReadonlySet<ComponentPropertyId>>): ReadonlySet<ComponentPropertyId>;
|
|
74
|
+
/**
|
|
75
|
+
* One side of `remapSlots`: the document, the component the instance shows,
|
|
76
|
+
* its overrides (for swaps of nested instances), and the component a nested
|
|
77
|
+
* instance at a layer path shows when a property computes it.
|
|
78
|
+
* @since 0.37.0
|
|
79
|
+
*/
|
|
80
|
+
export interface SlotRemapSide {
|
|
81
|
+
readonly document: DesignDocument;
|
|
82
|
+
readonly componentId: ComponentId;
|
|
83
|
+
readonly overrides?: Readonly<Record<string, LayerOverride>>;
|
|
84
|
+
readonly shownComponentAt?: (path: readonly NodeId[]) => ComponentId | undefined;
|
|
85
|
+
/** Bound slot properties already found for this side's document; see `boundSlotPropertyIds`. */
|
|
86
|
+
readonly boundSlots?: Map<ComponentId, ReadonlySet<ComponentPropertyId>>;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* What `remapSlots` gives: the instance's slots on the new side (undefined
|
|
90
|
+
* when none is left) and the hosts to delete, each with its content.
|
|
91
|
+
* @since 0.37.0
|
|
92
|
+
*/
|
|
93
|
+
export interface SlotRemap {
|
|
94
|
+
readonly slots: InstanceSlots | undefined;
|
|
95
|
+
readonly removedHostIds: readonly NodeId[];
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Carries an instance's slot entries from one component (`from`) to another
|
|
99
|
+
* (`to`), at every nested path: a path is kept as it is for the same
|
|
100
|
+
* component and matched by layer name and position for another one
|
|
101
|
+
* (`remapLayerPath`), and must still lead to a nested instance on the `to`
|
|
102
|
+
* side. In the component shown at that path, an entry keeps its
|
|
103
|
+
* property when that is a slot some frame binds; otherwise, when the path
|
|
104
|
+
* shows another component than before, it moves to the bound slot property
|
|
105
|
+
* with the same name. An entry with no match, or one whose target another
|
|
106
|
+
* entry took first (in the order of `ownedChildIds`), is removed: its host
|
|
107
|
+
* is listed in `removedHostIds`. The same `slots` object comes back when
|
|
108
|
+
* nothing changes.
|
|
109
|
+
* @since 0.37.0
|
|
110
|
+
*/
|
|
111
|
+
export declare function remapSlots(instance: InstanceNode, from: SlotRemapSide, to: SlotRemapSide): SlotRemap;
|
|
112
|
+
/** Whether `node` is a slot host frame. @since 0.37.0 */
|
|
113
|
+
export declare const isSlotHost: (node: SceneNode | undefined) => boolean;
|