@open-pencil/scene-graph 0.15.1 → 0.16.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/LICENSE +21 -0
- package/README.md +1 -1
- package/dist/chunks/behaviours/kinds.js +273 -0
- package/dist/chunks/behaviours/kinds.js.map +1 -0
- package/dist/chunks/behaviours/layers.js +607 -0
- package/dist/chunks/behaviours/layers.js.map +1 -0
- package/dist/chunks/behaviours/spec.js +181 -0
- package/dist/chunks/behaviours/spec.js.map +1 -0
- package/dist/chunks/buffered-events.js +57 -0
- package/dist/chunks/buffered-events.js.map +1 -0
- package/dist/chunks/color/contrast.js +57 -0
- package/dist/chunks/color/contrast.js.map +1 -0
- package/dist/chunks/{components/properties.js → color/index.js} +577 -522
- package/dist/chunks/color/index.js.map +1 -0
- package/dist/chunks/comments/document.js +32 -0
- package/dist/chunks/comments/document.js.map +1 -0
- package/dist/chunks/comments/list.js +182 -0
- package/dist/chunks/comments/list.js.map +1 -0
- package/dist/chunks/comments/merge.js +58 -0
- package/dist/chunks/comments/merge.js.map +1 -0
- package/dist/chunks/components/exposure.js +2291 -0
- package/dist/chunks/components/exposure.js.map +1 -0
- package/dist/chunks/components/variants.js +32 -0
- package/dist/chunks/components/variants.js.map +1 -0
- package/dist/chunks/coordinate.js +93 -55
- package/dist/chunks/coordinate.js.map +1 -1
- package/dist/chunks/css/grid.js +94 -0
- package/dist/chunks/css/grid.js.map +1 -0
- package/dist/chunks/css/index.js +160 -0
- package/dist/chunks/css/index.js.map +1 -0
- package/dist/chunks/fields/geometry.js +14 -0
- package/dist/chunks/fields/geometry.js.map +1 -0
- package/dist/chunks/group-bounds.js +104 -0
- package/dist/chunks/group-bounds.js.map +1 -0
- package/dist/chunks/icons/glyph.js +147 -0
- package/dist/chunks/icons/glyph.js.map +1 -0
- package/dist/chunks/index.js +1724 -663
- package/dist/chunks/index.js.map +1 -1
- package/dist/color.d.ts +72 -0
- package/dist/color.d.ts.map +1 -0
- package/dist/color.js +3 -0
- package/dist/constants.d.ts +2 -2
- package/dist/constants.js +2 -2
- package/dist/constants2.d.ts +3 -1
- package/dist/constants2.d.ts.map +1 -1
- package/dist/coordinate.d.ts +2 -448
- package/dist/coordinate.js +2 -2
- package/dist/copy.d.ts +1 -1
- package/dist/copy.js +1 -1
- package/dist/css.d.ts +39 -0
- package/dist/css.d.ts.map +1 -0
- package/dist/css.js +3 -0
- package/dist/geometry.d.ts +1 -1
- package/dist/geometry.js +1 -1
- package/dist/gradient.d.ts +51 -0
- package/dist/gradient.d.ts.map +1 -0
- package/dist/gradient.js +132 -0
- package/dist/gradient.js.map +1 -0
- package/dist/guides.d.ts +2 -2
- package/dist/guides.js +39 -0
- package/dist/guides.js.map +1 -0
- package/dist/hit-test.d.ts +2 -9
- package/dist/hit-test.js +176 -34
- package/dist/hit-test.js.map +1 -1
- package/dist/index.d.ts +4 -8
- package/dist/index.js +19 -5
- package/dist/instances.d.ts +2 -2
- package/dist/instances.js +2 -2
- package/dist/layout.d.ts +2 -0
- package/dist/layout.js +4 -0
- package/dist/node-defaults.d.ts +2 -8
- package/dist/node-defaults.js +2 -2
- package/dist/order-keys.d.ts +39 -0
- package/dist/order-keys.d.ts.map +1 -0
- package/dist/order-keys.js +136 -0
- package/dist/order-keys.js.map +1 -0
- package/dist/parse-path.d.ts +7 -1
- package/dist/parse-path.d.ts.map +1 -1
- package/dist/parse-path.js +6 -2
- package/dist/parse-path.js.map +1 -1
- package/dist/polygon.d.ts +53 -0
- package/dist/polygon.d.ts.map +1 -0
- package/dist/polygon.js +205 -0
- package/dist/polygon.js.map +1 -0
- package/dist/preview.d.ts +1 -1
- package/dist/random.d.ts +7 -0
- package/dist/random.d.ts.map +1 -0
- package/dist/random.js +2 -0
- package/dist/resize.d.ts +11 -2
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +2 -184
- package/dist/snap.d.ts +1 -1
- package/dist/text-direction.d.ts +13 -0
- package/dist/text-direction.d.ts.map +1 -0
- package/dist/text-direction.js +31 -0
- package/dist/text-direction.js.map +1 -0
- package/dist/text-picture.d.ts +1 -1
- package/dist/text-picture.d.ts.map +1 -1
- package/dist/text.d.ts +2 -0
- package/dist/text.js +3 -0
- package/dist/types.d.ts +2121 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +2 -1
- package/dist/types2.d.ts +2 -2
- package/dist/undo.d.ts +2 -2
- package/dist/undo.js +2 -2
- package/dist/undo2.d.ts +29 -1
- package/dist/undo2.d.ts.map +1 -1
- package/dist/variables.d.ts +2 -29
- package/dist/variables.js +2 -2
- package/dist/variant-properties.d.ts +9 -1
- package/dist/variant-properties.d.ts.map +1 -1
- package/dist/variant-properties.js +36 -1
- package/dist/variant-properties.js.map +1 -1
- package/dist/vector-network.d.ts +1 -1
- package/dist/vector-network.js +1 -1
- package/package.json +60 -9
- package/src/behaviours/kinds.ts +185 -0
- package/src/behaviours/layers.ts +51 -0
- package/src/behaviours/model.ts +192 -0
- package/src/behaviours/schema.ts +58 -0
- package/src/behaviours/spec.ts +223 -0
- package/src/buffered-events.ts +64 -0
- package/src/color/contrast.ts +31 -0
- package/src/color/index.ts +88 -0
- package/src/color/management.ts +210 -0
- package/src/color/normalize.ts +7 -0
- package/src/color/okhcl.ts +201 -0
- package/src/comments/document.ts +51 -0
- package/src/comments/list.ts +52 -0
- package/src/comments/merge.ts +63 -0
- package/src/comments/threads.ts +156 -0
- package/src/components/exposure.ts +61 -0
- package/src/components/properties.ts +18 -15
- package/src/components/variants.ts +53 -0
- package/src/constants.ts +2 -0
- package/src/coordinate.ts +100 -43
- package/src/copy.ts +17 -6
- package/src/css/grid.ts +70 -0
- package/src/css/index.ts +9 -0
- package/src/css/layout.ts +53 -0
- package/src/css/shadows.ts +68 -0
- package/src/css/values.ts +42 -0
- package/src/export-format.ts +12 -0
- package/src/fields/geometry.ts +11 -0
- package/src/fields/text.ts +35 -0
- package/src/gradient.ts +166 -0
- package/src/group-bounds.ts +131 -0
- package/src/guides.ts +39 -0
- package/src/hit-test.ts +262 -46
- package/src/icons/glyph.ts +128 -0
- package/src/icons/model.ts +54 -0
- package/src/icons/schema.ts +18 -0
- package/src/index.ts +347 -57
- package/src/instance-overrides.ts +27 -0
- package/src/instances/content-size.ts +58 -0
- package/src/instances/copies.ts +239 -0
- package/src/instances/cycles.ts +73 -0
- package/src/instances/fields.ts +143 -0
- package/src/instances/main-component.ts +19 -0
- package/src/instances/placement.ts +29 -0
- package/src/instances/source-walk.ts +41 -0
- package/src/instances/sync.ts +600 -0
- package/src/instances/vector-geometry.ts +81 -0
- package/src/instances.ts +125 -330
- package/src/layout/aspect-ratio.ts +51 -0
- package/src/{layout-guides.ts → layout/guides.ts} +41 -12
- package/src/layout/index.ts +4 -0
- package/src/layout/keywords.ts +73 -0
- package/src/layout/sizing.ts +196 -0
- package/src/mutation-impact.ts +25 -10
- package/src/node-defaults.ts +50 -2
- package/src/order-keys.ts +162 -0
- package/src/parse-path.ts +10 -2
- package/src/plugin-data/field.ts +131 -0
- package/src/plugin-data/fields.ts +246 -0
- package/src/polygon.ts +211 -0
- package/src/preview.ts +1 -1
- package/src/random.ts +28 -0
- package/src/resize.ts +99 -12
- package/src/scaling/index.ts +2 -0
- package/src/scaling/node.ts +164 -0
- package/src/scaling/tree.ts +50 -0
- package/src/shaders/model.ts +98 -0
- package/src/shaders/schema.ts +48 -0
- package/src/shared-styles.ts +5 -2
- package/src/slots/authoring.ts +101 -0
- package/src/slots/content.ts +124 -0
- package/src/slots/frames.ts +160 -0
- package/src/slots/limits.ts +49 -0
- package/src/text/direction.ts +55 -0
- package/src/text/index.ts +3 -0
- package/src/text/lists.ts +138 -0
- package/src/text/paragraphs.ts +241 -0
- package/src/{text-picture.ts → text/picture.ts} +21 -29
- package/src/text/references.ts +50 -0
- package/src/transfer/apply.ts +84 -0
- package/src/transfer/history.ts +49 -0
- package/src/transfer/identity.ts +16 -0
- package/src/transfer/index.ts +6 -0
- package/src/transfer/nodes.ts +120 -0
- package/src/transfer/plan.ts +130 -0
- package/src/transfer/references.ts +9 -0
- package/src/transfer/remove.ts +39 -0
- package/src/transfer/validation.ts +59 -0
- package/src/transfer/variables.ts +82 -0
- package/src/types.ts +257 -13
- package/src/undo.ts +54 -2
- package/src/variables/bindings.ts +224 -0
- package/src/variables/fields.ts +43 -0
- package/src/variables/token.ts +14 -0
- package/src/variables/units.ts +15 -0
- package/src/variables.ts +118 -69
- package/src/variant-properties.ts +50 -0
- package/dist/chunks/components/properties.js.map +0 -1
- package/dist/chunks/export-scale.js +0 -19
- package/dist/chunks/export-scale.js.map +0 -1
- package/dist/coordinate.d.ts.map +0 -1
- package/dist/coordinate2.d.ts +0 -2
- package/dist/guides2.d.ts +0 -11
- package/dist/guides2.d.ts.map +0 -1
- package/dist/hit-test.d.ts.map +0 -1
- package/dist/layout-guides.d.ts +0 -2
- package/dist/layout-guides.js +0 -2
- package/dist/layout-guides2.d.ts +0 -16
- package/dist/layout-guides2.d.ts.map +0 -1
- package/dist/node-defaults.d.ts.map +0 -1
- package/dist/preview2.d.ts +0 -12
- package/dist/preview2.d.ts.map +0 -1
- package/dist/resize.js.map +0 -1
- package/dist/variables.d.ts.map +0 -1
- package/dist/vector-network2.d.ts +0 -32
- package/dist/vector-network2.d.ts.map +0 -1
- package/src/instance-overrides.test.ts +0 -67
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { SceneGraph } from '../index'
|
|
2
|
+
import { getInstanceOverride, setInstanceOverride } from '../instance-overrides'
|
|
3
|
+
import { cloneChildrenWithMapping } from '../instances/sync'
|
|
4
|
+
import type { SceneNode } from '../types'
|
|
5
|
+
import { ownsSlotContent, slotPropertyId } from './frames'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Where layers may be added, moved, or removed under a parent:
|
|
9
|
+
* - `free`: outside any instance, or inside a component definition;
|
|
10
|
+
* - `slot`: inside a slot of an instance, whose content the instance owns or can claim;
|
|
11
|
+
* - `locked`: anywhere else inside an instance, whose layers come from its component.
|
|
12
|
+
*/
|
|
13
|
+
export type SlotScope =
|
|
14
|
+
| { kind: 'free' }
|
|
15
|
+
| { kind: 'slot'; frame: SceneNode; instance: SceneNode; propertyId: string }
|
|
16
|
+
| { kind: 'locked'; instance: SceneNode }
|
|
17
|
+
|
|
18
|
+
function parentOf(graph: SceneGraph, node: SceneNode): SceneNode | undefined {
|
|
19
|
+
return node.parentId ? graph.nodes.get(node.parentId) : undefined
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function nearestInstance(graph: SceneGraph, node: SceneNode): SceneNode | undefined {
|
|
23
|
+
let current = parentOf(graph, node)
|
|
24
|
+
while (current && current.type !== 'INSTANCE') current = parentOf(graph, current)
|
|
25
|
+
return current
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function slotScope(graph: SceneGraph, parentId: string): SlotScope {
|
|
29
|
+
let current = graph.nodes.get(parentId)
|
|
30
|
+
while (current) {
|
|
31
|
+
const propertyId = slotPropertyId(current)
|
|
32
|
+
if (propertyId) {
|
|
33
|
+
const instance = nearestInstance(graph, current)
|
|
34
|
+
return instance ? { kind: 'slot', frame: current, instance, propertyId } : { kind: 'free' }
|
|
35
|
+
}
|
|
36
|
+
if (current.type === 'INSTANCE') return { kind: 'locked', instance: current }
|
|
37
|
+
current = parentOf(graph, current)
|
|
38
|
+
}
|
|
39
|
+
return { kind: 'free' }
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Every layer below `node`; `enterInstances` also walks the layers of nested instances. */
|
|
43
|
+
function descendants(graph: SceneGraph, node: SceneNode, enterInstances: boolean): SceneNode[] {
|
|
44
|
+
const result: SceneNode[] = []
|
|
45
|
+
const visit = (parent: SceneNode): void => {
|
|
46
|
+
for (const childId of parent.childIds) {
|
|
47
|
+
const child = graph.nodes.get(childId)
|
|
48
|
+
if (!child) continue
|
|
49
|
+
result.push(child)
|
|
50
|
+
if (enterInstances || child.type !== 'INSTANCE') visit(child)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
visit(node)
|
|
54
|
+
return result
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Make the instance own its slot's content, as Figma does on the first edit: the layers
|
|
59
|
+
* stay where they are, but stop following the component. Overrides the instance held for
|
|
60
|
+
* them become their own values, or move to the nested instance they belong to.
|
|
61
|
+
*/
|
|
62
|
+
export function claimSlotContent(
|
|
63
|
+
graph: SceneGraph,
|
|
64
|
+
scope: Extract<SlotScope, { kind: 'slot' }>
|
|
65
|
+
): void {
|
|
66
|
+
const { frame, instance, propertyId } = scope
|
|
67
|
+
if (ownsSlotContent(graph, frame, propertyId)) return
|
|
68
|
+
const overrides = instance.instanceOverrides
|
|
69
|
+
for (const node of descendants(graph, frame, false)) {
|
|
70
|
+
overrides.descendants.delete(node.id)
|
|
71
|
+
if (node.type !== 'INSTANCE') {
|
|
72
|
+
graph.updateNode(node.id, { componentId: null })
|
|
73
|
+
continue
|
|
74
|
+
}
|
|
75
|
+
// A nested instance keeps the overrides its former owner applied inside it.
|
|
76
|
+
for (const inner of descendants(graph, node, true)) {
|
|
77
|
+
const fields = overrides.descendants.get(inner.id)
|
|
78
|
+
if (!fields) continue
|
|
79
|
+
for (const [field, value] of fields)
|
|
80
|
+
if (field !== 'sourceComponentId')
|
|
81
|
+
setInstanceOverride(node.instanceOverrides, node.id, inner.id, field, value)
|
|
82
|
+
overrides.descendants.delete(inner.id)
|
|
83
|
+
}
|
|
84
|
+
graph.updateNode(node.id, { instanceOverrides: node.instanceOverrides })
|
|
85
|
+
}
|
|
86
|
+
graph.updateNode(instance.id, {
|
|
87
|
+
instanceOverrides: overrides,
|
|
88
|
+
componentPropertyAssignments: { ...instance.componentPropertyAssignments, [propertyId]: '' }
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The component layer an instance layer was cloned from. */
|
|
93
|
+
function componentSource(instance: SceneNode, node: SceneNode): string | null {
|
|
94
|
+
const mapped = getInstanceOverride(
|
|
95
|
+
instance.instanceOverrides,
|
|
96
|
+
instance.id,
|
|
97
|
+
node.id,
|
|
98
|
+
'sourceComponentId'
|
|
99
|
+
)
|
|
100
|
+
return typeof mapped === 'string' ? mapped : node.componentId
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Return the slot to its component's content: the instance stops owning it. */
|
|
104
|
+
export function resetSlotContent(
|
|
105
|
+
graph: SceneGraph,
|
|
106
|
+
scope: Extract<SlotScope, { kind: 'slot' }>
|
|
107
|
+
): void {
|
|
108
|
+
const { frame, instance, propertyId } = scope
|
|
109
|
+
for (const childId of Array.from(frame.childIds)) graph.deleteNode(childId)
|
|
110
|
+
const assignments = { ...instance.componentPropertyAssignments }
|
|
111
|
+
Reflect.deleteProperty(assignments, propertyId)
|
|
112
|
+
graph.updateNode(instance.id, { componentPropertyAssignments: assignments })
|
|
113
|
+
const source = componentSource(instance, frame)
|
|
114
|
+
if (source && graph.nodes.has(source)) cloneChildrenWithMapping(graph, source, frame.id)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Remove everything from the slot; the instance keeps owning it, now empty. */
|
|
118
|
+
export function clearSlotContent(
|
|
119
|
+
graph: SceneGraph,
|
|
120
|
+
scope: Extract<SlotScope, { kind: 'slot' }>
|
|
121
|
+
): void {
|
|
122
|
+
claimSlotContent(graph, scope)
|
|
123
|
+
for (const childId of Array.from(scope.frame.childIds)) graph.deleteNode(childId)
|
|
124
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import type { SceneGraph } from '../index'
|
|
2
|
+
import { instanceMainComponent } from '../instances/main-component'
|
|
3
|
+
import type { SceneNode } from '../types'
|
|
4
|
+
|
|
5
|
+
/** The slot property a frame holds the content of, if it is a slot. */
|
|
6
|
+
export function slotPropertyId(node: SceneNode): string | undefined {
|
|
7
|
+
return node.componentPropertyReferences.find((reference) => reference.field === 'SLOT_CONTENT')
|
|
8
|
+
?.propertyId
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Whether this slot frame's children belong to its instance rather than its component: the
|
|
13
|
+
* nearest enclosing instance assigns the slot. Such content is never synced from the component.
|
|
14
|
+
*/
|
|
15
|
+
export function ownsSlotContent(
|
|
16
|
+
graph: SceneGraph,
|
|
17
|
+
node: SceneNode,
|
|
18
|
+
propertyId = slotPropertyId(node)
|
|
19
|
+
): boolean {
|
|
20
|
+
if (!propertyId) return false
|
|
21
|
+
let owner = node.parentId ? graph.nodes.get(node.parentId) : undefined
|
|
22
|
+
while (owner && owner.type !== 'INSTANCE')
|
|
23
|
+
owner = owner.parentId ? graph.nodes.get(owner.parentId) : undefined
|
|
24
|
+
return !!owner && Object.hasOwn(owner.componentPropertyAssignments, propertyId)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The name of a slot property as the instance's component (or the component's set) defines it. */
|
|
28
|
+
function slotName(graph: SceneGraph, instance: SceneNode, propertyId: string) {
|
|
29
|
+
const component = instanceMainComponent(graph, instance)
|
|
30
|
+
const set = component?.parentId ? graph.nodes.get(component.parentId) : undefined
|
|
31
|
+
return [component, set]
|
|
32
|
+
.flatMap((node) => node?.componentPropertyDefinitions ?? [])
|
|
33
|
+
.find((definition) => definition.id === propertyId && definition.type === 'SLOT')?.name
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** An instance's slot frames, without entering nested instances, whose slots are their own. */
|
|
37
|
+
export function instanceSlotFrames(graph: SceneGraph, instance: SceneNode): SceneNode[] {
|
|
38
|
+
const frames: SceneNode[] = []
|
|
39
|
+
const visit = (node: SceneNode): void => {
|
|
40
|
+
for (const childId of node.childIds) {
|
|
41
|
+
const child = graph.nodes.get(childId)
|
|
42
|
+
if (!child) continue
|
|
43
|
+
if (slotPropertyId(child)) frames.push(child)
|
|
44
|
+
else if (child.type !== 'INSTANCE') visit(child)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
visit(instance)
|
|
48
|
+
return frames
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** One slot's owned content, parked while the instance it belongs to is rebuilt. */
|
|
52
|
+
interface ParkedSlot {
|
|
53
|
+
/** Names of the nested instances leading to the slot's owner; empty for the instance itself. */
|
|
54
|
+
path: string[]
|
|
55
|
+
/** The slot property's name, which the replacement must share. */
|
|
56
|
+
name: string
|
|
57
|
+
/** Content layer ids, in layer order. */
|
|
58
|
+
content: string[]
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Owned slot content parked under an instance while its component changes. */
|
|
62
|
+
export interface ParkedSlotContent {
|
|
63
|
+
slots: ParkedSlot[]
|
|
64
|
+
/** The instance's own slot assignments the content was parked from. */
|
|
65
|
+
propertyIds: string[]
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Instances directly inside this one: not inside another nested instance or a slot. */
|
|
69
|
+
function nestedInstances(graph: SceneGraph, owner: SceneNode): SceneNode[] {
|
|
70
|
+
const found: SceneNode[] = []
|
|
71
|
+
const visit = (node: SceneNode): void => {
|
|
72
|
+
for (const childId of node.childIds) {
|
|
73
|
+
const child = graph.nodes.get(childId)
|
|
74
|
+
if (!child || slotPropertyId(child)) continue
|
|
75
|
+
if (child.type === 'INSTANCE') found.push(child)
|
|
76
|
+
else visit(child)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
visit(owner)
|
|
80
|
+
return found
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The nested instance a name path leads to from `root`, matched by layer name as Figma does. */
|
|
84
|
+
function instanceAtPath(
|
|
85
|
+
graph: SceneGraph,
|
|
86
|
+
root: SceneNode,
|
|
87
|
+
path: readonly string[]
|
|
88
|
+
): SceneNode | undefined {
|
|
89
|
+
let current: SceneNode | undefined = root
|
|
90
|
+
for (const name of path) {
|
|
91
|
+
current = current && nestedInstances(graph, current).find((node) => node.name === name)
|
|
92
|
+
}
|
|
93
|
+
return current
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Park the slot content an instance and the instances nested in it own, by slot name and the
|
|
98
|
+
* nested instance's name path, directly under the instance, so its component can be replaced.
|
|
99
|
+
* Figma keeps that content when a variant switch or swap leads to slots of the same names.
|
|
100
|
+
*/
|
|
101
|
+
export function detachOwnedSlotContent(graph: SceneGraph, instance: SceneNode): ParkedSlotContent {
|
|
102
|
+
const parked: ParkedSlotContent = { slots: [], propertyIds: [] }
|
|
103
|
+
const visit = (owner: SceneNode, path: string[]): void => {
|
|
104
|
+
for (const nested of nestedInstances(graph, owner)) visit(nested, [...path, nested.name])
|
|
105
|
+
for (const frame of instanceSlotFrames(graph, owner)) {
|
|
106
|
+
const propertyId = slotPropertyId(frame)
|
|
107
|
+
if (!propertyId || !Object.hasOwn(owner.componentPropertyAssignments, propertyId)) continue
|
|
108
|
+
if (owner.id === instance.id) parked.propertyIds.push(propertyId)
|
|
109
|
+
const name = slotName(graph, owner, propertyId)
|
|
110
|
+
const key = [...path, name].join('\u0000')
|
|
111
|
+
if (!name || parked.slots.some((slot) => [...slot.path, slot.name].join('\u0000') === key))
|
|
112
|
+
continue
|
|
113
|
+
const content = [...frame.childIds]
|
|
114
|
+
for (const id of content) graph.reorderChild(id, instance.id, instance.childIds.length)
|
|
115
|
+
parked.slots.push({ path, name, content })
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
visit(instance, [])
|
|
119
|
+
return parked
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Move parked content into the slots of the same names and paths, and reassign them. */
|
|
123
|
+
export function restoreOwnedSlotContent(
|
|
124
|
+
graph: SceneGraph,
|
|
125
|
+
instance: SceneNode,
|
|
126
|
+
parked: ParkedSlotContent
|
|
127
|
+
): void {
|
|
128
|
+
if (parked.slots.length === 0 && parked.propertyIds.length === 0) return
|
|
129
|
+
graph.updateNode(instance.id, {
|
|
130
|
+
componentPropertyAssignments: Object.fromEntries(
|
|
131
|
+
Object.entries(instance.componentPropertyAssignments).filter(
|
|
132
|
+
([id]) => !parked.propertyIds.includes(id)
|
|
133
|
+
)
|
|
134
|
+
)
|
|
135
|
+
})
|
|
136
|
+
for (const slot of parked.slots) {
|
|
137
|
+
const owner = instanceAtPath(graph, instance, slot.path)
|
|
138
|
+
const frame =
|
|
139
|
+
owner &&
|
|
140
|
+
instanceSlotFrames(graph, owner).find((candidate) => {
|
|
141
|
+
const propertyId = slotPropertyId(candidate)
|
|
142
|
+
return !!propertyId && slotName(graph, owner, propertyId) === slot.name
|
|
143
|
+
})
|
|
144
|
+
const propertyId = frame && slotPropertyId(frame)
|
|
145
|
+
if (
|
|
146
|
+
!owner ||
|
|
147
|
+
!frame ||
|
|
148
|
+
!propertyId ||
|
|
149
|
+
Object.hasOwn(owner.componentPropertyAssignments, propertyId)
|
|
150
|
+
) {
|
|
151
|
+
for (const id of slot.content) graph.deleteNode(id)
|
|
152
|
+
continue
|
|
153
|
+
}
|
|
154
|
+
for (const id of Array.from(frame.childIds)) graph.deleteNode(id)
|
|
155
|
+
for (const [index, id] of slot.content.entries()) graph.reorderChild(id, frame.id, index)
|
|
156
|
+
graph.updateNode(owner.id, {
|
|
157
|
+
componentPropertyAssignments: { ...owner.componentPropertyAssignments, [propertyId]: '' }
|
|
158
|
+
})
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { SceneGraph } from '../index'
|
|
2
|
+
import type { ComponentPropertyDefinition, SceneNode } from '../types'
|
|
3
|
+
|
|
4
|
+
/** A limit a slot's content breaks, named as Figma's `SlotNode.limitViolations` names them. */
|
|
5
|
+
export type SlotLimitViolation = 'BELOW_MIN' | 'ABOVE_MAX' | 'HAS_NON_PREFERRED'
|
|
6
|
+
|
|
7
|
+
/** Whether a component is among a slot's preferred values, by id, key, or library key. */
|
|
8
|
+
export function isPreferredComponent(
|
|
9
|
+
component: SceneNode | undefined,
|
|
10
|
+
preferredValues: readonly string[] = []
|
|
11
|
+
): boolean {
|
|
12
|
+
if (!component) return false
|
|
13
|
+
return [component.id, component.componentKey, component.sourceLibraryKey].some(
|
|
14
|
+
(value) => !!value && preferredValues.includes(value)
|
|
15
|
+
)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Content layers a preferred-only slot does not allow: anything but preferred instances. */
|
|
19
|
+
export function nonPreferredLayers(
|
|
20
|
+
graph: SceneGraph,
|
|
21
|
+
definition: ComponentPropertyDefinition,
|
|
22
|
+
content: readonly SceneNode[]
|
|
23
|
+
): SceneNode[] {
|
|
24
|
+
if (!definition.slotSettings?.allowPreferredValuesOnly) return []
|
|
25
|
+
return content.filter(
|
|
26
|
+
(node) =>
|
|
27
|
+
node.type !== 'INSTANCE' ||
|
|
28
|
+
!isPreferredComponent(
|
|
29
|
+
node.componentId ? graph.getNode(node.componentId) : undefined,
|
|
30
|
+
definition.preferredValues
|
|
31
|
+
)
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The limits a slot's content breaks; below-minimum and above-maximum exclude each other. */
|
|
36
|
+
export function slotLimitViolations(
|
|
37
|
+
graph: SceneGraph,
|
|
38
|
+
definition: ComponentPropertyDefinition,
|
|
39
|
+
content: readonly SceneNode[]
|
|
40
|
+
): SlotLimitViolation[] {
|
|
41
|
+
const settings = definition.slotSettings
|
|
42
|
+
const violations: SlotLimitViolation[] = []
|
|
43
|
+
if (settings?.minChildren !== undefined && content.length < settings.minChildren)
|
|
44
|
+
violations.push('BELOW_MIN')
|
|
45
|
+
else if (settings?.maxChildren !== undefined && content.length > settings.maxChildren)
|
|
46
|
+
violations.push('ABOVE_MAX')
|
|
47
|
+
if (nonPreferredLayers(graph, definition, content).length) violations.push('HAS_NON_PREFERRED')
|
|
48
|
+
return violations
|
|
49
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { LayoutDirection, SceneNode, TextDirection } from '../types'
|
|
2
|
+
|
|
3
|
+
const RTL_CHAR_RE =
|
|
4
|
+
/\p{Script=Arabic}|\p{Script=Hebrew}|\p{Script=Syriac}|\p{Script=Thaana}|\p{Script=Nko}|\p{Script=Adlam}/u
|
|
5
|
+
const LTR_CHAR_RE = /\p{Script=Latin}|\p{Script=Cyrillic}|\p{Script=Greek}/u
|
|
6
|
+
|
|
7
|
+
export function detectTextDirection(text: string): Exclude<TextDirection, 'AUTO'> {
|
|
8
|
+
for (const char of text) {
|
|
9
|
+
if (RTL_CHAR_RE.test(char)) return 'RTL'
|
|
10
|
+
if (LTR_CHAR_RE.test(char)) return 'LTR'
|
|
11
|
+
}
|
|
12
|
+
return 'LTR'
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function resolveTextDirection(
|
|
16
|
+
direction: TextDirection,
|
|
17
|
+
text: string
|
|
18
|
+
): Exclude<TextDirection, 'AUTO'> {
|
|
19
|
+
return direction === 'AUTO' ? detectTextDirection(text) : direction
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function resolveNodeTextDirection(
|
|
23
|
+
node: Pick<SceneNode, 'textDirection' | 'text'>
|
|
24
|
+
): 'LTR' | 'RTL' {
|
|
25
|
+
return resolveTextDirection(node.textDirection, node.text)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function resolveNodeLayoutDirection(
|
|
29
|
+
node: { layoutDirection?: LayoutDirection },
|
|
30
|
+
inheritedDirection: Exclude<LayoutDirection, 'AUTO'> = 'LTR'
|
|
31
|
+
): Exclude<LayoutDirection, 'AUTO'> {
|
|
32
|
+
return !node.layoutDirection || node.layoutDirection === 'AUTO'
|
|
33
|
+
? inheritedDirection
|
|
34
|
+
: node.layoutDirection
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function isLogicalTextAlignStart(
|
|
38
|
+
node: Pick<SceneNode, 'textAlignHorizontal' | 'textDirection' | 'text'>
|
|
39
|
+
): boolean {
|
|
40
|
+
const direction = resolveNodeTextDirection(node)
|
|
41
|
+
return (
|
|
42
|
+
(direction === 'LTR' && node.textAlignHorizontal === 'LEFT') ||
|
|
43
|
+
(direction === 'RTL' && node.textAlignHorizontal === 'RIGHT')
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function isLogicalTextAlignEnd(
|
|
48
|
+
node: Pick<SceneNode, 'textAlignHorizontal' | 'textDirection' | 'text'>
|
|
49
|
+
): boolean {
|
|
50
|
+
const direction = resolveNodeTextDirection(node)
|
|
51
|
+
return (
|
|
52
|
+
(direction === 'LTR' && node.textAlignHorizontal === 'RIGHT') ||
|
|
53
|
+
(direction === 'RTL' && node.textAlignHorizontal === 'LEFT')
|
|
54
|
+
)
|
|
55
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { TextParagraphStyle } from '../types'
|
|
2
|
+
import { paragraphStyleAt, textParagraphRanges, type TextParagraphRange } from './paragraphs'
|
|
3
|
+
|
|
4
|
+
/** How an ordered item's number is written at its nesting level. */
|
|
5
|
+
export type ListNumberFormat = 'DECIMAL' | 'LOWER_ALPHA' | 'LOWER_ROMAN'
|
|
6
|
+
|
|
7
|
+
/** A list item paragraph and what marks it. */
|
|
8
|
+
export interface TextListItem extends TextParagraphRange {
|
|
9
|
+
paragraph: number
|
|
10
|
+
listType: 'ORDERED' | 'UNORDERED'
|
|
11
|
+
/** Nesting level, from 1. */
|
|
12
|
+
level: number
|
|
13
|
+
/** The item's number among the items of its list at its level, from 1. */
|
|
14
|
+
ordinal: number
|
|
15
|
+
/** The marker drawn before the item: a bullet, or its number and a period. */
|
|
16
|
+
marker: string
|
|
17
|
+
/** Whether the item starts a new count at its level. */
|
|
18
|
+
startsList: boolean
|
|
19
|
+
/**
|
|
20
|
+
* The paragraph that starts the group of list items this item belongs to. A list group runs
|
|
21
|
+
* from a list item that follows a plain paragraph, or that starts a top-level list, and its
|
|
22
|
+
* first character styles every marker and indent in it.
|
|
23
|
+
*/
|
|
24
|
+
groupStart: number
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The marker of an unordered item, at every level. */
|
|
28
|
+
export const LIST_BULLET = '•'
|
|
29
|
+
|
|
30
|
+
const NUMBER_FORMATS: readonly ListNumberFormat[] = ['DECIMAL', 'LOWER_ALPHA', 'LOWER_ROMAN']
|
|
31
|
+
|
|
32
|
+
/** Ordered items count in decimal, then letters, then roman numerals, repeating as they nest. */
|
|
33
|
+
export function listNumberFormat(level: number): ListNumberFormat {
|
|
34
|
+
return NUMBER_FORMATS[(Math.max(1, level) - 1) % NUMBER_FORMATS.length]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function alphabetic(value: number): string {
|
|
38
|
+
let letters = ''
|
|
39
|
+
for (let rest = value; rest > 0; rest = Math.floor((rest - 1) / 26)) {
|
|
40
|
+
letters = String.fromCharCode(97 + ((rest - 1) % 26)) + letters
|
|
41
|
+
}
|
|
42
|
+
return letters
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const ROMAN_DIGITS: ReadonlyArray<readonly [number, string]> = [
|
|
46
|
+
[1000, 'm'],
|
|
47
|
+
[900, 'cm'],
|
|
48
|
+
[500, 'd'],
|
|
49
|
+
[400, 'cd'],
|
|
50
|
+
[100, 'c'],
|
|
51
|
+
[90, 'xc'],
|
|
52
|
+
[50, 'l'],
|
|
53
|
+
[40, 'xl'],
|
|
54
|
+
[10, 'x'],
|
|
55
|
+
[9, 'ix'],
|
|
56
|
+
[5, 'v'],
|
|
57
|
+
[4, 'iv'],
|
|
58
|
+
[1, 'i']
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
function roman(value: number): string {
|
|
62
|
+
let rest = value
|
|
63
|
+
let numeral = ''
|
|
64
|
+
for (const [amount, digits] of ROMAN_DIGITS) {
|
|
65
|
+
for (; rest >= amount; rest -= amount) numeral += digits
|
|
66
|
+
}
|
|
67
|
+
return numeral
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** An ordered item's number as its level writes it, without the period. */
|
|
71
|
+
export function formatListNumber(ordinal: number, format: ListNumberFormat): string {
|
|
72
|
+
if (format === 'LOWER_ALPHA') return alphabetic(ordinal)
|
|
73
|
+
if (format === 'LOWER_ROMAN') return roman(ordinal)
|
|
74
|
+
return String(ordinal)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
interface LevelCount {
|
|
78
|
+
listType: TextListItem['listType']
|
|
79
|
+
count: number
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The list items of a text, numbered as Figma numbers them: each level counts its own items,
|
|
84
|
+
* a deeper item leaves the count above it running, a shallower one restarts the counts below
|
|
85
|
+
* it, and a plain paragraph or an item of the other list type restarts the count.
|
|
86
|
+
*/
|
|
87
|
+
export function textListItems(
|
|
88
|
+
text: string,
|
|
89
|
+
paragraphs: readonly TextParagraphStyle[]
|
|
90
|
+
): TextListItem[] {
|
|
91
|
+
if (!paragraphs.some((paragraph) => paragraph.listType !== 'NONE')) return []
|
|
92
|
+
const items: TextListItem[] = []
|
|
93
|
+
const counts: Array<LevelCount | undefined> = []
|
|
94
|
+
let groupStart = -1
|
|
95
|
+
for (const [paragraph, range] of textParagraphRanges(text).entries()) {
|
|
96
|
+
const style = paragraphStyleAt(paragraphs, paragraph)
|
|
97
|
+
if (style.listType === 'NONE') {
|
|
98
|
+
counts.length = 0
|
|
99
|
+
groupStart = -1
|
|
100
|
+
continue
|
|
101
|
+
}
|
|
102
|
+
const level = Math.max(1, style.indentation)
|
|
103
|
+
counts.length = Math.min(counts.length, level)
|
|
104
|
+
const current = counts[level - 1]
|
|
105
|
+
const continues = current?.listType === style.listType
|
|
106
|
+
const count = continues ? current.count + 1 : 1
|
|
107
|
+
counts[level - 1] = { listType: style.listType, count }
|
|
108
|
+
const shallower = counts.slice(0, level - 1).some((entry) => entry !== undefined)
|
|
109
|
+
if (groupStart === -1 || (!continues && !shallower)) groupStart = paragraph
|
|
110
|
+
const marker =
|
|
111
|
+
style.listType === 'UNORDERED'
|
|
112
|
+
? LIST_BULLET
|
|
113
|
+
: `${formatListNumber(count, listNumberFormat(level))}.`
|
|
114
|
+
items.push({
|
|
115
|
+
...range,
|
|
116
|
+
paragraph,
|
|
117
|
+
listType: style.listType,
|
|
118
|
+
level,
|
|
119
|
+
ordinal: count,
|
|
120
|
+
marker,
|
|
121
|
+
startsList: !continues,
|
|
122
|
+
groupStart
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
return items
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Digits in the largest number of a top-level ordered item. Figma widens the indent of every
|
|
130
|
+
* ordered item by the extra digits, so a list's numbers line up past nine.
|
|
131
|
+
*/
|
|
132
|
+
export function topLevelNumberDigits(items: readonly TextListItem[]): number {
|
|
133
|
+
let largest = 0
|
|
134
|
+
for (const item of items) {
|
|
135
|
+
if (item.listType === 'ORDERED' && item.level === 1) largest = Math.max(largest, item.ordinal)
|
|
136
|
+
}
|
|
137
|
+
return largest === 0 ? 0 : String(largest).length
|
|
138
|
+
}
|