@pairdraw/dom-css 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 +219 -2
- package/dist/browser.d.ts +29 -0
- package/dist/browser.js +2 -0
- package/dist/chunks/behaviours/args.js +59 -0
- package/dist/chunks/behaviours/args.js.map +1 -0
- package/dist/chunks/behaviours/controls.js +109 -0
- package/dist/chunks/behaviours/controls.js.map +1 -0
- package/dist/chunks/behaviours/reset.js +40 -0
- package/dist/chunks/behaviours/reset.js.map +1 -0
- package/dist/chunks/behaviours/roles.js +51 -0
- package/dist/chunks/behaviours/roles.js.map +1 -0
- package/dist/chunks/behaviours/states/conditions.js +103 -0
- package/dist/chunks/behaviours/states/conditions.js.map +1 -0
- package/dist/chunks/behaviours/states/css.js +172 -0
- package/dist/chunks/behaviours/states/css.js.map +1 -0
- package/dist/chunks/behaviours/states/layers.js +1736 -0
- package/dist/chunks/behaviours/states/layers.js.map +1 -0
- package/dist/chunks/behaviours/states/model.js +144 -0
- package/dist/chunks/behaviours/states/model.js.map +1 -0
- package/dist/chunks/behaviours/states/tailwind.js +198 -0
- package/dist/chunks/behaviours/states/tailwind.js.map +1 -0
- package/dist/chunks/browser.js +1352 -0
- package/dist/chunks/browser.js.map +1 -0
- package/dist/chunks/export/bundle.js +337 -0
- package/dist/chunks/export/bundle.js.map +1 -0
- package/dist/chunks/export/components/drawn.js +60 -0
- package/dist/chunks/export/components/drawn.js.map +1 -0
- package/dist/chunks/export/components/fields.js +178 -0
- package/dist/chunks/export/components/fields.js.map +1 -0
- package/dist/chunks/export/components/groups.js +297 -0
- package/dist/chunks/export/components/groups.js.map +1 -0
- package/dist/chunks/export/components/icons.js +260 -0
- package/dist/chunks/export/components/icons.js.map +1 -0
- package/dist/chunks/export/components/layers.js +1594 -0
- package/dist/chunks/export/components/layers.js.map +1 -0
- package/dist/chunks/export/index.js +477 -0
- package/dist/chunks/export/index.js.map +1 -0
- package/dist/chunks/export/storybook/component.js +448 -0
- package/dist/chunks/export/storybook/component.js.map +1 -0
- package/dist/chunks/export/storybook/export.js +410 -0
- package/dist/chunks/export/storybook/export.js.map +1 -0
- package/dist/chunks/import/html.js +406 -0
- package/dist/chunks/import/html.js.map +1 -0
- package/dist/chunks/import/jsx/dev-runtime.js +37 -0
- package/dist/chunks/import/jsx/dev-runtime.js.map +1 -0
- package/dist/chunks/tokens/cssom-validator.js +10 -0
- package/dist/chunks/tokens/cssom-validator.js.map +1 -0
- package/dist/export.d.ts +594 -0
- package/dist/export.js +13 -0
- package/dist/index.d.ts +115 -0
- package/dist/index.js +7 -0
- package/dist/jsx-dev-runtime.d.ts +8 -0
- package/dist/jsx-dev-runtime.js +3 -0
- package/dist/jsx-runtime.d.ts +20 -0
- package/dist/jsx-runtime.js +3 -0
- package/dist/rolldown-runtime.js +13 -0
- package/dist/runtime.d.ts +10 -0
- package/dist/scene-graph.d.ts +2 -0
- package/dist/scene-graph.js +2 -0
- package/dist/scene-graph2.d.ts +10 -0
- package/dist/styling.d.ts +93 -0
- package/dist/tailwind.d.ts +39 -0
- package/dist/types.d.ts +89 -0
- package/package.json +73 -4
- package/src/behaviours/args.ts +85 -0
- package/src/behaviours/controls.ts +195 -0
- package/src/behaviours/index.ts +11 -0
- package/src/behaviours/reset.ts +37 -0
- package/src/behaviours/roles.ts +44 -0
- package/src/behaviours/states/conditions.ts +105 -0
- package/src/behaviours/states/css.ts +123 -0
- package/src/behaviours/states/index.ts +4 -0
- package/src/behaviours/states/layers.ts +205 -0
- package/src/behaviours/states/model.ts +175 -0
- package/src/behaviours/states/names.ts +39 -0
- package/src/behaviours/states/tailwind.ts +148 -0
- package/src/behaviours/states/types.ts +56 -0
- package/src/browser.ts +146 -0
- package/src/cssom.d.ts +38 -0
- package/src/export/bundle.ts +461 -0
- package/src/export/components/drawn.ts +73 -0
- package/src/export/components/fields.ts +178 -0
- package/src/export/components/groups.ts +78 -0
- package/src/export/components/icons.ts +230 -0
- package/src/export/components/layers.ts +121 -0
- package/src/export/components/model.ts +544 -0
- package/src/export/components/properties.ts +167 -0
- package/src/export/components/react/fields.ts +129 -0
- package/src/export/components/react/index.ts +606 -0
- package/src/export/components/react/shared.ts +76 -0
- package/src/export/components/references.ts +212 -0
- package/src/export/components/repeats.ts +128 -0
- package/src/export/components/shaders.ts +125 -0
- package/src/export/components/styling.ts +31 -0
- package/src/export/components/tree.ts +72 -0
- package/src/export/components/vue.ts +517 -0
- package/src/export/css.ts +57 -0
- package/src/export/grid.ts +42 -0
- package/src/export/html.ts +163 -0
- package/src/export/index.ts +44 -0
- package/src/export/projection.ts +581 -0
- package/src/export/storybook/component.ts +334 -0
- package/src/export/storybook/export.ts +430 -0
- package/src/export/storybook/groups.ts +161 -0
- package/src/export/storybook/module.ts +319 -0
- package/src/export/storybook/names.ts +51 -0
- package/src/export/tailwind-jsx.ts +156 -0
- package/src/export/text.ts +415 -0
- package/src/import/css-text.ts +4 -0
- package/src/import/css-values.ts +31 -0
- package/src/import/flow.ts +248 -0
- package/src/import/html.ts +88 -0
- package/src/import/jsx/core.ts +130 -0
- package/src/import/jsx/dev-runtime.ts +8 -0
- package/src/import/jsx/runtime.ts +76 -0
- package/src/import/scene-graph.ts +582 -0
- package/src/import/style-attribute.ts +25 -0
- package/src/import/tailwind.ts +54 -0
- package/src/import/text.ts +518 -0
- package/src/index.ts +51 -0
- package/src/runtime/browser/runtime.ts +301 -0
- package/src/runtime/headless/runtime.ts +99 -0
- package/src/runtime/headless/styles.ts +330 -0
- package/src/runtime/index.ts +11 -0
- package/src/tokens/conditions.ts +66 -0
- package/src/tokens/cssom-validator.ts +8 -0
- package/src/tokens/custom-properties.ts +331 -0
- package/src/tokens/expression.ts +128 -0
- package/src/tokens/formula.ts +140 -0
- package/src/tokens/index.ts +43 -0
- package/src/tokens/names.ts +161 -0
- package/src/tokens/references.ts +201 -0
- package/src/tokens/stylesheet.ts +304 -0
- package/src/tokens/validate.ts +86 -0
- package/src/tokens/values.ts +44 -0
- package/src/types.ts +59 -0
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
import { countBy } from 'es-toolkit/array'
|
|
2
|
+
import { isEqual } from 'es-toolkit/predicate'
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
CharacterStyleOverride,
|
|
6
|
+
HyperlinkTarget,
|
|
7
|
+
SceneNode,
|
|
8
|
+
StyleRun,
|
|
9
|
+
TextParagraphStyle
|
|
10
|
+
} from '@pairdraw/scene-graph'
|
|
11
|
+
import {
|
|
12
|
+
parseCSSColor,
|
|
13
|
+
parseCSSFontFamily,
|
|
14
|
+
parseCSSLetterSpacing,
|
|
15
|
+
parseCSSLineHeight,
|
|
16
|
+
parseCSSNumber
|
|
17
|
+
} from '@pairdraw/scene-graph/css'
|
|
18
|
+
|
|
19
|
+
import type { DesignElement, DesignNode, DesignStyleDeclaration } from '../types'
|
|
20
|
+
import { colorToFillFromCSS, mergedStyle, pickStyle } from './css-values'
|
|
21
|
+
|
|
22
|
+
/** The character styles a text layer and its runs take from CSS. */
|
|
23
|
+
export type CSSCharacterStyle = Pick<
|
|
24
|
+
SceneNode,
|
|
25
|
+
| 'fontFamily'
|
|
26
|
+
| 'fontSize'
|
|
27
|
+
| 'fontWeight'
|
|
28
|
+
| 'italic'
|
|
29
|
+
| 'letterSpacing'
|
|
30
|
+
| 'lineHeight'
|
|
31
|
+
| 'fills'
|
|
32
|
+
| 'textDecoration'
|
|
33
|
+
| 'textDecorationStyle'
|
|
34
|
+
| 'textDecorationThickness'
|
|
35
|
+
| 'textDecorationFills'
|
|
36
|
+
| 'textDecorationSkipInk'
|
|
37
|
+
| 'textUnderlineOffset'
|
|
38
|
+
| 'textCase'
|
|
39
|
+
>
|
|
40
|
+
|
|
41
|
+
/** A character's style and the link it is part of. */
|
|
42
|
+
type CharacterOfText = CSSCharacterStyle & { hyperlink?: HyperlinkTarget }
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* What characters take from the elements around them: the properties CSS inherits, and the
|
|
46
|
+
* decoration, which CSS draws through descendants.
|
|
47
|
+
*/
|
|
48
|
+
const CHARACTER_PROPERTIES = [
|
|
49
|
+
'color',
|
|
50
|
+
'font-family',
|
|
51
|
+
'font-size',
|
|
52
|
+
'font-style',
|
|
53
|
+
'font-weight',
|
|
54
|
+
'letter-spacing',
|
|
55
|
+
'line-height',
|
|
56
|
+
'white-space',
|
|
57
|
+
'text-transform',
|
|
58
|
+
'text-decoration-line',
|
|
59
|
+
'text-decoration-style',
|
|
60
|
+
'text-decoration-color',
|
|
61
|
+
'text-decoration-thickness',
|
|
62
|
+
'text-underline-offset',
|
|
63
|
+
'text-decoration-skip-ink'
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
const TEXT_BLOCK_TAGS = new Set(['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'])
|
|
67
|
+
|
|
68
|
+
/** What browsers' default stylesheet gives inline formatting elements that CSS does not set. */
|
|
69
|
+
const FORMATTING_DEFAULTS: Record<string, DesignStyleDeclaration> = {
|
|
70
|
+
b: { 'font-weight': '700' },
|
|
71
|
+
strong: { 'font-weight': '700' },
|
|
72
|
+
i: { 'font-style': 'italic' },
|
|
73
|
+
em: { 'font-style': 'italic' },
|
|
74
|
+
u: { 'text-decoration-line': 'underline' },
|
|
75
|
+
ins: { 'text-decoration-line': 'underline' },
|
|
76
|
+
s: { 'text-decoration-line': 'line-through' },
|
|
77
|
+
del: { 'text-decoration-line': 'line-through' }
|
|
78
|
+
}
|
|
79
|
+
const LIST_TAGS = new Set(['ul', 'ol'])
|
|
80
|
+
|
|
81
|
+
/** Lengths CSS gives as a number of pixels, or `null` for keywords such as `auto`. */
|
|
82
|
+
function pixels(value: string | undefined): number | null {
|
|
83
|
+
const trimmed = value?.trim()
|
|
84
|
+
if (!trimmed || !/^-?[\d.]+(px)?$/.test(trimmed)) return null
|
|
85
|
+
return parseCSSNumber(trimmed)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function decorationLine(value: string | undefined): SceneNode['textDecoration'] {
|
|
89
|
+
if (value?.includes('underline')) return 'UNDERLINE'
|
|
90
|
+
if (value?.includes('line-through')) return 'STRIKETHROUGH'
|
|
91
|
+
return 'NONE'
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function decorationStyle(value: string | undefined): SceneNode['textDecorationStyle'] {
|
|
95
|
+
const style = value?.trim().toLowerCase()
|
|
96
|
+
if (style === 'dotted') return 'DOTTED'
|
|
97
|
+
if (style === 'wavy') return 'WAVY'
|
|
98
|
+
return 'SOLID'
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function textCaseFromCSS(value: string | undefined): SceneNode['textCase'] {
|
|
102
|
+
if (value === 'uppercase') return 'UPPER'
|
|
103
|
+
if (value === 'lowercase') return 'LOWER'
|
|
104
|
+
if (value === 'capitalize') return 'TITLE'
|
|
105
|
+
return 'ORIGINAL'
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The character style of CSS declarations, over `base` for what they leave out. */
|
|
109
|
+
export function characterStyleFromCSS(
|
|
110
|
+
style: DesignStyleDeclaration,
|
|
111
|
+
base: CSSCharacterStyle
|
|
112
|
+
): CSSCharacterStyle {
|
|
113
|
+
const fills = colorToFillFromCSS(pickStyle(style, 'color'))
|
|
114
|
+
const family = parseCSSFontFamily(pickStyle(style, 'font-family'))
|
|
115
|
+
const decorationColor = pickStyle(style, 'text-decoration-color')
|
|
116
|
+
const decorationFills = parseCSSColor(decorationColor) ? colorToFillFromCSS(decorationColor) : []
|
|
117
|
+
const line = pickStyle(style, 'text-decoration-line')
|
|
118
|
+
const fontStyle = pickStyle(style, 'font-style')
|
|
119
|
+
const lineHeight = pickStyle(style, 'line-height')
|
|
120
|
+
// `normal` is automatic, a null; a value CSS writes some other way keeps the base.
|
|
121
|
+
const parsedLineHeight = parseCSSLineHeight(lineHeight)
|
|
122
|
+
const textTransform = pickStyle(style, 'text-transform')
|
|
123
|
+
return {
|
|
124
|
+
fontFamily: family || base.fontFamily,
|
|
125
|
+
fontSize: parseCSSNumber(pickStyle(style, 'font-size')) ?? base.fontSize,
|
|
126
|
+
fontWeight: parseCSSNumber(pickStyle(style, 'font-weight')) ?? base.fontWeight,
|
|
127
|
+
italic: fontStyle === undefined ? base.italic : fontStyle === 'italic',
|
|
128
|
+
letterSpacing: parseCSSLetterSpacing(pickStyle(style, 'letter-spacing')) ?? base.letterSpacing,
|
|
129
|
+
lineHeight: parsedLineHeight === undefined ? base.lineHeight : parsedLineHeight,
|
|
130
|
+
fills: fills.length > 0 ? fills : base.fills,
|
|
131
|
+
textDecoration: line === undefined ? base.textDecoration : decorationLine(line),
|
|
132
|
+
textDecorationStyle:
|
|
133
|
+
pickStyle(style, 'text-decoration-style') === undefined
|
|
134
|
+
? base.textDecorationStyle
|
|
135
|
+
: decorationStyle(pickStyle(style, 'text-decoration-style')),
|
|
136
|
+
textDecorationFills: decorationColor === undefined ? base.textDecorationFills : decorationFills,
|
|
137
|
+
textDecorationThickness:
|
|
138
|
+
pickStyle(style, 'text-decoration-thickness') === undefined
|
|
139
|
+
? base.textDecorationThickness
|
|
140
|
+
: pixels(pickStyle(style, 'text-decoration-thickness')),
|
|
141
|
+
textUnderlineOffset:
|
|
142
|
+
pickStyle(style, 'text-underline-offset') === undefined
|
|
143
|
+
? base.textUnderlineOffset
|
|
144
|
+
: pixels(pickStyle(style, 'text-underline-offset')),
|
|
145
|
+
textDecorationSkipInk:
|
|
146
|
+
pickStyle(style, 'text-decoration-skip-ink') === undefined
|
|
147
|
+
? base.textDecorationSkipInk
|
|
148
|
+
: pickStyle(style, 'text-decoration-skip-ink') !== 'none',
|
|
149
|
+
textCase: textTransform === undefined ? base.textCase : textCaseFromCSS(textTransform)
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** What the characters inside `element` take from it and the elements around it. */
|
|
154
|
+
function characterDeclarations(
|
|
155
|
+
element: DesignElement,
|
|
156
|
+
around: DesignStyleDeclaration
|
|
157
|
+
): DesignStyleDeclaration {
|
|
158
|
+
const own = { ...FORMATTING_DEFAULTS[element.tagName.toLowerCase()], ...mergedStyle(element) }
|
|
159
|
+
const inherited: DesignStyleDeclaration = {}
|
|
160
|
+
for (const property of CHARACTER_PROPERTIES) {
|
|
161
|
+
const value = pickStyle(own, property) ?? pickStyle(around, property)
|
|
162
|
+
if (value !== undefined) inherited[property] = value
|
|
163
|
+
}
|
|
164
|
+
return inherited
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Whether the element holds only characters: text, inline elements, and line breaks. */
|
|
168
|
+
export function isInlineContent(
|
|
169
|
+
element: DesignElement,
|
|
170
|
+
isInlineElement: (element: DesignElement) => boolean
|
|
171
|
+
): boolean {
|
|
172
|
+
return element.children.every(
|
|
173
|
+
(child) =>
|
|
174
|
+
child.type === 'text' ||
|
|
175
|
+
child.tagName.toLowerCase() === 'br' ||
|
|
176
|
+
(isInlineElement(child) && isInlineContent(child, isInlineElement))
|
|
177
|
+
)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function isWhitespace(node: DesignNode): boolean {
|
|
181
|
+
return node.type === 'text' && node.text.trim() === ''
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function isList(
|
|
185
|
+
element: DesignElement,
|
|
186
|
+
isListPlain: (element: DesignElement) => boolean,
|
|
187
|
+
isPlain: (element: DesignElement) => boolean
|
|
188
|
+
): boolean {
|
|
189
|
+
if (!LIST_TAGS.has(element.tagName.toLowerCase()) || !isListPlain(element)) return false
|
|
190
|
+
return element.children.every(
|
|
191
|
+
(child) =>
|
|
192
|
+
isWhitespace(child) ||
|
|
193
|
+
(child.type === 'element' && child.tagName.toLowerCase() === 'li' && isListItem(child))
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
function isListItem(item: DesignElement): boolean {
|
|
197
|
+
return item.children.every(
|
|
198
|
+
(child) =>
|
|
199
|
+
child.type === 'text' ||
|
|
200
|
+
child.tagName.toLowerCase() === 'br' ||
|
|
201
|
+
isList(child, isPlain, isPlain) ||
|
|
202
|
+
(!LIST_TAGS.has(child.tagName.toLowerCase()) && isPlain(child))
|
|
203
|
+
)
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Whether the element holds text in blocks, as Figma keeps it in one layer: it is a list, or
|
|
209
|
+
* holds only paragraphs, headings, and lists of characters with no box of their own.
|
|
210
|
+
*/
|
|
211
|
+
export function holdsTextBlocks(
|
|
212
|
+
element: DesignElement,
|
|
213
|
+
isPlain: (element: DesignElement) => boolean,
|
|
214
|
+
isInlineElement: (element: DesignElement) => boolean
|
|
215
|
+
): boolean {
|
|
216
|
+
if (LIST_TAGS.has(element.tagName.toLowerCase())) return isList(element, () => true, isPlain)
|
|
217
|
+
const blocks = element.children.filter((child) => !isWhitespace(child))
|
|
218
|
+
return (
|
|
219
|
+
blocks.length > 0 &&
|
|
220
|
+
blocks.every(
|
|
221
|
+
(child) =>
|
|
222
|
+
child.type === 'element' &&
|
|
223
|
+
(isList(child, isPlain, isPlain) ||
|
|
224
|
+
(TEXT_BLOCK_TAGS.has(child.tagName.toLowerCase()) &&
|
|
225
|
+
isPlain(child) &&
|
|
226
|
+
isInlineContent(child, isInlineElement)))
|
|
227
|
+
)
|
|
228
|
+
)
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Text with its runs and paragraphs, as one text layer holds it. */
|
|
232
|
+
export interface RichText {
|
|
233
|
+
text: string
|
|
234
|
+
styleRuns: StyleRun[]
|
|
235
|
+
textParagraphs: TextParagraphStyle[]
|
|
236
|
+
/** The space after each paragraph, from the margins between blocks. */
|
|
237
|
+
spaceAfter: number[]
|
|
238
|
+
/** Each paragraph's first-line indent. */
|
|
239
|
+
indents: number[]
|
|
240
|
+
/** Whether the outermost list puts its markers outside the text, with no indent of its own. */
|
|
241
|
+
hangingList: boolean
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
interface Collector {
|
|
245
|
+
base: CSSCharacterStyle
|
|
246
|
+
text: string
|
|
247
|
+
styles: CharacterOfText[]
|
|
248
|
+
/** The link the characters added now are part of. */
|
|
249
|
+
link: HyperlinkTarget | null
|
|
250
|
+
paragraphs: TextParagraphStyle[]
|
|
251
|
+
spaceAfter: number[]
|
|
252
|
+
indents: number[]
|
|
253
|
+
/** Space collected from margins since the last paragraph ended. */
|
|
254
|
+
pending: number
|
|
255
|
+
hangingList: boolean | undefined
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function preservesWhitespace(style: DesignStyleDeclaration): boolean {
|
|
259
|
+
const whiteSpace = pickStyle(style, 'white-space')
|
|
260
|
+
return whiteSpace === 'pre' || whiteSpace === 'pre-wrap' || whiteSpace === 'pre-line'
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function appendCharacters(collector: Collector, text: string, style: CharacterOfText): void {
|
|
264
|
+
const styled = collector.link ? { ...style, hyperlink: collector.link } : style
|
|
265
|
+
collector.text += text
|
|
266
|
+
collector.styles.push(...Array.from({ length: text.length }, () => styled))
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** Where a link element goes, for an `<a>` with an `href`. */
|
|
270
|
+
export function elementLink(element: DesignElement): HyperlinkTarget | null {
|
|
271
|
+
if (element.tagName.toLowerCase() !== 'a' || !Object.hasOwn(element.attrs, 'href')) return null
|
|
272
|
+
const href = element.attrs.href.trim()
|
|
273
|
+
return href ? { type: 'URL', value: href } : null
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Characters of `text` in `style`. A line break inside a block starts a paragraph of the
|
|
278
|
+
* block's kind, as text in Figma breaks into paragraphs at every newline.
|
|
279
|
+
*/
|
|
280
|
+
function addCharacters(collector: Collector, text: string, style: DesignStyleDeclaration): void {
|
|
281
|
+
const value = preservesWhitespace(style) ? text : text.replaceAll(/\s+/g, ' ')
|
|
282
|
+
const characterStyle = characterStyleFromCSS(style, collector.base)
|
|
283
|
+
for (const [index, line] of value.split('\n').entries()) {
|
|
284
|
+
if (index > 0) {
|
|
285
|
+
const paragraph = collector.paragraphs.at(-1)
|
|
286
|
+
if (paragraph) {
|
|
287
|
+
collector.spaceAfter.push(0)
|
|
288
|
+
collector.paragraphs.push({ ...paragraph })
|
|
289
|
+
collector.indents.push(0)
|
|
290
|
+
}
|
|
291
|
+
appendCharacters(collector, '\n', characterStyle)
|
|
292
|
+
}
|
|
293
|
+
appendCharacters(collector, line, characterStyle)
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function addInline(
|
|
298
|
+
collector: Collector,
|
|
299
|
+
nodes: readonly DesignNode[],
|
|
300
|
+
style: DesignStyleDeclaration
|
|
301
|
+
) {
|
|
302
|
+
for (const node of nodes) {
|
|
303
|
+
if (node.type === 'text') addCharacters(collector, node.text, style)
|
|
304
|
+
else if (isBreak(node)) addCharacters(collector, '\n', { ...style, 'white-space': 'pre' })
|
|
305
|
+
else if (!LIST_TAGS.has(node.tagName.toLowerCase())) {
|
|
306
|
+
const around = collector.link
|
|
307
|
+
collector.link = elementLink(node) ?? around
|
|
308
|
+
addInline(collector, node.children, characterDeclarations(node, style))
|
|
309
|
+
collector.link = around
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function isBreak(node: DesignNode): boolean {
|
|
315
|
+
return node.type === 'element' && node.tagName.toLowerCase() === 'br'
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function margin(element: DesignElement, side: 'top' | 'bottom'): number {
|
|
319
|
+
return Math.max(0, pixels(pickStyle(mergedStyle(element), `margin-${side}`)) ?? 0)
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** Starts a paragraph, ending the one before with the space collected since it. */
|
|
323
|
+
function startParagraph(collector: Collector, style: TextParagraphStyle, indent: number): void {
|
|
324
|
+
if (collector.paragraphs.length > 0) {
|
|
325
|
+
collector.spaceAfter.push(collector.pending)
|
|
326
|
+
appendCharacters(collector, '\n', collector.styles.at(-1) ?? collector.base)
|
|
327
|
+
}
|
|
328
|
+
collector.pending = 0
|
|
329
|
+
collector.paragraphs.push(style)
|
|
330
|
+
collector.indents.push(indent)
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** A paragraph's characters, trimmed of the spaces that CSS drops at a block's edges. */
|
|
334
|
+
function addParagraph(
|
|
335
|
+
collector: Collector,
|
|
336
|
+
nodes: readonly DesignNode[],
|
|
337
|
+
style: DesignStyleDeclaration,
|
|
338
|
+
paragraph: TextParagraphStyle,
|
|
339
|
+
indent: number
|
|
340
|
+
): void {
|
|
341
|
+
startParagraph(collector, paragraph, indent)
|
|
342
|
+
// A line break that ends a block adds no line: it gives an empty block its height.
|
|
343
|
+
const content = isBreak(nodes.at(-1) ?? { type: 'text', text: '' }) ? nodes.slice(0, -1) : nodes
|
|
344
|
+
addTrimmed(collector, content, style)
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** Inline content, trimmed of the spaces CSS drops at a block's edges unless it keeps them. */
|
|
348
|
+
function addTrimmed(
|
|
349
|
+
collector: Collector,
|
|
350
|
+
nodes: readonly DesignNode[],
|
|
351
|
+
style: DesignStyleDeclaration
|
|
352
|
+
): void {
|
|
353
|
+
const start = collector.text.length
|
|
354
|
+
addInline(collector, nodes, style)
|
|
355
|
+
if (preservesWhitespace(style)) return
|
|
356
|
+
const added = collector.text.slice(start)
|
|
357
|
+
const leading = added.length - added.trimStart().length
|
|
358
|
+
const trimmed = added.trim()
|
|
359
|
+
collector.text = collector.text.slice(0, start) + trimmed
|
|
360
|
+
collector.styles = [
|
|
361
|
+
...collector.styles.slice(0, start),
|
|
362
|
+
...collector.styles.slice(start + leading, start + leading + trimmed.length)
|
|
363
|
+
]
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function addList(
|
|
367
|
+
collector: Collector,
|
|
368
|
+
list: DesignElement,
|
|
369
|
+
around: DesignStyleDeclaration,
|
|
370
|
+
level: number
|
|
371
|
+
): void {
|
|
372
|
+
const listStyle = characterDeclarations(list, around)
|
|
373
|
+
collector.pending = Math.max(collector.pending, margin(list, 'top'))
|
|
374
|
+
if (level === 1 && collector.hangingList === undefined) {
|
|
375
|
+
collector.hangingList = pixels(pickStyle(mergedStyle(list), 'padding-inline-start')) === 0
|
|
376
|
+
}
|
|
377
|
+
const listType = list.tagName.toLowerCase() === 'ol' ? 'ORDERED' : 'UNORDERED'
|
|
378
|
+
for (const item of list.children) {
|
|
379
|
+
if (item.type !== 'element') continue
|
|
380
|
+
const itemStyle = characterDeclarations(item, listStyle)
|
|
381
|
+
const inline = item.children.filter(
|
|
382
|
+
(child) => child.type === 'text' || !LIST_TAGS.has(child.tagName.toLowerCase())
|
|
383
|
+
)
|
|
384
|
+
collector.pending = Math.max(collector.pending, margin(item, 'top'))
|
|
385
|
+
// An item holding only a nested list, as a skipped level is written, is no paragraph.
|
|
386
|
+
if (inline.some((child) => !isWhitespace(child))) {
|
|
387
|
+
addParagraph(collector, inline, itemStyle, { listType, indentation: level }, 0)
|
|
388
|
+
}
|
|
389
|
+
for (const child of item.children) {
|
|
390
|
+
if (child.type === 'element' && LIST_TAGS.has(child.tagName.toLowerCase())) {
|
|
391
|
+
addList(collector, child, itemStyle, level + 1)
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
collector.pending = Math.max(collector.pending, margin(item, 'bottom'))
|
|
395
|
+
}
|
|
396
|
+
collector.pending = Math.max(collector.pending, margin(list, 'bottom'))
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/** Runs of characters whose style differs from the text's own. */
|
|
400
|
+
function styleRunsFrom(styles: readonly CharacterOfText[], base: CSSCharacterStyle): StyleRun[] {
|
|
401
|
+
const runs: StyleRun[] = []
|
|
402
|
+
for (const [index, style] of styles.entries()) {
|
|
403
|
+
const override: CharacterStyleOverride = {}
|
|
404
|
+
for (const key of Object.keys(style) as (keyof CharacterOfText)[]) {
|
|
405
|
+
const own = key === 'hyperlink' ? undefined : base[key]
|
|
406
|
+
if (!isEqual(style[key], own)) Object.assign(override, { [key]: style[key] })
|
|
407
|
+
}
|
|
408
|
+
const previous = runs.at(-1)
|
|
409
|
+
if (
|
|
410
|
+
previous &&
|
|
411
|
+
previous.start + previous.length === index &&
|
|
412
|
+
isEqual(previous.style, override)
|
|
413
|
+
) {
|
|
414
|
+
previous.length++
|
|
415
|
+
} else if (Object.keys(override).length > 0) {
|
|
416
|
+
runs.push({ start: index, length: 1, style: override })
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return runs
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function richTextFrom(collector: Collector): RichText {
|
|
423
|
+
return {
|
|
424
|
+
text: collector.text,
|
|
425
|
+
styleRuns: styleRunsFrom(collector.styles, collector.base),
|
|
426
|
+
textParagraphs: collector.paragraphs,
|
|
427
|
+
spaceAfter: collector.spaceAfter,
|
|
428
|
+
indents: collector.indents,
|
|
429
|
+
hangingList: collector.hangingList ?? false
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
function collector(base: CSSCharacterStyle, link: HyperlinkTarget | null): Collector {
|
|
434
|
+
return {
|
|
435
|
+
base,
|
|
436
|
+
text: '',
|
|
437
|
+
styles: [],
|
|
438
|
+
link,
|
|
439
|
+
paragraphs: [],
|
|
440
|
+
spaceAfter: [],
|
|
441
|
+
indents: [],
|
|
442
|
+
pending: 0,
|
|
443
|
+
hangingList: undefined
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/** An element of inline content as one paragraph of text, its spans as runs. */
|
|
448
|
+
export function inlineRichText(
|
|
449
|
+
element: DesignElement,
|
|
450
|
+
style: DesignStyleDeclaration,
|
|
451
|
+
base: CSSCharacterStyle
|
|
452
|
+
): RichText {
|
|
453
|
+
// A link the element is itself, as an `<a>` holding spans, links all its characters.
|
|
454
|
+
const text = collector(base, elementLink(element))
|
|
455
|
+
addTrimmed(text, element.children, characterDeclarations(element, style))
|
|
456
|
+
return richTextFrom(text)
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/** An element of text blocks as paragraphs of one text, its spans as runs. */
|
|
460
|
+
export function blockRichText(
|
|
461
|
+
element: DesignElement,
|
|
462
|
+
style: DesignStyleDeclaration,
|
|
463
|
+
base: CSSCharacterStyle
|
|
464
|
+
): RichText {
|
|
465
|
+
const text = collector(base, elementLink(element))
|
|
466
|
+
const around = characterDeclarations(element, style)
|
|
467
|
+
const blocks = LIST_TAGS.has(element.tagName.toLowerCase()) ? [element] : element.children
|
|
468
|
+
for (const block of blocks) {
|
|
469
|
+
if (block.type !== 'element') continue
|
|
470
|
+
if (LIST_TAGS.has(block.tagName.toLowerCase())) {
|
|
471
|
+
addList(text, block, around, 1)
|
|
472
|
+
continue
|
|
473
|
+
}
|
|
474
|
+
text.pending = Math.max(text.pending, margin(block, 'top'))
|
|
475
|
+
const blockStyle = characterDeclarations(block, around)
|
|
476
|
+
const indent = Math.max(0, pixels(pickStyle(mergedStyle(block), 'text-indent')) ?? 0)
|
|
477
|
+
addParagraph(text, block.children, blockStyle, { listType: 'NONE', indentation: 0 }, indent)
|
|
478
|
+
text.pending = Math.max(text.pending, margin(block, 'bottom'))
|
|
479
|
+
}
|
|
480
|
+
return richTextFrom(text)
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/** The value most gaps of a kind share, which the text holds; the others are the paragraphs'. */
|
|
484
|
+
function sharedValue(values: readonly number[]): number {
|
|
485
|
+
const counts = countBy(values, String)
|
|
486
|
+
const shared = Object.entries(counts)
|
|
487
|
+
.toSorted((a, b) => b[1] - a[1])
|
|
488
|
+
.at(0)
|
|
489
|
+
return shared ? Number(shared[0]) : 0
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/** Paragraph styles with the spacing and indents of the text's blocks, by Figma's rules. */
|
|
493
|
+
export function applyParagraphs(node: SceneNode, rich: RichText): void {
|
|
494
|
+
const { textParagraphs: paragraphs, spaceAfter, indents } = rich
|
|
495
|
+
const betweenItems = (index: number) =>
|
|
496
|
+
paragraphs[index]?.listType !== 'NONE' && paragraphs[index + 1]?.listType !== 'NONE'
|
|
497
|
+
const gaps = spaceAfter.map((space, index) => ({ space, list: betweenItems(index) }))
|
|
498
|
+
node.paragraphSpacing = sharedValue(gaps.filter((gap) => !gap.list).map((gap) => gap.space))
|
|
499
|
+
node.listSpacing = sharedValue(gaps.filter((gap) => gap.list).map((gap) => gap.space))
|
|
500
|
+
node.paragraphIndent = sharedValue(
|
|
501
|
+
indents.filter((_, index) => paragraphs[index]?.listType === 'NONE')
|
|
502
|
+
)
|
|
503
|
+
node.hangingList = rich.hangingList
|
|
504
|
+
const styles = paragraphs.map((paragraph, index) => {
|
|
505
|
+
const style: TextParagraphStyle = { ...paragraph }
|
|
506
|
+
const gap = gaps.at(index)
|
|
507
|
+
const field = gap?.list ? 'listSpacing' : 'paragraphSpacing'
|
|
508
|
+
if (gap && gap.space !== node[field]) style[field] = gap.space
|
|
509
|
+
if (paragraph.listType === 'NONE' && indents[index] !== node.paragraphIndent) {
|
|
510
|
+
style.paragraphIndent = indents[index]
|
|
511
|
+
}
|
|
512
|
+
return style
|
|
513
|
+
})
|
|
514
|
+
const plain = styles.every(
|
|
515
|
+
(style) => style.listType === 'NONE' && Object.keys(style).length === 2
|
|
516
|
+
)
|
|
517
|
+
node.textParagraphs = plain ? [] : styles
|
|
518
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type * as DesignTypes from './types'
|
|
2
|
+
|
|
3
|
+
export { exportStorybook, STORY_MODES, STORYBOOK_FRAMEWORKS } from './export/storybook/export'
|
|
4
|
+
export { serializeNode } from './export/html'
|
|
5
|
+
export { createBrowserCSSRuntime, createCSSRuntime, createHeadlessCSSRuntime } from './runtime'
|
|
6
|
+
export {
|
|
7
|
+
htmlToDesignDocument,
|
|
8
|
+
htmlToSceneGraph,
|
|
9
|
+
tailwindHTMLToDesignDocument,
|
|
10
|
+
tailwindHTMLToSceneGraph
|
|
11
|
+
} from './import/html'
|
|
12
|
+
export { compileTailwindCSS } from './import/tailwind'
|
|
13
|
+
export {
|
|
14
|
+
jsxToDesignDocument,
|
|
15
|
+
jsxToSceneGraph,
|
|
16
|
+
tailwindJSXToDesignDocument,
|
|
17
|
+
tailwindJSXToSceneGraph
|
|
18
|
+
} from './import/jsx/runtime'
|
|
19
|
+
export type {
|
|
20
|
+
HTMLToDesignDocumentOptions,
|
|
21
|
+
HTMLToSceneGraphOptions,
|
|
22
|
+
TailwindHTMLToDesignDocumentOptions,
|
|
23
|
+
TailwindHTMLToSceneGraphOptions
|
|
24
|
+
} from './import/html'
|
|
25
|
+
export type { BrowserCSSRuntimeOptions } from './runtime'
|
|
26
|
+
export type {
|
|
27
|
+
JSXStyleObject,
|
|
28
|
+
JSXStyleValue,
|
|
29
|
+
JSXToDesignDocumentOptions,
|
|
30
|
+
JSXToSceneGraphOptions,
|
|
31
|
+
TailwindJSXToDesignDocumentOptions,
|
|
32
|
+
TailwindJSXToSceneGraphOptions
|
|
33
|
+
} from './import/jsx/runtime'
|
|
34
|
+
export type { CompileTailwindCSSOptions } from './import/tailwind'
|
|
35
|
+
export type {
|
|
36
|
+
ExportStorybookOptions,
|
|
37
|
+
StorybookFile,
|
|
38
|
+
StorybookFramework,
|
|
39
|
+
StoryMode,
|
|
40
|
+
StoryPlan,
|
|
41
|
+
StoryTarget
|
|
42
|
+
} from './export/storybook/export'
|
|
43
|
+
export type { SerializeHTMLOptions } from './export/html'
|
|
44
|
+
export type CSSComputeOptions = DesignTypes.CSSComputeOptions
|
|
45
|
+
export type CSSRuntime = DesignTypes.CSSRuntime
|
|
46
|
+
export type DesignDocument = DesignTypes.DesignDocument
|
|
47
|
+
export type DesignElement = DesignTypes.DesignElement
|
|
48
|
+
export type DesignNode = DesignTypes.DesignNode
|
|
49
|
+
export type DesignStyleDeclaration = DesignTypes.DesignStyleDeclaration
|
|
50
|
+
export type DesignStyleSheet = DesignTypes.DesignStyleSheet
|
|
51
|
+
export type DesignText = DesignTypes.DesignText
|