@pairdraw/dom-css 0.18.0 → 0.19.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/dist/chunks/behaviours/states/layers.js +333 -38
- package/dist/chunks/behaviours/states/layers.js.map +1 -1
- package/dist/chunks/browser.js +49 -8
- package/dist/chunks/browser.js.map +1 -1
- package/dist/chunks/export/components/groups.js +1 -1
- package/dist/chunks/export/storybook/component.js +47 -17
- package/dist/chunks/export/storybook/component.js.map +1 -1
- package/dist/chunks/export/storybook/export.js +63 -21
- package/dist/chunks/export/storybook/export.js.map +1 -1
- package/dist/chunks/import/html.js.map +1 -1
- package/dist/export.d.ts +7 -3
- package/dist/export.js +2 -2
- package/dist/styling.d.ts +6 -1
- package/package.json +3 -3
- package/src/cssom/types.ts +22 -0
- package/src/export/css.ts +191 -17
- package/src/export/index.ts +1 -0
- package/src/export/position.ts +111 -0
- package/src/export/projection.ts +40 -37
- package/src/export/storybook/component.ts +69 -14
- package/src/export/storybook/export.ts +103 -22
- package/src/export/text.ts +51 -2
- package/src/import/scene-graph.ts +61 -6
- package/src/import/style-attribute.ts +1 -1
- package/src/runtime/browser/runtime.ts +3 -1
- package/src/runtime/headless/styles.ts +6 -6
- package/src/tokens/references.ts +16 -2
- package/src/tokens/validate.ts +2 -1
|
@@ -9,9 +9,16 @@ import {
|
|
|
9
9
|
type Stroke
|
|
10
10
|
} from '@pairdraw/scene-graph'
|
|
11
11
|
import { TRANSPARENT } from '@pairdraw/scene-graph/constants'
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
parseCSSAlignSelf,
|
|
14
|
+
parseCSSBackgroundImages,
|
|
15
|
+
parseCSSNumber,
|
|
16
|
+
parseCSSShadows
|
|
17
|
+
} from '@pairdraw/scene-graph/css'
|
|
13
18
|
import { normalizeEffects } from '@pairdraw/scene-graph/effects'
|
|
19
|
+
import { blurRadiusForSigma } from '@pairdraw/scene-graph/geometry'
|
|
14
20
|
import { computeImageHash } from '@pairdraw/scene-graph/images'
|
|
21
|
+
import { textLayerName } from '@pairdraw/scene-graph/node-defaults'
|
|
15
22
|
|
|
16
23
|
import type { DesignDocument, DesignElement, DesignNode, DesignStyleDeclaration } from '../types'
|
|
17
24
|
import { colorToFillFromCSS, colorToStrokeFromCSS, mergedStyle, pickStyle } from './css-values'
|
|
@@ -37,6 +44,16 @@ import {
|
|
|
37
44
|
const DOM_CSS_PLUGIN_ID = 'pairdraw-dom-css'
|
|
38
45
|
const IMAGE_SOURCE_URL_KEY = 'image-source-url'
|
|
39
46
|
|
|
47
|
+
/**
|
|
48
|
+
* CSS shadows as the layer's effects, each blurred as the browser blurs it: CSS blurs a shadow
|
|
49
|
+
* by half its blur length, which the canvas draws from the radius that gives that sigma.
|
|
50
|
+
*/
|
|
51
|
+
function cssShadows(value: string | undefined) {
|
|
52
|
+
return parseCSSShadows(value).map((effect) =>
|
|
53
|
+
effect.type === 'SHADER' ? effect : { ...effect, radius: blurRadiusForSigma(effect.radius / 2) }
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
|
|
40
57
|
export interface DesignDocumentToSceneGraphOptions {
|
|
41
58
|
pageName?: string
|
|
42
59
|
}
|
|
@@ -76,6 +93,43 @@ function fillsFromStyle(style: DesignStyleDeclaration, property: string): Fill[]
|
|
|
76
93
|
return colorToFillFromCSS(pickStyle(style, property))
|
|
77
94
|
}
|
|
78
95
|
|
|
96
|
+
/**
|
|
97
|
+
* The `background-image` layers as fills, bottom-first as Pairdraw paints them: CSS paints the
|
|
98
|
+
* first layer on top, so the list reverses. A `url()` layer becomes an image fill when its
|
|
99
|
+
* bytes are inline; a remote address is skipped, as an unfetchable background paints nothing.
|
|
100
|
+
*/
|
|
101
|
+
function backgroundFills(
|
|
102
|
+
graph: SceneGraph,
|
|
103
|
+
node: SceneNode,
|
|
104
|
+
style: DesignStyleDeclaration
|
|
105
|
+
): Fill[] {
|
|
106
|
+
const layers = parseCSSBackgroundImages(
|
|
107
|
+
pickStyle(style, 'background-image'),
|
|
108
|
+
node.width,
|
|
109
|
+
node.height
|
|
110
|
+
)
|
|
111
|
+
const fills: Fill[] = []
|
|
112
|
+
for (const layer of layers) {
|
|
113
|
+
if (layer.kind === 'gradient') {
|
|
114
|
+
fills.push(layer.fill)
|
|
115
|
+
continue
|
|
116
|
+
}
|
|
117
|
+
const bytes = bytesFromDataURL(layer.url)
|
|
118
|
+
if (!bytes) continue
|
|
119
|
+
const imageHash = computeImageHash(bytes)
|
|
120
|
+
graph.images.set(imageHash, bytes)
|
|
121
|
+
fills.push({
|
|
122
|
+
type: 'IMAGE',
|
|
123
|
+
imageHash,
|
|
124
|
+
imageScaleMode: 'FILL',
|
|
125
|
+
color: { ...TRANSPARENT },
|
|
126
|
+
opacity: 1,
|
|
127
|
+
visible: true
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
return fills.reverse()
|
|
131
|
+
}
|
|
132
|
+
|
|
79
133
|
function aspectRatioFromCSS(value: string | undefined): number | null {
|
|
80
134
|
if (!value || value === 'auto') return null
|
|
81
135
|
const parts = value
|
|
@@ -311,7 +365,8 @@ function applyElementStyle(
|
|
|
311
365
|
applyPadding(node, style)
|
|
312
366
|
|
|
313
367
|
const fills = fillsFromStyle(style, 'background-color')
|
|
314
|
-
|
|
368
|
+
const background = backgroundFills(graph, node, style)
|
|
369
|
+
if (fills.length > 0 || background.length > 0) node.fills = [...fills, ...background]
|
|
315
370
|
applyImageFill(graph, node, element, style)
|
|
316
371
|
|
|
317
372
|
const strokes = colorToStrokeFromCSS(
|
|
@@ -328,7 +383,7 @@ function applyElementStyle(
|
|
|
328
383
|
setBorderWeights(node, style, strokes[0])
|
|
329
384
|
}
|
|
330
385
|
|
|
331
|
-
const effects =
|
|
386
|
+
const effects = cssShadows(pickStyle(style, 'box-shadow'))
|
|
332
387
|
if (effects.length > 0) node.effects = normalizeEffects(effects)
|
|
333
388
|
|
|
334
389
|
const opacity = parseCSSNumber(pickStyle(style, 'opacity'))
|
|
@@ -368,7 +423,7 @@ function applyTextStyle(node: SceneNode, style: DesignStyleDeclaration): void {
|
|
|
368
423
|
const opacity = parseCSSNumber(pickStyle(style, 'opacity'))
|
|
369
424
|
if (opacity !== null) node.opacity = opacity
|
|
370
425
|
|
|
371
|
-
const effects =
|
|
426
|
+
const effects = cssShadows(pickStyle(style, 'text-shadow'))
|
|
372
427
|
if (effects.length > 0) node.effects = normalizeEffects(effects)
|
|
373
428
|
|
|
374
429
|
const textAlign = pickStyle(style, 'text-align')?.toUpperCase()
|
|
@@ -397,7 +452,7 @@ function createRichTextNode(
|
|
|
397
452
|
const base = pick(node, CHARACTER_FIELDS)
|
|
398
453
|
const rich = blocks ? blockRichText(element, style, base) : inlineRichText(element, style, base)
|
|
399
454
|
node.text = rich.text
|
|
400
|
-
node.name = rich.text
|
|
455
|
+
node.name = textLayerName('TEXT', rich.text)
|
|
401
456
|
node.styleRuns = rich.styleRuns
|
|
402
457
|
if (blocks) applyParagraphs(node, rich)
|
|
403
458
|
applyFlowSizing(graph, node, style, display, parentFlow)
|
|
@@ -429,7 +484,7 @@ function createTextNode(
|
|
|
429
484
|
display: string,
|
|
430
485
|
parentFlow: ParentFlow
|
|
431
486
|
) {
|
|
432
|
-
const node = graph.createNode('TEXT', parentId, { name:
|
|
487
|
+
const node = graph.createNode('TEXT', parentId, { name: textLayerName('TEXT', text), text })
|
|
433
488
|
applyTextStyle(node, style)
|
|
434
489
|
applyFlowSizing(graph, node, style, display, parentFlow)
|
|
435
490
|
return node
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { parse } from '@acemir/cssom'
|
|
2
|
-
import type { CSSStyleRuleLike } from '@acemir/cssom'
|
|
3
2
|
|
|
3
|
+
import type { CSSStyleRuleLike } from '../cssom/types'
|
|
4
4
|
import type { DesignStyleDeclaration } from '../types'
|
|
5
5
|
|
|
6
6
|
function firstStyleRule(cssText: string): CSSStyleRuleLike | null {
|
|
@@ -255,7 +255,9 @@ async function computeStylesInIframe(
|
|
|
255
255
|
content.innerHTML = serializeHTML(designDocument)
|
|
256
256
|
iframeDocument.body.append(content)
|
|
257
257
|
|
|
258
|
-
|
|
258
|
+
// The sandbox sits offscreen, where WebKit never runs an animation frame of its own; the
|
|
259
|
+
// page's frame lays the iframe out too, and the page is visible whenever a person pastes.
|
|
260
|
+
await requestFrame(browserDocument)
|
|
259
261
|
return copyComputedStyles(designDocument, content, options)
|
|
260
262
|
} finally {
|
|
261
263
|
iframe.remove()
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CSSGroupingRuleLike,
|
|
3
|
+
CSSStyleDeclarationLike,
|
|
4
|
+
CSSStyleRuleLike
|
|
5
|
+
} from '#dom-css/cssom/types'
|
|
1
6
|
import type {
|
|
2
7
|
DesignDocument,
|
|
3
8
|
DesignElement,
|
|
4
9
|
DesignNode,
|
|
5
10
|
DesignStyleDeclaration
|
|
6
11
|
} from '#dom-css/types'
|
|
7
|
-
import {
|
|
8
|
-
parse,
|
|
9
|
-
type CSSGroupingRuleLike,
|
|
10
|
-
type CSSStyleDeclarationLike,
|
|
11
|
-
type CSSStyleRuleLike
|
|
12
|
-
} from '@acemir/cssom'
|
|
12
|
+
import { parse } from '@acemir/cssom'
|
|
13
13
|
|
|
14
14
|
import { tryParseColor } from '@pairdraw/scene-graph/color'
|
|
15
15
|
import { parseCSSNumber, splitCSSValue } from '@pairdraw/scene-graph/css'
|
package/src/tokens/references.ts
CHANGED
|
@@ -54,6 +54,20 @@ function isColor(value: unknown): value is Color {
|
|
|
54
54
|
return typeof value === 'object' && value !== null && 'r' in value && 'a' in value
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
/**
|
|
58
|
+
* A field's value as the number a token gives it. A text's letter spacing and line height are
|
|
59
|
+
* lengths: in pixels they match by their value, while a percent of the font size, or automatic
|
|
60
|
+
* line height, has no length token to match and stays literal.
|
|
61
|
+
*/
|
|
62
|
+
function drawnNumber(node: SceneNode, field: string): number | undefined {
|
|
63
|
+
if (field === 'letterSpacing' || field === 'lineHeight') {
|
|
64
|
+
const length = node[field]
|
|
65
|
+
return length?.unit === 'PIXELS' ? length.value : undefined
|
|
66
|
+
}
|
|
67
|
+
const value: unknown = Reflect.get(node, field)
|
|
68
|
+
return typeof value === 'number' ? value : undefined
|
|
69
|
+
}
|
|
70
|
+
|
|
57
71
|
/** Where an alias chain ends in the modes the canvas draws: a value, or a CSS expression. */
|
|
58
72
|
type TokenChain = {
|
|
59
73
|
ids: string[]
|
|
@@ -167,8 +181,8 @@ export class DesignTokens {
|
|
|
167
181
|
if (!paint?.visible || paint.type !== 'SOLID' || !isColor(value)) return false
|
|
168
182
|
return sameColor({ ...paint.color, a: paint.opacity }, value)
|
|
169
183
|
}
|
|
170
|
-
const drawn
|
|
171
|
-
if (
|
|
184
|
+
const drawn = drawnNumber(node, field)
|
|
185
|
+
if (drawn === undefined || typeof value !== 'number') return false
|
|
172
186
|
if (Math.fround(drawn) !== Math.fround(value)) return false
|
|
173
187
|
// An expression is the author's CSS for the value, whatever unit the token stores.
|
|
174
188
|
if (field === 'opacity') return expression || variableUnit(end) === 'none'
|
package/src/tokens/validate.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type { CSSStyleSheetLike } from '@acemir/cssom'
|
|
2
1
|
import valueParser from 'postcss-value-parser'
|
|
3
2
|
|
|
3
|
+
import type { CSSStyleSheetLike } from '../cssom/types'
|
|
4
|
+
|
|
4
5
|
type Parse = (cssText: string) => CSSStyleSheetLike
|
|
5
6
|
|
|
6
7
|
const AT_RULES = ['@media', '@supports', '@container'] as const
|