@open-pencil/scene-graph 0.17.0-beta.1 → 0.17.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/dist/arrow-caps.d.ts.map +1 -1
- package/dist/arrow-caps.js +2 -5
- package/dist/arrow-caps.js.map +1 -1
- package/dist/behaviours/index.d.ts +2 -2
- package/dist/behaviours/index.js +2 -2
- package/dist/behaviours/kinds.d.ts +11 -5
- package/dist/behaviours/kinds.d.ts.map +1 -1
- package/dist/behaviours/kinds.js +76 -75
- package/dist/behaviours/kinds.js.map +1 -1
- package/dist/behaviours/layers.d.ts.map +1 -1
- package/dist/behaviours/layers.js +3 -2
- package/dist/behaviours/layers.js.map +1 -1
- package/dist/behaviours/model.d.ts +7 -4
- package/dist/behaviours/model.d.ts.map +1 -1
- package/dist/behaviours/model.js +16 -9
- package/dist/behaviours/model.js.map +1 -1
- package/dist/behaviours/schema.d.ts +118 -4
- package/dist/behaviours/schema.d.ts.map +1 -1
- package/dist/behaviours/schema.js +18 -3
- package/dist/behaviours/schema.js.map +1 -1
- package/dist/behaviours/spec.d.ts.map +1 -1
- package/dist/behaviours/spec.js +3 -2
- package/dist/behaviours/spec.js.map +1 -1
- package/dist/bindings.js +9 -1
- package/dist/bindings.js.map +1 -1
- package/dist/checkpoint.d.ts +31 -2
- package/dist/checkpoint.d.ts.map +1 -1
- package/dist/checkpoint.js +176 -5
- package/dist/checkpoint.js.map +1 -1
- package/dist/components/index.d.ts +3 -3
- package/dist/components/index.js +3 -3
- package/dist/components/properties.d.ts +13 -1
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +58 -3
- package/dist/components/properties.js.map +1 -1
- package/dist/components/variants.d.ts +16 -10
- package/dist/components/variants.d.ts.map +1 -1
- package/dist/components/variants.js +39 -13
- package/dist/components/variants.js.map +1 -1
- package/dist/connectors/hit.d.ts.map +1 -1
- package/dist/connectors/hit.js +1 -7
- package/dist/connectors/hit.js.map +1 -1
- package/dist/connectors/pieces.d.ts.map +1 -1
- package/dist/connectors/pieces.js +2 -5
- package/dist/connectors/pieces.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +9 -15
- package/dist/copy.js.map +1 -1
- package/dist/css/blend.d.ts +11 -0
- package/dist/css/blend.d.ts.map +1 -0
- package/dist/css/blend.js +30 -0
- package/dist/css/blend.js.map +1 -0
- package/dist/css/index.d.ts +2 -1
- package/dist/css/index.js +2 -1
- package/dist/cubic.d.ts +7 -0
- package/dist/cubic.d.ts.map +1 -0
- package/dist/cubic.js +17 -0
- package/dist/cubic.js.map +1 -0
- package/dist/export-format.d.ts +14 -1
- package/dist/export-format.d.ts.map +1 -1
- package/dist/export-format.js +26 -1
- package/dist/export-format.js.map +1 -1
- package/dist/fields/text.d.ts +2 -2
- package/dist/fields/text.d.ts.map +1 -1
- package/dist/fields/text.js +2 -1
- package/dist/fields/text.js.map +1 -1
- package/dist/figjam/index.d.ts +2 -2
- package/dist/figjam/index.js +2 -2
- package/dist/figjam/palette.d.ts +13 -2
- package/dist/figjam/palette.d.ts.map +1 -1
- package/dist/figjam/palette.js +30 -2
- package/dist/figjam/palette.js.map +1 -1
- package/dist/generated/index.d.ts +2 -2
- package/dist/generated/index.js +2 -2
- package/dist/generated/shape-with-text.d.ts +7 -2
- package/dist/generated/shape-with-text.d.ts.map +1 -1
- package/dist/generated/shape-with-text.js +20 -3
- package/dist/generated/shape-with-text.js.map +1 -1
- package/dist/geometry.d.ts +30 -46
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +63 -131
- package/dist/geometry.js.map +1 -1
- package/dist/graph.d.ts +1 -0
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +15 -3
- package/dist/graph.js.map +1 -1
- package/dist/guides.d.ts +3 -1
- package/dist/guides.d.ts.map +1 -1
- package/dist/guides.js +5 -1
- package/dist/guides.js.map +1 -1
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +42 -29
- package/dist/hit-test.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/fields.d.ts +2 -2
- package/dist/instances/fields.d.ts.map +1 -1
- package/dist/instances/fields.js +8 -0
- package/dist/instances/fields.js.map +1 -1
- package/dist/instances/index.d.ts +1 -3
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +36 -30
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/kept-overrides.js +204 -0
- package/dist/instances/kept-overrides.js.map +1 -0
- package/dist/instances/main-component.d.ts +3 -1
- package/dist/instances/main-component.d.ts.map +1 -1
- package/dist/instances/main-component.js +10 -2
- package/dist/instances/main-component.js.map +1 -1
- package/dist/instances/overrides.d.ts +4 -1
- package/dist/instances/overrides.d.ts.map +1 -1
- package/dist/instances/overrides.js +18 -2
- package/dist/instances/overrides.js.map +1 -1
- package/dist/instances/swap.js +288 -0
- package/dist/instances/swap.js.map +1 -0
- package/dist/instances/sync.js +84 -32
- package/dist/instances/sync.js.map +1 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +1 -1
- package/dist/node-defaults.js.map +1 -1
- package/dist/outline/flatten.d.ts +15 -0
- package/dist/outline/flatten.d.ts.map +1 -0
- package/dist/outline/flatten.js +189 -0
- package/dist/outline/flatten.js.map +1 -0
- package/dist/outline/heads.js +34 -0
- package/dist/outline/heads.js.map +1 -0
- package/dist/outline/hit.d.ts +21 -0
- package/dist/outline/hit.d.ts.map +1 -0
- package/dist/outline/hit.js +276 -0
- package/dist/outline/hit.js.map +1 -0
- package/dist/outline/index.d.ts +5 -0
- package/dist/outline/index.js +5 -0
- package/dist/outline/point.d.ts +16 -0
- package/dist/outline/point.d.ts.map +1 -0
- package/dist/outline/point.js +42 -0
- package/dist/outline/point.js.map +1 -0
- package/dist/outline/shapes.d.ts +35 -0
- package/dist/outline/shapes.d.ts.map +1 -0
- package/dist/outline/shapes.js +181 -0
- package/dist/outline/shapes.js.map +1 -0
- package/dist/plugin-data/fields.d.ts +2 -1
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +19 -4
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/scaling/tree.js +1 -1
- package/dist/shared-styles.d.ts +14 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +63 -2
- package/dist/shared-styles.js.map +1 -1
- package/dist/slots/content.d.ts.map +1 -1
- package/dist/slots/content.js +2 -0
- package/dist/slots/content.js.map +1 -1
- package/dist/slots/frames.d.ts +2 -0
- package/dist/slots/frames.d.ts.map +1 -1
- package/dist/slots/frames.js +7 -2
- package/dist/slots/frames.js.map +1 -1
- package/dist/text/index.d.ts +2 -2
- package/dist/text/index.js +3 -3
- package/dist/text/lengths.d.ts +6 -1
- package/dist/text/lengths.d.ts.map +1 -1
- package/dist/text/lengths.js +10 -1
- package/dist/text/lengths.js.map +1 -1
- package/dist/text/overflow.d.ts +2 -2
- package/dist/text/overflow.js.map +1 -1
- package/dist/text/picture.d.ts +1 -2
- package/dist/text/picture.d.ts.map +1 -1
- package/dist/text/picture.js +1 -4
- package/dist/text/picture.js.map +1 -1
- package/dist/transfer/index.js +1 -1
- package/dist/transfer/nodes.js +9 -5
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/types.d.ts +27 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/variables/bindings.d.ts +28 -5
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +104 -14
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/fields.d.ts +19 -1
- package/dist/variables/fields.d.ts.map +1 -1
- package/dist/variables/fields.js +36 -1
- package/dist/variables/fields.js.map +1 -1
- package/dist/variables/index.d.ts +3 -3
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +23 -8
- package/dist/variables/index.js.map +1 -1
- package/package.json +7 -1
- package/src/arrow-caps.ts +2 -5
- package/src/behaviours/kinds.ts +51 -34
- package/src/behaviours/layers.ts +4 -2
- package/src/behaviours/model.ts +22 -11
- package/src/behaviours/schema.ts +20 -4
- package/src/behaviours/spec.ts +8 -2
- package/src/bindings.ts +11 -2
- package/src/checkpoint.ts +251 -4
- package/src/components/properties.ts +92 -2
- package/src/components/variants.ts +51 -30
- package/src/connectors/hit.ts +1 -12
- package/src/connectors/pieces.ts +2 -5
- package/src/copy.ts +9 -15
- package/src/css/blend.ts +27 -0
- package/src/css/index.ts +1 -0
- package/src/cubic.ts +14 -0
- package/src/export-format.ts +29 -0
- package/src/fields/text.ts +3 -1
- package/src/figjam/palette.ts +29 -1
- package/src/generated/shape-with-text.ts +28 -2
- package/src/geometry.ts +78 -198
- package/src/graph.ts +27 -3
- package/src/guides.ts +5 -0
- package/src/hit-test.ts +67 -54
- package/src/instances/fields.ts +8 -0
- package/src/instances/index.ts +58 -31
- package/src/instances/kept-overrides.ts +280 -0
- package/src/instances/main-component.ts +12 -0
- package/src/instances/overrides.ts +27 -1
- package/src/instances/swap.ts +451 -0
- package/src/instances/sync.ts +141 -42
- package/src/node-defaults.ts +2 -1
- package/src/outline/flatten.ts +190 -0
- package/src/outline/heads.ts +52 -0
- package/src/outline/hit.ts +369 -0
- package/src/outline/index.ts +4 -0
- package/src/outline/point.ts +59 -0
- package/src/outline/shapes.ts +207 -0
- package/src/plugin-data/fields.ts +15 -2
- package/src/shared-styles.ts +82 -1
- package/src/slots/content.ts +2 -0
- package/src/slots/frames.ts +9 -7
- package/src/text/lengths.ts +10 -0
- package/src/text/overflow.ts +2 -2
- package/src/text/picture.ts +0 -4
- package/src/transfer/nodes.ts +7 -5
- package/src/types.ts +28 -0
- package/src/variables/bindings.ts +125 -14
- package/src/variables/fields.ts +46 -1
- package/src/variables/index.ts +35 -2
package/src/export-format.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { omitBy } from 'es-toolkit/object'
|
|
2
|
+
|
|
3
|
+
import type { ExportSetting } from './types'
|
|
4
|
+
|
|
1
5
|
/**
|
|
2
6
|
* Formats a node's export settings may persist. Each id must name a Core IO
|
|
3
7
|
* export adapter that supports node export; labels and options come from that
|
|
@@ -10,3 +14,28 @@ export type ExportFormatId = (typeof EXPORT_FORMAT_IDS)[number]
|
|
|
10
14
|
export function isExportFormatId(value: unknown): value is ExportFormatId {
|
|
11
15
|
return EXPORT_FORMAT_IDS.some((id) => id === value)
|
|
12
16
|
}
|
|
17
|
+
|
|
18
|
+
/** What each of Figma's export options reads as when an export leaves it out. */
|
|
19
|
+
export const EXPORT_OPTION_DEFAULTS = {
|
|
20
|
+
suffix: '',
|
|
21
|
+
contentsOnly: true,
|
|
22
|
+
useAbsoluteBounds: false,
|
|
23
|
+
colorProfile: 'DOCUMENT',
|
|
24
|
+
svgOutlineText: true,
|
|
25
|
+
svgIdAttribute: false,
|
|
26
|
+
svgSimplifyStroke: true
|
|
27
|
+
} as const satisfies Required<Omit<ExportSetting, 'scale' | 'format' | 'size'>>
|
|
28
|
+
|
|
29
|
+
/** An export as the model keeps it: options that are unset or at Figma's default left out. */
|
|
30
|
+
export function compactExportSetting({
|
|
31
|
+
scale,
|
|
32
|
+
format,
|
|
33
|
+
size,
|
|
34
|
+
...options
|
|
35
|
+
}: ExportSetting): ExportSetting {
|
|
36
|
+
const changed = omitBy(
|
|
37
|
+
options,
|
|
38
|
+
(value, key) => value === undefined || value === EXPORT_OPTION_DEFAULTS[key]
|
|
39
|
+
)
|
|
40
|
+
return size ? { scale, format, size, ...changed } : { scale, format, ...changed }
|
|
41
|
+
}
|
package/src/fields/text.ts
CHANGED
|
@@ -16,7 +16,9 @@ export const TEXT_METRIC_FIELDS = [
|
|
|
16
16
|
'listSpacing',
|
|
17
17
|
'paragraphSpacing',
|
|
18
18
|
'paragraphIndent',
|
|
19
|
-
'hangingList'
|
|
19
|
+
'hangingList',
|
|
20
|
+
// A cap-height trim drops the space above and below the glyphs, so the text measures shorter.
|
|
21
|
+
'leadingTrim'
|
|
20
22
|
] as const satisfies readonly (keyof SceneNode)[]
|
|
21
23
|
|
|
22
24
|
export const TEXT_SHAPING_FIELDS = [
|
package/src/figjam/palette.ts
CHANGED
|
@@ -244,6 +244,27 @@ export function figJamOutlineColorName(color: Color | undefined): FigJamColorNam
|
|
|
244
244
|
)
|
|
245
245
|
}
|
|
246
246
|
|
|
247
|
+
/**
|
|
248
|
+
* How FigJam draws a section's title on a board, measured in FigJam: a pill in the colour of the
|
|
249
|
+
* outline drawn on top, with white text on the palette's darkest colours and black on the rest;
|
|
250
|
+
* null for a section without an outline from the palette. `colorOf` reads an outline's colour as
|
|
251
|
+
* drawn, such as a variable's value in the active mode.
|
|
252
|
+
*/
|
|
253
|
+
export function figJamSectionTitleColors(
|
|
254
|
+
section: Pick<SceneNode, 'strokes'>,
|
|
255
|
+
colorOf: (stroke: Stroke, index: number) => Color = (stroke) => stroke.color
|
|
256
|
+
): { strokeIndex: number; background: Color; lightText: boolean } | null {
|
|
257
|
+
const strokeIndex = section.strokes.findLastIndex(
|
|
258
|
+
(stroke) => stroke.visible && stroke.opacity > 0
|
|
259
|
+
)
|
|
260
|
+
const outline = section.strokes.at(strokeIndex)
|
|
261
|
+
if (strokeIndex === -1 || !outline) return null
|
|
262
|
+
const color = colorOf(outline, strokeIndex)
|
|
263
|
+
const name = figJamOutlineColorName(color)
|
|
264
|
+
if (!name) return null
|
|
265
|
+
return { strokeIndex, background: { ...color, a: 1 }, lightText: PALETTE[name].lightLabel }
|
|
266
|
+
}
|
|
267
|
+
|
|
247
268
|
/** The fill mode a shape's first fill shows. */
|
|
248
269
|
export function figJamFillMode(fill: Fill | undefined): FigJamFillMode {
|
|
249
270
|
if (!fill || !fill.visible || fill.opacity === 0) return 'none'
|
|
@@ -380,9 +401,16 @@ export function figJamSectionColorFields(
|
|
|
380
401
|
name: FigJamColorName
|
|
381
402
|
): Partial<SceneNode> {
|
|
382
403
|
const { sectionFill, sectionStroke } = PALETTE[name]
|
|
404
|
+
// The outline shows in full at FigJam's 2, as its menu draws it, unless the line menu hid it.
|
|
405
|
+
const strokes = strokesIn(section.strokes, parseColor(sectionStroke), SECTION_STROKE_WEIGHT).map(
|
|
406
|
+
(stroke): Stroke =>
|
|
407
|
+
stroke.opacity === 0
|
|
408
|
+
? stroke
|
|
409
|
+
: { ...stroke, opacity: 1, visible: true, weight: SECTION_STROKE_WEIGHT }
|
|
410
|
+
)
|
|
383
411
|
return {
|
|
384
412
|
fills: [{ type: 'SOLID', color: parseColor(sectionFill), opacity: 1, visible: true }],
|
|
385
|
-
strokes
|
|
413
|
+
strokes,
|
|
386
414
|
strokeWeight: SECTION_STROKE_WEIGHT
|
|
387
415
|
}
|
|
388
416
|
}
|
|
@@ -9,9 +9,15 @@ import type { GeneratedFrameKind } from './kind'
|
|
|
9
9
|
|
|
10
10
|
/** FigJam makes a shape with text 176 px square, from its tool and from its plugin API. */
|
|
11
11
|
export const SHAPE_WITH_TEXT_SIZE = 176
|
|
12
|
-
/** FigJam rounds every corner of a shape by 6 px. */
|
|
12
|
+
/** FigJam rounds every corner of a shape by 6 px, but a rounded rectangle's. */
|
|
13
13
|
export const SHAPE_WITH_TEXT_CORNER_RADIUS = 6
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* FigJam keeps a rounded rectangle's corner radius at 0.4 of its shorter side, through resizes,
|
|
17
|
+
* measured in FigJam; Figma draws the shape from that radius.
|
|
18
|
+
*/
|
|
19
|
+
const ROUNDED_RECTANGLE_RADIUS = 0.4
|
|
20
|
+
|
|
15
21
|
const LABEL_FILL: Fill = { type: 'SOLID', color: { ...BLACK }, opacity: 0.8, visible: true }
|
|
16
22
|
|
|
17
23
|
/** How the label sits in its shape: as wide as the label box, as tall as its text. */
|
|
@@ -42,7 +48,27 @@ export function shapeWithTextFrameFields(
|
|
|
42
48
|
paddingRight: Math.max(0, width - box.x - box.width),
|
|
43
49
|
paddingBottom: Math.max(0, height - box.y - box.height),
|
|
44
50
|
paddingLeft: box.x,
|
|
45
|
-
clipsContent: false
|
|
51
|
+
clipsContent: false,
|
|
52
|
+
...(kind === 'ROUNDED_RECTANGLE'
|
|
53
|
+
? { cornerRadius: ROUNDED_RECTANGLE_RADIUS * Math.min(width, height) }
|
|
54
|
+
: {})
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* What turning a shape into `kind` changes, keeping its size, as FigJam's shape switcher does: the
|
|
60
|
+
* label area, and the corners a rounded rectangle leaves go back to FigJam's 6.
|
|
61
|
+
*/
|
|
62
|
+
export function shapeWithTextKindFields(
|
|
63
|
+
shape: Pick<SceneNode, 'shapeWithTextType' | 'width' | 'height'>,
|
|
64
|
+
kind: ShapeWithTextType
|
|
65
|
+
): Partial<SceneNode> {
|
|
66
|
+
const leavesRounded =
|
|
67
|
+
shape.shapeWithTextType === 'ROUNDED_RECTANGLE' && kind !== shape.shapeWithTextType
|
|
68
|
+
return {
|
|
69
|
+
shapeWithTextType: kind,
|
|
70
|
+
...(leavesRounded ? { cornerRadius: SHAPE_WITH_TEXT_CORNER_RADIUS } : {}),
|
|
71
|
+
...shapeWithTextFrameFields(kind, shape.width, shape.height)
|
|
46
72
|
}
|
|
47
73
|
}
|
|
48
74
|
|
package/src/geometry.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { arrowCapOverflow } from './arrow-caps'
|
|
2
2
|
import type { Rect, Vector } from './primitives'
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import type { Effect, SceneNode, Stroke, StrokeCap, StrokeJoin, VectorNetwork } from './types'
|
|
4
|
+
|
|
5
|
+
export { cubicPoint } from './cubic'
|
|
5
6
|
|
|
6
7
|
export function degToRad(degrees: number): number {
|
|
7
8
|
return (degrees * Math.PI) / 180
|
|
@@ -216,6 +217,30 @@ function miterRatio(network: VectorNetwork, join: StrokeJoin | undefined, limit
|
|
|
216
217
|
return ratio
|
|
217
218
|
}
|
|
218
219
|
|
|
220
|
+
/**
|
|
221
|
+
* The blur radius Figma draws: a negative one, which only a bound variable can give, draws as no
|
|
222
|
+
* blur, though the layer keeps the value.
|
|
223
|
+
*/
|
|
224
|
+
export function drawnBlurRadius(radius: number): number {
|
|
225
|
+
return Math.max(0, radius)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const BLUR_SIGMA_PER_RADIUS = 0.43
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* The Gaussian sigma that draws a Figma blur radius as Figma does. Measured on Figma 126 exports,
|
|
232
|
+
* layer blur, both shadows, and background blur fall off as a Gaussian of 0.43 × radius; Figma's
|
|
233
|
+
* SVG export writes radius / 2, which is wider (`packages/core/tests/canvas/blur.test.ts`).
|
|
234
|
+
*/
|
|
235
|
+
export function blurSigma(radius: number): number {
|
|
236
|
+
return drawnBlurRadius(radius) * BLUR_SIGMA_PER_RADIUS
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** The Figma blur radius that draws a Gaussian of `sigma`, the inverse of `blurSigma`. */
|
|
240
|
+
export function blurRadiusForSigma(sigma: number): number {
|
|
241
|
+
return Math.max(0, sigma) / BLUR_SIGMA_PER_RADIUS
|
|
242
|
+
}
|
|
243
|
+
|
|
219
244
|
export function effectOverflow(effects?: Effect[]) {
|
|
220
245
|
let left = 0
|
|
221
246
|
let right = 0
|
|
@@ -231,7 +256,7 @@ export function effectOverflow(effects?: Effect[]) {
|
|
|
231
256
|
) {
|
|
232
257
|
continue
|
|
233
258
|
}
|
|
234
|
-
const blurSpread = effect.radius + effect.spread
|
|
259
|
+
const blurSpread = drawnBlurRadius(effect.radius) + effect.spread
|
|
235
260
|
left = Math.max(left, blurSpread + Math.max(0, -effect.offset.x))
|
|
236
261
|
right = Math.max(right, blurSpread + Math.max(0, effect.offset.x))
|
|
237
262
|
top = Math.max(top, blurSpread + Math.max(0, -effect.offset.y))
|
|
@@ -253,59 +278,6 @@ export function computeAbsoluteBounds(
|
|
|
253
278
|
return boundsToRect(bounds)
|
|
254
279
|
}
|
|
255
280
|
|
|
256
|
-
export function computeVisualBounds(
|
|
257
|
-
nodes: Iterable<
|
|
258
|
-
{
|
|
259
|
-
id: string
|
|
260
|
-
width: number
|
|
261
|
-
height: number
|
|
262
|
-
rotation?: number
|
|
263
|
-
strokes?: Stroke[]
|
|
264
|
-
strokeCap?: StrokeCap
|
|
265
|
-
vectorNetwork?: VectorNetwork | null
|
|
266
|
-
effects?: Effect[]
|
|
267
|
-
} & StrokeShape
|
|
268
|
-
>,
|
|
269
|
-
getAbsolutePosition: (id: string) => Vector
|
|
270
|
-
): Rect {
|
|
271
|
-
const bounds = createBoundsAccumulator()
|
|
272
|
-
|
|
273
|
-
for (const n of nodes) {
|
|
274
|
-
const abs = getAbsolutePosition(n.id)
|
|
275
|
-
const bbox = rotatedBBox(abs.x, abs.y, n.width, n.height, n.rotation ?? 0)
|
|
276
|
-
const stroke = strokeOverflow(n.strokes, n.strokeCap, n.vectorNetwork, n)
|
|
277
|
-
const effects = effectOverflow(n.effects)
|
|
278
|
-
includePoint(bounds, bbox.left - stroke - effects.left, bbox.top - stroke - effects.top)
|
|
279
|
-
includePoint(bounds, bbox.right + stroke + effects.right, bbox.bottom + stroke + effects.bottom)
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
return boundsToRect(bounds)
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
export interface VisualBoundsNode {
|
|
286
|
-
id: string
|
|
287
|
-
width: number
|
|
288
|
-
height: number
|
|
289
|
-
rotation?: number
|
|
290
|
-
flipX?: boolean
|
|
291
|
-
flipY?: boolean
|
|
292
|
-
strokes?: Stroke[]
|
|
293
|
-
strokeCap?: StrokeCap
|
|
294
|
-
vectorNetwork?: VectorNetwork | null
|
|
295
|
-
effects?: Effect[]
|
|
296
|
-
fillGeometry?: Array<{ commandsBlob: Uint8Array }>
|
|
297
|
-
strokeGeometry?: Array<{ commandsBlob: Uint8Array }>
|
|
298
|
-
childIds?: string[]
|
|
299
|
-
visible?: boolean
|
|
300
|
-
type?: string
|
|
301
|
-
clipsContent?: boolean
|
|
302
|
-
fontSize?: number
|
|
303
|
-
textDecoration?: string
|
|
304
|
-
textUnderlineOffset?: number | null
|
|
305
|
-
textDecorationThickness?: number | null
|
|
306
|
-
derivedTextHeight?: number | null
|
|
307
|
-
}
|
|
308
|
-
|
|
309
281
|
export function unionVisualBounds(
|
|
310
282
|
a: VisualBounds | null,
|
|
311
283
|
b: VisualBounds | null
|
|
@@ -328,163 +300,71 @@ export function intersectVisualBounds(a: VisualBounds, b: VisualBounds): VisualB
|
|
|
328
300
|
return minX < maxX && minY < maxY ? { minX, minY, maxX, maxY } : null
|
|
329
301
|
}
|
|
330
302
|
|
|
303
|
+
/** The path commands of a Figma geometry blob, by their byte code. */
|
|
304
|
+
export const GeometryCommand = { CLOSE: 0, MOVE: 1, LINE: 2, QUAD: 3, CUBIC: 4 } as const
|
|
305
|
+
export type GeometryCommand = (typeof GeometryCommand)[keyof typeof GeometryCommand]
|
|
306
|
+
|
|
331
307
|
/**
|
|
332
|
-
* Coordinate pairs per
|
|
333
|
-
*
|
|
334
|
-
* walkers stop instead of desyncing into coordinate bytes.
|
|
308
|
+
* Coordinate pairs per command: glyph-derived blobs are quad-heavy, so every code needs its
|
|
309
|
+
* count. Unknown codes are null, so readers stop instead of desyncing into coordinate bytes.
|
|
335
310
|
*/
|
|
336
|
-
|
|
337
|
-
if (command ===
|
|
338
|
-
if (command ===
|
|
339
|
-
if (command ===
|
|
340
|
-
if (command ===
|
|
311
|
+
function commandCoordCount(command: number): number | null {
|
|
312
|
+
if (command === GeometryCommand.CLOSE) return 0
|
|
313
|
+
if (command === GeometryCommand.MOVE || command === GeometryCommand.LINE) return 1
|
|
314
|
+
if (command === GeometryCommand.QUAD) return 2
|
|
315
|
+
if (command === GeometryCommand.CUBIC) return 3
|
|
341
316
|
return null
|
|
342
317
|
}
|
|
343
318
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
319
|
+
/**
|
|
320
|
+
* Walks a Figma geometry blob: `visit` gets each command, its coordinates as x, y pairs in one
|
|
321
|
+
* array reused between calls, and the byte offset of the first coordinate. It stops at an
|
|
322
|
+
* unknown command or one cut short.
|
|
323
|
+
*/
|
|
324
|
+
export function readGeometryBlob(
|
|
325
|
+
blob: Uint8Array,
|
|
326
|
+
visit: (command: GeometryCommand, coords: readonly number[], offset: number) => void
|
|
327
|
+
): void {
|
|
328
|
+
const view = new DataView(blob.buffer, blob.byteOffset, blob.byteLength)
|
|
329
|
+
const coords: number[] = []
|
|
330
|
+
let offset = 0
|
|
331
|
+
while (offset < blob.length) {
|
|
332
|
+
const command = blob[offset]
|
|
333
|
+
const count = commandCoordCount(command)
|
|
334
|
+
if (count === null || offset + 1 + count * 8 > blob.length) return
|
|
335
|
+
offset += 1
|
|
336
|
+
coords.length = 0
|
|
337
|
+
for (let i = 0; i < count; i++) {
|
|
338
|
+
coords.push(view.getFloat32(offset + i * 8, true), view.getFloat32(offset + i * 8 + 4, true))
|
|
362
339
|
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
return bounds.minX === Infinity ? null : boundsToRect(bounds)
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
function transformLocalPoint(node: VisualBoundsNode, point: Vector): Vector {
|
|
369
|
-
let x = node.flipX ? node.width - point.x : point.x
|
|
370
|
-
let y = node.flipY ? node.height - point.y : point.y
|
|
371
|
-
const rotation = node.rotation ?? 0
|
|
372
|
-
if (rotation !== 0) {
|
|
373
|
-
const rotated = rotatePoint(x, y, node.width / 2, node.height / 2, degToRad(rotation))
|
|
374
|
-
x = rotated.x
|
|
375
|
-
y = rotated.y
|
|
376
|
-
}
|
|
377
|
-
return { x, y }
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
function transformedLocalBounds(node: VisualBoundsNode, local: Rect, abs: Vector): VisualBounds {
|
|
381
|
-
const points = [
|
|
382
|
-
{ x: local.x, y: local.y },
|
|
383
|
-
{ x: local.x + local.width, y: local.y },
|
|
384
|
-
{ x: local.x + local.width, y: local.y + local.height },
|
|
385
|
-
{ x: local.x, y: local.y + local.height }
|
|
386
|
-
].map((point) => transformLocalPoint(node, point))
|
|
387
|
-
|
|
388
|
-
return {
|
|
389
|
-
minX: abs.x + Math.min(...points.map((point) => point.x)),
|
|
390
|
-
minY: abs.y + Math.min(...points.map((point) => point.y)),
|
|
391
|
-
maxX: abs.x + Math.max(...points.map((point) => point.x)),
|
|
392
|
-
maxY: abs.y + Math.max(...points.map((point) => point.y))
|
|
340
|
+
visit(command as GeometryCommand, coords, offset)
|
|
341
|
+
offset += count * 8
|
|
393
342
|
}
|
|
394
343
|
}
|
|
395
344
|
|
|
396
|
-
export function
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
)
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
let bounds: VisualBounds = {
|
|
403
|
-
minX: base.x,
|
|
404
|
-
minY: base.y,
|
|
405
|
-
maxX: base.x + base.width,
|
|
406
|
-
maxY: base.y + base.height
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
const hasNonInsideStroke = node.strokes?.some(
|
|
410
|
-
(stroke) => stroke.visible && stroke.align !== 'INSIDE'
|
|
411
|
-
)
|
|
412
|
-
const localGeometry = geometryBlobBounds([
|
|
413
|
-
...(node.fillGeometry ?? []),
|
|
414
|
-
...(hasNonInsideStroke ? (node.strokeGeometry ?? []) : [])
|
|
415
|
-
])
|
|
416
|
-
if (localGeometry) {
|
|
417
|
-
bounds = unionVisualBounds(bounds, transformedLocalBounds(node, localGeometry, abs)) ?? bounds
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
const linesOverflow = textLinesOverflow(node)
|
|
421
|
-
if (linesOverflow > 0) {
|
|
422
|
-
const lines = { x: 0, y: 0, width: node.width, height: node.height + linesOverflow }
|
|
423
|
-
bounds = unionVisualBounds(bounds, transformedLocalBounds(node, lines, abs)) ?? bounds
|
|
345
|
+
export function geometryBlobBounds(paths: Array<{ commandsBlob: Uint8Array }>): Rect | null {
|
|
346
|
+
const bounds = createBoundsAccumulator()
|
|
347
|
+
for (const path of paths) {
|
|
348
|
+
readGeometryBlob(path.commandsBlob, (_, coords) => {
|
|
349
|
+
for (let i = 0; i < coords.length; i += 2) includePoint(bounds, coords[i], coords[i + 1])
|
|
350
|
+
})
|
|
424
351
|
}
|
|
425
352
|
|
|
426
|
-
bounds.
|
|
427
|
-
|
|
428
|
-
return bounds
|
|
353
|
+
return bounds.minX === Infinity ? null : boundsToRect(bounds)
|
|
429
354
|
}
|
|
430
355
|
|
|
431
356
|
/** How far a text node's underline or strikethrough reaches below its box. */
|
|
432
|
-
export function textDecorationOverflow(
|
|
433
|
-
|
|
434
|
-
|
|
357
|
+
export function textDecorationOverflow(
|
|
358
|
+
node: Pick<
|
|
359
|
+
SceneNode,
|
|
360
|
+
'type' | 'fontSize' | 'textDecoration' | 'textUnderlineOffset' | 'textDecorationThickness'
|
|
361
|
+
>
|
|
362
|
+
): number {
|
|
363
|
+
if (node.type !== 'TEXT' || node.textDecoration === 'NONE') {
|
|
364
|
+
return 0
|
|
365
|
+
}
|
|
366
|
+
const { fontSize } = node
|
|
435
367
|
const underlineOffset = node.textUnderlineOffset ?? fontSize * 0.18
|
|
436
368
|
const thickness = node.textDecorationThickness ?? Math.max(1, fontSize / 16)
|
|
437
369
|
return underlineOffset + thickness + fontSize * 0.35
|
|
438
370
|
}
|
|
439
|
-
|
|
440
|
-
function collectDescendantVisualBounds(
|
|
441
|
-
nodeId: string,
|
|
442
|
-
getNode: (id: string) => VisualBoundsNode | undefined,
|
|
443
|
-
getAbsolutePosition: (id: string) => Vector,
|
|
444
|
-
clip: VisualBounds | null = null
|
|
445
|
-
): VisualBounds | null {
|
|
446
|
-
const node = getNode(nodeId)
|
|
447
|
-
if (!node?.visible) return null
|
|
448
|
-
|
|
449
|
-
const own = nodeVisualBounds(node, getAbsolutePosition)
|
|
450
|
-
let bounds = clip ? intersectVisualBounds(own, clip) : own
|
|
451
|
-
|
|
452
|
-
const isClippableContainer =
|
|
453
|
-
node.type === 'FRAME' || node.type === 'COMPONENT' || node.type === 'INSTANCE'
|
|
454
|
-
let childClip = clip
|
|
455
|
-
if (isClippableContainer && node.clipsContent) {
|
|
456
|
-
const abs = getAbsolutePosition(node.id)
|
|
457
|
-
const nodeClip = {
|
|
458
|
-
minX: abs.x,
|
|
459
|
-
minY: abs.y,
|
|
460
|
-
maxX: abs.x + node.width,
|
|
461
|
-
maxY: abs.y + node.height
|
|
462
|
-
}
|
|
463
|
-
childClip = childClip ? intersectVisualBounds(childClip, nodeClip) : nodeClip
|
|
464
|
-
if (!childClip) return bounds
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
for (const childId of node.childIds ?? []) {
|
|
468
|
-
bounds = unionVisualBounds(
|
|
469
|
-
bounds,
|
|
470
|
-
collectDescendantVisualBounds(childId, getNode, getAbsolutePosition, childClip)
|
|
471
|
-
)
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
return bounds
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
export function computeDescendantVisualBounds(
|
|
478
|
-
nodeIds: string[],
|
|
479
|
-
getNode: (id: string) => VisualBoundsNode | undefined,
|
|
480
|
-
getAbsolutePosition: (id: string) => Vector
|
|
481
|
-
): VisualBounds | null {
|
|
482
|
-
let bounds: VisualBounds | null = null
|
|
483
|
-
for (const nodeId of nodeIds) {
|
|
484
|
-
bounds = unionVisualBounds(
|
|
485
|
-
bounds,
|
|
486
|
-
collectDescendantVisualBounds(nodeId, getNode, getAbsolutePosition)
|
|
487
|
-
)
|
|
488
|
-
}
|
|
489
|
-
return bounds
|
|
490
|
-
}
|
package/src/graph.ts
CHANGED
|
@@ -25,7 +25,8 @@ import { CONTAINER_TYPES, createDefaultNode } from './node-defaults'
|
|
|
25
25
|
import { updateNodePreview, type NodePreviewObserver } from './preview'
|
|
26
26
|
import type { Color, Rect, Vector } from './primitives'
|
|
27
27
|
import { markSourceFieldsEdited } from './source-metadata'
|
|
28
|
-
import {
|
|
28
|
+
import { textCacheInvalidationChanges } from './text/picture'
|
|
29
|
+
import { remapCopiedReferences } from './transfer/copied'
|
|
29
30
|
import type {
|
|
30
31
|
DocumentColorSpace,
|
|
31
32
|
DocumentKind,
|
|
@@ -576,8 +577,15 @@ export class SceneGraph {
|
|
|
576
577
|
if (node.componentId) this.unindexInstance(node.componentId, id)
|
|
577
578
|
if (changes.componentId) this.indexInstance(changes.componentId, id)
|
|
578
579
|
}
|
|
579
|
-
if (node.type === 'TEXT') invalidateTextCaches(node, changes)
|
|
580
580
|
this.markEdited(node, Object.keys(changes))
|
|
581
|
+
// Cache resets that change the node travel with the change, under the caller's own values,
|
|
582
|
+
// so a layout put-back cannot drop their event.
|
|
583
|
+
if (node.type === 'TEXT') {
|
|
584
|
+
const resets = omitBy(textCacheInvalidationChanges(node, changes), (value, key) =>
|
|
585
|
+
Object.is(node[key], value)
|
|
586
|
+
)
|
|
587
|
+
changes = { ...resets, ...changes }
|
|
588
|
+
}
|
|
581
589
|
if (changes.vectorNetwork) {
|
|
582
590
|
changes = { ...changes, vectorNetwork: normalizeVectorNetwork(changes.vectorNetwork) }
|
|
583
591
|
}
|
|
@@ -589,6 +597,7 @@ export class SceneGraph {
|
|
|
589
597
|
Object.assign(node, changes)
|
|
590
598
|
if (changes.fills) removeStaleBindings(node, 'fills', changes)
|
|
591
599
|
if (changes.strokes) removeStaleBindings(node, 'strokes', changes)
|
|
600
|
+
if (changes.effects) removeStaleBindings(node, 'effects', changes)
|
|
592
601
|
for (const key of absent) Reflect.deleteProperty(node, key)
|
|
593
602
|
this.publishNodeUpdate(id, changes)
|
|
594
603
|
}
|
|
@@ -742,6 +751,20 @@ export class SceneGraph {
|
|
|
742
751
|
sourceId: string,
|
|
743
752
|
parentId: string,
|
|
744
753
|
overrides: Partial<SceneNode> = {}
|
|
754
|
+
): SceneNode | null {
|
|
755
|
+
const ids = new Map<string, string>()
|
|
756
|
+
const clone = this.cloneSubtree(sourceId, parentId, overrides, ids)
|
|
757
|
+
// Copies point at one another, as a paste's do: an instance at a component cloned with it,
|
|
758
|
+
// as Figma's clone does (desktop 126), and its override records at its copies' new ids.
|
|
759
|
+
remapCopiedReferences(this, ids)
|
|
760
|
+
return clone
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
private cloneSubtree(
|
|
764
|
+
sourceId: string,
|
|
765
|
+
parentId: string,
|
|
766
|
+
overrides: Partial<SceneNode>,
|
|
767
|
+
ids: Map<string, string>
|
|
745
768
|
): SceneNode | null {
|
|
746
769
|
const src = this.nodes.get(sourceId)
|
|
747
770
|
if (!src) return null
|
|
@@ -753,9 +776,10 @@ export class SceneGraph {
|
|
|
753
776
|
props.status = null
|
|
754
777
|
const clone = this.createNode(src.type, parentId, { ...props, ...overrides })
|
|
755
778
|
if (src.status) markSourceFieldsEdited(clone, ['status'])
|
|
779
|
+
ids.set(src.id, clone.id)
|
|
756
780
|
|
|
757
781
|
for (const childId of src.childIds) {
|
|
758
|
-
this.
|
|
782
|
+
this.cloneSubtree(childId, clone.id, {}, ids)
|
|
759
783
|
}
|
|
760
784
|
|
|
761
785
|
return clone
|
package/src/guides.ts
CHANGED
|
@@ -9,6 +9,11 @@ export interface CanvasGuide {
|
|
|
9
9
|
figGuid?: GUID
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
/** A new guide's id, unique across documents as a copied guide keeps its id. */
|
|
13
|
+
export function newGuideId(): string {
|
|
14
|
+
return `guide:${crypto.randomUUID()}`
|
|
15
|
+
}
|
|
16
|
+
|
|
12
17
|
const ownersByGraph = new WeakMap<SceneGraph, Set<string>>()
|
|
13
18
|
|
|
14
19
|
function ownerSet(graph: SceneGraph): Set<string> {
|