@open-pencil/scene-graph 0.16.0-beta.1 → 0.16.0-beta.3
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/chunks/asset-keys.js +27 -0
- package/dist/chunks/asset-keys.js.map +1 -0
- package/dist/chunks/behaviours/layers.js +16 -1
- package/dist/chunks/behaviours/layers.js.map +1 -1
- package/dist/chunks/color/contrast.js +23 -1
- package/dist/chunks/color/contrast.js.map +1 -1
- package/dist/chunks/color/index.js +43 -4
- package/dist/chunks/color/index.js.map +1 -1
- package/dist/chunks/components/exposure.js +273 -3
- package/dist/chunks/components/exposure.js.map +1 -1
- package/dist/chunks/index.js +247 -150
- package/dist/chunks/index.js.map +1 -1
- package/dist/color.d.ts +7 -1
- package/dist/color.d.ts.map +1 -1
- package/dist/color.js +2 -2
- package/dist/geometry.d.ts +2 -2
- package/dist/hit-test.js +24 -18
- package/dist/hit-test.js.map +1 -1
- package/dist/images.d.ts +2 -2
- package/dist/images.js +21 -38
- package/dist/images.js.map +1 -1
- package/dist/images2.d.ts +12 -1
- package/dist/images2.d.ts.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +6 -6
- package/dist/layout.d.ts +2 -2
- package/dist/layout.js +3 -3
- package/dist/text.d.ts +2 -2
- package/dist/text.js +2 -2
- package/dist/types.d.ts +129 -12
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +2 -2
- package/dist/types2.d.ts +2 -2
- package/package.json +2 -1
- package/src/asset-keys.ts +25 -0
- package/src/color/contrast.ts +28 -0
- package/src/copy.ts +4 -1
- package/src/geometry.ts +64 -14
- package/src/hit-test.ts +45 -23
- package/src/images.ts +24 -32
- package/src/index.ts +8 -4
- package/src/instances/content-size.ts +63 -0
- package/src/instances/fields.ts +1 -1
- package/src/instances/sync.ts +7 -6
- package/src/instances/vector-geometry.ts +81 -0
- package/src/instances.ts +3 -0
- package/src/layout/guides.ts +40 -11
- package/src/shaders/model.ts +4 -2
- package/src/shaders/still.ts +41 -0
- package/src/shared-styles.ts +5 -2
- package/src/text/index.ts +1 -0
- package/src/text/references.ts +50 -0
- package/src/transfer/nodes.ts +12 -0
- package/src/transfer/plan.ts +25 -2
- package/src/transfer/validation.ts +6 -1
- package/src/types.ts +50 -2
- package/src/variables/bindings.ts +110 -8
- package/dist/chunks/fields/text.js +0 -36
- package/dist/chunks/fields/text.js.map +0 -1
package/src/geometry.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { arrowCapOverflow } from './arrow-caps'
|
|
2
|
-
import type { Effect, Stroke, StrokeCap, VectorNetwork } from './index'
|
|
2
|
+
import type { Effect, Stroke, StrokeCap, StrokeJoin, VectorNetwork } from './index'
|
|
3
3
|
import type { Rect, Vector } from './primitives'
|
|
4
4
|
|
|
5
5
|
export function degToRad(degrees: number): number {
|
|
@@ -138,10 +138,17 @@ export function polygonVertices(node: {
|
|
|
138
138
|
})
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
+
/** A layer's stroke join and miter limit, for the tips mitered path corners reach. */
|
|
142
|
+
export interface StrokeCorners {
|
|
143
|
+
strokeJoin?: StrokeJoin
|
|
144
|
+
strokeMiterLimit?: number
|
|
145
|
+
}
|
|
146
|
+
|
|
141
147
|
export function strokeOverflow(
|
|
142
148
|
strokes?: Stroke[],
|
|
143
149
|
fallbackCap?: StrokeCap,
|
|
144
|
-
network?: VectorNetwork | null
|
|
150
|
+
network?: VectorNetwork | null,
|
|
151
|
+
corners?: StrokeCorners
|
|
145
152
|
): number {
|
|
146
153
|
let overflow = 0
|
|
147
154
|
for (const stroke of strokes ?? []) {
|
|
@@ -149,11 +156,52 @@ export function strokeOverflow(
|
|
|
149
156
|
let extra = 0
|
|
150
157
|
if (stroke.align === 'OUTSIDE') extra = stroke.weight
|
|
151
158
|
else if (stroke.align === 'CENTER') extra = stroke.weight / 2
|
|
152
|
-
|
|
159
|
+
const miter = network
|
|
160
|
+
? miterRatio(network, stroke.join ?? corners?.strokeJoin, corners?.strokeMiterLimit)
|
|
161
|
+
: 1
|
|
162
|
+
overflow = Math.max(overflow, extra * miter)
|
|
153
163
|
}
|
|
154
164
|
return Math.max(overflow, arrowCapOverflow(strokes, fallbackCap, network))
|
|
155
165
|
}
|
|
156
166
|
|
|
167
|
+
/** The direction a segment leaves `vertex` in, along its handle when it has one. */
|
|
168
|
+
function leaving(network: VectorNetwork, index: number, vertex: number): Vector {
|
|
169
|
+
const segment = network.segments[index]
|
|
170
|
+
const atStart = segment.start === vertex
|
|
171
|
+
const handle = atStart ? segment.tangentStart : segment.tangentEnd
|
|
172
|
+
if (handle.x !== 0 || handle.y !== 0) return handle
|
|
173
|
+
const from = network.vertices[vertex]
|
|
174
|
+
const to = network.vertices[atStart ? segment.end : segment.start]
|
|
175
|
+
return { x: to.x - from.x, y: to.y - from.y }
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* How far past half the stroke the sharpest mitered corner reaches, as a multiple: a corner of
|
|
180
|
+
* angle θ mitres out to 1/sin(θ/2), unless that passes the miter limit and it bevels instead.
|
|
181
|
+
* Corners are the vertices exactly two segments meet at, as the stroke joins them.
|
|
182
|
+
*/
|
|
183
|
+
function miterRatio(network: VectorNetwork, join: StrokeJoin | undefined, limit = 4): number {
|
|
184
|
+
const meeting = new Map<number, number[]>()
|
|
185
|
+
for (const [index, segment] of network.segments.entries()) {
|
|
186
|
+
for (const vertex of new Set([segment.start, segment.end])) {
|
|
187
|
+
meeting.set(vertex, [...(meeting.get(vertex) ?? []), index])
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
let ratio = 1
|
|
191
|
+
for (const [vertex, indices] of meeting) {
|
|
192
|
+
if (indices.length !== 2) continue
|
|
193
|
+
if ((network.vertices[vertex].strokeJoin ?? join ?? 'MITER') !== 'MITER') continue
|
|
194
|
+
const a = leaving(network, indices[0], vertex)
|
|
195
|
+
const b = leaving(network, indices[1], vertex)
|
|
196
|
+
const lengths = Math.hypot(a.x, a.y) * Math.hypot(b.x, b.y)
|
|
197
|
+
if (lengths === 0) continue
|
|
198
|
+
const angle = Math.acos(Math.max(-1, Math.min(1, (a.x * b.x + a.y * b.y) / lengths)))
|
|
199
|
+
const miter = 1 / Math.sin(angle / 2)
|
|
200
|
+
if (miter <= limit) ratio = Math.max(ratio, miter)
|
|
201
|
+
}
|
|
202
|
+
return ratio
|
|
203
|
+
}
|
|
204
|
+
|
|
157
205
|
export function effectOverflow(effects?: Effect[]) {
|
|
158
206
|
let left = 0
|
|
159
207
|
let right = 0
|
|
@@ -192,16 +240,18 @@ export function computeAbsoluteBounds(
|
|
|
192
240
|
}
|
|
193
241
|
|
|
194
242
|
export function computeVisualBounds(
|
|
195
|
-
nodes: Iterable<
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
243
|
+
nodes: Iterable<
|
|
244
|
+
{
|
|
245
|
+
id: string
|
|
246
|
+
width: number
|
|
247
|
+
height: number
|
|
248
|
+
rotation?: number
|
|
249
|
+
strokes?: Stroke[]
|
|
250
|
+
strokeCap?: StrokeCap
|
|
251
|
+
vectorNetwork?: VectorNetwork | null
|
|
252
|
+
effects?: Effect[]
|
|
253
|
+
} & StrokeCorners
|
|
254
|
+
>,
|
|
205
255
|
getAbsolutePosition: (id: string) => Vector
|
|
206
256
|
): Rect {
|
|
207
257
|
const bounds = createBoundsAccumulator()
|
|
@@ -209,7 +259,7 @@ export function computeVisualBounds(
|
|
|
209
259
|
for (const n of nodes) {
|
|
210
260
|
const abs = getAbsolutePosition(n.id)
|
|
211
261
|
const bbox = rotatedBBox(abs.x, abs.y, n.width, n.height, n.rotation ?? 0)
|
|
212
|
-
const stroke = strokeOverflow(n.strokes, n.strokeCap, n.vectorNetwork)
|
|
262
|
+
const stroke = strokeOverflow(n.strokes, n.strokeCap, n.vectorNetwork, n)
|
|
213
263
|
const effects = effectOverflow(n.effects)
|
|
214
264
|
includePoint(bounds, bbox.left - stroke - effects.left, bbox.top - stroke - effects.top)
|
|
215
265
|
includePoint(bounds, bbox.right + stroke + effects.right, bbox.bottom + stroke + effects.bottom)
|
package/src/hit-test.ts
CHANGED
|
@@ -49,20 +49,22 @@ function hasTransformedAncestor(
|
|
|
49
49
|
return transformed
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
/** Whether the point lies in the node's box, grown by `pad` canvas units on every side. */
|
|
52
53
|
function containsPoint(
|
|
53
54
|
px: number,
|
|
54
55
|
py: number,
|
|
55
56
|
node: SceneNode,
|
|
56
57
|
graph: SceneGraph,
|
|
57
|
-
transformCache: Map<string, boolean
|
|
58
|
+
transformCache: Map<string, boolean>,
|
|
59
|
+
pad = 0
|
|
58
60
|
): boolean {
|
|
59
61
|
if (!hasTransformedAncestor(node, graph, transformCache)) {
|
|
60
62
|
const absolute = graph.getAbsolutePosition(node.id)
|
|
61
63
|
return (
|
|
62
|
-
px >= absolute.x &&
|
|
63
|
-
px <= absolute.x + node.width &&
|
|
64
|
-
py >= absolute.y &&
|
|
65
|
-
py <= absolute.y + node.height
|
|
64
|
+
px >= absolute.x - pad &&
|
|
65
|
+
px <= absolute.x + node.width + pad &&
|
|
66
|
+
py >= absolute.y - pad &&
|
|
67
|
+
py <= absolute.y + node.height + pad
|
|
66
68
|
)
|
|
67
69
|
}
|
|
68
70
|
|
|
@@ -72,7 +74,9 @@ function containsPoint(
|
|
|
72
74
|
if (!inv) return false
|
|
73
75
|
|
|
74
76
|
const [localX, localY] = Matrix.mapPoints(inv, [px, py])
|
|
75
|
-
return
|
|
77
|
+
return (
|
|
78
|
+
localX >= -pad && localX <= node.width + pad && localY >= -pad && localY <= node.height + pad
|
|
79
|
+
)
|
|
76
80
|
}
|
|
77
81
|
|
|
78
82
|
function hitTestOpaqueContainer(
|
|
@@ -82,10 +86,11 @@ function hitTestOpaqueContainer(
|
|
|
82
86
|
child: SceneNode,
|
|
83
87
|
childId: string,
|
|
84
88
|
deep: boolean,
|
|
85
|
-
transformCache: Map<string, boolean
|
|
89
|
+
transformCache: Map<string, boolean>,
|
|
90
|
+
pad: number
|
|
86
91
|
): SceneNode | null {
|
|
87
92
|
if (!containsPoint(px, py, child, graph, transformCache)) return null
|
|
88
|
-
const childHit = hitTestChildren(graph, px, py, childId, deep, transformCache)
|
|
93
|
+
const childHit = hitTestChildren(graph, px, py, childId, deep, transformCache, pad)
|
|
89
94
|
if (childHit) return child
|
|
90
95
|
if (hasVisibleFillOrStroke(child)) return child
|
|
91
96
|
return null
|
|
@@ -97,17 +102,18 @@ function hitTestTransparentContainer(
|
|
|
97
102
|
child: SceneNode,
|
|
98
103
|
childId: string,
|
|
99
104
|
deep: boolean,
|
|
100
|
-
transformCache: Map<string, boolean
|
|
105
|
+
transformCache: Map<string, boolean>,
|
|
106
|
+
pad: number
|
|
101
107
|
): SceneNode | null {
|
|
102
108
|
if (child.type === 'GROUP') {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
return child
|
|
109
|
+
// A group fits its layers, so a padded click on one at its edge falls just outside it.
|
|
110
|
+
if (!containsPoint(px, py, child, graph, transformCache, pad)) return null
|
|
111
|
+
const childHit = hitTestChildren(graph, px, py, childId, deep, transformCache, pad)
|
|
112
|
+
if (childHit) return deep ? childHit : child
|
|
113
|
+
return containsPoint(px, py, child, graph, transformCache) ? child : null
|
|
108
114
|
}
|
|
109
115
|
|
|
110
|
-
const childHit = hitTestChildren(graph, px, py, childId, deep, transformCache)
|
|
116
|
+
const childHit = hitTestChildren(graph, px, py, childId, deep, transformCache, pad)
|
|
111
117
|
if (childHit) {
|
|
112
118
|
if (child.locked) return child
|
|
113
119
|
return childHit
|
|
@@ -121,13 +127,18 @@ function hitTestTransparentContainer(
|
|
|
121
127
|
return null
|
|
122
128
|
}
|
|
123
129
|
|
|
130
|
+
/**
|
|
131
|
+
* The topmost layer under the point among a container's children. `pad`, in canvas units, grows
|
|
132
|
+
* each leaf layer's box, as Figma takes a click a few screen pixels outside a shape.
|
|
133
|
+
*/
|
|
124
134
|
function hitTestChildren(
|
|
125
135
|
graph: SceneGraph,
|
|
126
136
|
px: number,
|
|
127
137
|
py: number,
|
|
128
138
|
parentId: string,
|
|
129
139
|
deep = false,
|
|
130
|
-
transformCache = new Map<string, boolean>()
|
|
140
|
+
transformCache = new Map<string, boolean>(),
|
|
141
|
+
pad = 0
|
|
131
142
|
): SceneNode | null {
|
|
132
143
|
const parent = graph.nodes.get(parentId)
|
|
133
144
|
if (!parent) return null
|
|
@@ -142,17 +153,26 @@ function hitTestChildren(
|
|
|
142
153
|
if (!child || child.internalOnly || !child.visible) continue
|
|
143
154
|
if (CONTAINER_TYPES.has(child.type)) {
|
|
144
155
|
if (OPAQUE_CONTAINER_TYPES.has(child.type) && !deep) {
|
|
145
|
-
const hit = hitTestOpaqueContainer(graph, px, py, child, childId, deep, transformCache)
|
|
156
|
+
const hit = hitTestOpaqueContainer(graph, px, py, child, childId, deep, transformCache, pad)
|
|
146
157
|
if (hit) return hit
|
|
147
158
|
continue
|
|
148
159
|
}
|
|
149
160
|
|
|
150
|
-
const hit = hitTestTransparentContainer(
|
|
161
|
+
const hit = hitTestTransparentContainer(
|
|
162
|
+
graph,
|
|
163
|
+
px,
|
|
164
|
+
py,
|
|
165
|
+
child,
|
|
166
|
+
childId,
|
|
167
|
+
deep,
|
|
168
|
+
transformCache,
|
|
169
|
+
pad
|
|
170
|
+
)
|
|
151
171
|
if (hit) return hit
|
|
152
172
|
continue
|
|
153
173
|
}
|
|
154
174
|
|
|
155
|
-
if (containsPoint(px, py, child, graph, transformCache)) return child
|
|
175
|
+
if (containsPoint(px, py, child, graph, transformCache, pad)) return child
|
|
156
176
|
}
|
|
157
177
|
|
|
158
178
|
return null
|
|
@@ -172,10 +192,11 @@ export function hitTestDeep(
|
|
|
172
192
|
graph: SceneGraph,
|
|
173
193
|
px: number,
|
|
174
194
|
py: number,
|
|
175
|
-
scopeId?: string
|
|
195
|
+
scopeId?: string,
|
|
196
|
+
pad = 0
|
|
176
197
|
): SceneNode | null {
|
|
177
198
|
const scope = scopeId ?? graph.rootId
|
|
178
|
-
return hitTestChildren(graph, px, py, scope, true)
|
|
199
|
+
return hitTestChildren(graph, px, py, scope, true, new Map(), pad)
|
|
179
200
|
}
|
|
180
201
|
|
|
181
202
|
/**
|
|
@@ -253,9 +274,10 @@ export function hitTestSelectable(
|
|
|
253
274
|
px: number,
|
|
254
275
|
py: number,
|
|
255
276
|
scopeId: string,
|
|
256
|
-
selectedIds: ReadonlySet<string
|
|
277
|
+
selectedIds: ReadonlySet<string>,
|
|
278
|
+
pad = 0
|
|
257
279
|
): SceneNode | null {
|
|
258
|
-
const deepest = hitTestChildren(graph, px, py, scopeId, true)
|
|
280
|
+
const deepest = hitTestChildren(graph, px, py, scopeId, true, new Map(), pad)
|
|
259
281
|
if (!deepest) {
|
|
260
282
|
const scope = graph.nodes.get(scopeId)
|
|
261
283
|
return scope ? frameChildAt(graph, scope, px, py) : null
|
package/src/images.ts
CHANGED
|
@@ -1,34 +1,26 @@
|
|
|
1
|
+
import { sha1 } from '@noble/hashes/legacy.js'
|
|
2
|
+
import { bytesToHex } from '@noble/hashes/utils.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The name an image's bytes are stored under: the hex SHA-1 of the bytes, as Figma names images,
|
|
6
|
+
* so `.fig` files and the Figma clipboard can carry the name as it is. Documents saved before may
|
|
7
|
+
* hold images under other names; anything that hands images to Figma still checks.
|
|
8
|
+
*/
|
|
1
9
|
export function computeImageHash(data: Uint8Array): string {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
case 2:
|
|
19
|
-
h3 ^= b
|
|
20
|
-
h3 = Math.imul(h3, 0x01000193) >>> 0
|
|
21
|
-
break
|
|
22
|
-
case 3:
|
|
23
|
-
h4 ^= b
|
|
24
|
-
h4 = Math.imul(h4, 0x01000193) >>> 0
|
|
25
|
-
break
|
|
26
|
-
default:
|
|
27
|
-
h5 ^= b
|
|
28
|
-
h5 = Math.imul(h5, 0x01000193) >>> 0
|
|
29
|
-
break
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
h5 = Math.imul(h5 ^ data.length, 0x01000193) >>> 0
|
|
33
|
-
return [h1, h2, h3, h4, h5].map((hash) => hash.toString(16).padStart(8, '0')).join('')
|
|
10
|
+
return bytesToHex(sha1(data))
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type ImageMimeType = 'image/png' | 'image/jpeg' | 'image/gif' | 'image/webp'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The media type of encoded image bytes, read from their signature, for writing them out as a
|
|
17
|
+
* data URL or file. Bytes with no known signature are reported as PNG, as image fills began.
|
|
18
|
+
*/
|
|
19
|
+
export function imageMimeType(data: Uint8Array): ImageMimeType {
|
|
20
|
+
if (data[0] === 0xff && data[1] === 0xd8 && data[2] === 0xff) return 'image/jpeg'
|
|
21
|
+
if (data[0] === 0x47 && data[1] === 0x49 && data[2] === 0x46) return 'image/gif'
|
|
22
|
+
const riff = data[0] === 0x52 && data[1] === 0x49 && data[2] === 0x46 && data[3] === 0x46
|
|
23
|
+
const webp = data[8] === 0x57 && data[9] === 0x45 && data[10] === 0x42 && data[11] === 0x50
|
|
24
|
+
if (riff && webp) return 'image/webp'
|
|
25
|
+
return 'image/png'
|
|
34
26
|
}
|
package/src/index.ts
CHANGED
|
@@ -12,6 +12,7 @@ export { TRANSFORM_FIELDS, SIZE_FIELDS } from './fields/geometry'
|
|
|
12
12
|
export { TEXT_METRIC_FIELDS, TEXT_SHAPING_FIELDS, TEXT_LAYOUT_FIELDS } from './fields/text'
|
|
13
13
|
export * from './transfer'
|
|
14
14
|
export * from './instance-overrides'
|
|
15
|
+
export * from './asset-keys'
|
|
15
16
|
export * from './images'
|
|
16
17
|
export * from './components/properties'
|
|
17
18
|
export * from './components/variants'
|
|
@@ -33,6 +34,7 @@ export * from './icons/model'
|
|
|
33
34
|
export * from './icons/glyph'
|
|
34
35
|
export * from './shaders/schema'
|
|
35
36
|
export * from './shaders/model'
|
|
37
|
+
export * from './shaders/still'
|
|
36
38
|
export * from './behaviours/layers'
|
|
37
39
|
export * from './behaviours/model'
|
|
38
40
|
export * from './behaviours/spec'
|
|
@@ -853,17 +855,19 @@ export class SceneGraph {
|
|
|
853
855
|
return HitTest.hitTest(this, px, py, scopeId)
|
|
854
856
|
}
|
|
855
857
|
|
|
856
|
-
|
|
857
|
-
|
|
858
|
+
/** `pad`, in canvas units, grows each shape's box, as Figma takes clicks just outside one. */
|
|
859
|
+
hitTestDeep(px: number, py: number, scopeId?: string, pad = 0): SceneNode | null {
|
|
860
|
+
return HitTest.hitTestDeep(this, px, py, scopeId, pad)
|
|
858
861
|
}
|
|
859
862
|
|
|
860
863
|
hitTestSelectable(
|
|
861
864
|
px: number,
|
|
862
865
|
py: number,
|
|
863
866
|
scopeId: string,
|
|
864
|
-
selectedIds: ReadonlySet<string
|
|
867
|
+
selectedIds: ReadonlySet<string>,
|
|
868
|
+
pad = 0
|
|
865
869
|
): SceneNode | null {
|
|
866
|
-
return HitTest.hitTestSelectable(this, px, py, scopeId, selectedIds)
|
|
870
|
+
return HitTest.hitTestSelectable(this, px, py, scopeId, selectedIds, pad)
|
|
867
871
|
}
|
|
868
872
|
|
|
869
873
|
hitTestOpenContainer(px: number, py: number, scopeId: string): SceneNode | null {
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { isEqual } from 'es-toolkit/predicate'
|
|
2
|
+
|
|
3
|
+
import type { SceneGraph, SceneNode } from '../'
|
|
4
|
+
import { TEXT_METRIC_FIELDS, TEXT_SHAPING_FIELDS } from '../fields/text'
|
|
5
|
+
import { layoutSizing } from '../layout/sizing'
|
|
6
|
+
|
|
7
|
+
const METRIC_FIELDS: ReadonlySet<string> = new Set(TEXT_METRIC_FIELDS)
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The axes along which a copy sizes itself to its own content rather than to its component,
|
|
11
|
+
* judged by the state the copy is about to have.
|
|
12
|
+
*/
|
|
13
|
+
function contentSizedAxes(
|
|
14
|
+
graph: SceneGraph,
|
|
15
|
+
current: SceneNode,
|
|
16
|
+
source: SceneNode,
|
|
17
|
+
updates: Partial<SceneNode>
|
|
18
|
+
): { width: boolean; height: boolean } {
|
|
19
|
+
const copy: SceneNode = { ...current, ...updates }
|
|
20
|
+
// Text measures its own characters and their style, set on the copy or by a property of an
|
|
21
|
+
// instance around it; a copy styled as its component takes the component's measure.
|
|
22
|
+
if (copy.type === 'TEXT') {
|
|
23
|
+
if (TEXT_SHAPING_FIELDS.every((field) => isEqual(copy[field], source[field])))
|
|
24
|
+
return { width: false, height: false }
|
|
25
|
+
const mode = copy.textAutoResize
|
|
26
|
+
return {
|
|
27
|
+
width: mode === 'WIDTH_AND_HEIGHT',
|
|
28
|
+
height: mode === 'WIDTH_AND_HEIGHT' || mode === 'HEIGHT'
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
// Auto layout hugs the copy's own layers, which may show or size differently, and a layer
|
|
32
|
+
// that fills its parent takes the size layout gives it there.
|
|
33
|
+
const laidOut = (axis: 'HORIZONTAL' | 'VERTICAL') => {
|
|
34
|
+
const sizing = layoutSizing(graph, copy, axis)
|
|
35
|
+
return sizing === 'FILL' || (sizing === 'HUG' && copy.layoutMode !== 'NONE')
|
|
36
|
+
}
|
|
37
|
+
return { width: laidOut('HORIZONTAL'), height: laidOut('VERTICAL') }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Keeps a copy's own size along the axes it sizes to its own content, as Figma sizes a badge to
|
|
42
|
+
* its count and the layers it shows rather than to the component's placeholder. Layout measures
|
|
43
|
+
* those axes; the component's size there would be stale.
|
|
44
|
+
*/
|
|
45
|
+
export function keepOwnContentSize(
|
|
46
|
+
graph: SceneGraph,
|
|
47
|
+
updates: Partial<SceneNode>,
|
|
48
|
+
copy: SceneNode,
|
|
49
|
+
source: SceneNode
|
|
50
|
+
): void {
|
|
51
|
+
const axes = contentSizedAxes(graph, copy, source, updates)
|
|
52
|
+
if (axes.width) Reflect.deleteProperty(updates, 'width')
|
|
53
|
+
if (axes.height) Reflect.deleteProperty(updates, 'height')
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Drops the size a text copy kept from the file once a metric reaches it from the component, so
|
|
58
|
+
* layout measures its characters again instead of trusting the size Figma saved for the old style.
|
|
59
|
+
*/
|
|
60
|
+
export function remeasureChangedText(updates: Partial<SceneNode>, copy: SceneNode): void {
|
|
61
|
+
if (copy.type !== 'TEXT' || !copy.derivedLayout) return
|
|
62
|
+
if (Object.keys(updates).some((key) => METRIC_FIELDS.has(key))) updates.derivedLayout = null
|
|
63
|
+
}
|
package/src/instances/fields.ts
CHANGED
|
@@ -77,7 +77,6 @@ export const INSTANCE_SYNC_PROPS: (keyof SceneNode)[] = [
|
|
|
77
77
|
'gridRowGap',
|
|
78
78
|
'gridPosition',
|
|
79
79
|
'clipsContent',
|
|
80
|
-
'counterAxisAlignContent',
|
|
81
80
|
'itemReverseZIndex',
|
|
82
81
|
'strokesIncludedInLayout',
|
|
83
82
|
'layoutGrids',
|
|
@@ -121,6 +120,7 @@ export const INSTANCE_FOLLOWED_FIELDS = [
|
|
|
121
120
|
'horizontalConstraint',
|
|
122
121
|
'verticalConstraint',
|
|
123
122
|
'layoutPositioning',
|
|
123
|
+
'counterAxisAlignContent',
|
|
124
124
|
'isMask',
|
|
125
125
|
'maskType',
|
|
126
126
|
'arcData',
|
package/src/instances/sync.ts
CHANGED
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
import { scaleNodeChanges } from '../scaling/node'
|
|
14
14
|
import { ownsSlotContent, slotPropertyId } from '../slots/frames'
|
|
15
15
|
import { scaleVariableBindingUnits } from '../variables/units'
|
|
16
|
+
import { keepOwnContentSize, remeasureChangedText } from './content-size'
|
|
16
17
|
import {
|
|
17
18
|
createInstanceSyncContext,
|
|
18
19
|
hasUnplacedCopiesBelow,
|
|
@@ -22,6 +23,7 @@ import {
|
|
|
22
23
|
} from './copies'
|
|
23
24
|
import { INSTANCE_SYNC_FIELD_OWNERS, INSTANCE_SYNC_FIELDS } from './fields'
|
|
24
25
|
import { PLACEMENT_FIELDS, placedByConstraints } from './placement'
|
|
26
|
+
import { followVectorGeometry } from './vector-geometry'
|
|
25
27
|
|
|
26
28
|
function setSceneProp<K extends keyof SceneNode>(
|
|
27
29
|
target: Partial<SceneNode>,
|
|
@@ -151,6 +153,8 @@ function copySyncedFields(
|
|
|
151
153
|
if (isEqual(copy[key], from[key])) continue
|
|
152
154
|
copyProp(updates, from, key)
|
|
153
155
|
}
|
|
156
|
+
followVectorGeometry(updates, copy, source)
|
|
157
|
+
remeasureChangedText(updates, copy)
|
|
154
158
|
}
|
|
155
159
|
|
|
156
160
|
export function updateSyncedProps(
|
|
@@ -187,9 +191,7 @@ export function cloneChildrenWithMapping(
|
|
|
187
191
|
destParentId: string,
|
|
188
192
|
mode: NodeCloneMode = 'deep'
|
|
189
193
|
): void {
|
|
190
|
-
//
|
|
191
|
-
// a self-referential or cyclic component (e.g. an INSTANCE whose componentId points
|
|
192
|
-
// to an ancestor) causes unbounded recursion and eventual stack overflow / OOM.
|
|
194
|
+
// A cyclic component, such as an instance of an ancestor, would clone itself without end.
|
|
193
195
|
if (sourceParentId === destParentId || graph.isDescendant(destParentId, sourceParentId)) return
|
|
194
196
|
|
|
195
197
|
const sourceParent = graph.nodes.get(sourceParentId)
|
|
@@ -519,9 +521,7 @@ export function syncChildren(
|
|
|
519
521
|
overrides: InstanceOverrideState,
|
|
520
522
|
context: InstanceSyncContext = createInstanceSyncContext(instParentId, overrides)
|
|
521
523
|
): void {
|
|
522
|
-
//
|
|
523
|
-
// subtree, syncing would clone the component into itself — a self-referential cycle
|
|
524
|
-
// that causes unbounded recursion and OOM.
|
|
524
|
+
// An instance inside its own component would clone the component into itself without end.
|
|
525
525
|
if (isCyclicSync(graph, compParentId, instParentId)) return
|
|
526
526
|
|
|
527
527
|
const compParent = graph.nodes.get(compParentId)
|
|
@@ -581,6 +581,7 @@ export function syncChildren(
|
|
|
581
581
|
skip: (key) => driven.has(key) || isProtectedSyncField(instChild, key, protectedField)
|
|
582
582
|
})
|
|
583
583
|
Object.assign(updates, assignedFieldValues(graph, instChild, compChild))
|
|
584
|
+
keepOwnContentSize(graph, updates, instChild, source)
|
|
584
585
|
updateSyncedProps(graph, instChild, updates)
|
|
585
586
|
|
|
586
587
|
if (
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { isEqual } from 'es-toolkit/predicate'
|
|
2
|
+
|
|
3
|
+
import type { SceneNode, VectorNetwork } from '../'
|
|
4
|
+
import { copyGeometryPaths, scaleGeometryPaths } from '../copy'
|
|
5
|
+
import { cloneVectorNetwork, transformVectorNetwork } from '../vector-network'
|
|
6
|
+
|
|
7
|
+
/** How far apart two paths' points may be and still count as one path, in pixels. */
|
|
8
|
+
const PATH_TOLERANCE = 1e-3
|
|
9
|
+
|
|
10
|
+
/** The stroke fields a vector's stroke outline is drawn from. */
|
|
11
|
+
const STROKE_OUTLINE_FIELDS = [
|
|
12
|
+
'strokes',
|
|
13
|
+
'strokeWeight',
|
|
14
|
+
'strokeAlign',
|
|
15
|
+
'strokeCap',
|
|
16
|
+
'strokeJoin',
|
|
17
|
+
'strokeMiterLimit',
|
|
18
|
+
'dashPattern'
|
|
19
|
+
] as const
|
|
20
|
+
|
|
21
|
+
const near = (a: number, b: number) => Math.abs(a - b) <= PATH_TOLERANCE
|
|
22
|
+
|
|
23
|
+
/** Whether two paths are one, up to the float error that files and scaling leave. */
|
|
24
|
+
function samePath(a: VectorNetwork | null, b: VectorNetwork | null): boolean {
|
|
25
|
+
if (!a || !b) return a === b
|
|
26
|
+
if (a.vertices.length !== b.vertices.length || a.segments.length !== b.segments.length)
|
|
27
|
+
return false
|
|
28
|
+
const sameVertices = a.vertices.every((vertex, index) => {
|
|
29
|
+
const { x, y, ...rest } = vertex
|
|
30
|
+
const { x: otherX, y: otherY, ...otherRest } = b.vertices[index]
|
|
31
|
+
return near(x, otherX) && near(y, otherY) && isEqual(rest, otherRest)
|
|
32
|
+
})
|
|
33
|
+
const sameSegments = a.segments.every((segment, index) => {
|
|
34
|
+
const other = b.segments[index]
|
|
35
|
+
return (
|
|
36
|
+
segment.start === other.start &&
|
|
37
|
+
segment.end === other.end &&
|
|
38
|
+
near(segment.tangentStart.x, other.tangentStart.x) &&
|
|
39
|
+
near(segment.tangentStart.y, other.tangentStart.y) &&
|
|
40
|
+
near(segment.tangentEnd.x, other.tangentEnd.x) &&
|
|
41
|
+
near(segment.tangentEnd.y, other.tangentEnd.y)
|
|
42
|
+
)
|
|
43
|
+
})
|
|
44
|
+
return sameVertices && sameSegments && isEqual(a.regions, b.regions)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Gives a vector's copy its component's path, which Figma refuses to override in an instance
|
|
49
|
+
* ("vector-data"), so every edit to the path reaches the copies, drawn at the size the copy ends
|
|
50
|
+
* up with. A copy whose path already matches keeps its own outlines, which may differ from the
|
|
51
|
+
* component's where the instance overrides its fills or strokes. A changed path brings the
|
|
52
|
+
* component's outlines with it, the stroke's only while the copy strokes as the component does;
|
|
53
|
+
* otherwise the canvas draws the stroke from the new path.
|
|
54
|
+
*/
|
|
55
|
+
export function followVectorGeometry(
|
|
56
|
+
updates: Partial<SceneNode>,
|
|
57
|
+
copy: SceneNode,
|
|
58
|
+
source: SceneNode
|
|
59
|
+
): void {
|
|
60
|
+
if (source.type !== 'VECTOR') return
|
|
61
|
+
const width = updates.width ?? copy.width
|
|
62
|
+
const height = updates.height ?? copy.height
|
|
63
|
+
// A straight line has no extent across it, so that axis keeps its scale.
|
|
64
|
+
const sx = source.width > 0 ? width / source.width : 1
|
|
65
|
+
const sy = source.height > 0 ? height / source.height : 1
|
|
66
|
+
const scaled = sx !== 1 || sy !== 1
|
|
67
|
+
const scaledNetwork = (network: VectorNetwork) =>
|
|
68
|
+
scaled
|
|
69
|
+
? transformVectorNetwork([sx, 0, 0, 0, sy, 0, 0, 0, 1], network)
|
|
70
|
+
: cloneVectorNetwork(network)
|
|
71
|
+
const network = source.vectorNetwork ? scaledNetwork(source.vectorNetwork) : null
|
|
72
|
+
if (samePath(copy.vectorNetwork, network)) return
|
|
73
|
+
const outlines = (paths: SceneNode['fillGeometry']) =>
|
|
74
|
+
scaled ? scaleGeometryPaths(paths, sx, sy) : copyGeometryPaths(paths)
|
|
75
|
+
const strokesAlike = STROKE_OUTLINE_FIELDS.every((field) =>
|
|
76
|
+
isEqual(updates[field] ?? copy[field], source[field])
|
|
77
|
+
)
|
|
78
|
+
updates.vectorNetwork = network
|
|
79
|
+
updates.fillGeometry = outlines(source.fillGeometry)
|
|
80
|
+
updates.strokeGeometry = strokesAlike ? outlines(source.strokeGeometry) : []
|
|
81
|
+
}
|
package/src/instances.ts
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
hasInstanceOverride as hasNodeInstanceOverride,
|
|
7
7
|
setInstanceOverride
|
|
8
8
|
} from './instance-overrides'
|
|
9
|
+
import { keepOwnContentSize } from './instances/content-size'
|
|
9
10
|
import {
|
|
10
11
|
createInstanceSyncContext,
|
|
11
12
|
groupCopyRemovals,
|
|
@@ -194,6 +195,8 @@ function syncInstancesOf(
|
|
|
194
195
|
if (isProtectedSyncField(instance, key, protectedField)) continue
|
|
195
196
|
copyProp(updates, source, key)
|
|
196
197
|
}
|
|
198
|
+
// An instance that hugs its layers sizes to what it shows, not to the component.
|
|
199
|
+
keepOwnContentSize(graph, updates, instance, source)
|
|
197
200
|
updateSyncedProps(graph, instance, updates)
|
|
198
201
|
const context = createInstanceSyncContext(instance.id, instance.instanceOverrides)
|
|
199
202
|
syncChildren(graph, component.id, instance.id, instance.instanceOverrides, context)
|