@pairdraw/scene-graph 0.19.1 → 0.20.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/dist/behaviours/index.d.ts +2 -2
- package/dist/behaviours/index.js +2 -2
- package/dist/behaviours/spec.d.ts +6 -1
- package/dist/behaviours/spec.d.ts.map +1 -1
- package/dist/behaviours/spec.js +62 -5
- package/dist/behaviours/spec.js.map +1 -1
- package/dist/canvas-layout.js +66 -0
- package/dist/canvas-layout.js.map +1 -0
- package/dist/components/index.d.ts +2 -2
- package/dist/components/index.js +2 -2
- package/dist/components/variants.d.ts +12 -1
- package/dist/components/variants.d.ts.map +1 -1
- package/dist/components/variants.js +30 -1
- package/dist/components/variants.js.map +1 -1
- package/dist/constants.d.ts +25 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +94 -1
- package/dist/constants.js.map +1 -1
- package/dist/coordinate.js +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +17 -1
- package/dist/copy.js.map +1 -1
- package/dist/css/index.d.ts +2 -2
- package/dist/css/index.js +2 -2
- package/dist/css/text.d.ts +22 -1
- package/dist/css/text.d.ts.map +1 -1
- package/dist/css/text.js +81 -1
- package/dist/css/text.js.map +1 -1
- package/dist/design-memory/index.d.ts +3 -0
- package/dist/design-memory/index.js +3 -0
- package/dist/design-memory/model.d.ts +33 -0
- package/dist/design-memory/model.d.ts.map +1 -0
- package/dist/design-memory/model.js +142 -0
- package/dist/design-memory/model.js.map +1 -0
- package/dist/design-memory/schema.d.ts +76 -0
- package/dist/design-memory/schema.d.ts.map +1 -0
- package/dist/design-memory/schema.js +72 -0
- package/dist/design-memory/schema.js.map +1 -0
- package/dist/effects.d.ts +7 -4
- package/dist/effects.d.ts.map +1 -1
- package/dist/effects.js +9 -4
- package/dist/effects.js.map +1 -1
- package/dist/empty-values.js +93 -0
- package/dist/empty-values.js.map +1 -0
- package/dist/fields/components.d.ts +9 -0
- package/dist/fields/components.d.ts.map +1 -0
- package/dist/fields/components.js +24 -0
- package/dist/fields/components.js.map +1 -0
- package/dist/fields/index.d.ts +3 -2
- package/dist/fields/index.js +3 -2
- package/dist/fields/layout.d.ts +3 -1
- package/dist/fields/layout.d.ts.map +1 -1
- package/dist/fields/layout.js +10 -1
- package/dist/fields/layout.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/graph.d.ts +8 -1
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +36 -7
- package/dist/graph.js.map +1 -1
- package/dist/grid-values.d.ts +10 -0
- package/dist/grid-values.d.ts.map +1 -0
- package/dist/grid-values.js +53 -0
- package/dist/grid-values.js.map +1 -0
- package/dist/group-bounds.d.ts +4 -1
- package/dist/group-bounds.d.ts.map +1 -1
- package/dist/group-bounds.js +11 -1
- package/dist/group-bounds.js.map +1 -1
- package/dist/hit-test/drop.d.ts +20 -0
- package/dist/hit-test/drop.d.ts.map +1 -0
- package/dist/hit-test/drop.js +54 -0
- package/dist/hit-test/drop.js.map +1 -0
- package/dist/hit-test/geometry.js +86 -0
- package/dist/hit-test/geometry.js.map +1 -0
- package/dist/hit-test/masks.d.ts +14 -0
- package/dist/hit-test/masks.d.ts.map +1 -0
- package/dist/hit-test/masks.js +41 -0
- package/dist/hit-test/masks.js.map +1 -0
- package/dist/hit-test/paint.d.ts +6 -0
- package/dist/hit-test/paint.d.ts.map +1 -0
- package/dist/hit-test/paint.js +19 -0
- package/dist/hit-test/paint.js.map +1 -0
- package/dist/hit-test.d.ts +4 -15
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +33 -178
- package/dist/hit-test.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/fields.d.ts.map +1 -1
- package/dist/instances/fields.js +2 -0
- package/dist/instances/fields.js.map +1 -1
- package/dist/instances/index.d.ts +2 -1
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +1 -1
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/source-walk.d.ts +12 -0
- package/dist/instances/source-walk.d.ts.map +1 -0
- package/dist/node-defaults.d.ts +21 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +63 -50
- package/dist/node-defaults.js.map +1 -1
- package/dist/plugin-data/fields.d.ts +75 -0
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +18 -1
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/preview.d.ts.map +1 -1
- package/dist/preview.js +2 -0
- package/dist/preview.js.map +1 -1
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +3 -6
- package/dist/resize.js.map +1 -1
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +28 -4
- package/dist/scaling/node.js.map +1 -1
- package/dist/shared-styles.d.ts +3 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +7 -1
- package/dist/shared-styles.js.map +1 -1
- package/dist/text/path.js +11 -0
- package/dist/text/path.js.map +1 -0
- package/dist/text/picture.d.ts.map +1 -1
- package/dist/text/picture.js +5 -5
- package/dist/text/picture.js.map +1 -1
- package/dist/token-sources/document.d.ts +26 -0
- package/dist/token-sources/document.d.ts.map +1 -0
- package/dist/token-sources/document.js +80 -0
- package/dist/token-sources/document.js.map +1 -0
- package/dist/token-sources/index.d.ts +3 -0
- package/dist/token-sources/index.js +3 -0
- package/dist/token-sources/schema.d.ts +43 -0
- package/dist/token-sources/schema.d.ts.map +1 -0
- package/dist/token-sources/schema.js +50 -0
- package/dist/token-sources/schema.js.map +1 -0
- package/dist/transfer/copied.d.ts +14 -0
- package/dist/transfer/copied.d.ts.map +1 -0
- package/dist/transfer/copied.js +52 -0
- package/dist/transfer/copied.js.map +1 -0
- package/dist/transfer/index.d.ts +2 -1
- package/dist/transfer/index.js +2 -1
- package/dist/transfer/nodes.js +7 -0
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/types.d.ts +104 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/undo.d.ts +6 -0
- package/dist/undo.d.ts.map +1 -1
- package/dist/undo.js +13 -0
- package/dist/undo.js.map +1 -1
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js.map +1 -1
- package/dist/variations/copy.d.ts +16 -0
- package/dist/variations/copy.d.ts.map +1 -0
- package/dist/variations/copy.js +78 -0
- package/dist/variations/copy.js.map +1 -0
- package/dist/variations/index.d.ts +6 -0
- package/dist/variations/index.js +6 -0
- package/dist/variations/match.d.ts +23 -0
- package/dist/variations/match.d.ts.map +1 -0
- package/dist/variations/match.js +80 -0
- package/dist/variations/match.js.map +1 -0
- package/dist/variations/model.d.ts +39 -0
- package/dist/variations/model.d.ts.map +1 -0
- package/dist/variations/model.js +90 -0
- package/dist/variations/model.js.map +1 -0
- package/dist/variations/pick.d.ts +43 -0
- package/dist/variations/pick.d.ts.map +1 -0
- package/dist/variations/pick.js +211 -0
- package/dist/variations/pick.js.map +1 -0
- package/dist/variations/schema.d.ts +27 -0
- package/dist/variations/schema.d.ts.map +1 -0
- package/dist/variations/schema.js +27 -0
- package/dist/variations/schema.js.map +1 -0
- package/package.json +21 -1
- package/src/behaviours/spec.ts +94 -4
- package/src/canvas-layout.ts +76 -0
- package/src/components/variants.ts +39 -0
- package/src/constants.ts +75 -0
- package/src/copy.ts +20 -1
- package/src/css/index.ts +12 -1
- package/src/css/text.ts +86 -0
- package/src/design-memory/index.ts +2 -0
- package/src/design-memory/model.ts +143 -0
- package/src/design-memory/schema.ts +84 -0
- package/src/effects.ts +16 -7
- package/src/empty-values.ts +101 -0
- package/src/fields/components.ts +21 -0
- package/src/fields/index.ts +1 -0
- package/src/fields/layout.ts +10 -0
- package/src/generated/shape-with-text.ts +28 -2
- package/src/graph.ts +48 -6
- package/src/grid-values.ts +47 -0
- package/src/group-bounds.ts +13 -0
- package/src/hit-test/drop.ts +90 -0
- package/src/hit-test/geometry.ts +131 -0
- package/src/hit-test/masks.ts +57 -0
- package/src/hit-test/paint.ts +24 -0
- package/src/hit-test.ts +78 -291
- package/src/instances/fields.ts +2 -0
- package/src/instances/index.ts +1 -0
- package/src/node-defaults.ts +71 -52
- package/src/plugin-data/fields.ts +18 -1
- package/src/preview.ts +2 -0
- package/src/resize.ts +3 -14
- package/src/scaling/node.ts +34 -5
- package/src/shared-styles.ts +8 -0
- package/src/text/path.ts +14 -0
- package/src/text/picture.ts +5 -2
- package/src/token-sources/document.ts +104 -0
- package/src/token-sources/index.ts +2 -0
- package/src/token-sources/schema.ts +59 -0
- package/src/transfer/copied.ts +72 -0
- package/src/transfer/index.ts +1 -0
- package/src/transfer/nodes.ts +10 -0
- package/src/types.ts +117 -14
- package/src/undo.ts +11 -0
- package/src/variables/index.ts +1 -4
- package/src/variations/copy.ts +89 -0
- package/src/variations/index.ts +5 -0
- package/src/variations/match.ts +100 -0
- package/src/variations/model.ts +132 -0
- package/src/variations/pick.ts +293 -0
- package/src/variations/schema.ts +29 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as v from "valibot";
|
|
2
|
+
//#region src/variations/schema.ts
|
|
3
|
+
/** The most versions one request makes. */
|
|
4
|
+
const VARIATION_MAX_COUNT = 8;
|
|
5
|
+
/** The longest label a version's name carries. */
|
|
6
|
+
const VARIATION_LABEL_MAX_LENGTH = 40;
|
|
7
|
+
/** The most layers one request copies, counting every version's layers. */
|
|
8
|
+
const VARIATION_MAX_LAYERS = 1e4;
|
|
9
|
+
/**
|
|
10
|
+
* What marks a section as a set of variations: the layer they are versions of, and a fingerprint
|
|
11
|
+
* of that layer when they were made, so a front end can say it changed since. The versions are
|
|
12
|
+
* the section's own children, so none of them needs a mark of its own.
|
|
13
|
+
*/
|
|
14
|
+
const variationSetSchema = v.object({
|
|
15
|
+
version: v.literal(1),
|
|
16
|
+
originalId: v.pipe(v.string(), v.nonEmpty()),
|
|
17
|
+
originalFingerprint: v.optional(v.string())
|
|
18
|
+
});
|
|
19
|
+
/**
|
|
20
|
+
* On a layer of a version, the id of the main component or component set of the original it
|
|
21
|
+
* stands in for: an instance of the component, or a frame of instances of the set's variants.
|
|
22
|
+
*/
|
|
23
|
+
const variationStandInSchema = v.pipe(v.string(), v.nonEmpty());
|
|
24
|
+
//#endregion
|
|
25
|
+
export { VARIATION_LABEL_MAX_LENGTH, VARIATION_MAX_COUNT, VARIATION_MAX_LAYERS, variationSetSchema, variationStandInSchema };
|
|
26
|
+
|
|
27
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","names":[],"sources":["../../src/variations/schema.ts"],"sourcesContent":["import * as v from 'valibot'\n\n/** The most versions one request makes. */\nexport const VARIATION_MAX_COUNT = 8\n\n/** The longest label a version's name carries. */\nexport const VARIATION_LABEL_MAX_LENGTH = 40\n\n/** The most layers one request copies, counting every version's layers. */\nexport const VARIATION_MAX_LAYERS = 10_000\n\n/**\n * What marks a section as a set of variations: the layer they are versions of, and a fingerprint\n * of that layer when they were made, so a front end can say it changed since. The versions are\n * the section's own children, so none of them needs a mark of its own.\n */\nexport const variationSetSchema = v.object({\n version: v.literal(1),\n originalId: v.pipe(v.string(), v.nonEmpty()),\n originalFingerprint: v.optional(v.string())\n})\n\nexport type VariationSet = v.InferOutput<typeof variationSetSchema>\n\n/**\n * On a layer of a version, the id of the main component or component set of the original it\n * stands in for: an instance of the component, or a frame of instances of the set's variants.\n */\nexport const variationStandInSchema = v.pipe(v.string(), v.nonEmpty())\n"],"mappings":";;;AAGA,MAAa,sBAAsB;;AAGnC,MAAa,6BAA6B;;AAG1C,MAAa,uBAAuB;;;;;;AAOpC,MAAa,qBAAqB,EAAE,OAAO;CACzC,SAAS,EAAE,QAAQ,CAAC;CACpB,YAAY,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,SAAS,CAAC;CAC3C,qBAAqB,EAAE,SAAS,EAAE,OAAO,CAAC;AAC5C,CAAC;;;;;AAQD,MAAa,yBAAyB,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,SAAS,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pairdraw/scene-graph",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -95,6 +95,11 @@
|
|
|
95
95
|
"import": "./dist/css/index.js",
|
|
96
96
|
"default": "./dist/css/index.js"
|
|
97
97
|
},
|
|
98
|
+
"./design-memory": {
|
|
99
|
+
"types": "./dist/design-memory/index.d.ts",
|
|
100
|
+
"import": "./dist/design-memory/index.js",
|
|
101
|
+
"default": "./dist/design-memory/index.js"
|
|
102
|
+
},
|
|
98
103
|
"./effects": {
|
|
99
104
|
"types": "./dist/effects.d.ts",
|
|
100
105
|
"import": "./dist/effects.js",
|
|
@@ -155,6 +160,11 @@
|
|
|
155
160
|
"import": "./dist/guides.js",
|
|
156
161
|
"default": "./dist/guides.js"
|
|
157
162
|
},
|
|
163
|
+
"./grid-values": {
|
|
164
|
+
"types": "./dist/grid-values.d.ts",
|
|
165
|
+
"import": "./dist/grid-values.js",
|
|
166
|
+
"default": "./dist/grid-values.js"
|
|
167
|
+
},
|
|
158
168
|
"./hit-test": {
|
|
159
169
|
"types": "./dist/hit-test.d.ts",
|
|
160
170
|
"import": "./dist/hit-test.js",
|
|
@@ -320,6 +330,11 @@
|
|
|
320
330
|
"import": "./dist/text/picture.js",
|
|
321
331
|
"default": "./dist/text/picture.js"
|
|
322
332
|
},
|
|
333
|
+
"./token-sources": {
|
|
334
|
+
"types": "./dist/token-sources/index.d.ts",
|
|
335
|
+
"import": "./dist/token-sources/index.js",
|
|
336
|
+
"default": "./dist/token-sources/index.js"
|
|
337
|
+
},
|
|
323
338
|
"./transfer": {
|
|
324
339
|
"types": "./dist/transfer/index.d.ts",
|
|
325
340
|
"import": "./dist/transfer/index.js",
|
|
@@ -345,6 +360,11 @@
|
|
|
345
360
|
"import": "./dist/variant-properties.js",
|
|
346
361
|
"default": "./dist/variant-properties.js"
|
|
347
362
|
},
|
|
363
|
+
"./variations": {
|
|
364
|
+
"types": "./dist/variations/index.d.ts",
|
|
365
|
+
"import": "./dist/variations/index.js",
|
|
366
|
+
"default": "./dist/variations/index.js"
|
|
367
|
+
},
|
|
348
368
|
"./vector-network": {
|
|
349
369
|
"types": "./dist/vector-network.d.ts",
|
|
350
370
|
"import": "./dist/vector-network.js",
|
package/src/behaviours/spec.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import { omitBy } from 'es-toolkit/object'
|
|
2
2
|
|
|
3
3
|
import type { SceneGraph } from '../graph'
|
|
4
|
+
import { slotPropertyId } from '../slots/frames'
|
|
4
5
|
import type { ComponentPropertyDefinition, SceneNode } from '../types'
|
|
5
6
|
import { behaviourContract } from './kinds'
|
|
6
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
behaviourProperties,
|
|
9
|
+
booleanBinding,
|
|
10
|
+
emptyBehaviour,
|
|
11
|
+
guessInteractionStates,
|
|
12
|
+
textBinding
|
|
13
|
+
} from './model'
|
|
7
14
|
import { isNumberRange, type Behaviour, type BehaviourSpec } from './schema'
|
|
8
15
|
|
|
9
16
|
/** Variant values that usually mean on, compared case-insensitively. */
|
|
@@ -58,13 +65,18 @@ function resolveValue(
|
|
|
58
65
|
binding: ValueBinding
|
|
59
66
|
): void {
|
|
60
67
|
const contract = behaviourContract(behaviour.kind)
|
|
61
|
-
const
|
|
62
|
-
const value = bindable.find((item) => item.id === valueId)
|
|
68
|
+
const value = contract.values.find((item) => item.id === valueId)
|
|
63
69
|
if (!value)
|
|
64
70
|
throw new Error(
|
|
65
|
-
`A ${behaviour.kind} has no
|
|
71
|
+
`A ${behaviour.kind} has no value "${valueId}"; it has ${listed(contract.values)}`
|
|
66
72
|
)
|
|
67
73
|
const name = typeof binding === 'string' ? binding : binding.property
|
|
74
|
+
if (value.type === 'choice') {
|
|
75
|
+
// A choice is which child of its slot is active, held by the variant property that draws it.
|
|
76
|
+
const definition = findProperty(properties, name, ['VARIANT'], valueId)
|
|
77
|
+
behaviour.booleans[valueId] = { propertyId: definition.id }
|
|
78
|
+
return
|
|
79
|
+
}
|
|
68
80
|
if (value.type === 'text') {
|
|
69
81
|
behaviour.texts[valueId] = { propertyId: findProperty(properties, name, ['TEXT'], valueId).id }
|
|
70
82
|
return
|
|
@@ -132,9 +144,87 @@ export function behaviourFromSpec(
|
|
|
132
144
|
behaviour.parts[partId] = id
|
|
133
145
|
}
|
|
134
146
|
if (spec.states !== undefined) resolveStates(behaviour, properties, spec.states)
|
|
147
|
+
validateChoiceValues(graph, owner, behaviour)
|
|
135
148
|
return behaviour
|
|
136
149
|
}
|
|
137
150
|
|
|
151
|
+
/** The words a layer reads, joined across its text. */
|
|
152
|
+
function textOf(graph: SceneGraph, node: SceneNode): string {
|
|
153
|
+
if (node.type === 'TEXT') return node.text
|
|
154
|
+
return graph
|
|
155
|
+
.getChildren(node.id)
|
|
156
|
+
.map((child) => textOf(graph, child))
|
|
157
|
+
.join(' ')
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** The frames in one variant bound to the tabs' list slot, without entering nested instances. */
|
|
161
|
+
function listFrames(graph: SceneGraph, variant: SceneNode, listPropertyId: string): SceneNode[] {
|
|
162
|
+
const frames: SceneNode[] = []
|
|
163
|
+
const visit = (node: SceneNode): void => {
|
|
164
|
+
for (const child of graph.getChildren(node.id)) {
|
|
165
|
+
if (slotPropertyId(child) === listPropertyId) frames.push(child)
|
|
166
|
+
else if (child.type !== 'INSTANCE') visit(child)
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
visit(variant)
|
|
170
|
+
return frames
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The trigger labels under the tabs' list slot, in layer order. Each variant must draw one,
|
|
175
|
+
* with the same triggers, so choosing a variant changes no more than the tab drawn.
|
|
176
|
+
*/
|
|
177
|
+
function triggerLabels(graph: SceneGraph, owner: SceneNode, listPropertyId: string): string[] {
|
|
178
|
+
const variants =
|
|
179
|
+
owner.type === 'COMPONENT_SET'
|
|
180
|
+
? graph.getChildren(owner.id).filter((child) => child.type === 'COMPONENT')
|
|
181
|
+
: [owner]
|
|
182
|
+
const frames: SceneNode[] = []
|
|
183
|
+
for (const variant of variants) {
|
|
184
|
+
const found = listFrames(graph, variant, listPropertyId)
|
|
185
|
+
if (!found.length)
|
|
186
|
+
throw new Error(`The variant "${variant.name}" has no frame bound to the tabs' list slot`)
|
|
187
|
+
frames.push(...found)
|
|
188
|
+
}
|
|
189
|
+
const labels = frames[0]
|
|
190
|
+
? graph.getChildren(frames[0].id).map((child) => textOf(graph, child))
|
|
191
|
+
: []
|
|
192
|
+
for (const frame of frames.slice(1)) {
|
|
193
|
+
const others = graph.getChildren(frame.id).map((child) => textOf(graph, child))
|
|
194
|
+
if (others.length !== labels.length || others.some((label, index) => label !== labels[index]))
|
|
195
|
+
throw new Error(
|
|
196
|
+
`The tabs' triggers differ between variants; ${frames[0] ? `the first has ${labels.join(', ')}` : 'they have none'}`
|
|
197
|
+
)
|
|
198
|
+
}
|
|
199
|
+
return labels
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Check the variant options a tabs' choice binds against the words its list draws. A choice
|
|
204
|
+
* picks the trigger of the same value, so the property must offer exactly those values.
|
|
205
|
+
*/
|
|
206
|
+
export function validateChoiceValues(
|
|
207
|
+
graph: SceneGraph,
|
|
208
|
+
owner: SceneNode,
|
|
209
|
+
behaviour: Behaviour
|
|
210
|
+
): void {
|
|
211
|
+
if (behaviour.kind !== 'tabs') return
|
|
212
|
+
const binding = booleanBinding(behaviour, 'value')
|
|
213
|
+
const listPropertyId = behaviour.parts.list
|
|
214
|
+
if (!binding || !listPropertyId) return
|
|
215
|
+
const definition = behaviourProperties(graph, owner).find(
|
|
216
|
+
(item) => item.id === binding.propertyId && item.type === 'VARIANT'
|
|
217
|
+
)
|
|
218
|
+
if (!definition) return
|
|
219
|
+
const labels = triggerLabels(graph, owner, listPropertyId)
|
|
220
|
+
const options = definition.variantOptions ?? []
|
|
221
|
+
if (labels.length === 0) return
|
|
222
|
+
if (labels.length !== options.length || labels.some((label, index) => label !== options[index]))
|
|
223
|
+
throw new Error(
|
|
224
|
+
`"${definition.name}" offers ${options.join(', ')}; the tabs' triggers are ${labels.join(', ')}`
|
|
225
|
+
)
|
|
226
|
+
}
|
|
227
|
+
|
|
138
228
|
function stripUndefined<T extends Record<string, unknown>>(record: T): Partial<T> {
|
|
139
229
|
return omitBy(record, (value) => value === undefined)
|
|
140
230
|
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { FRAME_LAYOUT_DEFAULTS } from './node-defaults'
|
|
2
|
+
import type { SceneNode } from './types'
|
|
3
|
+
|
|
4
|
+
type FrameLayoutField = keyof typeof FRAME_LAYOUT_DEFAULTS
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* `overrides` for a node of `type`, without the layout a page cannot hold. A document that names
|
|
8
|
+
* one on a page still loads: the field a page never stores simply does not arrive.
|
|
9
|
+
*/
|
|
10
|
+
export function pageLayoutOverrides(
|
|
11
|
+
type: SceneNode['type'],
|
|
12
|
+
overrides: Partial<SceneNode>
|
|
13
|
+
): Partial<SceneNode> {
|
|
14
|
+
if (type !== 'CANVAS') return overrides
|
|
15
|
+
const kept: Partial<SceneNode> = { ...overrides }
|
|
16
|
+
const taken = (field: FrameLayoutField) =>
|
|
17
|
+
kept[field] !== undefined && kept[field] !== FRAME_LAYOUT_DEFAULTS[field]
|
|
18
|
+
if (taken('layoutMode')) delete kept.layoutMode
|
|
19
|
+
if (taken('layoutDirection')) delete kept.layoutDirection
|
|
20
|
+
if (taken('layoutWrap')) delete kept.layoutWrap
|
|
21
|
+
if (taken('primaryAxisAlign')) delete kept.primaryAxisAlign
|
|
22
|
+
if (taken('counterAxisAlign')) delete kept.counterAxisAlign
|
|
23
|
+
if (taken('primaryAxisSizing')) delete kept.primaryAxisSizing
|
|
24
|
+
if (taken('counterAxisSizing')) delete kept.counterAxisSizing
|
|
25
|
+
if (taken('itemSpacing')) delete kept.itemSpacing
|
|
26
|
+
if (taken('counterAxisSpacing')) delete kept.counterAxisSpacing
|
|
27
|
+
if (taken('paddingTop')) delete kept.paddingTop
|
|
28
|
+
if (taken('paddingRight')) delete kept.paddingRight
|
|
29
|
+
if (taken('paddingBottom')) delete kept.paddingBottom
|
|
30
|
+
if (taken('paddingLeft')) delete kept.paddingLeft
|
|
31
|
+
return kept
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** A page was given auto layout or one of the properties an auto layout frame holds. */
|
|
35
|
+
export class CanvasLayoutError extends Error {
|
|
36
|
+
readonly pageId: string
|
|
37
|
+
readonly field: FrameLayoutField
|
|
38
|
+
|
|
39
|
+
constructor(pageId: string, field: FrameLayoutField) {
|
|
40
|
+
super(
|
|
41
|
+
`A page cannot hold auto layout (${field} was set on ${pageId}). Set auto layout on a frame inside the page.`
|
|
42
|
+
)
|
|
43
|
+
this.name = 'CanvasLayoutError'
|
|
44
|
+
this.pageId = pageId
|
|
45
|
+
this.field = field
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* `changes` without a geometry value that is not finite. A layout that cannot use its container
|
|
51
|
+
* can compute one; dropping it leaves the layer where it was rather than corrupting the document.
|
|
52
|
+
*/
|
|
53
|
+
export function finiteGeometry(changes: Partial<SceneNode>): Partial<SceneNode> {
|
|
54
|
+
const usable = (value: number | undefined) => value === undefined || Number.isFinite(value)
|
|
55
|
+
const { x, y, width, height } = changes
|
|
56
|
+
if (usable(x) && usable(y) && usable(width) && usable(height)) return changes
|
|
57
|
+
const kept: Partial<SceneNode> = { ...changes }
|
|
58
|
+
if (!usable(x)) delete kept.x
|
|
59
|
+
if (!usable(y)) delete kept.y
|
|
60
|
+
if (!usable(width)) delete kept.width
|
|
61
|
+
if (!usable(height)) delete kept.height
|
|
62
|
+
return kept
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Refuses the layout a page cannot hold, before it reaches the graph. Values equal to the ones a
|
|
67
|
+
* page already holds pass, so a reader that writes a whole node leaves a page alone.
|
|
68
|
+
*/
|
|
69
|
+
export function assertPageLayout(node: SceneNode, changes: Partial<SceneNode>): void {
|
|
70
|
+
if (node.type !== 'CANVAS') return
|
|
71
|
+
for (const [field, empty] of Object.entries(FRAME_LAYOUT_DEFAULTS)) {
|
|
72
|
+
const value = changes[field as FrameLayoutField]
|
|
73
|
+
if (value !== undefined && value !== empty)
|
|
74
|
+
throw new CanvasLayoutError(node.id, field as FrameLayoutField)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { SceneGraph } from '../graph'
|
|
2
|
+
import { instanceMainComponent } from '../instances/main-component'
|
|
2
3
|
import type { SceneNode } from '../types'
|
|
3
4
|
|
|
4
5
|
/** Variants in reading order on the canvas: top to bottom, then left to right, then by name. */
|
|
@@ -51,3 +52,41 @@ export function restingVariant(graph: SceneGraph, set: SceneNode): SceneNode | u
|
|
|
51
52
|
) ?? defaultVariant(graph, set)
|
|
52
53
|
)
|
|
53
54
|
}
|
|
55
|
+
|
|
56
|
+
/** The component set a variant belongs to, or the set of the variant an instance shows. */
|
|
57
|
+
function variantSetOf(graph: SceneGraph, node: SceneNode): SceneNode | undefined {
|
|
58
|
+
const variant = node.type === 'INSTANCE' ? instanceMainComponent(graph, node) : node
|
|
59
|
+
if (variant?.type !== 'COMPONENT' || !variant.parentId) return undefined
|
|
60
|
+
const set = graph.getNode(variant.parentId)
|
|
61
|
+
return set?.type === 'COMPONENT_SET' ? set : undefined
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The component sets whose variants a prototype's Change to on this layer may show, nearest
|
|
66
|
+
* first: the set of each variant, and of each instance of a variant, from the layer up.
|
|
67
|
+
*/
|
|
68
|
+
export function variantSetsAround(graph: SceneGraph, nodeId: string): SceneNode[] {
|
|
69
|
+
const sets: SceneNode[] = []
|
|
70
|
+
graph.closest(nodeId, (node) => {
|
|
71
|
+
const set = variantSetOf(graph, node)
|
|
72
|
+
if (set && !sets.includes(set)) sets.push(set)
|
|
73
|
+
return false
|
|
74
|
+
})
|
|
75
|
+
return sets
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The instance a Change to from this layer switches to another variant of `setId`: the layer
|
|
80
|
+
* itself or its nearest instance ancestor that shows a variant of that set. A main component has
|
|
81
|
+
* none, since it is the definition rather than a copy that changes.
|
|
82
|
+
*/
|
|
83
|
+
export function variantInstanceFor(
|
|
84
|
+
graph: SceneGraph,
|
|
85
|
+
nodeId: string,
|
|
86
|
+
setId: string
|
|
87
|
+
): SceneNode | undefined {
|
|
88
|
+
return graph.closest(
|
|
89
|
+
nodeId,
|
|
90
|
+
(node) => node.type === 'INSTANCE' && variantSetOf(graph, node)?.id === setId
|
|
91
|
+
)
|
|
92
|
+
}
|
package/src/constants.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import type { Color } from './primitives'
|
|
2
|
+
import type {
|
|
3
|
+
NoiseEffectParams,
|
|
4
|
+
OverflowDirection,
|
|
5
|
+
PrototypeEasingType,
|
|
6
|
+
ScrollBehavior,
|
|
7
|
+
TextureEffectParams
|
|
8
|
+
} from './types'
|
|
2
9
|
|
|
3
10
|
export const BLACK: Color = { r: 0, g: 0, b: 0, a: 1 }
|
|
4
11
|
export const WHITE: Color = { r: 1, g: 1, b: 1, a: 1 }
|
|
@@ -11,3 +18,71 @@ export const DEFAULT_STROKE_WEIGHT = 1
|
|
|
11
18
|
* instances of but never edits: a component library's definitions, and a `.pen` library's.
|
|
12
19
|
*/
|
|
13
20
|
export const LIBRARY_DEFINITIONS_PAGE_NAME = 'Pairdraw library definitions'
|
|
21
|
+
|
|
22
|
+
/** The grain of a noise effect whose values are missing: none of the layer covered. */
|
|
23
|
+
export const UNSET_NOISE: Readonly<NoiseEffectParams> = {
|
|
24
|
+
noiseType: 'MONOTONE',
|
|
25
|
+
noiseSize: { x: 1, y: 1 },
|
|
26
|
+
density: 0
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A new noise effect's grain, as Figma's effects panel starts one: black at 25%, fully dense. */
|
|
30
|
+
export const DEFAULT_NOISE: Readonly<NoiseEffectParams> = {
|
|
31
|
+
noiseType: 'MONOTONE',
|
|
32
|
+
noiseSize: { x: 0.5, y: 0.5 },
|
|
33
|
+
density: 1
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The colour a new noise effect's grains start in. */
|
|
37
|
+
export const DEFAULT_NOISE_COLOR: Readonly<Color> = { r: 0, g: 0, b: 0, a: 0.25 }
|
|
38
|
+
|
|
39
|
+
/** A new texture effect's grain, as Figma's effects panel starts one, with a radius of 4. */
|
|
40
|
+
export const DEFAULT_TEXTURE: Readonly<TextureEffectParams> = {
|
|
41
|
+
noiseSize: { x: 4, y: 4 },
|
|
42
|
+
clipToShape: false
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The grain of a texture effect whose values are missing; its radius of 0 moves nothing. */
|
|
46
|
+
export const UNSET_TEXTURE: Readonly<TextureEffectParams> = {
|
|
47
|
+
noiseSize: { x: 1, y: 1 },
|
|
48
|
+
clipToShape: true
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The ways a frame's content may scroll in play, as Figma's `overflowDirection` names them. */
|
|
52
|
+
export const OVERFLOW_DIRECTIONS = [
|
|
53
|
+
'NONE',
|
|
54
|
+
'HORIZONTAL',
|
|
55
|
+
'VERTICAL',
|
|
56
|
+
'BOTH'
|
|
57
|
+
] as const satisfies readonly OverflowDirection[]
|
|
58
|
+
|
|
59
|
+
/** How a layer moves while its parent frame scrolls in play, in Figma's names. */
|
|
60
|
+
export const SCROLL_BEHAVIORS = [
|
|
61
|
+
'SCROLLS',
|
|
62
|
+
'FIXED',
|
|
63
|
+
'STICKY_SCROLLS'
|
|
64
|
+
] as const satisfies readonly ScrollBehavior[]
|
|
65
|
+
|
|
66
|
+
/** The easings a prototype transition takes, Figma's presets, in the order its menu lists them. */
|
|
67
|
+
export const PROTOTYPE_EASINGS = [
|
|
68
|
+
'EASE_OUT',
|
|
69
|
+
'EASE_IN',
|
|
70
|
+
'EASE_IN_AND_OUT',
|
|
71
|
+
'LINEAR'
|
|
72
|
+
] as const satisfies readonly PrototypeEasingType[]
|
|
73
|
+
|
|
74
|
+
/** The easing a transition without one plays with, Figma's default. */
|
|
75
|
+
export const DEFAULT_PROTOTYPE_EASING: PrototypeEasingType = 'EASE_OUT'
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Each easing as the control points of its cubic Bézier, `[x1, y1, x2, y2]`: what a `.fig` file
|
|
79
|
+
* stores as an action's `easingFunction` and CSS reads as `cubic-bezier()`.
|
|
80
|
+
*/
|
|
81
|
+
export const PROTOTYPE_EASING_CURVES: Readonly<
|
|
82
|
+
Record<PrototypeEasingType, readonly [number, number, number, number]>
|
|
83
|
+
> = {
|
|
84
|
+
LINEAR: [0, 0, 1, 1],
|
|
85
|
+
EASE_IN: [0.42, 0, 1, 1],
|
|
86
|
+
EASE_OUT: [0, 0, 0.58, 1],
|
|
87
|
+
EASE_IN_AND_OUT: [0.42, 0, 0.58, 1]
|
|
88
|
+
}
|
package/src/copy.ts
CHANGED
|
@@ -56,9 +56,28 @@ export function copyStroke(s: Stroke): Stroke {
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
export function copyEffect(e: Effect): Effect {
|
|
59
|
-
if (e.type === 'SHADER') return structuredClone(e)
|
|
60
59
|
const normalized = normalizeEffects([e])[0] ?? e
|
|
61
60
|
if (normalized.type === 'SHADER') return structuredClone(normalized)
|
|
61
|
+
if (normalized.type === 'NOISE') {
|
|
62
|
+
return {
|
|
63
|
+
...normalized,
|
|
64
|
+
color: { ...normalized.color },
|
|
65
|
+
noise: {
|
|
66
|
+
...normalized.noise,
|
|
67
|
+
noiseSize: { ...normalized.noise.noiseSize },
|
|
68
|
+
secondaryColor: normalized.noise.secondaryColor
|
|
69
|
+
? { ...normalized.noise.secondaryColor }
|
|
70
|
+
: undefined
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (normalized.type === 'TEXTURE') {
|
|
75
|
+
return {
|
|
76
|
+
...normalized,
|
|
77
|
+
color: { ...normalized.color },
|
|
78
|
+
texture: { ...normalized.texture, noiseSize: { ...normalized.texture.noiseSize } }
|
|
79
|
+
}
|
|
80
|
+
}
|
|
62
81
|
return {
|
|
63
82
|
...normalized,
|
|
64
83
|
color: { ...normalized.color },
|
package/src/css/index.ts
CHANGED
|
@@ -7,5 +7,16 @@ export {
|
|
|
7
7
|
parseCSSJustifyContent
|
|
8
8
|
} from './layout'
|
|
9
9
|
export { parseCSSShadows } from './shadows'
|
|
10
|
-
export {
|
|
10
|
+
export {
|
|
11
|
+
parseCSSFontFamilies,
|
|
12
|
+
parseCSSFontFamily,
|
|
13
|
+
parseCSSLetterSpacing,
|
|
14
|
+
parseCSSLineHeight,
|
|
15
|
+
GENERIC_FONT_FAMILIES,
|
|
16
|
+
GENERIC_FONT_FAMILY_CLASS,
|
|
17
|
+
GENERIC_FONT_FAMILIES_BY_CLASS,
|
|
18
|
+
genericFontFamilyClass,
|
|
19
|
+
isGenericFontFamily,
|
|
20
|
+
type GenericFontClass
|
|
21
|
+
} from './text'
|
|
11
22
|
export { parseCSSColor, parseCSSNumber, splitCSSValue } from './values'
|
package/src/css/text.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import valueParser from 'postcss-value-parser'
|
|
2
2
|
|
|
3
|
+
import { DEFAULT_FONT_FAMILY } from '../constants'
|
|
3
4
|
import { percentLength, pixelLength } from '../text/lengths'
|
|
4
5
|
import type { TextLength } from '../types'
|
|
5
6
|
import { parseCSSNumber } from './values'
|
|
@@ -45,6 +46,91 @@ export function parseCSSFontFamily(value: string | undefined): string | null {
|
|
|
45
46
|
return words.trim().replace(/\s+/gu, ' ') || null
|
|
46
47
|
}
|
|
47
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Every family of a CSS `font-family` list, in order, without quotes and escapes. A quoted family
|
|
51
|
+
* is one name; an unquoted one joins its words with single spaces.
|
|
52
|
+
*/
|
|
53
|
+
export function parseCSSFontFamilies(value: string | undefined): string[] {
|
|
54
|
+
const text = value?.trim()
|
|
55
|
+
if (!text) return []
|
|
56
|
+
const families: string[] = []
|
|
57
|
+
let current: valueParser.Node[] = []
|
|
58
|
+
for (const node of valueParser(text).nodes) {
|
|
59
|
+
if (node.type === 'div' && node.value === ',') {
|
|
60
|
+
families.push(valueParser.stringify(current))
|
|
61
|
+
current = []
|
|
62
|
+
} else {
|
|
63
|
+
current.push(node)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
families.push(valueParser.stringify(current))
|
|
67
|
+
return families.flatMap((family) => {
|
|
68
|
+
const parsed = parseCSSFontFamily(family)
|
|
69
|
+
return parsed ? [parsed] : []
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** CSS generic `font-family` keywords: names no font provider serves, drawn by a concrete face instead. */
|
|
74
|
+
export const GENERIC_FONT_FAMILIES: ReadonlySet<string> = new Set([
|
|
75
|
+
'serif',
|
|
76
|
+
'sans-serif',
|
|
77
|
+
'monospace',
|
|
78
|
+
'cursive',
|
|
79
|
+
'fantasy',
|
|
80
|
+
'system-ui',
|
|
81
|
+
'ui-serif',
|
|
82
|
+
'ui-sans-serif',
|
|
83
|
+
'ui-monospace',
|
|
84
|
+
'ui-rounded',
|
|
85
|
+
'-apple-system',
|
|
86
|
+
'blinkmacsystemfont',
|
|
87
|
+
'math',
|
|
88
|
+
'emoji',
|
|
89
|
+
'fangsong'
|
|
90
|
+
])
|
|
91
|
+
|
|
92
|
+
/** Whether `family` is a CSS generic keyword rather than a face a provider or the host can load. */
|
|
93
|
+
export function isGenericFontFamily(family: string): boolean {
|
|
94
|
+
return GENERIC_FONT_FAMILIES.has(family.trim().toLowerCase())
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The class a CSS generic keyword stands for, when a concrete face of it can draw the text. */
|
|
98
|
+
export type GenericFontClass = 'sans-serif' | 'serif' | 'monospace'
|
|
99
|
+
|
|
100
|
+
/** CSS generic keywords, by the concrete face class they stand for. */
|
|
101
|
+
export const GENERIC_FONT_FAMILY_CLASS: Readonly<Record<string, GenericFontClass>> = {
|
|
102
|
+
'sans-serif': 'sans-serif',
|
|
103
|
+
'ui-sans-serif': 'sans-serif',
|
|
104
|
+
'system-ui': 'sans-serif',
|
|
105
|
+
'-apple-system': 'sans-serif',
|
|
106
|
+
blinkmacsystemfont: 'sans-serif',
|
|
107
|
+
serif: 'serif',
|
|
108
|
+
'ui-serif': 'serif',
|
|
109
|
+
monospace: 'monospace',
|
|
110
|
+
'ui-monospace': 'monospace'
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Known system faces of each generic class, best first. The app bundles Inter for sans-serif. */
|
|
114
|
+
export const GENERIC_FONT_FAMILIES_BY_CLASS = {
|
|
115
|
+
'sans-serif': [DEFAULT_FONT_FAMILY],
|
|
116
|
+
serif: ['Georgia', 'Times New Roman', 'Times', 'DejaVu Serif', 'Liberation Serif', 'Noto Serif'],
|
|
117
|
+
monospace: [
|
|
118
|
+
'Menlo',
|
|
119
|
+
'SF Mono',
|
|
120
|
+
'Consolas',
|
|
121
|
+
'Courier New',
|
|
122
|
+
'DejaVu Sans Mono',
|
|
123
|
+
'Liberation Mono',
|
|
124
|
+
'Noto Sans Mono',
|
|
125
|
+
'Ubuntu Mono'
|
|
126
|
+
]
|
|
127
|
+
} as const satisfies Record<GenericFontClass, readonly string[]>
|
|
128
|
+
|
|
129
|
+
/** The concrete face class `family` stands for, or null when its name is no CSS generic keyword. */
|
|
130
|
+
export function genericFontFamilyClass(family: string): GenericFontClass | null {
|
|
131
|
+
return GENERIC_FONT_FAMILY_CLASS[family.trim().toLowerCase()] ?? null
|
|
132
|
+
}
|
|
133
|
+
|
|
48
134
|
/** A CSS dimension's number and lowercase unit, or null. */
|
|
49
135
|
function dimension(value: string): { number: number; unit: string } | null {
|
|
50
136
|
const parsed = valueParser.unit(value)
|