@pairdraw/pen 0.0.0-stage → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +10 -2
- package/dist/chunks/annotations.js +510 -0
- package/dist/chunks/annotations.js.map +1 -0
- package/dist/chunks/assets.js +158 -0
- package/dist/chunks/assets.js.map +1 -0
- package/dist/chunks/effects.js +775 -0
- package/dist/chunks/effects.js.map +1 -0
- package/dist/chunks/geometry.js +95 -0
- package/dist/chunks/geometry.js.map +1 -0
- package/dist/chunks/icons.js +156 -0
- package/dist/chunks/icons.js.map +1 -0
- package/dist/chunks/imports.js +120 -0
- package/dist/chunks/imports.js.map +1 -0
- package/dist/chunks/index.js +1478 -0
- package/dist/chunks/index.js.map +1 -0
- package/dist/index.d.ts +492 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/package.json +31 -4
- package/src/annotations.ts +128 -0
- package/src/assets.ts +224 -0
- package/src/effects.ts +62 -0
- package/src/geometry.ts +71 -0
- package/src/icons.ts +173 -0
- package/src/images.ts +109 -0
- package/src/imports.ts +169 -0
- package/src/index.ts +23 -0
- package/src/instances.ts +272 -0
- package/src/jsonc.ts +77 -0
- package/src/layout.ts +135 -0
- package/src/library.ts +153 -0
- package/src/nodes.ts +410 -0
- package/src/paints.ts +417 -0
- package/src/placeholders.ts +86 -0
- package/src/placement.ts +84 -0
- package/src/plugin-data.ts +86 -0
- package/src/read.ts +150 -0
- package/src/schema.ts +299 -0
- package/src/slots.ts +41 -0
- package/src/text.ts +92 -0
- package/src/variables.ts +284 -0
- package/src/write.ts +594 -0
package/src/layout.ts
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
LayoutAlign,
|
|
3
|
+
LayoutCounterAlign,
|
|
4
|
+
LayoutMode,
|
|
5
|
+
LayoutSizing,
|
|
6
|
+
SceneNode
|
|
7
|
+
} from '@pairdraw/scene-graph'
|
|
8
|
+
import { parseAutoLayoutDirection } from '@pairdraw/scene-graph/layout'
|
|
9
|
+
|
|
10
|
+
import type { PenNode, PenNumber } from './schema'
|
|
11
|
+
import { bindIfVar, isVarRef, resolveNumber, type VarContext } from './variables'
|
|
12
|
+
|
|
13
|
+
const CORNERS = [
|
|
14
|
+
'topLeftRadius',
|
|
15
|
+
'topRightRadius',
|
|
16
|
+
'bottomRightRadius',
|
|
17
|
+
'bottomLeftRadius'
|
|
18
|
+
] as const
|
|
19
|
+
const PADDING = ['paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft'] as const
|
|
20
|
+
|
|
21
|
+
export function applyCornerRadius(
|
|
22
|
+
node: SceneNode,
|
|
23
|
+
radius: PenNode['cornerRadius'],
|
|
24
|
+
ctx: VarContext
|
|
25
|
+
): void {
|
|
26
|
+
if (radius === undefined) return
|
|
27
|
+
if (Array.isArray(radius)) {
|
|
28
|
+
node.independentCorners = true
|
|
29
|
+
for (const [index, field] of CORNERS.entries()) {
|
|
30
|
+
node[field] = resolveNumber(radius[index], ctx)
|
|
31
|
+
bindIfVar(node, field, radius[index], ctx)
|
|
32
|
+
}
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
node.cornerRadius = resolveNumber(radius, ctx)
|
|
36
|
+
bindIfVar(node, 'cornerRadius', radius, ctx)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** pen.dev padding: one value, vertical and horizontal, or top, right, bottom, and left. */
|
|
40
|
+
function paddingSides(padding: NonNullable<PenNode['padding']>): PenNumber[] {
|
|
41
|
+
if (!Array.isArray(padding)) return [padding, padding, padding, padding]
|
|
42
|
+
if (padding.length === 2) return [padding[0], padding[1], padding[0], padding[1]]
|
|
43
|
+
return padding
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function applyPadding(node: SceneNode, padding: PenNode['padding'], ctx: VarContext): void {
|
|
47
|
+
if (padding === undefined) return
|
|
48
|
+
const sides = paddingSides(padding)
|
|
49
|
+
for (const [index, field] of PADDING.entries()) {
|
|
50
|
+
node[field] = resolveNumber(sides[index], ctx)
|
|
51
|
+
bindIfVar(node, field, sides[index], ctx)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface ParsedSize {
|
|
56
|
+
value: number
|
|
57
|
+
sizing: LayoutSizing
|
|
58
|
+
/** Fallback used only when fit_content(N) has no children. */
|
|
59
|
+
fitContentFallback?: number
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function parseParameterizedFallback(value: string, behavior: string): number | undefined {
|
|
63
|
+
const prefix = `${behavior}(`
|
|
64
|
+
if (!value.startsWith(prefix) || !value.endsWith(')')) return undefined
|
|
65
|
+
|
|
66
|
+
const rawFallback = value.slice(prefix.length, -1).trim()
|
|
67
|
+
if (rawFallback === '') return undefined
|
|
68
|
+
|
|
69
|
+
const parsedFallback = Number(rawFallback)
|
|
70
|
+
return Number.isFinite(parsedFallback) ? parsedFallback : undefined
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function parseSizingBehavior(value: string, fallback: number): ParsedSize | undefined {
|
|
74
|
+
if (value === 'fill_container') return { value: fallback, sizing: 'FILL' }
|
|
75
|
+
if (value === 'fit_content') return { value: fallback, sizing: 'HUG' }
|
|
76
|
+
|
|
77
|
+
// Older generated .pen files used hug_content as an alias for fit_content.
|
|
78
|
+
if (value === 'hug_content') return { value: fallback, sizing: 'HUG' }
|
|
79
|
+
|
|
80
|
+
const fillFallback = parseParameterizedFallback(value, 'fill_container')
|
|
81
|
+
if (fillFallback !== undefined) return { value: fillFallback, sizing: 'FILL' }
|
|
82
|
+
|
|
83
|
+
const fitFallback =
|
|
84
|
+
parseParameterizedFallback(value, 'fit_content') ??
|
|
85
|
+
parseParameterizedFallback(value, 'hug_content')
|
|
86
|
+
if (fitFallback !== undefined) {
|
|
87
|
+
return { value: fitFallback, sizing: 'HUG', fitContentFallback: fitFallback }
|
|
88
|
+
}
|
|
89
|
+
return undefined
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function parseSize(
|
|
93
|
+
value: number | string | undefined,
|
|
94
|
+
fallback: number,
|
|
95
|
+
ctx?: VarContext
|
|
96
|
+
): ParsedSize {
|
|
97
|
+
if (value === undefined) return { value: fallback, sizing: 'FIXED' }
|
|
98
|
+
if (typeof value === 'number') return { value, sizing: 'FIXED' }
|
|
99
|
+
|
|
100
|
+
const behavior = parseSizingBehavior(value, fallback)
|
|
101
|
+
if (behavior) return behavior
|
|
102
|
+
|
|
103
|
+
if (isVarRef(value) && ctx) return { value: ctx.resolveNumber(value), sizing: 'FIXED' }
|
|
104
|
+
const parsed = Number(value)
|
|
105
|
+
return { value: Number.isFinite(parsed) ? parsed : fallback, sizing: 'FIXED' }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function mapLayoutMode(pen: PenNode): LayoutMode {
|
|
109
|
+
const direction = parseAutoLayoutDirection(pen.layout)
|
|
110
|
+
if (direction) return direction
|
|
111
|
+
return pen.type === 'frame' && pen.layout === undefined ? 'HORIZONTAL' : 'NONE'
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** pen.dev's main axis alignments. */
|
|
115
|
+
const JUSTIFY_CONTENT: Partial<Record<string, LayoutAlign>> = {
|
|
116
|
+
start: 'MIN',
|
|
117
|
+
center: 'CENTER',
|
|
118
|
+
end: 'MAX',
|
|
119
|
+
space_between: 'SPACE_BETWEEN',
|
|
120
|
+
space_around: 'SPACE_AROUND'
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const ALIGN_ITEMS: Partial<Record<string, LayoutCounterAlign>> = {
|
|
124
|
+
start: 'MIN',
|
|
125
|
+
center: 'CENTER',
|
|
126
|
+
end: 'MAX'
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function mapJustifyContent(value: string | undefined): LayoutAlign {
|
|
130
|
+
return JUSTIFY_CONTENT[value ?? 'start'] ?? 'MIN'
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function mapAlignItems(value: string | undefined): LayoutCounterAlign {
|
|
134
|
+
return ALIGN_ITEMS[value ?? 'start'] ?? 'MIN'
|
|
135
|
+
}
|
package/src/library.ts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import type { SceneGraph } from '@pairdraw/scene-graph'
|
|
2
|
+
import { libraryDefinitionsPage } from '@pairdraw/scene-graph/components'
|
|
3
|
+
|
|
4
|
+
import { withImports, type PenImports } from './imports'
|
|
5
|
+
import type { Build } from './nodes'
|
|
6
|
+
import type { PenDocument, PenNode, PenOverride } from './schema'
|
|
7
|
+
|
|
8
|
+
/** A component an imported document defines, with the outermost component holding it. */
|
|
9
|
+
interface Owner {
|
|
10
|
+
alias: string
|
|
11
|
+
path: string
|
|
12
|
+
root: PenNode
|
|
13
|
+
/** The themes of the layers around the root in its document, which it was drawn in. */
|
|
14
|
+
theme: Record<string, string> | undefined
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** An imported component the document draws, as it went on the library page. */
|
|
18
|
+
export interface LibraryRoot {
|
|
19
|
+
id: string
|
|
20
|
+
alias: string
|
|
21
|
+
path: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The component IDs a layer and its overrides name: its `ref`s and the slots' suggestions. */
|
|
25
|
+
function namedComponents(node: PenOverride, found: string[]): void {
|
|
26
|
+
if (typeof node.ref === 'string') found.push(node.ref)
|
|
27
|
+
if (Array.isArray(node.slot)) found.push(...node.slot)
|
|
28
|
+
for (const child of node.children ?? []) namedComponents(child, found)
|
|
29
|
+
for (const override of Object.values(node.descendants ?? {})) namedComponents(override, found)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function mergeTheme(
|
|
33
|
+
outer: Record<string, string> | undefined,
|
|
34
|
+
inner: Record<string, string> | undefined
|
|
35
|
+
): Record<string, string> | undefined {
|
|
36
|
+
return outer || inner ? { ...outer, ...inner } : undefined
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Each reusable layer of the imported documents by ID, with the outermost component holding it. */
|
|
40
|
+
function componentOwners(imports: PenImports): Map<string, Owner> {
|
|
41
|
+
const owners = new Map<string, Owner>()
|
|
42
|
+
for (const [alias, { path, doc }] of imports) {
|
|
43
|
+
const visit = (nodes: readonly PenNode[], root: PenNode | undefined, theme: Owner['theme']) => {
|
|
44
|
+
for (const node of nodes) {
|
|
45
|
+
const own = root ?? (node.reusable ? node : undefined)
|
|
46
|
+
if (node.reusable && own) owners.set(node.id, { alias, path, root: own, theme })
|
|
47
|
+
visit(node.children ?? [], own, own ? theme : mergeTheme(theme, node.theme))
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
visit(doc.children, undefined, undefined)
|
|
51
|
+
}
|
|
52
|
+
return owners
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The imported components a document draws, through the components they draw in turn, each as the
|
|
57
|
+
* outermost component holding it, in the order the document first names them. A library's other
|
|
58
|
+
* components stay out of the document, which only ever shows instances of the ones it uses.
|
|
59
|
+
*/
|
|
60
|
+
function usedRoots(doc: PenDocument, imports: PenImports): Owner[] {
|
|
61
|
+
const owners = componentOwners(imports)
|
|
62
|
+
const pending: string[] = []
|
|
63
|
+
for (const node of doc.children) namedComponents(node, pending)
|
|
64
|
+
const roots = new Map<string, Owner>()
|
|
65
|
+
// Each root's own names join the list as it is read.
|
|
66
|
+
for (const id of pending) {
|
|
67
|
+
const owner = owners.get(id)
|
|
68
|
+
if (!owner || roots.has(owner.root.id)) continue
|
|
69
|
+
roots.set(owner.root.id, owner)
|
|
70
|
+
namedComponents(owner.root, pending)
|
|
71
|
+
}
|
|
72
|
+
return [...roots.values()]
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The document with the imported components it uses after its own layers, and the imported
|
|
77
|
+
* variables and themes, so `penAssets` lists every icon and image it draws and no other of a
|
|
78
|
+
* library's.
|
|
79
|
+
*/
|
|
80
|
+
export function withUsedImports(doc: PenDocument, imports: PenImports): PenDocument {
|
|
81
|
+
if (imports.size === 0) return doc
|
|
82
|
+
return {
|
|
83
|
+
...withImports(doc, imports),
|
|
84
|
+
children: [...doc.children, ...usedRoots(doc, imports).map((owner) => owner.root)]
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Draws the imported components the document uses on the hidden library page, in the themes they
|
|
90
|
+
* sit in in their own documents, so its instances of them resolve. Nothing is drawn when the
|
|
91
|
+
* document uses none.
|
|
92
|
+
*/
|
|
93
|
+
export function createLibraryRoots(
|
|
94
|
+
doc: PenDocument,
|
|
95
|
+
imports: PenImports,
|
|
96
|
+
build: Build,
|
|
97
|
+
create: (pen: PenNode, parentId: string, build: Build) => string
|
|
98
|
+
): LibraryRoot[] {
|
|
99
|
+
const roots = usedRoots(doc, imports)
|
|
100
|
+
if (roots.length === 0) return []
|
|
101
|
+
const page = libraryDefinitionsPage(build.graph)
|
|
102
|
+
return roots.map(({ alias, path, root, theme }) => {
|
|
103
|
+
const leave = theme ? build.ctx.enterTheme(theme) : undefined
|
|
104
|
+
try {
|
|
105
|
+
return { id: create(root, page.id, build), alias, path }
|
|
106
|
+
} finally {
|
|
107
|
+
leave?.()
|
|
108
|
+
}
|
|
109
|
+
})
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** `value` with what `allowed` rejects replaced, starting with a letter or digit, at most `max` long. */
|
|
113
|
+
function libraryName(value: string, allowed: RegExp, max: number): string {
|
|
114
|
+
const name = value.replace(allowed, '_')
|
|
115
|
+
return (/^[a-zA-Z0-9]/.test(name) ? name : `x${name}`).slice(0, max)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** The ID a library's assets name it by: `pen:<alias>`, in the characters library IDs take. */
|
|
119
|
+
function penLibraryId(alias: string): string {
|
|
120
|
+
return libraryName(`pen:${alias}`, /[^a-zA-Z0-9._:-]/g, 128)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** A component's key in its library: its ID in the imported document, without the alias. */
|
|
124
|
+
function penLibraryAssetKey(alias: string, id: string): string {
|
|
125
|
+
const prefix = `${alias}:`
|
|
126
|
+
const own = id.startsWith(prefix) ? id.slice(prefix.length) : id
|
|
127
|
+
return libraryName(own, /[^a-zA-Z0-9._:/-]/g, 256)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Marks the imported components as a library's definitions: read-only, locked, and each with its
|
|
132
|
+
* library and key, so the editor treats them as it treats a component library's, and their
|
|
133
|
+
* instances as library instances. The file they came from is their revision.
|
|
134
|
+
*/
|
|
135
|
+
export function markLibraryRoots(graph: SceneGraph, roots: readonly LibraryRoot[]): void {
|
|
136
|
+
for (const { id, alias, path } of roots) {
|
|
137
|
+
const root = graph.getNode(id)
|
|
138
|
+
if (!root) continue
|
|
139
|
+
const libraryId = penLibraryId(alias)
|
|
140
|
+
const queue = [root]
|
|
141
|
+
for (const node of queue) {
|
|
142
|
+
if (node === root || node.type === 'COMPONENT' || node.type === 'COMPONENT_SET') {
|
|
143
|
+
node.librarySource = {
|
|
144
|
+
identity: { libraryId, assetKey: penLibraryAssetKey(alias, node.id), revisionId: path },
|
|
145
|
+
sourceNodeId: null,
|
|
146
|
+
readOnly: true
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
queue.push(...graph.getChildren(node.id))
|
|
150
|
+
}
|
|
151
|
+
root.locked = true
|
|
152
|
+
}
|
|
153
|
+
}
|
package/src/nodes.ts
ADDED
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AxisSizingMode,
|
|
3
|
+
LayoutMode,
|
|
4
|
+
LayoutSizing,
|
|
5
|
+
NodeType,
|
|
6
|
+
SceneGraph,
|
|
7
|
+
SceneNode
|
|
8
|
+
} from '@pairdraw/scene-graph'
|
|
9
|
+
import { CORNER_FIELDS } from '@pairdraw/scene-graph/fields'
|
|
10
|
+
import { fillSizingFields } from '@pairdraw/scene-graph/layout'
|
|
11
|
+
import type { Color } from '@pairdraw/scene-graph/primitives'
|
|
12
|
+
|
|
13
|
+
import { applyAnnotation, createAnnotation, isAnnotation } from './annotations'
|
|
14
|
+
import { convertEffects } from './effects'
|
|
15
|
+
import { ellipseArc } from './geometry'
|
|
16
|
+
import { drawIcon, insideComponent, useIconComponent } from './icons'
|
|
17
|
+
import type { PenImages } from './images'
|
|
18
|
+
import {
|
|
19
|
+
applyCornerRadius,
|
|
20
|
+
applyPadding,
|
|
21
|
+
mapAlignItems,
|
|
22
|
+
mapJustifyContent,
|
|
23
|
+
mapLayoutMode,
|
|
24
|
+
parseSize
|
|
25
|
+
} from './layout'
|
|
26
|
+
import { applyStroke, convertPaints, strokeFields, type PenShaderSources } from './paints'
|
|
27
|
+
import { applyPlaceholderLook, isPlaceholder, placeholderLabel } from './placeholders'
|
|
28
|
+
import { isPlain, type PenPlacement } from './placement'
|
|
29
|
+
import { applyPenPluginData, UNRESOLVED_REF } from './plugin-data'
|
|
30
|
+
import type { NodeFields, PenNode, PenOverride } from './schema'
|
|
31
|
+
import { createPenSlot } from './slots'
|
|
32
|
+
import { applyTextProps } from './text'
|
|
33
|
+
import { bindIfVar, resolveBoolean, resolveNumber, type VarContext } from './variables'
|
|
34
|
+
|
|
35
|
+
/** Where icon glyphs come from, as the host loaded them. */
|
|
36
|
+
export interface PenIconSource {
|
|
37
|
+
/**
|
|
38
|
+
* Draws the icon `name` into the frame, `size` square at its origin, painted `color` where the
|
|
39
|
+
* icon takes its colour and drawn with `options` where its set has them. False when the source
|
|
40
|
+
* does not have it.
|
|
41
|
+
*/
|
|
42
|
+
draw(
|
|
43
|
+
graph: SceneGraph,
|
|
44
|
+
frameId: string,
|
|
45
|
+
name: string,
|
|
46
|
+
size: number,
|
|
47
|
+
color: Color,
|
|
48
|
+
options?: Record<string, number>
|
|
49
|
+
): boolean
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** What reading one document builds up. */
|
|
53
|
+
export interface Build {
|
|
54
|
+
graph: SceneGraph
|
|
55
|
+
ctx: VarContext
|
|
56
|
+
images: PenImages
|
|
57
|
+
/** Shader files the document's paints and effects name, by URL, as their source. */
|
|
58
|
+
shaders: PenShaderSources | undefined
|
|
59
|
+
icons: PenIconSource | undefined
|
|
60
|
+
/** Reusable nodes by their pen.dev ID. */
|
|
61
|
+
components: Map<string, PenNode>
|
|
62
|
+
/** The pen.dev fields each layer was built from, its instance's and overrides' merged in. */
|
|
63
|
+
effective: Map<string, PenNode>
|
|
64
|
+
/** Instances in reading order, with the `ref` node that made each. */
|
|
65
|
+
instances: Map<string, PenNode>
|
|
66
|
+
/** pen.dev's placement of each turned or mirrored layer, and of every group. */
|
|
67
|
+
placements: Map<string, PenPlacement>
|
|
68
|
+
/** Groups in reading order. */
|
|
69
|
+
groupIds: Set<string>
|
|
70
|
+
/** The components icons in components are instances of, by glyph and size. */
|
|
71
|
+
iconComponents: Map<string, string>
|
|
72
|
+
/** The page those components are on, added with the first one. */
|
|
73
|
+
iconPageId: string | null
|
|
74
|
+
/** Layers whose children an override replaced, whose instances are detached. */
|
|
75
|
+
replaced: Set<string>
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function penPlacement(pen: PenOverride, ctx: VarContext): PenPlacement {
|
|
79
|
+
return {
|
|
80
|
+
x: pen.x ?? 0,
|
|
81
|
+
y: pen.y ?? 0,
|
|
82
|
+
rotation: resolveNumber(pen.rotation, ctx),
|
|
83
|
+
flipX: resolveBoolean(pen.flipX, ctx, false),
|
|
84
|
+
flipY: resolveBoolean(pen.flipY, ctx, false)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* A `line` runs from its box's top-left corner to its bottom-right, so a line with no height is
|
|
90
|
+
* a line layer and any other one a vector drawn across its box.
|
|
91
|
+
*/
|
|
92
|
+
function isFlatLine(pen: PenNode, ctx: VarContext): boolean {
|
|
93
|
+
const height = parseSize(pen.height, 100, ctx)
|
|
94
|
+
return height.sizing === 'FIXED' && height.value === 0
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function sceneType(pen: PenNode, ctx: VarContext): NodeType {
|
|
98
|
+
if (pen.type === 'frame') return pen.reusable ? 'COMPONENT' : 'FRAME'
|
|
99
|
+
if (pen.type === 'line') return isFlatLine(pen, ctx) ? 'LINE' : 'VECTOR'
|
|
100
|
+
if (isAnnotation(pen)) return 'STICKY'
|
|
101
|
+
if (pen.type === 'rectangle') return 'RECTANGLE'
|
|
102
|
+
if (pen.type === 'ellipse') return 'ELLIPSE'
|
|
103
|
+
if (pen.type === 'polygon') return 'POLYGON'
|
|
104
|
+
if (pen.type === 'group') return 'GROUP'
|
|
105
|
+
if (pen.type === 'text' || pen.type === 'icon_font') return 'TEXT'
|
|
106
|
+
if (pen.type === 'path') return 'VECTOR'
|
|
107
|
+
return 'FRAME'
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function resolveSizing(pen: PenNode, ctx: VarContext) {
|
|
111
|
+
const isTextLike = pen.type === 'text' || pen.type === 'icon_font'
|
|
112
|
+
const defaultSize = isTextLike ? 20 : 100
|
|
113
|
+
const defaultW = isTextLike && pen.width === undefined ? 0 : defaultSize
|
|
114
|
+
const w = parseSize(pen.width, defaultW, ctx)
|
|
115
|
+
const h = parseSize(pen.height, defaultSize, ctx)
|
|
116
|
+
const layout = mapLayoutMode(pen)
|
|
117
|
+
|
|
118
|
+
if (pen.width === undefined && layout !== 'NONE') w.sizing = 'HUG'
|
|
119
|
+
if (pen.height === undefined && layout !== 'NONE') h.sizing = 'HUG'
|
|
120
|
+
|
|
121
|
+
return { w, h, layout, isTextLike }
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function applyAutoLayout(
|
|
125
|
+
node: SceneNode,
|
|
126
|
+
layoutMode: LayoutMode,
|
|
127
|
+
pen: PenNode,
|
|
128
|
+
widthSizing: LayoutSizing,
|
|
129
|
+
heightSizing: LayoutSizing,
|
|
130
|
+
ctx: VarContext
|
|
131
|
+
): void {
|
|
132
|
+
node.layoutMode = layoutMode
|
|
133
|
+
node.primaryAxisAlign = mapJustifyContent(pen.justifyContent)
|
|
134
|
+
node.counterAxisAlign = mapAlignItems(pen.alignItems)
|
|
135
|
+
node.itemSpacing = resolveNumber(pen.gap, ctx)
|
|
136
|
+
bindIfVar(node, 'itemSpacing', pen.gap, ctx)
|
|
137
|
+
node.strokesIncludedInLayout = pen.layoutIncludeStroke ?? false
|
|
138
|
+
|
|
139
|
+
// Fill is the child's grow or stretch, set with its parent; the frame itself stays fixed.
|
|
140
|
+
const own = (sizing: LayoutSizing): AxisSizingMode => (sizing === 'HUG' ? 'HUG' : 'FIXED')
|
|
141
|
+
if (layoutMode === 'VERTICAL') {
|
|
142
|
+
node.primaryAxisSizing = own(heightSizing)
|
|
143
|
+
node.counterAxisSizing = own(widthSizing)
|
|
144
|
+
} else {
|
|
145
|
+
node.primaryAxisSizing = own(widthSizing)
|
|
146
|
+
node.counterAxisSizing = own(heightSizing)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const PLACEMENT_FIELDS = ['x', 'y', 'rotation', 'flipX', 'flipY'] as const
|
|
151
|
+
|
|
152
|
+
/** What `applyStroke` writes: a stroke's weight and alignment are its paints'. */
|
|
153
|
+
const STROKE_WRITES = [
|
|
154
|
+
'strokes',
|
|
155
|
+
'independentStrokeWeights',
|
|
156
|
+
'borderTopWeight',
|
|
157
|
+
'borderRightWeight',
|
|
158
|
+
'borderBottomWeight',
|
|
159
|
+
'borderLeftWeight',
|
|
160
|
+
'strokeCap',
|
|
161
|
+
'strokeJoin'
|
|
162
|
+
] as const
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* The layer fields each property `applyPenProperties` reads writes. A property that writes none of
|
|
166
|
+
* the layer's own fields maps to none: bookkeeping, the decorations pen.dev does not draw, and an
|
|
167
|
+
* icon's glyph, which is the layer's children or the component it shows.
|
|
168
|
+
*/
|
|
169
|
+
const PROPERTY_FIELDS = {
|
|
170
|
+
name: ['name'],
|
|
171
|
+
context: ['pluginData'],
|
|
172
|
+
metadata: ['pluginData'],
|
|
173
|
+
x: PLACEMENT_FIELDS,
|
|
174
|
+
y: PLACEMENT_FIELDS,
|
|
175
|
+
width: ['width'],
|
|
176
|
+
height: ['height'],
|
|
177
|
+
reusable: [],
|
|
178
|
+
theme: ['variableModes'],
|
|
179
|
+
enabled: ['visible'],
|
|
180
|
+
opacity: ['opacity'],
|
|
181
|
+
flipX: PLACEMENT_FIELDS,
|
|
182
|
+
flipY: PLACEMENT_FIELDS,
|
|
183
|
+
layoutPosition: ['layoutPositioning'],
|
|
184
|
+
rotation: PLACEMENT_FIELDS,
|
|
185
|
+
fill: ['fills'],
|
|
186
|
+
stroke: STROKE_WRITES,
|
|
187
|
+
strokeWidth: STROKE_WRITES,
|
|
188
|
+
strokeLinecap: ['strokes', 'strokeCap'],
|
|
189
|
+
strokeLinejoin: ['strokes', 'strokeJoin'],
|
|
190
|
+
strokeAlignment: ['strokes'],
|
|
191
|
+
effect: ['effects'],
|
|
192
|
+
cornerRadius: CORNER_FIELDS,
|
|
193
|
+
layout: ['layoutMode'],
|
|
194
|
+
gap: ['itemSpacing'],
|
|
195
|
+
layoutIncludeStroke: ['strokesIncludedInLayout'],
|
|
196
|
+
padding: ['paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft'],
|
|
197
|
+
justifyContent: ['primaryAxisAlign'],
|
|
198
|
+
alignItems: ['counterAxisAlign'],
|
|
199
|
+
clip: ['clipsContent'],
|
|
200
|
+
placeholder: ['pluginData'],
|
|
201
|
+
slot: ['pluginData'],
|
|
202
|
+
innerRadius: ['arcData'],
|
|
203
|
+
startAngle: ['arcData'],
|
|
204
|
+
sweepAngle: ['arcData'],
|
|
205
|
+
polygonCount: ['pointCount'],
|
|
206
|
+
geometry: ['vectorNetwork'],
|
|
207
|
+
fillRule: ['vectorNetwork'],
|
|
208
|
+
viewBox: ['vectorNetwork'],
|
|
209
|
+
content: ['text'],
|
|
210
|
+
textGrowth: ['textAutoResize'],
|
|
211
|
+
fontFamily: ['fontFamily'],
|
|
212
|
+
fontSize: ['fontSize'],
|
|
213
|
+
fontWeight: ['fontWeight'],
|
|
214
|
+
fontStyle: ['italic'],
|
|
215
|
+
letterSpacing: ['letterSpacing'],
|
|
216
|
+
lineHeight: ['lineHeight'],
|
|
217
|
+
textAlign: ['textAlignHorizontal'],
|
|
218
|
+
textAlignVertical: ['textAlignVertical'],
|
|
219
|
+
underline: [],
|
|
220
|
+
strikethrough: [],
|
|
221
|
+
href: ['styleRuns'],
|
|
222
|
+
library: [],
|
|
223
|
+
icon: [],
|
|
224
|
+
weight: [],
|
|
225
|
+
iconFontName: [],
|
|
226
|
+
iconFontFamily: [],
|
|
227
|
+
ref: [],
|
|
228
|
+
model: ['pluginData'],
|
|
229
|
+
scriptUri: ['pluginData'],
|
|
230
|
+
inputs: ['pluginData'],
|
|
231
|
+
url: ['pluginData'],
|
|
232
|
+
deviceId: ['pluginData'],
|
|
233
|
+
zoom: ['pluginData'],
|
|
234
|
+
scrollX: ['pluginData'],
|
|
235
|
+
scrollY: ['pluginData']
|
|
236
|
+
} satisfies Record<keyof NodeFields, readonly (keyof SceneNode)[]>
|
|
237
|
+
|
|
238
|
+
const isProperty = (key: string): key is keyof typeof PROPERTY_FIELDS => key in PROPERTY_FIELDS
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* The fields the properties `pen` carries write, whatever their values: pen.dev keeps a property
|
|
242
|
+
* an instance sets even when it equals its component's, so a later change to the component does
|
|
243
|
+
* not reach it.
|
|
244
|
+
*/
|
|
245
|
+
export function fieldsSetBy(pen: PenOverride): (keyof SceneNode)[] {
|
|
246
|
+
return Object.keys(pen).flatMap((key) => (isProperty(key) ? PROPERTY_FIELDS[key] : []))
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function drawIconLayer(node: SceneNode, pen: PenNode, build: Build): void {
|
|
250
|
+
if (node.type === 'INSTANCE') useIconComponent(node, pen, build)
|
|
251
|
+
else drawIcon(node, pen, build)
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Draws a filling or fitting icon again at its laid-out size, which reading it could only guess.
|
|
256
|
+
* Drawing takes the icon's colour from its fill and moves it to the glyph, so the fill is read again.
|
|
257
|
+
*/
|
|
258
|
+
export function redrawIconAtLaidOutSize(node: SceneNode, pen: PenNode, build: Build): void {
|
|
259
|
+
const { w, h } = resolveSizing(pen, build.ctx)
|
|
260
|
+
if (w.sizing === 'FIXED' && h.sizing === 'FIXED') return
|
|
261
|
+
node.fills = convertPaints(pen.fill, 'fills', build.ctx, node, build.images, build.shaders)
|
|
262
|
+
drawIconLayer(node, pen, build)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Sets every field a layer takes from its pen.dev fields, so a layer copied from a component can be
|
|
267
|
+
* given its overrides by applying its merged fields again. Turned and mirrored layers are built
|
|
268
|
+
* upright and placed once laid out.
|
|
269
|
+
*/
|
|
270
|
+
// eslint-disable-next-line complexity -- .pen node mapping touches many format-specific fields
|
|
271
|
+
export function applyPenProperties(node: SceneNode, pen: PenNode, build: Build): void {
|
|
272
|
+
if (node.type === 'STICKY') {
|
|
273
|
+
applyAnnotation(node, pen, build)
|
|
274
|
+
return
|
|
275
|
+
}
|
|
276
|
+
const { graph, ctx } = build
|
|
277
|
+
const { w, h, layout, isTextLike } = resolveSizing(pen, ctx)
|
|
278
|
+
const hasChildren = (pen.children?.length ?? 0) > 0 || node.childIds.length > 0
|
|
279
|
+
const parentLayout = (node.parentId && graph.getNode(node.parentId)?.layoutMode) || 'NONE'
|
|
280
|
+
// An absolute layer leaves its parent's flow; fill sizing then falls back to its value.
|
|
281
|
+
const absolute = pen.layoutPosition === 'absolute' && parentLayout !== 'NONE'
|
|
282
|
+
const flowLayout = absolute ? 'NONE' : parentLayout
|
|
283
|
+
|
|
284
|
+
Object.assign(node, {
|
|
285
|
+
name: pen.name ?? (pen.type === 'icon_font' ? (pen.iconFontName ?? 'Icon') : pen.type),
|
|
286
|
+
x: pen.x ?? 0,
|
|
287
|
+
y: pen.y ?? 0,
|
|
288
|
+
rotation: 0,
|
|
289
|
+
flipX: false,
|
|
290
|
+
flipY: false,
|
|
291
|
+
width: w.value,
|
|
292
|
+
height: h.value,
|
|
293
|
+
minWidth: !hasChildren && w.fitContentFallback !== undefined ? w.fitContentFallback : null,
|
|
294
|
+
minHeight: !hasChildren && h.fitContentFallback !== undefined ? h.fitContentFallback : null,
|
|
295
|
+
visible: resolveBoolean(pen.enabled, ctx),
|
|
296
|
+
opacity: resolveNumber(pen.opacity, ctx, 1),
|
|
297
|
+
clipsContent: resolveBoolean(pen.clip, ctx, false),
|
|
298
|
+
layoutPositioning: absolute ? 'ABSOLUTE' : 'AUTO',
|
|
299
|
+
layoutGrow: 0,
|
|
300
|
+
layoutAlignSelf: 'AUTO',
|
|
301
|
+
boundVariables: {},
|
|
302
|
+
variableModes: pen.theme && ctx.collectionId ? { [ctx.collectionId]: ctx.modeId() } : {}
|
|
303
|
+
} satisfies Partial<SceneNode>)
|
|
304
|
+
bindIfVar(node, 'visible', pen.enabled, ctx)
|
|
305
|
+
bindIfVar(node, 'opacity', pen.opacity, ctx)
|
|
306
|
+
bindIfVar(node, 'width', pen.width, ctx)
|
|
307
|
+
bindIfVar(node, 'height', pen.height, ctx)
|
|
308
|
+
|
|
309
|
+
node.layoutMode = 'NONE'
|
|
310
|
+
if (layout !== 'NONE') {
|
|
311
|
+
const widthSizing = flowLayout === 'NONE' && w.sizing === 'FILL' ? 'FIXED' : w.sizing
|
|
312
|
+
const heightSizing = flowLayout === 'NONE' && h.sizing === 'FILL' ? 'FIXED' : h.sizing
|
|
313
|
+
applyAutoLayout(node, layout, pen, widthSizing, heightSizing, ctx)
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const placement = penPlacement(pen, ctx)
|
|
317
|
+
if (pen.type === 'group') build.groupIds.add(node.id)
|
|
318
|
+
if (pen.type === 'group' || !isPlain(placement)) build.placements.set(node.id, placement)
|
|
319
|
+
else build.placements.delete(node.id)
|
|
320
|
+
|
|
321
|
+
node.fills = convertPaints(pen.fill, 'fills', ctx, node, build.images, build.shaders)
|
|
322
|
+
applyStroke(node, strokeFields(pen), ctx, build.images, build.shaders)
|
|
323
|
+
// A line layer has no inside or outside; its stroke is centred on it.
|
|
324
|
+
if (node.type === 'LINE')
|
|
325
|
+
node.strokes = node.strokes.map((paint) => ({ ...paint, align: 'CENTER' }))
|
|
326
|
+
node.effects = convertEffects(pen.effect, ctx, build.shaders)
|
|
327
|
+
applyCornerRadius(node, pen.cornerRadius, ctx)
|
|
328
|
+
applyPadding(node, pen.padding, ctx)
|
|
329
|
+
|
|
330
|
+
// Text without a width resizes to its content, which laying out the document measures.
|
|
331
|
+
if (isTextLike) applyTextProps(node, pen, ctx)
|
|
332
|
+
|
|
333
|
+
if (pen.type === 'icon') drawIconLayer(node, pen, build)
|
|
334
|
+
if (pen.type === 'ellipse') node.arcData = ellipseArc(pen, ctx)
|
|
335
|
+
if (pen.type === 'polygon') node.pointCount = Math.max(3, resolveNumber(pen.polygonCount, ctx, 3))
|
|
336
|
+
|
|
337
|
+
if (flowLayout !== 'NONE') {
|
|
338
|
+
if (w.sizing === 'FILL') Object.assign(node, fillSizingFields(flowLayout, 'HORIZONTAL'))
|
|
339
|
+
if (h.sizing === 'FILL') Object.assign(node, fillSizingFields(flowLayout, 'VERTICAL'))
|
|
340
|
+
}
|
|
341
|
+
if (isPlaceholder(pen)) applyPlaceholderLook(node, pen)
|
|
342
|
+
applyPenPluginData(node, pen)
|
|
343
|
+
build.effective.set(node.id, pen)
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* The fields an instance draws with: its component's, with the `ref` node's own replacing them one
|
|
348
|
+
* by one, as pen.dev reads them. A theme on both merges axis by axis. A `ref` whose component is
|
|
349
|
+
* missing, such as one from a library the host could not read, is a placeholder naming it.
|
|
350
|
+
*/
|
|
351
|
+
function instanceFields(ref: PenNode, component: PenNode | undefined): PenNode {
|
|
352
|
+
const { ref: _ref, type: _type, descendants: _descendants, children: _children, ...own } = ref
|
|
353
|
+
if (!component) return { ...own, type: 'frame', [UNRESOLVED_REF]: ref.ref ?? '' }
|
|
354
|
+
const {
|
|
355
|
+
id: _id,
|
|
356
|
+
reusable: _reusable,
|
|
357
|
+
ref: _componentRef,
|
|
358
|
+
children: _componentChildren,
|
|
359
|
+
descendants: _componentDescendants,
|
|
360
|
+
...inherited
|
|
361
|
+
} = component
|
|
362
|
+
const theme = component.theme || ref.theme ? { ...component.theme, ...ref.theme } : undefined
|
|
363
|
+
return { ...inherited, ...own, theme, name: ref.name ?? component.name }
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Builds the layers inside a layer that is no instance: a `ref`'s copies of its component's, named
|
|
368
|
+
* by the path to them, or the layer's own, then a placeholder's label and a slot.
|
|
369
|
+
*/
|
|
370
|
+
function createChildren(node: SceneNode, pen: PenNode, fields: PenNode, build: Build): void {
|
|
371
|
+
const component = pen.type === 'ref' && pen.ref ? build.components.get(pen.ref) : undefined
|
|
372
|
+
const children = pen.type === 'ref' ? (component?.children ?? []) : (pen.children ?? [])
|
|
373
|
+
for (const child of children) {
|
|
374
|
+
createSceneNode(
|
|
375
|
+
pen.type === 'ref' ? { ...child, id: `${pen.id}/${child.id}` } : child,
|
|
376
|
+
node.id,
|
|
377
|
+
build
|
|
378
|
+
)
|
|
379
|
+
}
|
|
380
|
+
const label = placeholderLabel(fields, node.id)
|
|
381
|
+
if (label) createSceneNode(label, node.id, build)
|
|
382
|
+
createPenSlot(node, fields, build)
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/** Builds a layer and its children in the layer's theme, merged over its parent's. */
|
|
386
|
+
export function createSceneNode(pen: PenNode, parentId: string, build: Build): string {
|
|
387
|
+
const component = pen.type === 'ref' && pen.ref ? build.components.get(pen.ref) : undefined
|
|
388
|
+
// A component that is not a frame has no instances; its uses are plain copies.
|
|
389
|
+
const instanceOf = pen.type === 'ref' && component?.type === 'frame' ? component : undefined
|
|
390
|
+
const fields = pen.type === 'ref' ? instanceFields(pen, component) : pen
|
|
391
|
+
const iconInstance = fields.type === 'icon' && insideComponent(build.graph, parentId)
|
|
392
|
+
const leaveTheme = fields.theme ? build.ctx.enterTheme(fields.theme) : undefined
|
|
393
|
+
try {
|
|
394
|
+
const type = instanceOf || iconInstance ? 'INSTANCE' : sceneType(fields, build.ctx)
|
|
395
|
+
const node =
|
|
396
|
+
type === 'STICKY'
|
|
397
|
+
? createAnnotation({ ...fields, id: pen.id }, parentId, build)
|
|
398
|
+
: build.graph.createNode(type, parentId, { id: pen.id })
|
|
399
|
+
applyPenProperties(node, fields, build)
|
|
400
|
+
if (instanceOf) {
|
|
401
|
+
node.componentId = instanceOf.id
|
|
402
|
+
build.instances.set(node.id, pen)
|
|
403
|
+
} else {
|
|
404
|
+
createChildren(node, pen, fields, build)
|
|
405
|
+
}
|
|
406
|
+
return node.id
|
|
407
|
+
} finally {
|
|
408
|
+
leaveTheme?.()
|
|
409
|
+
}
|
|
410
|
+
}
|