@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/layout/guides.ts
CHANGED
|
@@ -7,10 +7,21 @@ export interface LayoutGuideLine {
|
|
|
7
7
|
|
|
8
8
|
const MAX_LAYOUT_GUIDE_LINES = 10_000
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
/** Figma's Auto count, as its archive stores it: as many sections as fit. */
|
|
11
|
+
export const AUTO_LAYOUT_GRID_COUNT = 2_147_483_647
|
|
12
|
+
|
|
13
|
+
/** Whether a guide's count is Auto: the archive's value, or the plugin API's `Infinity` or `null`. */
|
|
14
|
+
export function isAutoGridCount(count: number | null | undefined): boolean {
|
|
15
|
+
return count === null || count === Infinity || (count ?? 0) >= AUTO_LAYOUT_GRID_COUNT
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A guide's count, set in Figma's plugin shape (`count`) or read from a file (`numSections`); an
|
|
20
|
+
* edit's `count` wins over the count the file had. Auto is {@link AUTO_LAYOUT_GRID_COUNT}.
|
|
21
|
+
*/
|
|
22
|
+
export function layoutGridCount(grid: LayoutGrid): number {
|
|
23
|
+
const count = grid.count === undefined ? grid.numSections : grid.count
|
|
24
|
+
return isAutoGridCount(count) ? AUTO_LAYOUT_GRID_COUNT : (count ?? 1)
|
|
14
25
|
}
|
|
15
26
|
|
|
16
27
|
function pattern(grid: LayoutGrid): 'COLUMNS' | 'ROWS' | 'GRID' {
|
|
@@ -25,17 +36,35 @@ function alignment(grid: LayoutGrid): 'MIN' | 'CENTER' | 'MAX' | 'STRETCH' {
|
|
|
25
36
|
return 'MIN'
|
|
26
37
|
}
|
|
27
38
|
|
|
28
|
-
|
|
39
|
+
/**
|
|
40
|
+
* How many sections a guide draws. Auto fits as many fixed sections as the frame holds, counting
|
|
41
|
+
* the offset once from the aligned edge, as Figma desktop 126 draws them.
|
|
42
|
+
*/
|
|
43
|
+
function sectionCount(nodeSize: number, grid: LayoutGrid): number {
|
|
44
|
+
const value = layoutGridCount(grid)
|
|
45
|
+
if (value === AUTO_LAYOUT_GRID_COUNT) {
|
|
46
|
+
const size = grid.sectionSize ?? 0
|
|
47
|
+
if (alignment(grid) === 'STRETCH' || size <= 0) return 0
|
|
48
|
+
const gutter = grid.gutterSize ?? 0
|
|
49
|
+
const offset = alignment(grid) === 'CENTER' ? 0 : (grid.offset ?? 0)
|
|
50
|
+
return Math.min(
|
|
51
|
+
MAX_LAYOUT_GUIDE_LINES,
|
|
52
|
+
Math.max(0, Math.floor((nodeSize - offset + gutter) / (size + gutter)))
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
if (!Number.isFinite(value)) return 0
|
|
56
|
+
return Math.min(MAX_LAYOUT_GUIDE_LINES, Math.max(0, Math.floor(value)))
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function sectionSize(nodeSize: number, grid: LayoutGrid, count: number): number {
|
|
29
60
|
const fixed = grid.sectionSize ?? 0
|
|
30
61
|
if (alignment(grid) !== 'STRETCH') return fixed
|
|
31
|
-
const count = normalizedCount(grid)
|
|
32
62
|
const gutter = grid.gutterSize ?? 0
|
|
33
63
|
const offset = grid.offset ?? 0
|
|
34
64
|
return (nodeSize - offset * 2 - Math.max(0, count - 1) * gutter) / count
|
|
35
65
|
}
|
|
36
66
|
|
|
37
|
-
function start(nodeSize: number, grid: LayoutGrid, size: number): number {
|
|
38
|
-
const count = normalizedCount(grid)
|
|
67
|
+
function start(nodeSize: number, grid: LayoutGrid, size: number, count: number): number {
|
|
39
68
|
const gutter = grid.gutterSize ?? 0
|
|
40
69
|
const offset = grid.offset ?? 0
|
|
41
70
|
const span = count * size + Math.max(0, count - 1) * gutter
|
|
@@ -57,10 +86,10 @@ export function layoutGuideSections(
|
|
|
57
86
|
if (grid.visible === false || pattern(grid) === 'GRID') return []
|
|
58
87
|
const axis = pattern(grid) === 'ROWS' ? 'y' : 'x'
|
|
59
88
|
const nodeSize = axis === 'x' ? node.width : node.height
|
|
60
|
-
const
|
|
61
|
-
const
|
|
89
|
+
const count = sectionCount(nodeSize, grid)
|
|
90
|
+
const size = sectionSize(nodeSize, grid, count)
|
|
62
91
|
if (count <= 0 || size <= 0) return []
|
|
63
|
-
const first = start(nodeSize, grid, size)
|
|
92
|
+
const first = start(nodeSize, grid, size, count)
|
|
64
93
|
const step = size + (grid.gutterSize ?? 0)
|
|
65
94
|
return Array.from({ length: count }, (_, index) => {
|
|
66
95
|
const sectionStart = first + index * step
|
package/src/shaders/model.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { sha1 } from '@noble/hashes/legacy.js'
|
|
2
|
+
import { bytesToHex, utf8ToBytes } from '@noble/hashes/utils.js'
|
|
3
|
+
|
|
1
4
|
import { TRANSPARENT } from '../constants'
|
|
2
|
-
import { computeImageHash } from '../images'
|
|
3
5
|
import { readAllPluginData, withAllPluginData } from '../plugin-data/field'
|
|
4
6
|
import { OPEN_PENCIL_PLUGIN_DATA } from '../plugin-data/fields'
|
|
5
7
|
import { randomHex } from '../random'
|
|
@@ -77,7 +79,7 @@ export function createShaderPaint(preset: ShaderPreset): {
|
|
|
77
79
|
|
|
78
80
|
/** A short key that tells one preset from another, which a rendered frame records. */
|
|
79
81
|
export function shaderPresetKey(preset: ShaderPreset): string {
|
|
80
|
-
return
|
|
82
|
+
return bytesToHex(sha1(utf8ToBytes(JSON.stringify(preset))))
|
|
81
83
|
}
|
|
82
84
|
|
|
83
85
|
/**
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Size } from '../primitives'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* How a shader paint's still frame is drawn and stored, for every renderer that draws one: the
|
|
5
|
+
* editor's canvas and the tools that prebuild documents. The frame is an image fill in `.fig`
|
|
6
|
+
* files, so Figma must be able to read it, which rules out WebP.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Frames are drawn at twice the layer's size, for sharp high-density displays. */
|
|
10
|
+
const FRAME_SCALE = 2
|
|
11
|
+
/** The longest side a frame is drawn at, however large the layer. */
|
|
12
|
+
const MAX_FRAME_EDGE = 2048
|
|
13
|
+
/** JPEG quality for opaque frames: no visible difference from PNG at a fraction of the size. */
|
|
14
|
+
const JPEG_QUALITY = 0.9
|
|
15
|
+
|
|
16
|
+
/** The pixels a frame for a layer of `size` is drawn at. */
|
|
17
|
+
export function shaderFramePixels(size: Size): Size {
|
|
18
|
+
const scale = Math.min(FRAME_SCALE, MAX_FRAME_EDGE / Math.max(size.width, size.height, 1))
|
|
19
|
+
return {
|
|
20
|
+
width: Math.max(1, Math.round(size.width * scale)),
|
|
21
|
+
height: Math.max(1, Math.round(size.height * scale))
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ShaderFrameEncoding {
|
|
26
|
+
type: 'image/jpeg' | 'image/png'
|
|
27
|
+
/** For `canvas.toBlob` and `toDataURL`; PNG takes none. */
|
|
28
|
+
quality?: number
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The encoding a frame with these RGBA pixels is stored in: JPEG when every pixel is opaque, which
|
|
33
|
+
* is about 25 times smaller than PNG for a shader, and PNG when any pixel lets the layer behind
|
|
34
|
+
* show through, which JPEG cannot keep.
|
|
35
|
+
*/
|
|
36
|
+
export function shaderFrameEncoding(rgba: ArrayLike<number>): ShaderFrameEncoding {
|
|
37
|
+
for (let alpha = 3; alpha < rgba.length; alpha += 4) {
|
|
38
|
+
if (rgba[alpha] !== 255) return { type: 'image/png' }
|
|
39
|
+
}
|
|
40
|
+
return { type: 'image/jpeg', quality: JPEG_QUALITY }
|
|
41
|
+
}
|
package/src/shared-styles.ts
CHANGED
|
@@ -16,7 +16,8 @@ const STYLE_TYPES = {
|
|
|
16
16
|
grid: 'GRID'
|
|
17
17
|
} as const satisfies Record<SharedStyleKind, SharedStyleType>
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
/** The fields a text style gives what uses it; changing one leaves the style. */
|
|
20
|
+
export const SHARED_TEXT_STYLE_FIELDS = [
|
|
20
21
|
'fontFamily',
|
|
21
22
|
'fontWeight',
|
|
22
23
|
'italic',
|
|
@@ -26,7 +27,9 @@ const TEXT_STYLE_KEYS = new Set<keyof SceneNode>([
|
|
|
26
27
|
'textDecoration',
|
|
27
28
|
'textCase',
|
|
28
29
|
'fontFeatures'
|
|
29
|
-
])
|
|
30
|
+
] as const satisfies readonly (keyof SceneNode)[]
|
|
31
|
+
|
|
32
|
+
const TEXT_STYLE_KEYS = new Set<keyof SceneNode>(SHARED_TEXT_STYLE_FIELDS)
|
|
30
33
|
|
|
31
34
|
export function sharedStyleRefKey(kind: SharedStyleKind): (typeof STYLE_REF_KEYS)[SharedStyleKind] {
|
|
32
35
|
return STYLE_REF_KEYS[kind]
|
package/src/text/index.ts
CHANGED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { mapValues } from 'es-toolkit/object'
|
|
2
|
+
|
|
3
|
+
import type { StyleRun } from '../types'
|
|
4
|
+
|
|
5
|
+
/** What a text's runs refer to outside the text: the variables they bind and the styles they use. */
|
|
6
|
+
export interface StyleRunReferences {
|
|
7
|
+
variables: string[]
|
|
8
|
+
styles: string[]
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** The variables and styles a text's runs refer to, which copies and transfers carry with it. */
|
|
12
|
+
export function styleRunReferences(runs: readonly StyleRun[]): StyleRunReferences {
|
|
13
|
+
const variables = new Set<string>()
|
|
14
|
+
const styles = new Set<string>()
|
|
15
|
+
for (const { style } of runs) {
|
|
16
|
+
for (const id of Object.values(style.boundVariables ?? {})) if (id) variables.add(id)
|
|
17
|
+
if (style.textStyleId) styles.add(style.textStyleId)
|
|
18
|
+
if (style.fillStyleId) styles.add(style.fillStyleId)
|
|
19
|
+
}
|
|
20
|
+
return { variables: [...variables], styles: [...styles] }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Runs whose variables, styles, and linked layers are renamed, as a copy into another document
|
|
25
|
+
* renames them. Layers are renamed only when given, since a link may name a layer left behind.
|
|
26
|
+
*/
|
|
27
|
+
export function remapStyleRunReferences(
|
|
28
|
+
runs: readonly StyleRun[],
|
|
29
|
+
rename: {
|
|
30
|
+
variable: (id: string) => string
|
|
31
|
+
style: (id: string) => string
|
|
32
|
+
node?: (id: string) => string
|
|
33
|
+
}
|
|
34
|
+
): StyleRun[] {
|
|
35
|
+
return runs.map((run) => {
|
|
36
|
+
const { boundVariables, textStyleId, fillStyleId, hyperlink } = run.style
|
|
37
|
+
const linksLayer = hyperlink?.type === 'NODE' && rename.node !== undefined
|
|
38
|
+
if (!boundVariables && !textStyleId && !fillStyleId && !linksLayer) return run
|
|
39
|
+
const style = { ...run.style }
|
|
40
|
+
if (hyperlink?.type === 'NODE' && rename.node) {
|
|
41
|
+
style.hyperlink = { type: 'NODE', value: rename.node(hyperlink.value) }
|
|
42
|
+
}
|
|
43
|
+
if (boundVariables) {
|
|
44
|
+
style.boundVariables = mapValues(boundVariables, (id) => (id ? rename.variable(id) : id))
|
|
45
|
+
}
|
|
46
|
+
if (textStyleId) style.textStyleId = rename.style(textStyleId)
|
|
47
|
+
if (fillStyleId) style.fillStyleId = rename.style(fillStyleId)
|
|
48
|
+
return { ...run, style }
|
|
49
|
+
})
|
|
50
|
+
}
|
package/src/transfer/nodes.ts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import { cloneNodeProps } from '../copy'
|
|
2
2
|
import { remapInstanceOverrideState } from '../instance-overrides'
|
|
3
|
+
import { remapStyleRunReferences, styleRunReferences } from '../text/references'
|
|
3
4
|
import type { ComponentPropertyType, SceneNode } from '../types'
|
|
4
5
|
import { requireTransferReference as required } from './references'
|
|
5
6
|
import type { VariableTransferReferences } from './variables'
|
|
6
7
|
|
|
7
8
|
export function semanticTransferReferences(node: SceneNode): string[] {
|
|
9
|
+
const runs = styleRunReferences(node.styleRuns)
|
|
8
10
|
return [
|
|
9
11
|
...(node.componentId ? [node.componentId] : []),
|
|
10
12
|
...Object.values(node.boundVariables),
|
|
13
|
+
...runs.variables,
|
|
14
|
+
...runs.styles,
|
|
11
15
|
...[
|
|
12
16
|
node.fillStyleId,
|
|
13
17
|
node.strokeStyleId,
|
|
@@ -89,6 +93,14 @@ export function prepareNodeTransfer(
|
|
|
89
93
|
] as const) {
|
|
90
94
|
result[field] = source[field] ? style(source[field]) : null
|
|
91
95
|
}
|
|
96
|
+
if (result.styleRuns) {
|
|
97
|
+
// A link to a layer the transfer leaves behind keeps naming it.
|
|
98
|
+
result.styleRuns = remapStyleRunReferences(result.styleRuns, {
|
|
99
|
+
variable,
|
|
100
|
+
style,
|
|
101
|
+
node: (id) => refs.nodes.get(id) ?? id
|
|
102
|
+
})
|
|
103
|
+
}
|
|
92
104
|
for (const paths of [result.fillGeometry, result.strokeGeometry]) {
|
|
93
105
|
for (const path of paths ?? []) if (path.fillStyleId) path.fillStyleId = style(path.fillStyleId)
|
|
94
106
|
}
|
package/src/transfer/plan.ts
CHANGED
|
@@ -12,6 +12,18 @@ export interface GraphTransferInput {
|
|
|
12
12
|
parentId: string
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
/** A transferred style's props under the ID the transfer gives it. */
|
|
16
|
+
function withStyleId(
|
|
17
|
+
node: SceneNode,
|
|
18
|
+
props: Partial<SceneNode>,
|
|
19
|
+
styles: ReadonlyMap<string, string>
|
|
20
|
+
): Partial<SceneNode> {
|
|
21
|
+
const id =
|
|
22
|
+
node.sharedStyleType !== null && node.source.id ? styles.get(node.source.id) : undefined
|
|
23
|
+
if (!id || id === node.source.id) return props
|
|
24
|
+
return { ...props, source: { ...(props.source ?? node.source), id } }
|
|
25
|
+
}
|
|
26
|
+
|
|
15
27
|
/** Preflight a self-contained fragment; allocation does not insert anything into the target. */
|
|
16
28
|
export function prepareGraphTransfer(input: GraphTransferInput) {
|
|
17
29
|
const { source, target, parentId } = input
|
|
@@ -59,6 +71,17 @@ export function prepareGraphTransfer(input: GraphTransferInput) {
|
|
|
59
71
|
propertyTypes.set(definition.id, definition.type)
|
|
60
72
|
if (!properties.has(definition.id)) properties.set(definition.id, allocate())
|
|
61
73
|
}
|
|
74
|
+
// Layers name a style by the style's own ID, which a transferred style keeps unless the
|
|
75
|
+
// target already holds another style by it: IDs read from Figma are only unique in one file.
|
|
76
|
+
const targetStyleIds = new Set<string>()
|
|
77
|
+
for (const node of target.getAllNodes())
|
|
78
|
+
if (node.sharedStyleType !== null && node.source.id) targetStyleIds.add(node.source.id)
|
|
79
|
+
const styles = new Map<string, string>()
|
|
80
|
+
for (const node of ordered) {
|
|
81
|
+
const id = node.source.id
|
|
82
|
+
if (node.sharedStyleType === null || !id) continue
|
|
83
|
+
styles.set(id, targetStyleIds.has(id) ? allocate() : id)
|
|
84
|
+
}
|
|
62
85
|
const references = {
|
|
63
86
|
nodes,
|
|
64
87
|
variables,
|
|
@@ -66,7 +89,7 @@ export function prepareGraphTransfer(input: GraphTransferInput) {
|
|
|
66
89
|
modes,
|
|
67
90
|
properties,
|
|
68
91
|
propertyTypes,
|
|
69
|
-
styles
|
|
92
|
+
styles
|
|
70
93
|
}
|
|
71
94
|
const resources = prepareVariableTransfer(
|
|
72
95
|
[...source.variables.values()],
|
|
@@ -93,7 +116,7 @@ export function prepareGraphTransfer(input: GraphTransferInput) {
|
|
|
93
116
|
id: mapped(node.id),
|
|
94
117
|
type: node.type,
|
|
95
118
|
parentId: destinationParent(node),
|
|
96
|
-
props: prepareNodeTransfer(node, references)
|
|
119
|
+
props: withStyleId(node, prepareNodeTransfer(node, references), styles)
|
|
97
120
|
})),
|
|
98
121
|
...resources,
|
|
99
122
|
activeModes: new Map(
|
|
@@ -25,9 +25,14 @@ export function validateTransferReferences(target: SceneGraph, plan: GraphTransf
|
|
|
25
25
|
propertyTypes.set(definition.id, definition.type)
|
|
26
26
|
}
|
|
27
27
|
const collections = new Map(plan.collections.map((collection) => [collection.id, collection]))
|
|
28
|
+
const styles = new Map(
|
|
29
|
+
[...nodes.values()].flatMap((node) =>
|
|
30
|
+
node.sharedStyleType !== null && node.source.id ? [[node.source.id, node.source.id]] : []
|
|
31
|
+
)
|
|
32
|
+
)
|
|
28
33
|
const references = {
|
|
29
34
|
nodes: identities,
|
|
30
|
-
styles
|
|
35
|
+
styles,
|
|
31
36
|
properties,
|
|
32
37
|
propertyTypes,
|
|
33
38
|
...variableIdentityReferences(plan.variables, plan.collections)
|
package/src/types.ts
CHANGED
|
@@ -196,12 +196,14 @@ export type MaskType = 'ALPHA' | 'VECTOR' | 'LUMINANCE'
|
|
|
196
196
|
export interface LayoutGrid {
|
|
197
197
|
visible?: boolean
|
|
198
198
|
color?: Color
|
|
199
|
-
|
|
199
|
+
/** `STRIPES` is the archive's name for columns or rows, along `axis`. */
|
|
200
|
+
pattern?: 'COLUMNS' | 'ROWS' | 'GRID' | 'STRIPES'
|
|
200
201
|
axis?: 'X' | 'Y'
|
|
201
202
|
type?: 'MIN' | 'CENTER' | 'MAX' | 'STRETCH'
|
|
202
203
|
alignment?: 'MIN' | 'CENTER' | 'MAX' | 'STRETCH'
|
|
203
204
|
numSections?: number
|
|
204
|
-
count
|
|
205
|
+
/** Figma's plugin API reports an Auto count as `null` and accepts `Infinity` for it. */
|
|
206
|
+
count?: number | null
|
|
205
207
|
offset?: number
|
|
206
208
|
sectionSize?: number
|
|
207
209
|
gutterSize?: number
|
|
@@ -270,6 +272,29 @@ export interface FontFeature {
|
|
|
270
272
|
enabled: boolean
|
|
271
273
|
}
|
|
272
274
|
|
|
275
|
+
/** Where a stretch of text links to, as Figma's `HyperlinkTarget`: a URL, or a node by its ID. */
|
|
276
|
+
export interface HyperlinkTarget {
|
|
277
|
+
type: 'URL' | 'NODE'
|
|
278
|
+
value: string
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** The text fields a range of characters can bind to a variable, as Figma lists them. */
|
|
282
|
+
export const TEXT_BINDABLE_FIELDS = [
|
|
283
|
+
'fontFamily',
|
|
284
|
+
'fontSize',
|
|
285
|
+
'fontStyle',
|
|
286
|
+
'fontWeight',
|
|
287
|
+
'letterSpacing',
|
|
288
|
+
'lineHeight',
|
|
289
|
+
'paragraphSpacing',
|
|
290
|
+
'paragraphIndent'
|
|
291
|
+
] as const
|
|
292
|
+
|
|
293
|
+
export type TextBindableField = (typeof TEXT_BINDABLE_FIELDS)[number]
|
|
294
|
+
|
|
295
|
+
/** Variable IDs bound to text fields, by field. */
|
|
296
|
+
export type TextBoundVariables = Partial<Record<TextBindableField, string>>
|
|
297
|
+
|
|
273
298
|
export interface CharacterStyleOverride {
|
|
274
299
|
fontWeight?: number
|
|
275
300
|
italic?: boolean
|
|
@@ -287,6 +312,15 @@ export interface CharacterStyleOverride {
|
|
|
287
312
|
fontVariations?: FontVariation[]
|
|
288
313
|
fontFeatures?: FontFeature[]
|
|
289
314
|
textLanguage?: string | null
|
|
315
|
+
textCase?: TextCase
|
|
316
|
+
/** A link over the characters; `null`, like leaving it out, links nowhere. */
|
|
317
|
+
hyperlink?: HyperlinkTarget | null
|
|
318
|
+
/** The text style the characters use in place of the text's own; `null` uses none. */
|
|
319
|
+
textStyleId?: string | null
|
|
320
|
+
/** The fill style the characters use in place of the text's own; `null` uses none. */
|
|
321
|
+
fillStyleId?: string | null
|
|
322
|
+
/** The characters' text field bindings, in place of the text's own. */
|
|
323
|
+
boundVariables?: TextBoundVariables
|
|
290
324
|
}
|
|
291
325
|
|
|
292
326
|
export interface StyleRun {
|
|
@@ -820,6 +854,8 @@ export interface VariableCollectionMode {
|
|
|
820
854
|
* at-rule prelude (`@media (max-width: 640px)`). Absent means the default for its axis.
|
|
821
855
|
*/
|
|
822
856
|
condition?: string
|
|
857
|
+
/** Where Figma lists the mode, as the file saved it; Figma orders a collection's modes by it. */
|
|
858
|
+
sortPosition?: string
|
|
823
859
|
}
|
|
824
860
|
|
|
825
861
|
export interface VariableCollection {
|
|
@@ -835,6 +871,18 @@ export interface VariableCollection {
|
|
|
835
871
|
*/
|
|
836
872
|
modeAttribute?: string
|
|
837
873
|
pluginData?: PluginDataEntry[]
|
|
874
|
+
/**
|
|
875
|
+
* Published library key, for a collection the file copies from a library. Such a collection
|
|
876
|
+
* belongs to the library: Figma lists it with the library's, not the file's own.
|
|
877
|
+
*/
|
|
878
|
+
key?: string
|
|
879
|
+
/** Published library version, which references by key carry with it. */
|
|
880
|
+
version?: string
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
/** Whether a collection is a library's copy, which takes no variables or modes of the file's. */
|
|
884
|
+
export function isLibraryCollection(collection: Pick<VariableCollection, 'key'>): boolean {
|
|
885
|
+
return !!collection.key
|
|
838
886
|
}
|
|
839
887
|
|
|
840
888
|
/** An HTML attribute name a stylesheet can select on unescaped: `data-theme`, `theme`. */
|
|
@@ -2,7 +2,7 @@ import { copyFills, copyStrokes } from '../copy'
|
|
|
2
2
|
import type { SceneGraph } from '../index'
|
|
3
3
|
import { hasInstanceOverride } from '../instances'
|
|
4
4
|
import type { Color } from '../primitives'
|
|
5
|
-
import type { Fill, SceneNode, Stroke } from '../types'
|
|
5
|
+
import type { Fill, SceneNode, Stroke, StyleRun, VariableValue } from '../types'
|
|
6
6
|
import type { VariableModeFallback } from '../variables'
|
|
7
7
|
import { NUMERIC_FIELDS, isNumericVariableBindingField } from './fields'
|
|
8
8
|
export { isNumericVariableBindingField } from './fields'
|
|
@@ -57,6 +57,41 @@ export function resolvedNumericBindings(
|
|
|
57
57
|
return updates
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
/**
|
|
61
|
+
* The text a variable bound to text content shows: a string as it is, and a number or boolean
|
|
62
|
+
* as Figma writes it, since Figma lets either be text content.
|
|
63
|
+
*/
|
|
64
|
+
export function variableText(value: VariableValue | undefined): string | undefined {
|
|
65
|
+
if (typeof value === 'string') return value
|
|
66
|
+
if (typeof value === 'boolean') return String(value)
|
|
67
|
+
if (typeof value === 'number' && Number.isFinite(value)) return numberText(value)
|
|
68
|
+
return undefined
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const INT32_MIN = -2_147_483_648
|
|
72
|
+
const INT32_MAX = 2_147_483_647
|
|
73
|
+
|
|
74
|
+
/** How near a whole number a value must be for Figma to write it as one. */
|
|
75
|
+
const WHOLE_NUMBER_TOLERANCE = 1e-6
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* A number as Figma desktop 126 writes it as text: one within a millionth of a whole number as
|
|
79
|
+
* that integer, clamped to 32 bits, and anything else with two decimals, its sign kept and an
|
|
80
|
+
* exact tie rounded to even, as C's `%.2f` does (0.125 is "0.12", 0.999 is "1.00", -0.004 is
|
|
81
|
+
* "-0.00").
|
|
82
|
+
*/
|
|
83
|
+
function numberText(value: number): string {
|
|
84
|
+
const whole = Math.round(value)
|
|
85
|
+
if (Math.abs(value - whole) < WHOLE_NUMBER_TOLERANCE)
|
|
86
|
+
return String(Math.min(INT32_MAX, Math.max(INT32_MIN, whole)) || 0)
|
|
87
|
+
const scaled = Math.abs(value) * 100
|
|
88
|
+
const floor = Math.floor(scaled)
|
|
89
|
+
const tie = scaled - floor === 0.5
|
|
90
|
+
const cents = tie ? floor + (floor % 2) : Math.round(scaled)
|
|
91
|
+
const sign = value < 0 ? '-' : ''
|
|
92
|
+
return `${sign}${Math.floor(cents / 100)}.${String(cents % 100).padStart(2, '0')}`
|
|
93
|
+
}
|
|
94
|
+
|
|
60
95
|
/**
|
|
61
96
|
* The text and visibility a node's string and boolean bindings resolve to in its mode, for the
|
|
62
97
|
* fields whose stored value differs. Text or visibility an instance overrides, which typing in a
|
|
@@ -75,13 +110,50 @@ export function resolvedValueBindings(
|
|
|
75
110
|
if (!variableId || hasInstanceOverride(graph, node.id, field)) return undefined
|
|
76
111
|
return graph.resolveVariableForNode(node.id, variableId, fallback)
|
|
77
112
|
}
|
|
78
|
-
const text = node.type === 'TEXT' ? resolve('text') : undefined
|
|
79
|
-
if (
|
|
113
|
+
const text = node.type === 'TEXT' ? variableText(resolve('text')) : undefined
|
|
114
|
+
if (text !== undefined && text !== node.text) updates.text = text
|
|
80
115
|
const visible = resolve('visible')
|
|
81
116
|
if (typeof visible === 'boolean' && visible !== node.visible) updates.visible = visible
|
|
82
117
|
return updates
|
|
83
118
|
}
|
|
84
119
|
|
|
120
|
+
/** Fields of a range of characters a bound number gives its value; the rest need fonts loaded. */
|
|
121
|
+
const NUMERIC_RUN_FIELDS = ['fontSize', 'fontWeight', 'letterSpacing', 'lineHeight'] as const
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The runs a text's character bindings resolve to in its mode, when a bound number differs from
|
|
125
|
+
* what its characters hold. Bound font families and styles are left as stored, as the text's own.
|
|
126
|
+
*/
|
|
127
|
+
export function resolvedRunBindings(
|
|
128
|
+
graph: SceneGraph,
|
|
129
|
+
node: SceneNode,
|
|
130
|
+
fallback: VariableModeFallback = 'active'
|
|
131
|
+
): Pick<Partial<SceneNode>, 'styleRuns'> {
|
|
132
|
+
const styleRuns = node.styleRuns.map((run): StyleRun => {
|
|
133
|
+
const bound = run.style.boundVariables
|
|
134
|
+
if (!bound) return run
|
|
135
|
+
let style = run.style
|
|
136
|
+
for (const field of NUMERIC_RUN_FIELDS) {
|
|
137
|
+
const variableId = bound[field]
|
|
138
|
+
if (!variableId) continue
|
|
139
|
+
const value = graph.resolveNumberVariableForNode(node.id, variableId, fallback)
|
|
140
|
+
if (value === undefined || !Number.isFinite(value) || style[field] === value) continue
|
|
141
|
+
style = { ...style, [field]: value }
|
|
142
|
+
}
|
|
143
|
+
return style === run.style ? run : { ...run, style }
|
|
144
|
+
})
|
|
145
|
+
const changed = styleRuns.some((run, index) => run !== node.styleRuns[index])
|
|
146
|
+
return changed ? { styleRuns } : {}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** The variables a node binds, its own and its characters'. */
|
|
150
|
+
function boundVariableIds(node: SceneNode): string[] {
|
|
151
|
+
return [
|
|
152
|
+
...Object.values(node.boundVariables),
|
|
153
|
+
...node.styleRuns.flatMap((run) => Object.values(run.style.boundVariables ?? {}))
|
|
154
|
+
].filter((id): id is string => typeof id === 'string')
|
|
155
|
+
}
|
|
156
|
+
|
|
85
157
|
/**
|
|
86
158
|
* A bound paint takes its whole color from the variable, alpha included, the way Figma stores
|
|
87
159
|
* it: the variable's RGB at full alpha, and its alpha as the paint's opacity.
|
|
@@ -157,13 +229,42 @@ function subtreeNodes(graph: SceneGraph, roots: Iterable<string>): SceneNode[] {
|
|
|
157
229
|
return nodes
|
|
158
230
|
}
|
|
159
231
|
|
|
232
|
+
/**
|
|
233
|
+
* The collections whose variables the layers in these subtrees resolve: those they bind, and
|
|
234
|
+
* those the bound variables alias, so a mode set on the subtrees can change what they show.
|
|
235
|
+
*/
|
|
236
|
+
export function collectionsBoundIn(graph: SceneGraph, roots: Iterable<string>): Set<string> {
|
|
237
|
+
const pending = subtreeNodes(graph, roots).flatMap(boundVariableIds)
|
|
238
|
+
const seen = new Set<string>()
|
|
239
|
+
const collections = new Set<string>()
|
|
240
|
+
for (let id = pending.pop(); id !== undefined; id = pending.pop()) {
|
|
241
|
+
if (seen.has(id)) continue
|
|
242
|
+
seen.add(id)
|
|
243
|
+
const variable = graph.variables.get(id)
|
|
244
|
+
if (!variable) continue
|
|
245
|
+
collections.add(variable.collectionId)
|
|
246
|
+
for (const value of Object.values(variable.valuesByMode)) {
|
|
247
|
+
if (typeof value === 'object' && 'aliasId' in value) pending.push(value.aliasId)
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return collections
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* The layers a change to these variables reaches: those binding one of them, or a variable that
|
|
255
|
+
* aliases one, on the layer itself or on its characters.
|
|
256
|
+
*/
|
|
257
|
+
export function layersUsingVariables(graph: SceneGraph, ids: Iterable<string>): SceneNode[] {
|
|
258
|
+
const variables = variablesResolvingThrough(graph, ids)
|
|
259
|
+
return [...graph.getAllNodes()].filter((node) =>
|
|
260
|
+
boundVariableIds(node).some((id) => variables.has(id))
|
|
261
|
+
)
|
|
262
|
+
}
|
|
263
|
+
|
|
160
264
|
function scopedNodes(graph: SceneGraph, scope: BindingScope | undefined): Iterable<SceneNode> {
|
|
161
265
|
if (!scope) return graph.getAllNodes()
|
|
162
266
|
if ('subtrees' in scope) return subtreeNodes(graph, scope.subtrees)
|
|
163
|
-
|
|
164
|
-
return [...graph.getAllNodes()].filter((node) =>
|
|
165
|
-
Object.values(node.boundVariables).some((id) => variables.has(id))
|
|
166
|
-
)
|
|
267
|
+
return layersUsingVariables(graph, scope.variables)
|
|
167
268
|
}
|
|
168
269
|
|
|
169
270
|
/**
|
|
@@ -176,7 +277,8 @@ export function reconcileVariableBindings(graph: SceneGraph, scope?: BindingScop
|
|
|
176
277
|
for (const node of scopedNodes(graph, scope)) {
|
|
177
278
|
const updates = {
|
|
178
279
|
...resolvedNumericBindings(graph, node),
|
|
179
|
-
...resolvedValueBindings(graph, node)
|
|
280
|
+
...resolvedValueBindings(graph, node),
|
|
281
|
+
...resolvedRunBindings(graph, node)
|
|
180
282
|
}
|
|
181
283
|
if (Object.keys(updates).length === 0) continue
|
|
182
284
|
graph.updateNode(node.id, updates)
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
//#region src/fields/text.ts
|
|
2
|
-
/** Fields shared by shaping, paragraph measurement, and auto-width invalidation. */
|
|
3
|
-
const TEXT_METRIC_FIELDS = [
|
|
4
|
-
"text",
|
|
5
|
-
"fontSize",
|
|
6
|
-
"fontFamily",
|
|
7
|
-
"fontWeight",
|
|
8
|
-
"italic",
|
|
9
|
-
"letterSpacing",
|
|
10
|
-
"styleRuns",
|
|
11
|
-
"fontVariations",
|
|
12
|
-
"fontFeatures",
|
|
13
|
-
"textParagraphs",
|
|
14
|
-
"listSpacing",
|
|
15
|
-
"paragraphSpacing",
|
|
16
|
-
"paragraphIndent",
|
|
17
|
-
"hangingList"
|
|
18
|
-
];
|
|
19
|
-
const TEXT_SHAPING_FIELDS = [
|
|
20
|
-
...TEXT_METRIC_FIELDS,
|
|
21
|
-
"lineHeight",
|
|
22
|
-
"textCase",
|
|
23
|
-
"textDirection",
|
|
24
|
-
"textAlignHorizontal",
|
|
25
|
-
"textAlignVertical"
|
|
26
|
-
];
|
|
27
|
-
const TEXT_LAYOUT_FIELDS = [
|
|
28
|
-
"width",
|
|
29
|
-
"height",
|
|
30
|
-
"textAutoResize",
|
|
31
|
-
"maxLines"
|
|
32
|
-
];
|
|
33
|
-
//#endregion
|
|
34
|
-
export { TEXT_LAYOUT_FIELDS, TEXT_METRIC_FIELDS, TEXT_SHAPING_FIELDS };
|
|
35
|
-
|
|
36
|
-
//# sourceMappingURL=text.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"text.js","names":[],"sources":["../../../src/fields/text.ts"],"sourcesContent":["import type { SceneNode } from '../types'\n\n/** Fields shared by shaping, paragraph measurement, and auto-width invalidation. */\nexport const TEXT_METRIC_FIELDS = [\n 'text',\n 'fontSize',\n 'fontFamily',\n 'fontWeight',\n 'italic',\n 'letterSpacing',\n 'styleRuns',\n 'fontVariations',\n 'fontFeatures',\n 'textParagraphs',\n 'listSpacing',\n 'paragraphSpacing',\n 'paragraphIndent',\n 'hangingList'\n] as const satisfies readonly (keyof SceneNode)[]\n\nexport const TEXT_SHAPING_FIELDS = [\n ...TEXT_METRIC_FIELDS,\n 'lineHeight',\n 'textCase',\n 'textDirection',\n 'textAlignHorizontal',\n 'textAlignVertical'\n] as const satisfies readonly (keyof SceneNode)[]\n\nexport const TEXT_LAYOUT_FIELDS = [\n 'width',\n 'height',\n 'textAutoResize',\n 'maxLines'\n] as const satisfies readonly (keyof SceneNode)[]\n"],"mappings":";;AAGA,MAAa,qBAAqB;CAChC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,MAAa,sBAAsB;CACjC,GAAG;CACH;CACA;CACA;CACA;CACA;AACF;AAEA,MAAa,qBAAqB;CAChC;CACA;CACA;CACA;AACF"}
|