@open-pencil/scene-graph 0.14.0 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/arrow-caps.d.ts +44 -0
- package/dist/arrow-caps.d.ts.map +1 -0
- package/dist/arrow-caps.js +131 -0
- package/dist/arrow-caps.js.map +1 -0
- package/dist/chunks/checkpoint.js +78 -0
- package/dist/chunks/checkpoint.js.map +1 -0
- package/dist/chunks/components/properties.js +1571 -0
- package/dist/chunks/components/properties.js.map +1 -0
- package/dist/chunks/coordinate.js +87 -33
- package/dist/chunks/coordinate.js.map +1 -1
- package/dist/chunks/font-style.js +2 -1
- package/dist/chunks/font-style.js.map +1 -1
- package/dist/chunks/index.js +1283 -0
- package/dist/chunks/index.js.map +1 -0
- package/dist/constants.js +1 -18
- package/dist/constants2.d.ts +0 -1
- package/dist/constants2.d.ts.map +1 -1
- package/dist/coordinate.d.ts +107 -11
- package/dist/coordinate.d.ts.map +1 -1
- package/dist/coordinate.js +2 -2
- package/dist/coordinate2.d.ts +2 -2
- package/dist/copy.d.ts +2 -2
- package/dist/copy.js +2 -2
- package/dist/geometry.d.ts +2 -2
- package/dist/geometry.js +2 -431
- package/dist/guides.d.ts +2 -0
- package/dist/guides.js +0 -0
- package/dist/guides2.d.ts +11 -0
- package/dist/guides2.d.ts.map +1 -0
- package/dist/hit-test.d.ts +1 -2
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +26 -14
- package/dist/hit-test.js.map +1 -1
- package/dist/images.js +0 -1
- package/dist/images.js.map +1 -1
- package/dist/images2.d.ts.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.js +5 -6
- package/dist/instances.d.ts +2 -2
- package/dist/instances.js +2 -2
- package/dist/layout-guides.d.ts +2 -0
- package/dist/layout-guides.js +2 -0
- package/dist/layout-guides2.d.ts +16 -0
- package/dist/layout-guides2.d.ts.map +1 -0
- package/dist/matrix2.d.ts +0 -1
- package/dist/matrix2.d.ts.map +1 -1
- package/dist/node-defaults.d.ts +1 -2
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +1 -1
- package/dist/parse-path.d.ts +1 -2
- package/dist/parse-path.d.ts.map +1 -1
- package/dist/parse-path.js.map +1 -1
- package/dist/preview.d.ts +2 -12
- package/dist/preview.js +1 -1
- package/dist/preview2.d.ts +12 -0
- package/dist/preview2.d.ts.map +1 -0
- package/dist/primitives2.d.ts.map +1 -1
- package/dist/resize.d.ts +18 -4
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +70 -22
- package/dist/resize.js.map +1 -1
- package/dist/snap.js +1 -1
- package/dist/source-metadata.d.ts +1 -2
- package/dist/source-metadata.d.ts.map +1 -1
- package/dist/source-metadata.js +1 -1
- package/dist/text-picture.d.ts +13 -2
- package/dist/text-picture.d.ts.map +1 -1
- package/dist/text-picture.js +2 -2
- package/dist/types.d.ts +499 -2
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -1296
- package/dist/types2.d.ts +2 -430
- package/dist/undo.js +1 -1
- package/dist/undo2.d.ts +5 -0
- package/dist/undo2.d.ts.map +1 -1
- package/dist/variables.d.ts +1 -2
- package/dist/variables.d.ts.map +1 -1
- package/dist/variables.js +1 -1
- package/dist/variant-name.d.ts.map +1 -1
- package/dist/variant-properties.d.ts +10 -0
- package/dist/variant-properties.d.ts.map +1 -0
- package/dist/variant-properties.js +39 -0
- package/dist/variant-properties.js.map +1 -0
- package/dist/vector-network.js +1 -1
- package/dist/vector-network2.d.ts +1 -2
- package/dist/vector-network2.d.ts.map +1 -1
- package/package.json +40 -15
- package/src/arrow-caps.ts +146 -0
- package/src/bindings.ts +20 -0
- package/src/checkpoint.ts +123 -0
- package/src/components/properties.ts +238 -0
- package/src/constants.ts +6 -0
- package/src/coordinate.ts +226 -0
- package/src/copy.ts +294 -0
- package/src/events.ts +21 -0
- package/src/export-scale.ts +16 -0
- package/src/font-style.ts +77 -0
- package/src/geometry.ts +530 -0
- package/src/guides.ts +8 -0
- package/src/hit-test.ts +205 -0
- package/src/images.ts +34 -0
- package/src/index.ts +686 -0
- package/src/instance-overrides.test.ts +67 -0
- package/src/instance-overrides.ts +152 -0
- package/src/instances.ts +480 -0
- package/src/layout-guides.ts +103 -0
- package/src/matrix.ts +116 -0
- package/src/mutation-impact.ts +70 -0
- package/src/node-defaults.ts +186 -0
- package/src/parse-path.ts +191 -0
- package/src/preview.ts +80 -0
- package/src/primitives.ts +42 -0
- package/src/resize.ts +302 -0
- package/src/shared-styles.ts +73 -0
- package/src/snap.ts +129 -0
- package/src/source-metadata.ts +8 -0
- package/src/text-picture.ts +65 -0
- package/src/types.ts +635 -0
- package/src/undo.ts +171 -0
- package/src/variables.ts +399 -0
- package/src/variant-name.ts +15 -0
- package/src/variant-properties.ts +48 -0
- package/src/vector-network.ts +293 -0
- package/dist/chunks/copy.js +0 -576
- package/dist/chunks/copy.js.map +0 -1
- package/dist/constants.js.map +0 -1
- package/dist/geometry.js.map +0 -1
- package/dist/preview.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
- package/dist/types2.d.ts.map +0 -1
package/src/preview.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { textCacheInvalidationChanges } from './text-picture'
|
|
2
|
+
import type { SceneNode } from './types'
|
|
3
|
+
import { normalizeVectorNetwork } from './vector-network'
|
|
4
|
+
|
|
5
|
+
export type NodePreviewObserver = (node: SceneNode, changes: Partial<SceneNode>) => void
|
|
6
|
+
|
|
7
|
+
type PreviewGraph = {
|
|
8
|
+
nodes: Map<string, SceneNode>
|
|
9
|
+
positionPreviewVersion: number
|
|
10
|
+
clearAbsPosCache: () => void
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const LAYOUT_AFFECTING_KEYS = new Set<string>([
|
|
14
|
+
'x',
|
|
15
|
+
'y',
|
|
16
|
+
'width',
|
|
17
|
+
'height',
|
|
18
|
+
'rotation',
|
|
19
|
+
'flipX',
|
|
20
|
+
'flipY',
|
|
21
|
+
'parentId',
|
|
22
|
+
'childIds',
|
|
23
|
+
'layoutMode',
|
|
24
|
+
'layoutDirection',
|
|
25
|
+
'layoutWrap',
|
|
26
|
+
'primaryAxisSizing',
|
|
27
|
+
'counterAxisSizing',
|
|
28
|
+
'itemSpacing',
|
|
29
|
+
'counterAxisSpacing',
|
|
30
|
+
'paddingTop',
|
|
31
|
+
'paddingRight',
|
|
32
|
+
'paddingBottom',
|
|
33
|
+
'paddingLeft',
|
|
34
|
+
'layoutGrow',
|
|
35
|
+
'layoutAlignSelf',
|
|
36
|
+
'layoutPositioning',
|
|
37
|
+
'minWidth',
|
|
38
|
+
'maxWidth',
|
|
39
|
+
'minHeight',
|
|
40
|
+
'maxHeight',
|
|
41
|
+
'visible',
|
|
42
|
+
'text',
|
|
43
|
+
'fontSize',
|
|
44
|
+
'lineHeight',
|
|
45
|
+
'letterSpacing',
|
|
46
|
+
'styleRuns',
|
|
47
|
+
'textAutoResize'
|
|
48
|
+
])
|
|
49
|
+
|
|
50
|
+
export function updateNodePreview(
|
|
51
|
+
graph: PreviewGraph,
|
|
52
|
+
id: string,
|
|
53
|
+
changes: Partial<SceneNode>,
|
|
54
|
+
beforeUpdate?: NodePreviewObserver
|
|
55
|
+
): Partial<SceneNode> | null {
|
|
56
|
+
const node = graph.nodes.get(id)
|
|
57
|
+
if (!node) return null
|
|
58
|
+
changes = Object.fromEntries(
|
|
59
|
+
(Object.entries(changes) as Array<[string, unknown]>).filter(([, value]) => value !== undefined)
|
|
60
|
+
) as Partial<SceneNode>
|
|
61
|
+
if ((Object.keys(changes) as (keyof SceneNode)[]).every((key) => node[key] === changes[key])) {
|
|
62
|
+
return null
|
|
63
|
+
}
|
|
64
|
+
const affectsLayout = Object.keys(changes).some((key) => LAYOUT_AFFECTING_KEYS.has(key))
|
|
65
|
+
if (affectsLayout) graph.clearAbsPosCache()
|
|
66
|
+
let normalizedChanges = changes
|
|
67
|
+
if (node.type === 'TEXT') {
|
|
68
|
+
normalizedChanges = { ...textCacheInvalidationChanges(node, changes), ...changes }
|
|
69
|
+
}
|
|
70
|
+
if (changes.vectorNetwork) {
|
|
71
|
+
normalizedChanges = {
|
|
72
|
+
...normalizedChanges,
|
|
73
|
+
vectorNetwork: normalizeVectorNetwork(changes.vectorNetwork)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
beforeUpdate?.(node, normalizedChanges)
|
|
77
|
+
graph.positionPreviewVersion++
|
|
78
|
+
Object.assign(node, normalizedChanges)
|
|
79
|
+
return normalizedChanges
|
|
80
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export interface GUID {
|
|
2
|
+
sessionID: number
|
|
3
|
+
localID: number
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface Color {
|
|
7
|
+
r: number
|
|
8
|
+
g: number
|
|
9
|
+
b: number
|
|
10
|
+
a: number
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface Vector {
|
|
14
|
+
x: number
|
|
15
|
+
y: number
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface Matrix {
|
|
19
|
+
m00: number
|
|
20
|
+
m01: number
|
|
21
|
+
m02: number
|
|
22
|
+
m10: number
|
|
23
|
+
m11: number
|
|
24
|
+
m12: number
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface Size {
|
|
28
|
+
width: number
|
|
29
|
+
height: number
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface Rect {
|
|
33
|
+
x: number
|
|
34
|
+
y: number
|
|
35
|
+
width: number
|
|
36
|
+
height: number
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type JSONPrimitive = string | number | boolean | null
|
|
40
|
+
export type JSONValue = JSONPrimitive | JSONObject | JSONArray
|
|
41
|
+
export type JSONObject = { [key: string]: unknown }
|
|
42
|
+
export type JSONArray = unknown[]
|
package/src/resize.ts
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import {
|
|
2
|
+
copyDerivedGlyphs,
|
|
3
|
+
copyGeometryPaths,
|
|
4
|
+
copyStroke,
|
|
5
|
+
copyStrokes,
|
|
6
|
+
scaleGeometryPaths
|
|
7
|
+
} from './copy'
|
|
8
|
+
import type { Rect } from './primitives'
|
|
9
|
+
import type { ConstraintType, DerivedTextGlyph, SceneNode, Stroke, VectorNetwork } from './types'
|
|
10
|
+
import { cloneVectorNetwork } from './vector-network'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Drag-start state a live resize re-scales from. Path text needs more than the
|
|
14
|
+
* layout rect: its OUTSIDE stroke silhouettes and glyph outlines are baked
|
|
15
|
+
* geometry that must scale with the node, and textPathBox is the layout box the
|
|
16
|
+
* glyphs were reflowed against.
|
|
17
|
+
*/
|
|
18
|
+
export type ResizeSnapshot = Pick<
|
|
19
|
+
SceneNode,
|
|
20
|
+
| 'x'
|
|
21
|
+
| 'y'
|
|
22
|
+
| 'width'
|
|
23
|
+
| 'height'
|
|
24
|
+
| 'vectorNetwork'
|
|
25
|
+
| 'fillGeometry'
|
|
26
|
+
| 'strokeGeometry'
|
|
27
|
+
| 'derivedTextGlyphs'
|
|
28
|
+
| 'strokes'
|
|
29
|
+
| 'textPathData'
|
|
30
|
+
| 'textPathBox'
|
|
31
|
+
>
|
|
32
|
+
|
|
33
|
+
interface ResizeGraph {
|
|
34
|
+
getNode(id: string): SceneNode | undefined
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const CONSTRAINT_CONTAINER_TYPES = new Set([
|
|
38
|
+
'FRAME',
|
|
39
|
+
'COMPONENT',
|
|
40
|
+
'COMPONENT_SET',
|
|
41
|
+
'INSTANCE',
|
|
42
|
+
'GROUP',
|
|
43
|
+
'BOOLEAN_OPERATION'
|
|
44
|
+
])
|
|
45
|
+
|
|
46
|
+
function constrainedAxis(
|
|
47
|
+
position: number,
|
|
48
|
+
size: number,
|
|
49
|
+
parentBefore: number,
|
|
50
|
+
parentAfter: number,
|
|
51
|
+
constraint: ConstraintType
|
|
52
|
+
): { position: number; size: number } {
|
|
53
|
+
const delta = parentAfter - parentBefore
|
|
54
|
+
if (constraint === 'MAX') return { position: position + delta, size }
|
|
55
|
+
if (constraint === 'CENTER') return { position: position + delta / 2, size }
|
|
56
|
+
if (constraint === 'STRETCH') return { position, size: Math.max(1, size + delta) }
|
|
57
|
+
if (constraint === 'SCALE' && parentBefore > 0) {
|
|
58
|
+
const scale = parentAfter / parentBefore
|
|
59
|
+
return { position: position * scale, size: Math.max(1, size * scale) }
|
|
60
|
+
}
|
|
61
|
+
return { position, size }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function constrainedChildRect(
|
|
65
|
+
child: Rect,
|
|
66
|
+
parentBefore: Pick<Rect, 'width' | 'height'>,
|
|
67
|
+
parentAfter: Pick<Rect, 'width' | 'height'>,
|
|
68
|
+
horizontal: ConstraintType,
|
|
69
|
+
vertical: ConstraintType
|
|
70
|
+
): Rect {
|
|
71
|
+
const x = constrainedAxis(child.x, child.width, parentBefore.width, parentAfter.width, horizontal)
|
|
72
|
+
const y = constrainedAxis(
|
|
73
|
+
child.y,
|
|
74
|
+
child.height,
|
|
75
|
+
parentBefore.height,
|
|
76
|
+
parentAfter.height,
|
|
77
|
+
vertical
|
|
78
|
+
)
|
|
79
|
+
return {
|
|
80
|
+
x: Math.round(x.position),
|
|
81
|
+
y: Math.round(y.position),
|
|
82
|
+
width: Math.round(x.size),
|
|
83
|
+
height: Math.round(y.size)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function scaledChildRect(
|
|
88
|
+
child: Rect,
|
|
89
|
+
parentBefore: Pick<Rect, 'width' | 'height'>,
|
|
90
|
+
parentAfter: Pick<Rect, 'width' | 'height'>
|
|
91
|
+
): Rect {
|
|
92
|
+
return constrainedChildRect(child, parentBefore, parentAfter, 'SCALE', 'SCALE')
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function scaleVectorNetworkForResize(
|
|
96
|
+
vectorNetwork: VectorNetwork | null,
|
|
97
|
+
originalWidth: number,
|
|
98
|
+
originalHeight: number,
|
|
99
|
+
width: number,
|
|
100
|
+
height: number
|
|
101
|
+
): VectorNetwork | null {
|
|
102
|
+
if (!vectorNetwork || originalWidth <= 0 || originalHeight <= 0) return null
|
|
103
|
+
|
|
104
|
+
const scaleX = width / originalWidth
|
|
105
|
+
const scaleY = height / originalHeight
|
|
106
|
+
if (scaleX === 1 && scaleY === 1) return null
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
vertices: vectorNetwork.vertices.map((vertex) => ({
|
|
110
|
+
...vertex,
|
|
111
|
+
x: vertex.x * scaleX,
|
|
112
|
+
y: vertex.y * scaleY
|
|
113
|
+
})),
|
|
114
|
+
segments: vectorNetwork.segments.map((segment) => ({
|
|
115
|
+
...segment,
|
|
116
|
+
tangentStart: {
|
|
117
|
+
x: segment.tangentStart.x * scaleX,
|
|
118
|
+
y: segment.tangentStart.y * scaleY
|
|
119
|
+
},
|
|
120
|
+
tangentEnd: {
|
|
121
|
+
x: segment.tangentEnd.x * scaleX,
|
|
122
|
+
y: segment.tangentEnd.y * scaleY
|
|
123
|
+
}
|
|
124
|
+
})),
|
|
125
|
+
regions: vectorNetwork.regions
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Snapshot one node's resize-relevant state (rect + geometry that must scale with it). */
|
|
130
|
+
export function createResizeSnapshot(node: SceneNode): ResizeSnapshot {
|
|
131
|
+
return {
|
|
132
|
+
x: node.x,
|
|
133
|
+
y: node.y,
|
|
134
|
+
width: node.width,
|
|
135
|
+
height: node.height,
|
|
136
|
+
vectorNetwork: node.vectorNetwork ? cloneVectorNetwork(node.vectorNetwork) : null,
|
|
137
|
+
fillGeometry: copyGeometryPaths(node.fillGeometry),
|
|
138
|
+
strokeGeometry: copyGeometryPaths(node.strokeGeometry),
|
|
139
|
+
derivedTextGlyphs: copyDerivedGlyphs(node.derivedTextGlyphs),
|
|
140
|
+
strokes: copyStrokes(node.strokes),
|
|
141
|
+
textPathData: node.textPathData ? structuredClone(node.textPathData) : null,
|
|
142
|
+
textPathBox: node.textPathBox ? { ...node.textPathBox } : null
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function scaleDerivedGlyphs(
|
|
147
|
+
glyphs: DerivedTextGlyph[] | null,
|
|
148
|
+
sx: number,
|
|
149
|
+
sy: number
|
|
150
|
+
): DerivedTextGlyph[] | null {
|
|
151
|
+
if (!glyphs?.length) return glyphs
|
|
152
|
+
return glyphs.map((g) => ({
|
|
153
|
+
...g,
|
|
154
|
+
x: g.x * sx,
|
|
155
|
+
y: g.y * sy,
|
|
156
|
+
scaleX: (g.scaleX ?? 1) * sx,
|
|
157
|
+
scaleY: (g.scaleY ?? 1) * sy,
|
|
158
|
+
commandsBlob: new Uint8Array(g.commandsBlob)
|
|
159
|
+
}))
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function scaleStrokes(strokes: Stroke[], sx: number, sy: number): Stroke[] {
|
|
163
|
+
if (strokes.length === 0) return strokes
|
|
164
|
+
// Weight is a scalar; average scale is the usual 2D → 1D compromise.
|
|
165
|
+
const weightScale = (Math.abs(sx) + Math.abs(sy)) / 2
|
|
166
|
+
return strokes.map((s) => ({ ...copyStroke(s), weight: s.weight * weightScale }))
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* All geometry that must track a width/height change. Historically only
|
|
171
|
+
* vectorNetwork + fillGeometry were updated — path-text strokeGeometry and
|
|
172
|
+
* glyphs stayed at the pre-resize size, so shrinking a sticker made the white
|
|
173
|
+
* OUTSIDE outlines look massively thick (DomeSticker resize bug).
|
|
174
|
+
*/
|
|
175
|
+
export function scaledGeometryChanges(
|
|
176
|
+
orig: Pick<
|
|
177
|
+
ResizeSnapshot,
|
|
178
|
+
| 'vectorNetwork'
|
|
179
|
+
| 'fillGeometry'
|
|
180
|
+
| 'strokeGeometry'
|
|
181
|
+
| 'derivedTextGlyphs'
|
|
182
|
+
| 'strokes'
|
|
183
|
+
| 'textPathData'
|
|
184
|
+
| 'textPathBox'
|
|
185
|
+
>,
|
|
186
|
+
origWidth: number,
|
|
187
|
+
origHeight: number,
|
|
188
|
+
width: number,
|
|
189
|
+
height: number
|
|
190
|
+
): Partial<SceneNode> {
|
|
191
|
+
if (origWidth <= 0 || origHeight <= 0) return {}
|
|
192
|
+
const sx = width / origWidth
|
|
193
|
+
const sy = height / origHeight
|
|
194
|
+
if (sx === 1 && sy === 1) return {}
|
|
195
|
+
|
|
196
|
+
const changes: Partial<SceneNode> = {}
|
|
197
|
+
const resizedVN = scaleVectorNetworkForResize(
|
|
198
|
+
orig.vectorNetwork,
|
|
199
|
+
origWidth,
|
|
200
|
+
origHeight,
|
|
201
|
+
width,
|
|
202
|
+
height
|
|
203
|
+
)
|
|
204
|
+
if (resizedVN) changes.vectorNetwork = resizedVN
|
|
205
|
+
|
|
206
|
+
if (orig.fillGeometry.length > 0) {
|
|
207
|
+
changes.fillGeometry = scaleGeometryPaths(orig.fillGeometry, sx, sy)
|
|
208
|
+
}
|
|
209
|
+
if (orig.strokeGeometry.length > 0) {
|
|
210
|
+
changes.strokeGeometry = scaleGeometryPaths(orig.strokeGeometry, sx, sy)
|
|
211
|
+
}
|
|
212
|
+
if (orig.derivedTextGlyphs?.length) {
|
|
213
|
+
changes.derivedTextGlyphs = scaleDerivedGlyphs(orig.derivedTextGlyphs, sx, sy)
|
|
214
|
+
// Keep the layout/selection box in sync with the scaled glyphs. reflow
|
|
215
|
+
// overrides this when it applies; this covers the fallback where reflow
|
|
216
|
+
// returns null (no path data) so the box doesn't carry a stale size.
|
|
217
|
+
if (orig.textPathBox) {
|
|
218
|
+
changes.textPathBox = {
|
|
219
|
+
x: orig.textPathBox.x * sx,
|
|
220
|
+
y: orig.textPathBox.y * sy,
|
|
221
|
+
width: orig.textPathBox.width * sx,
|
|
222
|
+
height: orig.textPathBox.height * sy
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if (orig.strokes.length > 0) {
|
|
227
|
+
changes.strokes = scaleStrokes(orig.strokes, sx, sy)
|
|
228
|
+
}
|
|
229
|
+
return changes
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export function collectResizeDescendants(
|
|
233
|
+
graph: ResizeGraph,
|
|
234
|
+
rootId: string
|
|
235
|
+
): Map<string, ResizeSnapshot> | null {
|
|
236
|
+
const root = graph.getNode(rootId)
|
|
237
|
+
if (!root || !CONSTRAINT_CONTAINER_TYPES.has(root.type)) return null
|
|
238
|
+
const snapshots = new Map<string, ResizeSnapshot>()
|
|
239
|
+
|
|
240
|
+
const collect = (parentId: string) => {
|
|
241
|
+
const parent = graph.getNode(parentId)
|
|
242
|
+
if (!parent) return
|
|
243
|
+
for (const childId of parent.childIds) {
|
|
244
|
+
const child = graph.getNode(childId)
|
|
245
|
+
if (!child) continue
|
|
246
|
+
snapshots.set(childId, createResizeSnapshot(child))
|
|
247
|
+
collect(childId)
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
collect(rootId)
|
|
252
|
+
return snapshots.size > 0 ? snapshots : null
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export function computeConstrainedResizeChanges(
|
|
256
|
+
graph: ResizeGraph,
|
|
257
|
+
rootId: string,
|
|
258
|
+
rootBefore: Pick<Rect, 'width' | 'height'>,
|
|
259
|
+
rootAfter: Pick<Rect, 'width' | 'height'>,
|
|
260
|
+
originals: ReadonlyMap<string, ResizeSnapshot>
|
|
261
|
+
): Map<string, Partial<SceneNode>> {
|
|
262
|
+
const changes = new Map<string, Partial<SceneNode>>()
|
|
263
|
+
|
|
264
|
+
const compute = (
|
|
265
|
+
parentId: string,
|
|
266
|
+
parentBefore: Pick<Rect, 'width' | 'height'>,
|
|
267
|
+
parentAfter: Pick<Rect, 'width' | 'height'>
|
|
268
|
+
) => {
|
|
269
|
+
const parent = graph.getNode(parentId)
|
|
270
|
+
if (!parent) return
|
|
271
|
+
const scalesChildren = parent.type === 'GROUP' || parent.type === 'BOOLEAN_OPERATION'
|
|
272
|
+
for (const childId of parent.childIds) {
|
|
273
|
+
const original = originals.get(childId)
|
|
274
|
+
const child = graph.getNode(childId)
|
|
275
|
+
if (!original || !child) continue
|
|
276
|
+
const isInFlow = parent.layoutMode !== 'NONE' && child.layoutPositioning !== 'ABSOLUTE'
|
|
277
|
+
if (isInFlow) {
|
|
278
|
+
compute(childId, original, child)
|
|
279
|
+
continue
|
|
280
|
+
}
|
|
281
|
+
const rect = scalesChildren
|
|
282
|
+
? scaledChildRect(original, parentBefore, parentAfter)
|
|
283
|
+
: constrainedChildRect(
|
|
284
|
+
original,
|
|
285
|
+
parentBefore,
|
|
286
|
+
parentAfter,
|
|
287
|
+
child.horizontalConstraint,
|
|
288
|
+
child.verticalConstraint
|
|
289
|
+
)
|
|
290
|
+
const childChanges: Partial<SceneNode> = {
|
|
291
|
+
...rect,
|
|
292
|
+
...scaledGeometryChanges(original, original.width, original.height, rect.width, rect.height)
|
|
293
|
+
}
|
|
294
|
+
changes.set(childId, childChanges)
|
|
295
|
+
// The final pass sees layout containers after Yoga has resolved HUG/FILL sizing.
|
|
296
|
+
compute(childId, original, child.layoutMode === 'NONE' ? rect : child)
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
compute(rootId, rootBefore, rootAfter)
|
|
301
|
+
return changes
|
|
302
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { SceneGraph, SceneNode, SharedStyle, SharedStyleKind, SharedStyleType } from './index'
|
|
2
|
+
|
|
3
|
+
const STYLE_REF_KEYS = {
|
|
4
|
+
fill: 'fillStyleId',
|
|
5
|
+
stroke: 'strokeStyleId',
|
|
6
|
+
text: 'textStyleId',
|
|
7
|
+
effect: 'effectStyleId',
|
|
8
|
+
grid: 'gridStyleId'
|
|
9
|
+
} as const satisfies Record<SharedStyleKind, keyof SceneNode>
|
|
10
|
+
|
|
11
|
+
const STYLE_TYPES = {
|
|
12
|
+
fill: 'FILL',
|
|
13
|
+
stroke: 'FILL',
|
|
14
|
+
text: 'TEXT',
|
|
15
|
+
effect: 'EFFECT',
|
|
16
|
+
grid: 'GRID'
|
|
17
|
+
} as const satisfies Record<SharedStyleKind, SharedStyleType>
|
|
18
|
+
|
|
19
|
+
const TEXT_STYLE_KEYS = new Set<keyof SceneNode>([
|
|
20
|
+
'fontFamily',
|
|
21
|
+
'fontWeight',
|
|
22
|
+
'italic',
|
|
23
|
+
'fontSize',
|
|
24
|
+
'lineHeight',
|
|
25
|
+
'letterSpacing',
|
|
26
|
+
'textDecoration',
|
|
27
|
+
'textCase',
|
|
28
|
+
'fontFeatures'
|
|
29
|
+
])
|
|
30
|
+
|
|
31
|
+
export function sharedStyleRefKey(kind: SharedStyleKind): (typeof STYLE_REF_KEYS)[SharedStyleKind] {
|
|
32
|
+
return STYLE_REF_KEYS[kind]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function sharedStyleTypeForKind(kind: SharedStyleKind): SharedStyleType {
|
|
36
|
+
return STYLE_TYPES[kind]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function getSharedStyles(graph: SceneGraph, kind: SharedStyleKind): SharedStyle[] {
|
|
40
|
+
const type = sharedStyleTypeForKind(kind)
|
|
41
|
+
const styles: SharedStyle[] = []
|
|
42
|
+
for (const node of graph.getAllNodes()) {
|
|
43
|
+
if (node.sharedStyleType !== type || !node.source.id) continue
|
|
44
|
+
styles.push({ id: node.source.id, nodeId: node.id, name: node.name, type })
|
|
45
|
+
}
|
|
46
|
+
return styles.sort((left, right) => left.name.localeCompare(right.name))
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function styleDetachmentChanges(
|
|
50
|
+
node: SceneNode,
|
|
51
|
+
changes: Partial<SceneNode>
|
|
52
|
+
): Partial<SceneNode> {
|
|
53
|
+
const next = { ...changes }
|
|
54
|
+
if ('fills' in changes && !('fillStyleId' in changes) && node.fillStyleId) {
|
|
55
|
+
next.fillStyleId = null
|
|
56
|
+
}
|
|
57
|
+
if ('strokes' in changes && !('strokeStyleId' in changes) && node.strokeStyleId) {
|
|
58
|
+
next.strokeStyleId = null
|
|
59
|
+
}
|
|
60
|
+
if ('effects' in changes && !('effectStyleId' in changes) && node.effectStyleId) {
|
|
61
|
+
next.effectStyleId = null
|
|
62
|
+
}
|
|
63
|
+
if ('layoutGrids' in changes && !('gridStyleId' in changes) && node.gridStyleId) {
|
|
64
|
+
next.gridStyleId = null
|
|
65
|
+
}
|
|
66
|
+
const changesTextStyle = (Object.keys(changes) as (keyof SceneNode)[]).some((key) =>
|
|
67
|
+
TEXT_STYLE_KEYS.has(key)
|
|
68
|
+
)
|
|
69
|
+
if (changesTextStyle && !('textStyleId' in changes) && node.textStyleId) {
|
|
70
|
+
next.textStyleId = null
|
|
71
|
+
}
|
|
72
|
+
return next
|
|
73
|
+
}
|
package/src/snap.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import type { SceneNode } from './'
|
|
2
|
+
import { rotatedBBox } from './geometry'
|
|
3
|
+
import type { Rect } from './primitives'
|
|
4
|
+
|
|
5
|
+
const DEFAULT_SNAP_THRESHOLD = 5
|
|
6
|
+
|
|
7
|
+
export interface SnapGuide {
|
|
8
|
+
axis: 'x' | 'y'
|
|
9
|
+
position: number
|
|
10
|
+
from: number
|
|
11
|
+
to: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface SnapResult {
|
|
15
|
+
dx: number
|
|
16
|
+
dy: number
|
|
17
|
+
guides: SnapGuide[]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function getEdges(node: SceneNode) {
|
|
21
|
+
return rotatedBBox(node.x, node.y, node.width, node.height, node.rotation)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Pairwise edge/center matching intentionally keeps both axes in one pass.
|
|
25
|
+
// eslint-disable-next-line complexity
|
|
26
|
+
export function computeSnap(
|
|
27
|
+
movingIds: Set<string>,
|
|
28
|
+
movingBounds: Rect,
|
|
29
|
+
allNodes: SceneNode[],
|
|
30
|
+
threshold = DEFAULT_SNAP_THRESHOLD
|
|
31
|
+
): SnapResult {
|
|
32
|
+
const targets = allNodes.filter((n) => !movingIds.has(n.id))
|
|
33
|
+
if (targets.length === 0) return { dx: 0, dy: 0, guides: [] }
|
|
34
|
+
|
|
35
|
+
const m = {
|
|
36
|
+
left: movingBounds.x,
|
|
37
|
+
right: movingBounds.x + movingBounds.width,
|
|
38
|
+
centerX: movingBounds.x + movingBounds.width / 2,
|
|
39
|
+
top: movingBounds.y,
|
|
40
|
+
bottom: movingBounds.y + movingBounds.height,
|
|
41
|
+
centerY: movingBounds.y + movingBounds.height / 2
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let bestDx = Infinity
|
|
45
|
+
let bestDy = Infinity
|
|
46
|
+
const guides: SnapGuide[] = []
|
|
47
|
+
|
|
48
|
+
for (const target of targets) {
|
|
49
|
+
const t = getEdges(target)
|
|
50
|
+
|
|
51
|
+
// X-axis snapping: left-to-left, left-to-right, right-to-left, right-to-right, center-to-center
|
|
52
|
+
const xPairs: [number, number][] = [
|
|
53
|
+
[m.left, t.left],
|
|
54
|
+
[m.left, t.right],
|
|
55
|
+
[m.right, t.left],
|
|
56
|
+
[m.right, t.right],
|
|
57
|
+
[m.centerX, t.centerX]
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
for (const [mVal, tVal] of xPairs) {
|
|
61
|
+
const d = tVal - mVal
|
|
62
|
+
if (Math.abs(d) < threshold && Math.abs(d) <= Math.abs(bestDx)) {
|
|
63
|
+
if (Math.abs(d) < Math.abs(bestDx)) {
|
|
64
|
+
bestDx = d
|
|
65
|
+
guides.length = guides.filter((g) => g.axis === 'y').length
|
|
66
|
+
? guides.length
|
|
67
|
+
: guides.length
|
|
68
|
+
// Remove old x guides if we found a closer snap
|
|
69
|
+
for (let i = guides.length - 1; i >= 0; i--) {
|
|
70
|
+
if (guides[i].axis === 'x') guides.splice(i, 1)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (Math.abs(d) === Math.abs(bestDx)) {
|
|
74
|
+
const minY = Math.min(m.top, t.top)
|
|
75
|
+
const maxY = Math.max(m.bottom, t.bottom)
|
|
76
|
+
guides.push({ axis: 'x', position: tVal, from: minY, to: maxY })
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Y-axis snapping
|
|
82
|
+
const yPairs: [number, number][] = [
|
|
83
|
+
[m.top, t.top],
|
|
84
|
+
[m.top, t.bottom],
|
|
85
|
+
[m.bottom, t.top],
|
|
86
|
+
[m.bottom, t.bottom],
|
|
87
|
+
[m.centerY, t.centerY]
|
|
88
|
+
]
|
|
89
|
+
|
|
90
|
+
for (const [mVal, tVal] of yPairs) {
|
|
91
|
+
const d = tVal - mVal
|
|
92
|
+
if (Math.abs(d) < threshold && Math.abs(d) <= Math.abs(bestDy)) {
|
|
93
|
+
if (Math.abs(d) < Math.abs(bestDy)) {
|
|
94
|
+
for (let i = guides.length - 1; i >= 0; i--) {
|
|
95
|
+
if (guides[i].axis === 'y') guides.splice(i, 1)
|
|
96
|
+
}
|
|
97
|
+
bestDy = d
|
|
98
|
+
}
|
|
99
|
+
if (Math.abs(d) === Math.abs(bestDy)) {
|
|
100
|
+
const minX = Math.min(m.left, t.left)
|
|
101
|
+
const maxX = Math.max(m.right, t.right)
|
|
102
|
+
guides.push({ axis: 'y', position: tVal, from: minX, to: maxX })
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
dx: Math.abs(bestDx) <= threshold ? bestDx : 0,
|
|
110
|
+
dy: Math.abs(bestDy) <= threshold ? bestDy : 0,
|
|
111
|
+
guides
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function computeSelectionBounds(nodes: SceneNode[]): Rect | null {
|
|
116
|
+
if (nodes.length === 0) return null
|
|
117
|
+
let minX = Infinity
|
|
118
|
+
let minY = Infinity
|
|
119
|
+
let maxX = -Infinity
|
|
120
|
+
let maxY = -Infinity
|
|
121
|
+
for (const n of nodes) {
|
|
122
|
+
const edges = getEdges(n)
|
|
123
|
+
minX = Math.min(minX, edges.left)
|
|
124
|
+
minY = Math.min(minY, edges.top)
|
|
125
|
+
maxX = Math.max(maxX, edges.right)
|
|
126
|
+
maxY = Math.max(maxY, edges.bottom)
|
|
127
|
+
}
|
|
128
|
+
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY }
|
|
129
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { SceneNode } from './types'
|
|
2
|
+
|
|
3
|
+
export function markSourceFieldsEdited(node: SceneNode, changeKeys: string[]): void {
|
|
4
|
+
if (changeKeys.length === 0) return
|
|
5
|
+
const editedFields = new Set(node.source.editedFields)
|
|
6
|
+
for (const key of changeKeys) editedFields.add(key)
|
|
7
|
+
node.source.editedFields = [...editedFields]
|
|
8
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { SceneNode } from './types'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Invalidate cached Skia textPicture (Paragraph snapshot). Includes width/height
|
|
5
|
+
* because wrapping/layout depends on the box.
|
|
6
|
+
*/
|
|
7
|
+
export const TEXT_PICTURE_KEYS: ReadonlySet<string> = new Set([
|
|
8
|
+
'text',
|
|
9
|
+
'fontSize',
|
|
10
|
+
'fontFamily',
|
|
11
|
+
'fontWeight',
|
|
12
|
+
'italic',
|
|
13
|
+
'textAlignHorizontal',
|
|
14
|
+
'textDirection',
|
|
15
|
+
'textAlignVertical',
|
|
16
|
+
'lineHeight',
|
|
17
|
+
'letterSpacing',
|
|
18
|
+
'textDecoration',
|
|
19
|
+
'textCase',
|
|
20
|
+
'styleRuns',
|
|
21
|
+
'fills',
|
|
22
|
+
'width',
|
|
23
|
+
'height'
|
|
24
|
+
])
|
|
25
|
+
|
|
26
|
+
export const GLYPH_AFFECTING_KEYS: ReadonlySet<string> = new Set([
|
|
27
|
+
'text',
|
|
28
|
+
'fontSize',
|
|
29
|
+
'fontFamily',
|
|
30
|
+
'fontWeight',
|
|
31
|
+
'italic',
|
|
32
|
+
'textDirection',
|
|
33
|
+
'lineHeight',
|
|
34
|
+
'letterSpacing',
|
|
35
|
+
'textCase',
|
|
36
|
+
'styleRuns'
|
|
37
|
+
])
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Shared by SceneGraph.updateNode and updateNodePreview (drag hot path) so the
|
|
41
|
+
* two invalidation rules cannot drift. Glyphs are kept when the caller
|
|
42
|
+
* replaces them in the same update (resize supplies scaled copies).
|
|
43
|
+
*/
|
|
44
|
+
export function textCacheInvalidationChanges(
|
|
45
|
+
node: SceneNode,
|
|
46
|
+
changes: Partial<SceneNode>
|
|
47
|
+
): Partial<SceneNode> {
|
|
48
|
+
const invalidated: Partial<SceneNode> = {}
|
|
49
|
+
const keys = Object.keys(changes)
|
|
50
|
+
if (node.textPicture && keys.some((key) => TEXT_PICTURE_KEYS.has(key)))
|
|
51
|
+
invalidated.textPicture = null
|
|
52
|
+
const glyphsInvalidated = keys.some((key) => GLYPH_AFFECTING_KEYS.has(key))
|
|
53
|
+
// A successful path-text edit supplies reflowed glyphs in `changes`. Every
|
|
54
|
+
// other mutation path must drop stale baked glyphs and path identity rather
|
|
55
|
+
// than pair new text/style with old visible outlines.
|
|
56
|
+
if (node.derivedTextGlyphs && glyphsInvalidated && !changes.derivedTextGlyphs) {
|
|
57
|
+
invalidated.derivedTextGlyphs = null
|
|
58
|
+
invalidated.textPathData = null
|
|
59
|
+
}
|
|
60
|
+
return invalidated
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function invalidateTextCaches(node: SceneNode, changes: Partial<SceneNode>): void {
|
|
64
|
+
Object.assign(node, textCacheInvalidationChanges(node, changes))
|
|
65
|
+
}
|