@pairdraw/design-jsx 0.20.0 → 0.21.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunks/component-properties.js +6 -6
- package/dist/chunks/component-properties.js.map +1 -1
- package/dist/chunks/elements.js +37 -32
- package/dist/chunks/elements.js.map +1 -1
- package/dist/chunks/export/decoration.js +4 -4
- package/dist/chunks/export/decoration.js.map +1 -1
- package/dist/chunks/export/index.js +20 -20
- package/dist/chunks/export/index.js.map +1 -1
- package/dist/chunks/fields.js +27 -27
- package/dist/chunks/fields.js.map +1 -1
- package/dist/chunks/index.js +1 -1
- package/dist/chunks/index.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/export/decoration.ts +4 -3
- package/src/export/helpers.ts +8 -6
- package/src/export/index.ts +2 -2
- package/src/export/paint.ts +45 -24
- package/src/export/props.ts +9 -9
- package/src/export/text.ts +14 -8
- package/src/export/variables.ts +1 -1
- package/src/overrides/effects.ts +7 -3
- package/src/overrides/fills.ts +6 -3
- package/src/overrides/strokes.ts +9 -5
- package/src/overrides/text.ts +4 -3
- package/src/props-overrides.ts +37 -19
- package/src/reference/authoring.md +1 -1
- package/src/renderer.ts +20 -7
- package/src/shapes-with-text.ts +2 -2
- package/src/variables/graph.ts +1 -1
package/src/export/paint.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type {
|
|
2
|
+
ColorProfile,
|
|
2
3
|
Effect,
|
|
3
4
|
Fill,
|
|
4
5
|
GradientTransform,
|
|
@@ -42,7 +43,7 @@ function options(entries: Options): Options | undefined {
|
|
|
42
43
|
return Object.values(entries).some((value) => value !== undefined) ? entries : undefined
|
|
43
44
|
}
|
|
44
45
|
|
|
45
|
-
function fillValue(fill: Fill, node: SceneNode): JSXValue {
|
|
46
|
+
function fillValue(fill: Fill, node: SceneNode, profile: ColorProfile): JSXValue {
|
|
46
47
|
const visible = fill.visible ? undefined : false
|
|
47
48
|
const shader = shaderOfPaint(node, fill)
|
|
48
49
|
if (shader && isShaderPresetPaint(shader)) {
|
|
@@ -73,7 +74,7 @@ function fillValue(fill: Fill, node: SceneNode): JSXValue {
|
|
|
73
74
|
)
|
|
74
75
|
}
|
|
75
76
|
if (fill.type === 'SOLID') {
|
|
76
|
-
const color = formatColor(fill.color, fill.color.a)
|
|
77
|
+
const color = formatColor(fill.color, fill.color.a, profile)
|
|
77
78
|
const opacity = fill.opacity === fill.color.a ? undefined : fill.opacity
|
|
78
79
|
return helperCall(
|
|
79
80
|
'solid',
|
|
@@ -84,7 +85,7 @@ function fillValue(fill: Fill, node: SceneNode): JSXValue {
|
|
|
84
85
|
const helper = GRADIENT_HELPERS[fill.type]
|
|
85
86
|
if (helper && fill.gradientStops) {
|
|
86
87
|
const stops = fill.gradientStops.map((stop) => [
|
|
87
|
-
formatColor(stop.color, stop.color.a),
|
|
88
|
+
formatColor(stop.color, stop.color.a, profile),
|
|
88
89
|
stop.position
|
|
89
90
|
])
|
|
90
91
|
return helperCall(
|
|
@@ -109,29 +110,37 @@ function fillValue(fill: Fill, node: SceneNode): JSXValue {
|
|
|
109
110
|
}
|
|
110
111
|
|
|
111
112
|
/** A single visible solid paint fits the `bg` or text `color` shorthand. */
|
|
112
|
-
function solidShorthand(fills: Fill[]): string | null {
|
|
113
|
+
function solidShorthand(fills: Fill[], profile: ColorProfile): string | null {
|
|
113
114
|
if (fills.length !== 1) return null
|
|
114
115
|
const [fill] = fills
|
|
115
116
|
if (fill.type !== 'SOLID' || !fill.visible || blendMode(fill.blendMode)) return null
|
|
116
117
|
// The shorthand has one alpha; a fill whose opacity differs from its colour's needs `solid`.
|
|
117
118
|
if (fill.opacity !== fill.color.a) return null
|
|
118
|
-
return formatColor(fill.color, fill.color.a)
|
|
119
|
+
return formatColor(fill.color, fill.color.a, profile)
|
|
119
120
|
}
|
|
120
121
|
|
|
121
122
|
/** One paint as a colour prop takes it: a colour string when it is plain, its helper otherwise. */
|
|
122
|
-
export function paintPropValue(
|
|
123
|
-
|
|
123
|
+
export function paintPropValue(
|
|
124
|
+
fill: Fill,
|
|
125
|
+
node: SceneNode,
|
|
126
|
+
profile: ColorProfile = 'srgb'
|
|
127
|
+
): JSXValue {
|
|
128
|
+
return solidShorthand([fill], profile) ?? fillValue(fill, node, profile)
|
|
124
129
|
}
|
|
125
130
|
|
|
126
|
-
export function collectFillProps(
|
|
131
|
+
export function collectFillProps(
|
|
132
|
+
node: SceneNode,
|
|
133
|
+
props: JSXProp[],
|
|
134
|
+
profile: ColorProfile = 'srgb'
|
|
135
|
+
): void {
|
|
127
136
|
if (node.fills.length === 0) {
|
|
128
137
|
// A text is painted black unless it says it is not painted.
|
|
129
138
|
if (node.type === 'TEXT') props.push(['fills', []])
|
|
130
139
|
return
|
|
131
140
|
}
|
|
132
|
-
const shorthand = solidShorthand(node.fills)
|
|
141
|
+
const shorthand = solidShorthand(node.fills, profile)
|
|
133
142
|
if (shorthand) props.push([node.type === 'TEXT' ? 'color' : 'bg', shorthand])
|
|
134
|
-
else props.push(['fills', node.fills.map((fill) => fillValue(fill, node))])
|
|
143
|
+
else props.push(['fills', node.fills.map((fill) => fillValue(fill, node, profile))])
|
|
135
144
|
}
|
|
136
145
|
|
|
137
146
|
interface StrokeValue {
|
|
@@ -147,9 +156,9 @@ interface StrokeValue {
|
|
|
147
156
|
visible?: false
|
|
148
157
|
}
|
|
149
158
|
|
|
150
|
-
function strokeValue(stroke: Stroke): StrokeValue {
|
|
159
|
+
function strokeValue(stroke: Stroke, profile: ColorProfile): StrokeValue {
|
|
151
160
|
return {
|
|
152
|
-
color: formatColor(stroke.color, stroke.color.a),
|
|
161
|
+
color: formatColor(stroke.color, stroke.color.a, profile),
|
|
153
162
|
opacity: stroke.opacity === stroke.color.a ? undefined : stroke.opacity,
|
|
154
163
|
weight: stroke.weight === 1 ? undefined : stroke.weight,
|
|
155
164
|
align: stroke.align === 'INSIDE' ? undefined : STROKE_ALIGN.print(stroke.align),
|
|
@@ -182,8 +191,12 @@ function fitsStrokeShorthand(node: SceneNode, stroke: StrokeValue): boolean {
|
|
|
182
191
|
return (stroke.cap ?? nodeCap) === nodeCap && (stroke.join ?? nodeJoin) === nodeJoin
|
|
183
192
|
}
|
|
184
193
|
|
|
185
|
-
export function collectStrokeProps(
|
|
186
|
-
|
|
194
|
+
export function collectStrokeProps(
|
|
195
|
+
node: SceneNode,
|
|
196
|
+
props: JSXProp[],
|
|
197
|
+
profile: ColorProfile = 'srgb'
|
|
198
|
+
): void {
|
|
199
|
+
const strokes = node.strokes.map((stroke) => strokeValue(stroke, profile))
|
|
187
200
|
const [only] = strokes
|
|
188
201
|
const shorthand = strokes.length === 1 && fitsStrokeShorthand(node, only)
|
|
189
202
|
if (shorthand) {
|
|
@@ -231,7 +244,7 @@ function noiseSizeValue(size: Vector): JSXValue {
|
|
|
231
244
|
return size.x === size.y ? size.x : { x: size.x, y: size.y }
|
|
232
245
|
}
|
|
233
246
|
|
|
234
|
-
function effectValue(effect: Exclude<Effect, ShaderEffect
|
|
247
|
+
function effectValue(effect: Exclude<Effect, ShaderEffect>, profile: ColorProfile): HelperCall {
|
|
235
248
|
const visible = effect.visible ? undefined : false
|
|
236
249
|
if (effect.type === 'DROP_SHADOW' || effect.type === 'INNER_SHADOW') {
|
|
237
250
|
return helperCall(
|
|
@@ -241,7 +254,7 @@ function effectValue(effect: Exclude<Effect, ShaderEffect>): HelperCall {
|
|
|
241
254
|
y: effect.offset.y,
|
|
242
255
|
radius: effect.radius,
|
|
243
256
|
spread: effect.spread === 0 ? undefined : effect.spread,
|
|
244
|
-
color: formatColor(effect.color, effect.color.a),
|
|
257
|
+
color: formatColor(effect.color, effect.color.a, profile),
|
|
245
258
|
visible,
|
|
246
259
|
blendMode: blendMode(effect.blendMode),
|
|
247
260
|
showShadowBehindNode: effect.showShadowBehindNode
|
|
@@ -256,9 +269,9 @@ function effectValue(effect: Exclude<Effect, ShaderEffect>): HelperCall {
|
|
|
256
269
|
noiseType: grain.noiseType,
|
|
257
270
|
noiseSize: noiseSizeValue(grain.noiseSize),
|
|
258
271
|
density: grain.density,
|
|
259
|
-
color: formatColor(effect.color, effect.color.a),
|
|
272
|
+
color: formatColor(effect.color, effect.color.a, profile),
|
|
260
273
|
secondaryColor: grain.secondaryColor
|
|
261
|
-
? formatColor(grain.secondaryColor, grain.secondaryColor.a)
|
|
274
|
+
? formatColor(grain.secondaryColor, grain.secondaryColor.a, profile)
|
|
262
275
|
: undefined,
|
|
263
276
|
opacity: grain.opacity,
|
|
264
277
|
seed: grain.seed,
|
|
@@ -285,22 +298,22 @@ function effectValue(effect: Exclude<Effect, ShaderEffect>): HelperCall {
|
|
|
285
298
|
)
|
|
286
299
|
}
|
|
287
300
|
|
|
288
|
-
function plainDropShadow(effect: Effect): string | null {
|
|
301
|
+
function plainDropShadow(effect: Effect, profile: ColorProfile): string | null {
|
|
289
302
|
const plain =
|
|
290
303
|
effect.type === 'DROP_SHADOW' &&
|
|
291
304
|
effect.spread === 0 &&
|
|
292
305
|
!blendMode(effect.blendMode) &&
|
|
293
306
|
effect.showShadowBehindNode === undefined
|
|
294
|
-
return plain ? formatShadow(effect) : null
|
|
307
|
+
return plain ? formatShadow(effect, profile) : null
|
|
295
308
|
}
|
|
296
309
|
|
|
297
310
|
/**
|
|
298
311
|
* `shadow` and `blur` describe a plain drop shadow followed by a layer blur, each optional,
|
|
299
312
|
* in the order the renderer appends them; anything else needs `effects`.
|
|
300
313
|
*/
|
|
301
|
-
function effectShorthand(effects: Effect[]): JSXProp[] | null {
|
|
314
|
+
function effectShorthand(effects: Effect[], profile: ColorProfile): JSXProp[] | null {
|
|
302
315
|
if (effects.some((effect) => !effect.visible)) return null
|
|
303
|
-
const shadow = effects.length > 0 ? plainDropShadow(effects[0]) : null
|
|
316
|
+
const shadow = effects.length > 0 ? plainDropShadow(effects[0], profile) : null
|
|
304
317
|
const rest = shadow ? effects.slice(1) : effects
|
|
305
318
|
if (rest.length > 1 || rest.some((effect) => effect.type !== 'LAYER_BLUR')) return null
|
|
306
319
|
const props: JSXProp[] = []
|
|
@@ -316,9 +329,17 @@ function hasJSXHelper(effect: Effect): effect is Exclude<Effect, ShaderEffect> {
|
|
|
316
329
|
return effect.type !== 'SHADER' && Object.hasOwn(EFFECT_HELPERS, effect.type)
|
|
317
330
|
}
|
|
318
331
|
|
|
319
|
-
export function collectEffectProps(
|
|
332
|
+
export function collectEffectProps(
|
|
333
|
+
node: SceneNode,
|
|
334
|
+
props: JSXProp[],
|
|
335
|
+
profile: ColorProfile = 'srgb'
|
|
336
|
+
): void {
|
|
320
337
|
// An effect without a helper cannot be written as JSX; leaving it out keeps the source importable.
|
|
321
338
|
const effects = node.effects.filter(hasJSXHelper)
|
|
322
339
|
if (effects.length === 0) return
|
|
323
|
-
props.push(
|
|
340
|
+
props.push(
|
|
341
|
+
...(effectShorthand(effects, profile) ?? [
|
|
342
|
+
['effects', effects.map((effect) => effectValue(effect, profile))]
|
|
343
|
+
])
|
|
344
|
+
)
|
|
324
345
|
}
|
package/src/export/props.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isEqual, omitBy, pick } from 'es-toolkit'
|
|
2
2
|
|
|
3
|
-
import type { ArcData, SceneGraph, SceneNode } from '@pairdraw/scene-graph'
|
|
3
|
+
import type { ArcData, ColorProfile, SceneGraph, SceneNode } from '@pairdraw/scene-graph'
|
|
4
4
|
import { DEFAULT_FONT_FAMILY } from '@pairdraw/scene-graph/constants'
|
|
5
5
|
import { layoutSizing } from '@pairdraw/scene-graph/layout'
|
|
6
6
|
import { createDefaultNode } from '@pairdraw/scene-graph/node-defaults'
|
|
@@ -118,9 +118,9 @@ function collectCornerRadiiProps(node: SceneNode, props: JSXProp[]): void {
|
|
|
118
118
|
}
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
function collectAppearanceProps(node: SceneNode, props: JSXProp[]): void {
|
|
122
|
-
collectFillProps(node, props)
|
|
123
|
-
collectStrokeProps(node, props)
|
|
121
|
+
function collectAppearanceProps(node: SceneNode, props: JSXProp[], profile: ColorProfile): void {
|
|
122
|
+
collectFillProps(node, props, profile)
|
|
123
|
+
collectStrokeProps(node, props, profile)
|
|
124
124
|
collectCornerRadiiProps(node, props)
|
|
125
125
|
|
|
126
126
|
if (node.cornerSmoothing > 0) props.push(['cornerSmoothing', node.cornerSmoothing])
|
|
@@ -132,7 +132,7 @@ function collectAppearanceProps(node: SceneNode, props: JSXProp[]): void {
|
|
|
132
132
|
props.push(['blendMode', BLEND_MODE.print(node.blendMode)])
|
|
133
133
|
}
|
|
134
134
|
if (node.clipsContent) props.push(['overflow', 'hidden'])
|
|
135
|
-
collectEffectProps(node, props)
|
|
135
|
+
collectEffectProps(node, props, profile)
|
|
136
136
|
}
|
|
137
137
|
|
|
138
138
|
function collectPositionProps(
|
|
@@ -257,7 +257,7 @@ export function fontWeightValue(weight: number): string | number {
|
|
|
257
257
|
return weight
|
|
258
258
|
}
|
|
259
259
|
|
|
260
|
-
function collectTextNodeProps(node: SceneNode, props: JSXProp[]): void {
|
|
260
|
+
function collectTextNodeProps(node: SceneNode, props: JSXProp[], profile: ColorProfile): void {
|
|
261
261
|
const direction = resolveNodeTextDirection(node)
|
|
262
262
|
if (node.fontSize !== 14) props.push(['size', node.fontSize])
|
|
263
263
|
if (node.fontFamily && node.fontFamily !== DEFAULT_FONT_FAMILY)
|
|
@@ -277,7 +277,7 @@ function collectTextNodeProps(node: SceneNode, props: JSXProp[]): void {
|
|
|
277
277
|
}
|
|
278
278
|
if (node.textDecoration !== 'NONE')
|
|
279
279
|
props.push(['textDecoration', TEXT_DECORATION.print(node.textDecoration)])
|
|
280
|
-
collectDecorationProps(ownTextFields(node, DECORATION_FIELDS), node, props)
|
|
280
|
+
collectDecorationProps(ownTextFields(node, DECORATION_FIELDS), node, props, profile)
|
|
281
281
|
collectTextFontSettingProps(node, props)
|
|
282
282
|
if (node.textCase !== 'ORIGINAL') props.push(['textCase', TEXT_CASE.print(node.textCase)])
|
|
283
283
|
if (node.maxLines != null) props.push(['maxLines', node.maxLines])
|
|
@@ -357,9 +357,9 @@ export function collectProps(
|
|
|
357
357
|
if (ctx.parentIsGrid) collectGridPositionProps(node, props)
|
|
358
358
|
if (ctx.isFlex) collectFlexAlignmentProps(node, props)
|
|
359
359
|
if (ctx.isAutoLayout) collectAutoLayoutPaddingProps(node, props)
|
|
360
|
-
collectAppearanceProps(node, props)
|
|
360
|
+
collectAppearanceProps(node, props, graph.documentColorSpace)
|
|
361
361
|
if (node.type === 'TEXT') {
|
|
362
|
-
collectTextNodeProps(node, props)
|
|
362
|
+
collectTextNodeProps(node, props, graph.documentColorSpace)
|
|
363
363
|
collectTextAutoResizeProp(node, ctx, props)
|
|
364
364
|
}
|
|
365
365
|
collectShapeNodeProps(node, props)
|
package/src/export/text.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { isEmptyObject, isEqual, omitBy, pick } from 'es-toolkit'
|
|
|
2
2
|
|
|
3
3
|
import { jsx, type SyntaxNode } from '@pairdraw/emit'
|
|
4
4
|
import type {
|
|
5
|
+
ColorProfile,
|
|
5
6
|
SceneNode,
|
|
6
7
|
TextParagraphSpacingField,
|
|
7
8
|
TextParagraphStyle
|
|
@@ -49,13 +50,13 @@ function characterStyles(node: SceneNode): SpanStyle[] {
|
|
|
49
50
|
)
|
|
50
51
|
}
|
|
51
52
|
|
|
52
|
-
function spanProps(style: SpanStyle, node: SceneNode): JSXProp[] {
|
|
53
|
+
function spanProps(style: SpanStyle, node: SceneNode, profile: ColorProfile): JSXProp[] {
|
|
53
54
|
const props: JSXProp[] = []
|
|
54
55
|
if (style.fontSize !== undefined) props.push(['size', style.fontSize])
|
|
55
56
|
if (style.fontFamily !== undefined) props.push(['font', style.fontFamily])
|
|
56
57
|
if (style.fontWeight !== undefined) props.push(['weight', fontWeightValue(style.fontWeight)])
|
|
57
58
|
if (style.italic !== undefined) props.push(['italic', style.italic])
|
|
58
|
-
if (style.fills) collectFillProps({ ...node, fills: style.fills }, props)
|
|
59
|
+
if (style.fills) collectFillProps({ ...node, fills: style.fills }, props, profile)
|
|
59
60
|
if (style.lineHeight !== undefined) {
|
|
60
61
|
props.push(['lineHeight', style.lineHeight ? textLengthJSX(style.lineHeight) : AUTO])
|
|
61
62
|
}
|
|
@@ -64,7 +65,7 @@ function spanProps(style: SpanStyle, node: SceneNode): JSXProp[] {
|
|
|
64
65
|
}
|
|
65
66
|
if (style.textDecoration !== undefined)
|
|
66
67
|
props.push(['textDecoration', TEXT_DECORATION.print(style.textDecoration)])
|
|
67
|
-
collectDecorationProps(style, node, props)
|
|
68
|
+
collectDecorationProps(style, node, props, profile)
|
|
68
69
|
collectFontSettingProps(style, props)
|
|
69
70
|
if (style.textCase !== undefined) props.push(['textCase', TEXT_CASE.print(style.textCase)])
|
|
70
71
|
if (style.hyperlink) props.push(['href', style.hyperlink.value])
|
|
@@ -76,7 +77,8 @@ function inlineContent(
|
|
|
76
77
|
node: SceneNode,
|
|
77
78
|
styles: readonly SpanStyle[],
|
|
78
79
|
start: number,
|
|
79
|
-
end: number
|
|
80
|
+
end: number,
|
|
81
|
+
profile: ColorProfile
|
|
80
82
|
): SyntaxNode[] {
|
|
81
83
|
const content: SyntaxNode[] = []
|
|
82
84
|
let from = start
|
|
@@ -87,7 +89,7 @@ function inlineContent(
|
|
|
87
89
|
content.push(
|
|
88
90
|
isEmptyObject(style)
|
|
89
91
|
? text
|
|
90
|
-
: jsx.element('Span', spanProps(style, node).map(propAttribute), [text], 0, true)
|
|
92
|
+
: jsx.element('Span', spanProps(style, node, profile).map(propAttribute), [text], 0, true)
|
|
91
93
|
)
|
|
92
94
|
from = index
|
|
93
95
|
}
|
|
@@ -129,7 +131,11 @@ function paragraphProps(style: TextParagraphStyle): JSXProp[] {
|
|
|
129
131
|
* `<Span>`s for characters styled apart, and as `<Paragraph>`s when a paragraph is a list item
|
|
130
132
|
* or has spacing other than the text's.
|
|
131
133
|
*/
|
|
132
|
-
export function textChildren(
|
|
134
|
+
export function textChildren(
|
|
135
|
+
node: SceneNode,
|
|
136
|
+
depth: number,
|
|
137
|
+
profile: ColorProfile = 'srgb'
|
|
138
|
+
): TextChildren {
|
|
133
139
|
const styles = characterStyles(node)
|
|
134
140
|
const ranges = textParagraphRanges(node.text)
|
|
135
141
|
const paragraphs = ranges.map((_, index) => ownParagraphStyle(node, index))
|
|
@@ -140,13 +146,13 @@ export function textChildren(node: SceneNode, depth: number): TextChildren {
|
|
|
140
146
|
if (styles.every(isEmptyObject)) {
|
|
141
147
|
return { children: node.text ? [jsx.text(node.text)] : [], inline: true }
|
|
142
148
|
}
|
|
143
|
-
return { children: inlineContent(node, styles, 0, node.text.length), inline: true }
|
|
149
|
+
return { children: inlineContent(node, styles, 0, node.text.length, profile), inline: true }
|
|
144
150
|
}
|
|
145
151
|
const children = ranges.map(({ start, end }, index) =>
|
|
146
152
|
jsx.element(
|
|
147
153
|
'Paragraph',
|
|
148
154
|
paragraphProps(paragraphs[index]).map(propAttribute),
|
|
149
|
-
inlineContent(node, styles, start, end),
|
|
155
|
+
inlineContent(node, styles, start, end, profile),
|
|
150
156
|
depth + 1,
|
|
151
157
|
true
|
|
152
158
|
)
|
package/src/export/variables.ts
CHANGED
|
@@ -327,7 +327,7 @@ function definitionExport(graph: SceneGraph, rootIds: readonly string[]): Variab
|
|
|
327
327
|
if (typeof raw === 'number' || typeof raw === 'boolean') return raw
|
|
328
328
|
if (typeof raw !== 'object') return undefined
|
|
329
329
|
if ('aliasId' in raw) return variable(raw.aliasId)
|
|
330
|
-
return 'r' in raw ? formatColor(raw, raw.a) : undefined
|
|
330
|
+
return 'r' in raw ? formatColor(raw, raw.a, graph.documentColorSpace) : undefined
|
|
331
331
|
}
|
|
332
332
|
const values = (variables: Variable[], modeId: string, base?: string) =>
|
|
333
333
|
Object.fromEntries(
|
package/src/overrides/effects.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as v from 'valibot'
|
|
2
2
|
|
|
3
|
-
import type { SceneNode } from '@pairdraw/scene-graph'
|
|
3
|
+
import type { DocumentColorSpace, SceneNode } from '@pairdraw/scene-graph'
|
|
4
4
|
import { colorSchema } from '@pairdraw/scene-graph/color'
|
|
5
5
|
import { TRANSPARENT } from '@pairdraw/scene-graph/constants'
|
|
6
6
|
import { parseCSSShadows } from '@pairdraw/scene-graph/css'
|
|
@@ -102,14 +102,18 @@ function forNormalize(effect: ParsedEffect): NormalizableEffect {
|
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
/** `effects` takes structured effects; `shadow` appends its shadows and `blur` a layer blur. */
|
|
105
|
-
export function applyEffectOverrides(
|
|
105
|
+
export function applyEffectOverrides(
|
|
106
|
+
props: Record<string, unknown>,
|
|
107
|
+
o: Partial<SceneNode>,
|
|
108
|
+
profile: DocumentColorSpace = 'srgb'
|
|
109
|
+
): void {
|
|
106
110
|
if (Array.isArray(props.effects) && props.effects.length > 0) {
|
|
107
111
|
const effects = parseScriptInput('effects', effectsSchema, props.effects).map(forNormalize)
|
|
108
112
|
o.effects = normalizeEffects(effects)
|
|
109
113
|
}
|
|
110
114
|
|
|
111
115
|
// A CSS shadow list, as `box-shadow` takes it.
|
|
112
|
-
const shadows = typeof props.shadow === 'string' ? parseCSSShadows(props.shadow) : []
|
|
116
|
+
const shadows = typeof props.shadow === 'string' ? parseCSSShadows(props.shadow, profile) : []
|
|
113
117
|
if (shadows.length > 0) o.effects = [...(o.effects ?? []), ...shadows]
|
|
114
118
|
|
|
115
119
|
if (typeof props.blur === 'number') {
|
package/src/overrides/fills.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as v from 'valibot'
|
|
2
2
|
|
|
3
|
-
import type { Fill, GradientTransform } from '@pairdraw/scene-graph'
|
|
3
|
+
import type { DocumentColorSpace, Fill, GradientTransform } from '@pairdraw/scene-graph'
|
|
4
4
|
import { colorToFill } from '@pairdraw/scene-graph/color'
|
|
5
5
|
import { paintTransformSchema } from '@pairdraw/scene-graph/paints'
|
|
6
6
|
import type { Color } from '@pairdraw/scene-graph/primitives'
|
|
@@ -45,8 +45,11 @@ function parseTransform(
|
|
|
45
45
|
)
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
export function fillFromValue(
|
|
49
|
-
|
|
48
|
+
export function fillFromValue(
|
|
49
|
+
value: string | Color | Fill,
|
|
50
|
+
profile: DocumentColorSpace = 'srgb'
|
|
51
|
+
): Fill {
|
|
52
|
+
if (!isFill(value)) return colorToFill(value, profile)
|
|
50
53
|
const fill = structuredClone(value)
|
|
51
54
|
// A paint written as an object, as export writes an image, gives its transforms as Figma's rows.
|
|
52
55
|
for (const field of TRANSFORM_FIELDS) {
|
package/src/overrides/strokes.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { designJSXProp } from '#design-jsx/definitions'
|
|
2
2
|
import { STROKE_ALIGN, STROKE_CAP, STROKE_JOIN } from '#design-jsx/enums'
|
|
3
3
|
|
|
4
|
-
import type { SceneNode, Stroke } from '@pairdraw/scene-graph'
|
|
4
|
+
import type { DocumentColorSpace, SceneNode, Stroke } from '@pairdraw/scene-graph'
|
|
5
5
|
import { parseColor } from '@pairdraw/scene-graph/color'
|
|
6
6
|
import type { Color } from '@pairdraw/scene-graph/primitives'
|
|
7
7
|
|
|
@@ -35,8 +35,8 @@ function dashValue(value: unknown): number[] | undefined {
|
|
|
35
35
|
return dash.length > 0 ? dash : undefined
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
function toStroke(value: StrokeValue): Stroke {
|
|
39
|
-
const color = typeof value.color === 'string' ? parseColor(value.color) : value.color
|
|
38
|
+
function toStroke(value: StrokeValue, profile: DocumentColorSpace): Stroke {
|
|
39
|
+
const color = typeof value.color === 'string' ? parseColor(value.color, profile) : value.color
|
|
40
40
|
const stroke: Stroke = {
|
|
41
41
|
type: 'SOLID',
|
|
42
42
|
color,
|
|
@@ -80,7 +80,11 @@ function applyStrokeWeights(value: unknown, o: Partial<SceneNode>): void {
|
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
/** `strokes` takes structured strokes; `stroke` and its sibling props describe a single one. */
|
|
83
|
-
export function applyStrokeOverrides(
|
|
83
|
+
export function applyStrokeOverrides(
|
|
84
|
+
props: LayerProps,
|
|
85
|
+
o: Partial<SceneNode>,
|
|
86
|
+
profile: DocumentColorSpace = 'srgb'
|
|
87
|
+
): void {
|
|
84
88
|
applyStrokeWeights(props.strokeWeights, o)
|
|
85
89
|
// Node-level cap and join, which strokes without their own use.
|
|
86
90
|
const nodeCap = STROKE_CAP.parse(props.strokeCap)
|
|
@@ -91,7 +95,7 @@ export function applyStrokeOverrides(props: LayerProps, o: Partial<SceneNode>):
|
|
|
91
95
|
const nodeDash = dashValue(props.dashPattern)
|
|
92
96
|
if (nodeDash) o.dashPattern = nodeDash
|
|
93
97
|
const strokes = strokeValues(props).map((value) =>
|
|
94
|
-
toStroke({ ...value, dash: value.dash ?? nodeDash })
|
|
98
|
+
toStroke({ ...value, dash: value.dash ?? nodeDash }, profile)
|
|
95
99
|
)
|
|
96
100
|
if (strokes.length > 0) o.strokes = strokes
|
|
97
101
|
}
|
package/src/overrides/text.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { uniqBy } from 'es-toolkit'
|
|
2
2
|
import * as v from 'valibot'
|
|
3
3
|
|
|
4
|
-
import type { Fill, SceneNode, TextLength } from '@pairdraw/scene-graph'
|
|
4
|
+
import type { DocumentColorSpace, Fill, SceneNode, TextLength } from '@pairdraw/scene-graph'
|
|
5
5
|
import type { Color } from '@pairdraw/scene-graph/primitives'
|
|
6
6
|
import { percentLength, pixelLength } from '@pairdraw/scene-graph/text'
|
|
7
7
|
|
|
@@ -75,7 +75,8 @@ function autoLength(value: number | typeof AUTO): number | null {
|
|
|
75
75
|
*/
|
|
76
76
|
export function applyTextDetailOverrides(
|
|
77
77
|
props: Record<string, unknown>,
|
|
78
|
-
o: Partial<SceneNode
|
|
78
|
+
o: Partial<SceneNode>,
|
|
79
|
+
profile: DocumentColorSpace
|
|
79
80
|
): void {
|
|
80
81
|
const parsed = parseScriptInput('Invalid <Text> props', textPropsSchema, props)
|
|
81
82
|
const color = parsed.textDecorationColor
|
|
@@ -85,7 +86,7 @@ export function applyTextDetailOverrides(
|
|
|
85
86
|
o.textDecorationFills =
|
|
86
87
|
typeof color === 'string' && color.toLowerCase() === CURRENT_COLOR.toLowerCase()
|
|
87
88
|
? []
|
|
88
|
-
: [fillFromValue(color)]
|
|
89
|
+
: [fillFromValue(color, profile)]
|
|
89
90
|
}
|
|
90
91
|
if (parsed.textDecorationThickness !== undefined)
|
|
91
92
|
o.textDecorationThickness = autoLength(parsed.textDecorationThickness)
|
package/src/props-overrides.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Fill, SceneNode, ArcData, NodeType } from '@pairdraw/scene-graph'
|
|
1
|
+
import type { DocumentColorSpace, Fill, SceneNode, ArcData, NodeType } from '@pairdraw/scene-graph'
|
|
2
2
|
import { colorToFill, tryParseColor } from '@pairdraw/scene-graph/color'
|
|
3
3
|
import { parseCSSGridTracks, parseCSSNumber } from '@pairdraw/scene-graph/css'
|
|
4
4
|
import {
|
|
@@ -95,14 +95,18 @@ export function applySizeOverrides(
|
|
|
95
95
|
* Sets the layer's fills, moving each shader paint's preset into the layer's plugin data,
|
|
96
96
|
* where a shader paint keeps it.
|
|
97
97
|
*/
|
|
98
|
-
function setFills(
|
|
98
|
+
function setFills(
|
|
99
|
+
values: (string | Color | Fill)[],
|
|
100
|
+
o: Partial<SceneNode>,
|
|
101
|
+
profile: DocumentColorSpace
|
|
102
|
+
): void {
|
|
99
103
|
const shaders: ShaderPaint[] = []
|
|
100
104
|
// A colour string no parser reads would otherwise become black, opposite to leaving it out.
|
|
101
105
|
const usable = values.filter(
|
|
102
|
-
(value) => typeof value !== 'string' || tryParseColor(value) !== null
|
|
106
|
+
(value) => typeof value !== 'string' || tryParseColor(value, profile) !== null
|
|
103
107
|
)
|
|
104
108
|
const fills = usable.map((value) => {
|
|
105
|
-
const fill = fillFromValue(value)
|
|
109
|
+
const fill = fillFromValue(value, profile)
|
|
106
110
|
if (!isShaderFill(fill)) return fill
|
|
107
111
|
const { shader, ...paint } = fill
|
|
108
112
|
if (paint.imageHash) shaders.push(shader)
|
|
@@ -114,19 +118,23 @@ function setFills(values: (string | Color | Fill)[], o: Partial<SceneNode>): voi
|
|
|
114
118
|
o.pluginData = withShaderPaints({ pluginData: o.pluginData ?? [] }, fills, shaders)
|
|
115
119
|
}
|
|
116
120
|
|
|
117
|
-
function applyFillOverride(
|
|
121
|
+
function applyFillOverride(
|
|
122
|
+
props: Record<string, unknown>,
|
|
123
|
+
o: Partial<SceneNode>,
|
|
124
|
+
profile: DocumentColorSpace
|
|
125
|
+
): void {
|
|
118
126
|
// `fills={[]}` paints nothing, which a text, painted black by default, has to say.
|
|
119
127
|
if (Array.isArray(props.fills) && props.fills.length === 0) {
|
|
120
128
|
o.fills = []
|
|
121
129
|
return
|
|
122
130
|
}
|
|
123
131
|
if (Array.isArray(props.fills)) {
|
|
124
|
-
setFills(props.fills.filter(isFillValue), o)
|
|
132
|
+
setFills(props.fills.filter(isFillValue), o, profile)
|
|
125
133
|
return
|
|
126
134
|
}
|
|
127
135
|
|
|
128
136
|
const bg = designJSXProp(props, 'bg')
|
|
129
|
-
if (isFillValue(bg)) setFills([bg], o)
|
|
137
|
+
if (isFillValue(bg)) setFills([bg], o, profile)
|
|
130
138
|
}
|
|
131
139
|
|
|
132
140
|
function applyCornerOverrides(props: LayerProps, o: Partial<SceneNode>): void {
|
|
@@ -149,9 +157,13 @@ function applyCornerOverrides(props: LayerProps, o: Partial<SceneNode>): void {
|
|
|
149
157
|
if (props.cornerSmoothing !== undefined) o.cornerSmoothing = props.cornerSmoothing
|
|
150
158
|
}
|
|
151
159
|
|
|
152
|
-
function applyVisualOverrides(
|
|
153
|
-
|
|
154
|
-
|
|
160
|
+
function applyVisualOverrides(
|
|
161
|
+
props: LayerProps,
|
|
162
|
+
o: Partial<SceneNode>,
|
|
163
|
+
profile: DocumentColorSpace
|
|
164
|
+
): void {
|
|
165
|
+
applyFillOverride(props, o, profile)
|
|
166
|
+
applyStrokeOverrides(props, o, profile)
|
|
155
167
|
applyCornerOverrides(props, o)
|
|
156
168
|
|
|
157
169
|
if (props.opacity !== undefined) o.opacity = props.opacity
|
|
@@ -337,7 +349,11 @@ function applyLayoutOverrides(
|
|
|
337
349
|
if (props.grow !== undefined) o.layoutGrow = props.grow as number
|
|
338
350
|
}
|
|
339
351
|
|
|
340
|
-
function applyTextStyleOverrides(
|
|
352
|
+
function applyTextStyleOverrides(
|
|
353
|
+
props: LayerProps,
|
|
354
|
+
o: Partial<SceneNode>,
|
|
355
|
+
profile: DocumentColorSpace
|
|
356
|
+
): void {
|
|
341
357
|
const fontSize = designJSXProp(props, 'size')
|
|
342
358
|
if (typeof fontSize === 'number') o.fontSize = fontSize
|
|
343
359
|
|
|
@@ -354,7 +370,7 @@ function applyTextStyleOverrides(props: LayerProps, o: Partial<SceneNode>): void
|
|
|
354
370
|
}
|
|
355
371
|
|
|
356
372
|
if (typeof props.color === 'string' || isColor(props.color)) {
|
|
357
|
-
o.fills = [colorToFill(props.color)]
|
|
373
|
+
o.fills = [colorToFill(props.color, profile)]
|
|
358
374
|
}
|
|
359
375
|
|
|
360
376
|
// A value it cannot read is left out, so the text keeps its own.
|
|
@@ -364,7 +380,7 @@ function applyTextStyleOverrides(props: LayerProps, o: Partial<SceneNode>): void
|
|
|
364
380
|
if (letterSpacing) o.letterSpacing = letterSpacing
|
|
365
381
|
const textDecoration = TEXT_DECORATION.parse(props.textDecoration)
|
|
366
382
|
if (textDecoration) o.textDecoration = textDecoration
|
|
367
|
-
applyTextDetailOverrides(props, o)
|
|
383
|
+
applyTextDetailOverrides(props, o, profile)
|
|
368
384
|
const textCase = TEXT_CASE.parse(props.textCase)
|
|
369
385
|
if (textCase) o.textCase = textCase
|
|
370
386
|
if (props.maxLines !== undefined) {
|
|
@@ -417,9 +433,10 @@ function applyTextAutoResize(
|
|
|
417
433
|
function applyTextOverrides(
|
|
418
434
|
props: LayerProps,
|
|
419
435
|
o: Partial<SceneNode>,
|
|
420
|
-
parentLayout: SceneNode['layoutMode']
|
|
436
|
+
parentLayout: SceneNode['layoutMode'],
|
|
437
|
+
profile: DocumentColorSpace
|
|
421
438
|
): void {
|
|
422
|
-
applyTextStyleOverrides(props, o)
|
|
439
|
+
applyTextStyleOverrides(props, o, profile)
|
|
423
440
|
// Without `dir` the node keeps the style's own direction, which is auto by default; writing an
|
|
424
441
|
// undefined override would erase it and stop Arabic text from being detected as RTL.
|
|
425
442
|
const direction = parseDirection(props.dir)
|
|
@@ -474,7 +491,8 @@ export function shapeOverrides(
|
|
|
474
491
|
export function propsToOverrides(
|
|
475
492
|
props: Record<string, unknown>,
|
|
476
493
|
isText: boolean,
|
|
477
|
-
parentLayout: SceneNode['layoutMode']
|
|
494
|
+
parentLayout: SceneNode['layoutMode'],
|
|
495
|
+
profile: DocumentColorSpace = 'srgb'
|
|
478
496
|
): Partial<SceneNode> {
|
|
479
497
|
// Layer props are read once, after `style` gives the ones the props leave unset.
|
|
480
498
|
const layer = readLayerProps(normalizeStyleProps(props))
|
|
@@ -483,10 +501,10 @@ export function propsToOverrides(
|
|
|
483
501
|
if (layer.name) o.name = layer.name
|
|
484
502
|
|
|
485
503
|
const { w, h } = applySizeOverrides(layer, o, parentLayout)
|
|
486
|
-
applyVisualOverrides(layer, o)
|
|
504
|
+
applyVisualOverrides(layer, o, profile)
|
|
487
505
|
applyLayoutOverrides(layer, o, w, h, isText, parentLayout)
|
|
488
|
-
if (isText) applyTextOverrides(layer, o, parentLayout)
|
|
489
|
-
applyEffectOverrides(layer, o)
|
|
506
|
+
if (isText) applyTextOverrides(layer, o, parentLayout, profile)
|
|
507
|
+
applyEffectOverrides(layer, o, profile)
|
|
490
508
|
applyStateOverrides(layer, o)
|
|
491
509
|
|
|
492
510
|
return o
|
|
@@ -18,7 +18,7 @@ This reference describes scene creation, not React DOM output. Use the `render`
|
|
|
18
18
|
## Paint, text, and artwork
|
|
19
19
|
|
|
20
20
|
- `bg` / `fill`, `stroke`, and text `color` accept colors and supported variable references. Set colors explicitly for predictable contrast. `fills` accepts structured paints; gradient helpers include `linearGradient`, `radialGradient`, `angularGradient`, and `diamondGradient`, each taking an array of stops and optional `{ opacity, transform }`: `fills={[linearGradient([['#3b82f6', 0], ['#8b5cf6', 1]])]}`. Without a `transform` a linear gradient runs left to right and the others are centred; `transform` is Figma's `gradientTransform`, mapping the layer's unit square onto gradient space. Write it as the plugin API's rows, as export does: `linearGradient(stops, { transform: [[0, 1, 0], [-1, 0, 1]] })` runs top to bottom. A paint written as an object takes its `gradientTransform` and `imageTransform` as rows too; the matrix fields `{ m00, m01, m02, m10, m11, m12 }` are accepted as well.
|
|
21
|
-
- `shader(effects, { opacity, visible, blendMode })` fills a layer with a shader from the [`shaders`](https://shaders.com) library: an array of effects such as `bg={shader([{ type: 'Aurora', props: { speed: 2 } }])}`, or a preset as shaders.com exports it, `{ components: [...] }`. The canvas shows the shader's first frame, preview plays
|
|
21
|
+
- `shader(effects, { opacity, visible, blendMode })` fills a layer with a shader from the [`shaders`](https://shaders.com) library: an array of effects such as `bg={shader([{ type: 'Aurora', props: { speed: 2 } }])}`, or a preset as shaders.com exports it, `{ components: [...] }`. A gradient effect takes `stops` as `{ color, position }` pairs, and an effect that draws on a shape takes `shape` as JSON such as `'{"type":"sphere3D","radius":0.35}'`. The app's gallery starts one from 21 complete presets, including `liquidChrome`, `flutedGlass`, `paperStock`, `godRays`, `thermal`, `voronoiCells`, `deepWater`, `smokePlume`, `neonPulse`, `grainWash`, `obsidian`, and `marbleInk`. The canvas shows the shader's first frame, preview plays the shaders on screen, and files and other formats keep that frame as an image.
|
|
22
22
|
- `shaderSource(code, { uniforms, opacity, visible, blendMode })` fills a layer with shader code written here rather than a preset; `uniforms` gives the values its `@directive` inputs start with, such as `bg={shaderSource(code, { uniforms: { u_color: '#ff0000' } })}`.
|
|
23
23
|
- `stroke` describes one stroke, refined by `strokeWidth`, `strokeAlign` (`"inside"`, `"center"`, `"outside"`), `strokeDash`, `strokeCap`, and `strokeJoin`. `strokes` takes several, as objects with `color`, `weight`, `align`, `dash`, `cap`, `join`, and `visible`; `strokeWeights={{ top, right, bottom, left }}` sets per-side widths. `dashPattern` sets the node's own dash pattern, separate from the per-stroke `strokeDash`.
|
|
24
24
|
- `rounded` and `roundedTL`/`roundedTR`/`roundedBL`/`roundedBR` control corners. `strokeWidth`, `opacity`, `rotate`, and `blendMode` control appearance. `overflow="hidden"` clips content; do not hide accidental text overflow to make a broken layout appear correct.
|