@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,1352 @@
|
|
|
1
|
+
import { serializeHTML } from "./behaviours/states/tailwind.js";
|
|
2
|
+
import { isValid, toUint8Array } from "js-base64";
|
|
3
|
+
import { TRANSPARENT } from "@pairdraw/scene-graph/constants";
|
|
4
|
+
import { computeImageHash } from "@pairdraw/scene-graph/images";
|
|
5
|
+
import { autoLayoutSizingFields, fillSizingFields, layoutSizing, textAutoResizeFor } from "@pairdraw/scene-graph/layout";
|
|
6
|
+
import { countBy } from "es-toolkit/array";
|
|
7
|
+
import { isEqual } from "es-toolkit/predicate";
|
|
8
|
+
import { pick } from "es-toolkit";
|
|
9
|
+
import { parseCSSAlignItems, parseCSSAlignSelf, parseCSSColor, parseCSSFlexDirection, parseCSSFontFamily, parseCSSJustifyContent, parseCSSLetterSpacing, parseCSSLineHeight, parseCSSNumber, parseCSSShadows } from "@pairdraw/scene-graph/css";
|
|
10
|
+
import { SceneGraph } from "@pairdraw/scene-graph";
|
|
11
|
+
//#region src/runtime/browser/runtime.ts
|
|
12
|
+
const DEFAULT_COMPUTED_PROPERTIES = [
|
|
13
|
+
"align-items",
|
|
14
|
+
"aspect-ratio",
|
|
15
|
+
"background-color",
|
|
16
|
+
"background-image",
|
|
17
|
+
"border-bottom-color",
|
|
18
|
+
"border-bottom-style",
|
|
19
|
+
"border-bottom-left-radius",
|
|
20
|
+
"border-bottom-right-radius",
|
|
21
|
+
"border-bottom-width",
|
|
22
|
+
"border-left-color",
|
|
23
|
+
"border-left-style",
|
|
24
|
+
"border-left-width",
|
|
25
|
+
"border-radius",
|
|
26
|
+
"border-right-color",
|
|
27
|
+
"border-right-style",
|
|
28
|
+
"border-right-width",
|
|
29
|
+
"border-top-color",
|
|
30
|
+
"border-top-style",
|
|
31
|
+
"border-top-left-radius",
|
|
32
|
+
"border-top-right-radius",
|
|
33
|
+
"border-top-width",
|
|
34
|
+
"box-shadow",
|
|
35
|
+
"box-sizing",
|
|
36
|
+
"color",
|
|
37
|
+
"column-gap",
|
|
38
|
+
"display",
|
|
39
|
+
"align-self",
|
|
40
|
+
"bottom",
|
|
41
|
+
"flex-direction",
|
|
42
|
+
"flex-wrap",
|
|
43
|
+
"font-family",
|
|
44
|
+
"font-size",
|
|
45
|
+
"font-style",
|
|
46
|
+
"font-weight",
|
|
47
|
+
"gap",
|
|
48
|
+
"height",
|
|
49
|
+
"justify-content",
|
|
50
|
+
"letter-spacing",
|
|
51
|
+
"left",
|
|
52
|
+
"line-height",
|
|
53
|
+
"max-height",
|
|
54
|
+
"max-width",
|
|
55
|
+
"min-height",
|
|
56
|
+
"min-width",
|
|
57
|
+
"object-fit",
|
|
58
|
+
"opacity",
|
|
59
|
+
"overflow",
|
|
60
|
+
"padding-bottom",
|
|
61
|
+
"padding-left",
|
|
62
|
+
"padding-right",
|
|
63
|
+
"padding-top",
|
|
64
|
+
"position",
|
|
65
|
+
"right",
|
|
66
|
+
"row-gap",
|
|
67
|
+
"text-align",
|
|
68
|
+
"text-decoration-line",
|
|
69
|
+
"text-shadow",
|
|
70
|
+
"text-transform",
|
|
71
|
+
"top",
|
|
72
|
+
"white-space",
|
|
73
|
+
"width"
|
|
74
|
+
];
|
|
75
|
+
function resolveBrowserDocument$1(documentOverride) {
|
|
76
|
+
if (documentOverride) return documentOverride;
|
|
77
|
+
if (typeof document === "undefined") throw new TypeError("Browser CSS runtime requires a DOM document");
|
|
78
|
+
return document;
|
|
79
|
+
}
|
|
80
|
+
function attributesToRecord(element) {
|
|
81
|
+
const attrs = {};
|
|
82
|
+
for (const attr of Array.from(element.attributes)) {
|
|
83
|
+
if (attr.name.toLowerCase().startsWith("on")) continue;
|
|
84
|
+
attrs[attr.name] = attr.value;
|
|
85
|
+
}
|
|
86
|
+
return attrs;
|
|
87
|
+
}
|
|
88
|
+
function styleToRecord(style) {
|
|
89
|
+
const entries = {};
|
|
90
|
+
for (const property of Array.from(style)) {
|
|
91
|
+
const value = style.getPropertyValue(property);
|
|
92
|
+
if (value) entries[property] = value;
|
|
93
|
+
}
|
|
94
|
+
return Object.keys(entries).length > 0 ? entries : void 0;
|
|
95
|
+
}
|
|
96
|
+
const NON_RENDERED_TAGS = /* @__PURE__ */ new Set([
|
|
97
|
+
"head",
|
|
98
|
+
"link",
|
|
99
|
+
"meta",
|
|
100
|
+
"script",
|
|
101
|
+
"style",
|
|
102
|
+
"template",
|
|
103
|
+
"title"
|
|
104
|
+
]);
|
|
105
|
+
function domNodeToDesignNode(node) {
|
|
106
|
+
if (node.nodeType === 3) {
|
|
107
|
+
const text = node.textContent ?? "";
|
|
108
|
+
return text.length > 0 ? {
|
|
109
|
+
type: "text",
|
|
110
|
+
text
|
|
111
|
+
} : null;
|
|
112
|
+
}
|
|
113
|
+
if (node.nodeType !== 1) return null;
|
|
114
|
+
const element = node;
|
|
115
|
+
if (NON_RENDERED_TAGS.has(element.tagName.toLowerCase())) return null;
|
|
116
|
+
const children = Array.from(element.childNodes).map(domNodeToDesignNode).filter((child) => child !== null);
|
|
117
|
+
const style = "style" in element ? element.style : void 0;
|
|
118
|
+
return {
|
|
119
|
+
type: "element",
|
|
120
|
+
tagName: element.tagName.toLowerCase(),
|
|
121
|
+
attrs: attributesToRecord(element),
|
|
122
|
+
children,
|
|
123
|
+
inlineStyle: style ? styleToRecord(style) : void 0
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function parseHTMLWithDocument(browserDocument, html) {
|
|
127
|
+
const Parser = browserDocument.defaultView?.DOMParser;
|
|
128
|
+
if (!Parser) throw new TypeError("Browser CSS runtime requires DOMParser");
|
|
129
|
+
const parsed = new Parser().parseFromString(html, "text/html");
|
|
130
|
+
return {
|
|
131
|
+
type: "document",
|
|
132
|
+
children: Array.from(parsed.body.childNodes).map(domNodeToDesignNode).filter((node) => node !== null)
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
function collectElementPairs(designNode, domNode, pairs) {
|
|
136
|
+
if (designNode.type === "text") return;
|
|
137
|
+
const view = domNode.ownerDocument?.defaultView;
|
|
138
|
+
if (!view || !(domNode instanceof view.Element)) return;
|
|
139
|
+
pairs.push([designNode, domNode]);
|
|
140
|
+
const elementChildren = designNode.children.filter((child) => child.type === "element");
|
|
141
|
+
const domChildren = Array.from(domNode.children);
|
|
142
|
+
for (const [index, child] of elementChildren.entries()) {
|
|
143
|
+
const domChild = domChildren.at(index);
|
|
144
|
+
if (domChild) collectElementPairs(child, domChild, pairs);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
function computedStyleToRecord(style, options) {
|
|
148
|
+
const entries = {};
|
|
149
|
+
const properties = options.includeBrowserDefaults ? Array.from(style) : DEFAULT_COMPUTED_PROPERTIES;
|
|
150
|
+
for (const property of properties) {
|
|
151
|
+
const value = style.getPropertyValue(property);
|
|
152
|
+
if (value) entries[property] = value;
|
|
153
|
+
}
|
|
154
|
+
return entries;
|
|
155
|
+
}
|
|
156
|
+
function requestFrame(browserDocument) {
|
|
157
|
+
const requestAnimationFrame = browserDocument.defaultView?.requestAnimationFrame;
|
|
158
|
+
if (!requestAnimationFrame) return Promise.resolve();
|
|
159
|
+
return new Promise((resolve) => {
|
|
160
|
+
requestAnimationFrame(() => resolve());
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
function applySandboxHostStyle(element) {
|
|
164
|
+
element.style.cssText = [
|
|
165
|
+
"position: fixed",
|
|
166
|
+
"left: -100000px",
|
|
167
|
+
"top: 0",
|
|
168
|
+
"width: 1000px",
|
|
169
|
+
"height: auto",
|
|
170
|
+
"visibility: hidden",
|
|
171
|
+
"pointer-events: none",
|
|
172
|
+
"contain: layout style paint"
|
|
173
|
+
].join(";");
|
|
174
|
+
}
|
|
175
|
+
async function computeStylesInShadowRoot(browserDocument, designDocument, cssText, options) {
|
|
176
|
+
const host = browserDocument.createElement("div");
|
|
177
|
+
applySandboxHostStyle(host);
|
|
178
|
+
const shadow = host.attachShadow({ mode: "open" });
|
|
179
|
+
const style = browserDocument.createElement("style");
|
|
180
|
+
style.textContent = cssText;
|
|
181
|
+
shadow.append(style);
|
|
182
|
+
const content = browserDocument.createElement("div");
|
|
183
|
+
content.innerHTML = serializeHTML(designDocument);
|
|
184
|
+
shadow.append(content);
|
|
185
|
+
browserDocument.body.append(host);
|
|
186
|
+
try {
|
|
187
|
+
await requestFrame(browserDocument);
|
|
188
|
+
return copyComputedStyles(designDocument, content, options);
|
|
189
|
+
} finally {
|
|
190
|
+
host.remove();
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
async function computeStylesInIframe(browserDocument, designDocument, cssText, options) {
|
|
194
|
+
const iframe = browserDocument.createElement("iframe");
|
|
195
|
+
applySandboxHostStyle(iframe);
|
|
196
|
+
iframe.setAttribute("sandbox", "allow-same-origin");
|
|
197
|
+
browserDocument.body.append(iframe);
|
|
198
|
+
try {
|
|
199
|
+
const iframeDocument = iframe.contentDocument;
|
|
200
|
+
if (!iframeDocument) throw new TypeError("Browser CSS runtime could not create iframe document");
|
|
201
|
+
iframeDocument.open();
|
|
202
|
+
iframeDocument.write(`<!doctype html><html><head></head><body></body></html>`);
|
|
203
|
+
iframeDocument.close();
|
|
204
|
+
const style = iframeDocument.createElement("style");
|
|
205
|
+
style.textContent = cssText;
|
|
206
|
+
iframeDocument.head.append(style);
|
|
207
|
+
const content = iframeDocument.createElement("div");
|
|
208
|
+
content.innerHTML = serializeHTML(designDocument);
|
|
209
|
+
iframeDocument.body.append(content);
|
|
210
|
+
await requestFrame(iframeDocument);
|
|
211
|
+
return copyComputedStyles(designDocument, content, options);
|
|
212
|
+
} finally {
|
|
213
|
+
iframe.remove();
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
function copyComputedStyles(designDocument, content, options) {
|
|
217
|
+
const view = content.ownerDocument.defaultView;
|
|
218
|
+
if (!view) throw new TypeError("Browser CSS runtime requires getComputedStyle");
|
|
219
|
+
const nextDocument = structuredClone(designDocument);
|
|
220
|
+
const pairs = [];
|
|
221
|
+
const domChildren = Array.from(content.childNodes);
|
|
222
|
+
for (const [index, child] of nextDocument.children.entries()) {
|
|
223
|
+
const domChild = domChildren.at(index);
|
|
224
|
+
if (domChild) collectElementPairs(child, domChild, pairs);
|
|
225
|
+
}
|
|
226
|
+
for (const [designElement, domElement] of pairs) designElement.computedStyle = computedStyleToRecord(view.getComputedStyle(domElement), options);
|
|
227
|
+
return nextDocument;
|
|
228
|
+
}
|
|
229
|
+
function createBrowserCSSRuntime(options = {}) {
|
|
230
|
+
const browserDocument = resolveBrowserDocument$1(options.document);
|
|
231
|
+
const sandbox = options.sandbox ?? "shadow-root";
|
|
232
|
+
return {
|
|
233
|
+
kind: "browser",
|
|
234
|
+
parseHTML: (html) => parseHTMLWithDocument(browserDocument, html),
|
|
235
|
+
serializeHTML,
|
|
236
|
+
computeStyles: (designDocument, cssText = "", computeOptions = {}) => sandbox === "iframe" ? computeStylesInIframe(browserDocument, designDocument, cssText, computeOptions) : computeStylesInShadowRoot(browserDocument, designDocument, cssText, computeOptions)
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
//#endregion
|
|
240
|
+
//#region src/import/css-values.ts
|
|
241
|
+
function colorToFillFromCSS(value) {
|
|
242
|
+
const color = parseCSSColor(value);
|
|
243
|
+
if (!color) return [];
|
|
244
|
+
return [{
|
|
245
|
+
type: "SOLID",
|
|
246
|
+
color,
|
|
247
|
+
opacity: color.a,
|
|
248
|
+
visible: true
|
|
249
|
+
}];
|
|
250
|
+
}
|
|
251
|
+
function colorToStrokeFromCSS(colorValue, weightValue) {
|
|
252
|
+
const color = parseCSSColor(colorValue);
|
|
253
|
+
const weight = parseCSSNumber(weightValue);
|
|
254
|
+
if (!color || weight === null || weight <= 0) return [];
|
|
255
|
+
return [{
|
|
256
|
+
type: "SOLID",
|
|
257
|
+
color,
|
|
258
|
+
weight,
|
|
259
|
+
opacity: color.a,
|
|
260
|
+
visible: true,
|
|
261
|
+
align: "INSIDE"
|
|
262
|
+
}];
|
|
263
|
+
}
|
|
264
|
+
function pickStyle(elementStyle, property) {
|
|
265
|
+
return elementStyle?.[property];
|
|
266
|
+
}
|
|
267
|
+
function mergedStyle(node) {
|
|
268
|
+
return {
|
|
269
|
+
...node.inlineStyle,
|
|
270
|
+
...node.computedStyle
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
//#endregion
|
|
274
|
+
//#region src/import/flow.ts
|
|
275
|
+
/** Tags browsers lay out in a line by default. */
|
|
276
|
+
const INLINE_TAGS = /* @__PURE__ */ new Set([
|
|
277
|
+
"a",
|
|
278
|
+
"abbr",
|
|
279
|
+
"b",
|
|
280
|
+
"code",
|
|
281
|
+
"del",
|
|
282
|
+
"em",
|
|
283
|
+
"i",
|
|
284
|
+
"ins",
|
|
285
|
+
"kbd",
|
|
286
|
+
"label",
|
|
287
|
+
"mark",
|
|
288
|
+
"s",
|
|
289
|
+
"small",
|
|
290
|
+
"span",
|
|
291
|
+
"strong",
|
|
292
|
+
"sub",
|
|
293
|
+
"sup",
|
|
294
|
+
"u"
|
|
295
|
+
]);
|
|
296
|
+
const INLINE_BLOCK_TAGS = /* @__PURE__ */ new Set([
|
|
297
|
+
"button",
|
|
298
|
+
"img",
|
|
299
|
+
"input",
|
|
300
|
+
"select",
|
|
301
|
+
"textarea"
|
|
302
|
+
]);
|
|
303
|
+
/** Elements browsers center their text in by default. */
|
|
304
|
+
const CENTERED_TAGS = /* @__PURE__ */ new Set(["button"]);
|
|
305
|
+
/** What a run of text takes from its element: the properties CSS inherits, and decoration. */
|
|
306
|
+
const TEXT_PROPERTIES = [
|
|
307
|
+
"color",
|
|
308
|
+
"font-family",
|
|
309
|
+
"font-size",
|
|
310
|
+
"font-style",
|
|
311
|
+
"font-weight",
|
|
312
|
+
"letter-spacing",
|
|
313
|
+
"line-height",
|
|
314
|
+
"text-align",
|
|
315
|
+
"text-decoration-line",
|
|
316
|
+
"text-shadow",
|
|
317
|
+
"text-transform",
|
|
318
|
+
"white-space"
|
|
319
|
+
];
|
|
320
|
+
/**
|
|
321
|
+
* The style a run of text directly inside an element takes from it. The element's own box,
|
|
322
|
+
* position, and opacity stay on the element, so the text is neither placed nor faded twice.
|
|
323
|
+
*/
|
|
324
|
+
function inheritedTextStyle(element, style) {
|
|
325
|
+
const inherited = pick(style, TEXT_PROPERTIES);
|
|
326
|
+
if (!inherited["text-align"] && CENTERED_TAGS.has(element.tagName.toLowerCase())) inherited["text-align"] = "center";
|
|
327
|
+
return inherited;
|
|
328
|
+
}
|
|
329
|
+
/** The element's `display`, or the one browsers give its tag by default. */
|
|
330
|
+
function elementDisplay(element, style) {
|
|
331
|
+
const display = pickStyle(style, "display")?.trim().toLowerCase();
|
|
332
|
+
if (display) return display;
|
|
333
|
+
const tag = element.tagName.toLowerCase();
|
|
334
|
+
if (INLINE_TAGS.has(tag)) return "inline";
|
|
335
|
+
return INLINE_BLOCK_TAGS.has(tag) ? "inline-block" : "block";
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* The flow an element lays its own children out in. An `inline-block` sits in a line but holds
|
|
339
|
+
* a block of its own, so only a plain `inline` element lays its children out in a line.
|
|
340
|
+
*/
|
|
341
|
+
function flowOf(display) {
|
|
342
|
+
if (display === "flex" || display === "inline-flex") return "flex";
|
|
343
|
+
return display === "inline" ? "inline" : "block";
|
|
344
|
+
}
|
|
345
|
+
/** Whether the element sits in a line rather than on its own row, so it hugs its content. */
|
|
346
|
+
function isInlineLevel(display) {
|
|
347
|
+
return display.startsWith("inline");
|
|
348
|
+
}
|
|
349
|
+
function textAlignAxis(element, style) {
|
|
350
|
+
const align = pickStyle(style, "text-align")?.trim().toLowerCase();
|
|
351
|
+
if (align === "center") return "CENTER";
|
|
352
|
+
if (align === "right" || align === "end") return "MAX";
|
|
353
|
+
if (align) return "MIN";
|
|
354
|
+
return CENTERED_TAGS.has(element.tagName.toLowerCase()) ? "CENTER" : "MIN";
|
|
355
|
+
}
|
|
356
|
+
/** The auto layout an element's own flow gives its children. */
|
|
357
|
+
function applyContainerLayout(node, element, style, flow) {
|
|
358
|
+
if (flow === "flex") {
|
|
359
|
+
node.layoutMode = parseCSSFlexDirection(pickStyle(style, "flex-direction")) ?? "HORIZONTAL";
|
|
360
|
+
node.primaryAxisAlign = parseCSSJustifyContent(pickStyle(style, "justify-content")) ?? "MIN";
|
|
361
|
+
node.counterAxisAlign = parseCSSAlignItems(pickStyle(style, "align-items")) ?? "STRETCH";
|
|
362
|
+
node.layoutWrap = pickStyle(style, "flex-wrap") === "wrap" ? "WRAP" : "NO_WRAP";
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
if (flow === "block") {
|
|
366
|
+
node.layoutMode = "VERTICAL";
|
|
367
|
+
if (CENTERED_TAGS.has(element.tagName.toLowerCase())) {
|
|
368
|
+
node.primaryAxisAlign = "CENTER";
|
|
369
|
+
node.counterAxisAlign = "CENTER";
|
|
370
|
+
}
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
node.layoutMode = "HORIZONTAL";
|
|
374
|
+
node.primaryAxisAlign = textAlignAxis(element, style);
|
|
375
|
+
node.counterAxisAlign = "CENTER";
|
|
376
|
+
}
|
|
377
|
+
function grows(style) {
|
|
378
|
+
const grow = parseCSSNumber(pickStyle(style, "flex-grow"));
|
|
379
|
+
if (grow !== null) return grow > 0;
|
|
380
|
+
const flex = pickStyle(style, "flex")?.trim().split(/\s+/)[0];
|
|
381
|
+
if (flex === "auto") return true;
|
|
382
|
+
const shorthand = parseCSSNumber(flex);
|
|
383
|
+
return shorthand !== null && shorthand > 0;
|
|
384
|
+
}
|
|
385
|
+
function isFullSize(style, property) {
|
|
386
|
+
return pickStyle(style, property)?.trim() === "100%";
|
|
387
|
+
}
|
|
388
|
+
/** Fill from the parent's flow: block children across, growing items along, `100%` either way. */
|
|
389
|
+
function applyParentFill(graph, node, style, display, parentFlow, { fixedWidth, fixedHeight }) {
|
|
390
|
+
const parent = node.parentId ? graph.getNode(node.parentId) : void 0;
|
|
391
|
+
const parentLayout = parent?.layoutMode ?? "NONE";
|
|
392
|
+
if (!parent || parentLayout === "NONE" || node.layoutPositioning === "ABSOLUTE") return;
|
|
393
|
+
const fill = (axis) => Object.assign(node, fillSizingFields(parentLayout, axis));
|
|
394
|
+
const mainAxis = parentLayout === "VERTICAL" ? "VERTICAL" : "HORIZONTAL";
|
|
395
|
+
if (parentFlow === "block" && !isInlineLevel(display) && !fixedWidth && hasDefiniteWidth(graph, parent)) fill("HORIZONTAL");
|
|
396
|
+
if (parentFlow === "flex" && grows(style)) fill(mainAxis);
|
|
397
|
+
if (isFullSize(style, "width")) fill("HORIZONTAL");
|
|
398
|
+
if (isFullSize(style, "height")) fill("VERTICAL");
|
|
399
|
+
if (parentFlow === "flex") keepFixedCrossSize(node, parent, mainAxis, {
|
|
400
|
+
fixedWidth,
|
|
401
|
+
fixedHeight
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
/** An item with its own cross size keeps it in a stretching flex container, as in CSS. */
|
|
405
|
+
function keepFixedCrossSize(node, parent, mainAxis, { fixedWidth, fixedHeight }) {
|
|
406
|
+
const crossFixed = mainAxis === "VERTICAL" ? fixedWidth : fixedHeight;
|
|
407
|
+
const inheritsStretch = node.layoutAlignSelf === "AUTO" && parent.counterAxisAlign === "STRETCH";
|
|
408
|
+
if (crossFixed && inheritsStretch) node.layoutAlignSelf = "MIN";
|
|
409
|
+
}
|
|
410
|
+
function hasDefiniteWidth(graph, node) {
|
|
411
|
+
return layoutSizing(graph, node, "HORIZONTAL") !== "HUG";
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Text wraps where its width is fixed or fills a parent with a width of its own, and hugs its
|
|
415
|
+
* content otherwise. It is measured when the document is laid out.
|
|
416
|
+
*/
|
|
417
|
+
function applyTextSizing(graph, node, size) {
|
|
418
|
+
const parent = node.parentId ? graph.getNode(node.parentId) : void 0;
|
|
419
|
+
const fillsWidth = parent !== void 0 && hasDefiniteWidth(graph, parent) && layoutSizing(graph, node, "HORIZONTAL") === "FILL";
|
|
420
|
+
node.textAutoResize = size.fixedWidth || fillsWidth ? textAutoResizeFor(false, !size.fixedHeight) : "WIDTH_AND_HEIGHT";
|
|
421
|
+
if (!size.fixedWidth) node.width = 0;
|
|
422
|
+
if (!size.fixedHeight) node.height = 0;
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* How a child sizes in its parent's flow, as CSS sizes it: an explicit size is fixed, block-level
|
|
426
|
+
* children of block flow and items of a stretching flex container fill across, a growing flex
|
|
427
|
+
* item fills along, `100%` fills, and everything else hugs. Text that fills its width wraps.
|
|
428
|
+
*/
|
|
429
|
+
function applyFlowSizing(graph, node, style, display, parentFlow) {
|
|
430
|
+
const size = {
|
|
431
|
+
fixedWidth: parseCSSNumber(pickStyle(style, "width")) !== null,
|
|
432
|
+
fixedHeight: parseCSSNumber(pickStyle(style, "height")) !== null
|
|
433
|
+
};
|
|
434
|
+
if (node.layoutMode !== "NONE" && node.layoutMode !== "GRID") {
|
|
435
|
+
const own = (fixed) => fixed ? "FIXED" : "HUG";
|
|
436
|
+
Object.assign(node, autoLayoutSizingFields(node.layoutMode, own(size.fixedWidth), own(size.fixedHeight)));
|
|
437
|
+
}
|
|
438
|
+
applyParentFill(graph, node, style, display, parentFlow, size);
|
|
439
|
+
if (node.type === "TEXT") applyTextSizing(graph, node, size);
|
|
440
|
+
}
|
|
441
|
+
//#endregion
|
|
442
|
+
//#region src/import/css-text.ts
|
|
443
|
+
function mergeCSSText(...parts) {
|
|
444
|
+
const text = parts.map((part) => part?.trim()).filter((part) => !!part);
|
|
445
|
+
return text.length > 0 ? text.join("\n") : void 0;
|
|
446
|
+
}
|
|
447
|
+
//#endregion
|
|
448
|
+
//#region src/import/text.ts
|
|
449
|
+
/**
|
|
450
|
+
* What characters take from the elements around them: the properties CSS inherits, and the
|
|
451
|
+
* decoration, which CSS draws through descendants.
|
|
452
|
+
*/
|
|
453
|
+
const CHARACTER_PROPERTIES = [
|
|
454
|
+
"color",
|
|
455
|
+
"font-family",
|
|
456
|
+
"font-size",
|
|
457
|
+
"font-style",
|
|
458
|
+
"font-weight",
|
|
459
|
+
"letter-spacing",
|
|
460
|
+
"line-height",
|
|
461
|
+
"white-space",
|
|
462
|
+
"text-transform",
|
|
463
|
+
"text-decoration-line",
|
|
464
|
+
"text-decoration-style",
|
|
465
|
+
"text-decoration-color",
|
|
466
|
+
"text-decoration-thickness",
|
|
467
|
+
"text-underline-offset",
|
|
468
|
+
"text-decoration-skip-ink"
|
|
469
|
+
];
|
|
470
|
+
const TEXT_BLOCK_TAGS = /* @__PURE__ */ new Set([
|
|
471
|
+
"p",
|
|
472
|
+
"h1",
|
|
473
|
+
"h2",
|
|
474
|
+
"h3",
|
|
475
|
+
"h4",
|
|
476
|
+
"h5",
|
|
477
|
+
"h6"
|
|
478
|
+
]);
|
|
479
|
+
/** What browsers' default stylesheet gives inline formatting elements that CSS does not set. */
|
|
480
|
+
const FORMATTING_DEFAULTS = {
|
|
481
|
+
b: { "font-weight": "700" },
|
|
482
|
+
strong: { "font-weight": "700" },
|
|
483
|
+
i: { "font-style": "italic" },
|
|
484
|
+
em: { "font-style": "italic" },
|
|
485
|
+
u: { "text-decoration-line": "underline" },
|
|
486
|
+
ins: { "text-decoration-line": "underline" },
|
|
487
|
+
s: { "text-decoration-line": "line-through" },
|
|
488
|
+
del: { "text-decoration-line": "line-through" }
|
|
489
|
+
};
|
|
490
|
+
const LIST_TAGS = /* @__PURE__ */ new Set(["ul", "ol"]);
|
|
491
|
+
/** Lengths CSS gives as a number of pixels, or `null` for keywords such as `auto`. */
|
|
492
|
+
function pixels(value) {
|
|
493
|
+
const trimmed = value?.trim();
|
|
494
|
+
if (!trimmed || !/^-?[\d.]+(px)?$/.test(trimmed)) return null;
|
|
495
|
+
return parseCSSNumber(trimmed);
|
|
496
|
+
}
|
|
497
|
+
function decorationLine(value) {
|
|
498
|
+
if (value?.includes("underline")) return "UNDERLINE";
|
|
499
|
+
if (value?.includes("line-through")) return "STRIKETHROUGH";
|
|
500
|
+
return "NONE";
|
|
501
|
+
}
|
|
502
|
+
function decorationStyle(value) {
|
|
503
|
+
const style = value?.trim().toLowerCase();
|
|
504
|
+
if (style === "dotted") return "DOTTED";
|
|
505
|
+
if (style === "wavy") return "WAVY";
|
|
506
|
+
return "SOLID";
|
|
507
|
+
}
|
|
508
|
+
function textCaseFromCSS(value) {
|
|
509
|
+
if (value === "uppercase") return "UPPER";
|
|
510
|
+
if (value === "lowercase") return "LOWER";
|
|
511
|
+
if (value === "capitalize") return "TITLE";
|
|
512
|
+
return "ORIGINAL";
|
|
513
|
+
}
|
|
514
|
+
/** The character style of CSS declarations, over `base` for what they leave out. */
|
|
515
|
+
function characterStyleFromCSS(style, base) {
|
|
516
|
+
const fills = colorToFillFromCSS(pickStyle(style, "color"));
|
|
517
|
+
const family = parseCSSFontFamily(pickStyle(style, "font-family"));
|
|
518
|
+
const decorationColor = pickStyle(style, "text-decoration-color");
|
|
519
|
+
const decorationFills = parseCSSColor(decorationColor) ? colorToFillFromCSS(decorationColor) : [];
|
|
520
|
+
const line = pickStyle(style, "text-decoration-line");
|
|
521
|
+
const fontStyle = pickStyle(style, "font-style");
|
|
522
|
+
const lineHeight = pickStyle(style, "line-height");
|
|
523
|
+
const parsedLineHeight = parseCSSLineHeight(lineHeight);
|
|
524
|
+
const textTransform = pickStyle(style, "text-transform");
|
|
525
|
+
return {
|
|
526
|
+
fontFamily: family || base.fontFamily,
|
|
527
|
+
fontSize: parseCSSNumber(pickStyle(style, "font-size")) ?? base.fontSize,
|
|
528
|
+
fontWeight: parseCSSNumber(pickStyle(style, "font-weight")) ?? base.fontWeight,
|
|
529
|
+
italic: fontStyle === void 0 ? base.italic : fontStyle === "italic",
|
|
530
|
+
letterSpacing: parseCSSLetterSpacing(pickStyle(style, "letter-spacing")) ?? base.letterSpacing,
|
|
531
|
+
lineHeight: parsedLineHeight === void 0 ? base.lineHeight : parsedLineHeight,
|
|
532
|
+
fills: fills.length > 0 ? fills : base.fills,
|
|
533
|
+
textDecoration: line === void 0 ? base.textDecoration : decorationLine(line),
|
|
534
|
+
textDecorationStyle: pickStyle(style, "text-decoration-style") === void 0 ? base.textDecorationStyle : decorationStyle(pickStyle(style, "text-decoration-style")),
|
|
535
|
+
textDecorationFills: decorationColor === void 0 ? base.textDecorationFills : decorationFills,
|
|
536
|
+
textDecorationThickness: pickStyle(style, "text-decoration-thickness") === void 0 ? base.textDecorationThickness : pixels(pickStyle(style, "text-decoration-thickness")),
|
|
537
|
+
textUnderlineOffset: pickStyle(style, "text-underline-offset") === void 0 ? base.textUnderlineOffset : pixels(pickStyle(style, "text-underline-offset")),
|
|
538
|
+
textDecorationSkipInk: pickStyle(style, "text-decoration-skip-ink") === void 0 ? base.textDecorationSkipInk : pickStyle(style, "text-decoration-skip-ink") !== "none",
|
|
539
|
+
textCase: textTransform === void 0 ? base.textCase : textCaseFromCSS(textTransform)
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
/** What the characters inside `element` take from it and the elements around it. */
|
|
543
|
+
function characterDeclarations(element, around) {
|
|
544
|
+
const own = {
|
|
545
|
+
...FORMATTING_DEFAULTS[element.tagName.toLowerCase()],
|
|
546
|
+
...mergedStyle(element)
|
|
547
|
+
};
|
|
548
|
+
const inherited = {};
|
|
549
|
+
for (const property of CHARACTER_PROPERTIES) {
|
|
550
|
+
const value = pickStyle(own, property) ?? pickStyle(around, property);
|
|
551
|
+
if (value !== void 0) inherited[property] = value;
|
|
552
|
+
}
|
|
553
|
+
return inherited;
|
|
554
|
+
}
|
|
555
|
+
/** Whether the element holds only characters: text, inline elements, and line breaks. */
|
|
556
|
+
function isInlineContent(element, isInlineElement) {
|
|
557
|
+
return element.children.every((child) => child.type === "text" || child.tagName.toLowerCase() === "br" || isInlineElement(child) && isInlineContent(child, isInlineElement));
|
|
558
|
+
}
|
|
559
|
+
function isWhitespace(node) {
|
|
560
|
+
return node.type === "text" && node.text.trim() === "";
|
|
561
|
+
}
|
|
562
|
+
function isList(element, isListPlain, isPlain) {
|
|
563
|
+
if (!LIST_TAGS.has(element.tagName.toLowerCase()) || !isListPlain(element)) return false;
|
|
564
|
+
return element.children.every((child) => isWhitespace(child) || child.type === "element" && child.tagName.toLowerCase() === "li" && isListItem(child));
|
|
565
|
+
function isListItem(item) {
|
|
566
|
+
return item.children.every((child) => child.type === "text" || child.tagName.toLowerCase() === "br" || isList(child, isPlain, isPlain) || !LIST_TAGS.has(child.tagName.toLowerCase()) && isPlain(child));
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* Whether the element holds text in blocks, as Figma keeps it in one layer: it is a list, or
|
|
571
|
+
* holds only paragraphs, headings, and lists of characters with no box of their own.
|
|
572
|
+
*/
|
|
573
|
+
function holdsTextBlocks(element, isPlain, isInlineElement) {
|
|
574
|
+
if (LIST_TAGS.has(element.tagName.toLowerCase())) return isList(element, () => true, isPlain);
|
|
575
|
+
const blocks = element.children.filter((child) => !isWhitespace(child));
|
|
576
|
+
return blocks.length > 0 && blocks.every((child) => child.type === "element" && (isList(child, isPlain, isPlain) || TEXT_BLOCK_TAGS.has(child.tagName.toLowerCase()) && isPlain(child) && isInlineContent(child, isInlineElement)));
|
|
577
|
+
}
|
|
578
|
+
function preservesWhitespace(style) {
|
|
579
|
+
const whiteSpace = pickStyle(style, "white-space");
|
|
580
|
+
return whiteSpace === "pre" || whiteSpace === "pre-wrap" || whiteSpace === "pre-line";
|
|
581
|
+
}
|
|
582
|
+
function appendCharacters(collector, text, style) {
|
|
583
|
+
const styled = collector.link ? {
|
|
584
|
+
...style,
|
|
585
|
+
hyperlink: collector.link
|
|
586
|
+
} : style;
|
|
587
|
+
collector.text += text;
|
|
588
|
+
collector.styles.push(...Array.from({ length: text.length }, () => styled));
|
|
589
|
+
}
|
|
590
|
+
/** Where a link element goes, for an `<a>` with an `href`. */
|
|
591
|
+
function elementLink(element) {
|
|
592
|
+
if (element.tagName.toLowerCase() !== "a" || !Object.hasOwn(element.attrs, "href")) return null;
|
|
593
|
+
const href = element.attrs.href.trim();
|
|
594
|
+
return href ? {
|
|
595
|
+
type: "URL",
|
|
596
|
+
value: href
|
|
597
|
+
} : null;
|
|
598
|
+
}
|
|
599
|
+
/**
|
|
600
|
+
* Characters of `text` in `style`. A line break inside a block starts a paragraph of the
|
|
601
|
+
* block's kind, as text in Figma breaks into paragraphs at every newline.
|
|
602
|
+
*/
|
|
603
|
+
function addCharacters(collector, text, style) {
|
|
604
|
+
const value = preservesWhitespace(style) ? text : text.replaceAll(/\s+/g, " ");
|
|
605
|
+
const characterStyle = characterStyleFromCSS(style, collector.base);
|
|
606
|
+
for (const [index, line] of value.split("\n").entries()) {
|
|
607
|
+
if (index > 0) {
|
|
608
|
+
const paragraph = collector.paragraphs.at(-1);
|
|
609
|
+
if (paragraph) {
|
|
610
|
+
collector.spaceAfter.push(0);
|
|
611
|
+
collector.paragraphs.push({ ...paragraph });
|
|
612
|
+
collector.indents.push(0);
|
|
613
|
+
}
|
|
614
|
+
appendCharacters(collector, "\n", characterStyle);
|
|
615
|
+
}
|
|
616
|
+
appendCharacters(collector, line, characterStyle);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
function addInline(collector, nodes, style) {
|
|
620
|
+
for (const node of nodes) if (node.type === "text") addCharacters(collector, node.text, style);
|
|
621
|
+
else if (isBreak(node)) addCharacters(collector, "\n", {
|
|
622
|
+
...style,
|
|
623
|
+
"white-space": "pre"
|
|
624
|
+
});
|
|
625
|
+
else if (!LIST_TAGS.has(node.tagName.toLowerCase())) {
|
|
626
|
+
const around = collector.link;
|
|
627
|
+
collector.link = elementLink(node) ?? around;
|
|
628
|
+
addInline(collector, node.children, characterDeclarations(node, style));
|
|
629
|
+
collector.link = around;
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
function isBreak(node) {
|
|
633
|
+
return node.type === "element" && node.tagName.toLowerCase() === "br";
|
|
634
|
+
}
|
|
635
|
+
function margin(element, side) {
|
|
636
|
+
return Math.max(0, pixels(pickStyle(mergedStyle(element), `margin-${side}`)) ?? 0);
|
|
637
|
+
}
|
|
638
|
+
/** Starts a paragraph, ending the one before with the space collected since it. */
|
|
639
|
+
function startParagraph(collector, style, indent) {
|
|
640
|
+
if (collector.paragraphs.length > 0) {
|
|
641
|
+
collector.spaceAfter.push(collector.pending);
|
|
642
|
+
appendCharacters(collector, "\n", collector.styles.at(-1) ?? collector.base);
|
|
643
|
+
}
|
|
644
|
+
collector.pending = 0;
|
|
645
|
+
collector.paragraphs.push(style);
|
|
646
|
+
collector.indents.push(indent);
|
|
647
|
+
}
|
|
648
|
+
/** A paragraph's characters, trimmed of the spaces that CSS drops at a block's edges. */
|
|
649
|
+
function addParagraph(collector, nodes, style, paragraph, indent) {
|
|
650
|
+
startParagraph(collector, paragraph, indent);
|
|
651
|
+
addTrimmed(collector, isBreak(nodes.at(-1) ?? {
|
|
652
|
+
type: "text",
|
|
653
|
+
text: ""
|
|
654
|
+
}) ? nodes.slice(0, -1) : nodes, style);
|
|
655
|
+
}
|
|
656
|
+
/** Inline content, trimmed of the spaces CSS drops at a block's edges unless it keeps them. */
|
|
657
|
+
function addTrimmed(collector, nodes, style) {
|
|
658
|
+
const start = collector.text.length;
|
|
659
|
+
addInline(collector, nodes, style);
|
|
660
|
+
if (preservesWhitespace(style)) return;
|
|
661
|
+
const added = collector.text.slice(start);
|
|
662
|
+
const leading = added.length - added.trimStart().length;
|
|
663
|
+
const trimmed = added.trim();
|
|
664
|
+
collector.text = collector.text.slice(0, start) + trimmed;
|
|
665
|
+
collector.styles = [...collector.styles.slice(0, start), ...collector.styles.slice(start + leading, start + leading + trimmed.length)];
|
|
666
|
+
}
|
|
667
|
+
function addList(collector, list, around, level) {
|
|
668
|
+
const listStyle = characterDeclarations(list, around);
|
|
669
|
+
collector.pending = Math.max(collector.pending, margin(list, "top"));
|
|
670
|
+
if (level === 1 && collector.hangingList === void 0) collector.hangingList = pixels(pickStyle(mergedStyle(list), "padding-inline-start")) === 0;
|
|
671
|
+
const listType = list.tagName.toLowerCase() === "ol" ? "ORDERED" : "UNORDERED";
|
|
672
|
+
for (const item of list.children) {
|
|
673
|
+
if (item.type !== "element") continue;
|
|
674
|
+
const itemStyle = characterDeclarations(item, listStyle);
|
|
675
|
+
const inline = item.children.filter((child) => child.type === "text" || !LIST_TAGS.has(child.tagName.toLowerCase()));
|
|
676
|
+
collector.pending = Math.max(collector.pending, margin(item, "top"));
|
|
677
|
+
if (inline.some((child) => !isWhitespace(child))) addParagraph(collector, inline, itemStyle, {
|
|
678
|
+
listType,
|
|
679
|
+
indentation: level
|
|
680
|
+
}, 0);
|
|
681
|
+
for (const child of item.children) if (child.type === "element" && LIST_TAGS.has(child.tagName.toLowerCase())) addList(collector, child, itemStyle, level + 1);
|
|
682
|
+
collector.pending = Math.max(collector.pending, margin(item, "bottom"));
|
|
683
|
+
}
|
|
684
|
+
collector.pending = Math.max(collector.pending, margin(list, "bottom"));
|
|
685
|
+
}
|
|
686
|
+
/** Runs of characters whose style differs from the text's own. */
|
|
687
|
+
function styleRunsFrom(styles, base) {
|
|
688
|
+
const runs = [];
|
|
689
|
+
for (const [index, style] of styles.entries()) {
|
|
690
|
+
const override = {};
|
|
691
|
+
for (const key of Object.keys(style)) {
|
|
692
|
+
const own = key === "hyperlink" ? void 0 : base[key];
|
|
693
|
+
if (!isEqual(style[key], own)) Object.assign(override, { [key]: style[key] });
|
|
694
|
+
}
|
|
695
|
+
const previous = runs.at(-1);
|
|
696
|
+
if (previous && previous.start + previous.length === index && isEqual(previous.style, override)) previous.length++;
|
|
697
|
+
else if (Object.keys(override).length > 0) runs.push({
|
|
698
|
+
start: index,
|
|
699
|
+
length: 1,
|
|
700
|
+
style: override
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
return runs;
|
|
704
|
+
}
|
|
705
|
+
function richTextFrom(collector) {
|
|
706
|
+
return {
|
|
707
|
+
text: collector.text,
|
|
708
|
+
styleRuns: styleRunsFrom(collector.styles, collector.base),
|
|
709
|
+
textParagraphs: collector.paragraphs,
|
|
710
|
+
spaceAfter: collector.spaceAfter,
|
|
711
|
+
indents: collector.indents,
|
|
712
|
+
hangingList: collector.hangingList ?? false
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
function collector(base, link) {
|
|
716
|
+
return {
|
|
717
|
+
base,
|
|
718
|
+
text: "",
|
|
719
|
+
styles: [],
|
|
720
|
+
link,
|
|
721
|
+
paragraphs: [],
|
|
722
|
+
spaceAfter: [],
|
|
723
|
+
indents: [],
|
|
724
|
+
pending: 0,
|
|
725
|
+
hangingList: void 0
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
/** An element of inline content as one paragraph of text, its spans as runs. */
|
|
729
|
+
function inlineRichText(element, style, base) {
|
|
730
|
+
const text = collector(base, elementLink(element));
|
|
731
|
+
addTrimmed(text, element.children, characterDeclarations(element, style));
|
|
732
|
+
return richTextFrom(text);
|
|
733
|
+
}
|
|
734
|
+
/** An element of text blocks as paragraphs of one text, its spans as runs. */
|
|
735
|
+
function blockRichText(element, style, base) {
|
|
736
|
+
const text = collector(base, elementLink(element));
|
|
737
|
+
const around = characterDeclarations(element, style);
|
|
738
|
+
const blocks = LIST_TAGS.has(element.tagName.toLowerCase()) ? [element] : element.children;
|
|
739
|
+
for (const block of blocks) {
|
|
740
|
+
if (block.type !== "element") continue;
|
|
741
|
+
if (LIST_TAGS.has(block.tagName.toLowerCase())) {
|
|
742
|
+
addList(text, block, around, 1);
|
|
743
|
+
continue;
|
|
744
|
+
}
|
|
745
|
+
text.pending = Math.max(text.pending, margin(block, "top"));
|
|
746
|
+
const blockStyle = characterDeclarations(block, around);
|
|
747
|
+
const indent = Math.max(0, pixels(pickStyle(mergedStyle(block), "text-indent")) ?? 0);
|
|
748
|
+
addParagraph(text, block.children, blockStyle, {
|
|
749
|
+
listType: "NONE",
|
|
750
|
+
indentation: 0
|
|
751
|
+
}, indent);
|
|
752
|
+
text.pending = Math.max(text.pending, margin(block, "bottom"));
|
|
753
|
+
}
|
|
754
|
+
return richTextFrom(text);
|
|
755
|
+
}
|
|
756
|
+
/** The value most gaps of a kind share, which the text holds; the others are the paragraphs'. */
|
|
757
|
+
function sharedValue(values) {
|
|
758
|
+
const counts = countBy(values, String);
|
|
759
|
+
const shared = Object.entries(counts).toSorted((a, b) => b[1] - a[1]).at(0);
|
|
760
|
+
return shared ? Number(shared[0]) : 0;
|
|
761
|
+
}
|
|
762
|
+
/** Paragraph styles with the spacing and indents of the text's blocks, by Figma's rules. */
|
|
763
|
+
function applyParagraphs(node, rich) {
|
|
764
|
+
const { textParagraphs: paragraphs, spaceAfter, indents } = rich;
|
|
765
|
+
const betweenItems = (index) => paragraphs[index]?.listType !== "NONE" && paragraphs[index + 1]?.listType !== "NONE";
|
|
766
|
+
const gaps = spaceAfter.map((space, index) => ({
|
|
767
|
+
space,
|
|
768
|
+
list: betweenItems(index)
|
|
769
|
+
}));
|
|
770
|
+
node.paragraphSpacing = sharedValue(gaps.filter((gap) => !gap.list).map((gap) => gap.space));
|
|
771
|
+
node.listSpacing = sharedValue(gaps.filter((gap) => gap.list).map((gap) => gap.space));
|
|
772
|
+
node.paragraphIndent = sharedValue(indents.filter((_, index) => paragraphs[index]?.listType === "NONE"));
|
|
773
|
+
node.hangingList = rich.hangingList;
|
|
774
|
+
const styles = paragraphs.map((paragraph, index) => {
|
|
775
|
+
const style = { ...paragraph };
|
|
776
|
+
const gap = gaps.at(index);
|
|
777
|
+
const field = gap?.list ? "listSpacing" : "paragraphSpacing";
|
|
778
|
+
if (gap && gap.space !== node[field]) style[field] = gap.space;
|
|
779
|
+
if (paragraph.listType === "NONE" && indents[index] !== node.paragraphIndent) style.paragraphIndent = indents[index];
|
|
780
|
+
return style;
|
|
781
|
+
});
|
|
782
|
+
node.textParagraphs = styles.every((style) => style.listType === "NONE" && Object.keys(style).length === 2) ? [] : styles;
|
|
783
|
+
}
|
|
784
|
+
//#endregion
|
|
785
|
+
//#region src/import/scene-graph.ts
|
|
786
|
+
const DOM_CSS_PLUGIN_ID = "pairdraw-dom-css";
|
|
787
|
+
const IMAGE_SOURCE_URL_KEY = "image-source-url";
|
|
788
|
+
function textContent(node) {
|
|
789
|
+
if (node.type === "text") return node.text;
|
|
790
|
+
return node.children.map(textContent).join("");
|
|
791
|
+
}
|
|
792
|
+
function isTextLikeElement(node) {
|
|
793
|
+
return [
|
|
794
|
+
"span",
|
|
795
|
+
"p",
|
|
796
|
+
"label",
|
|
797
|
+
"strong",
|
|
798
|
+
"em",
|
|
799
|
+
"button",
|
|
800
|
+
"a",
|
|
801
|
+
"h1",
|
|
802
|
+
"h2",
|
|
803
|
+
"h3",
|
|
804
|
+
"h4",
|
|
805
|
+
"h5",
|
|
806
|
+
"h6"
|
|
807
|
+
].includes(node.tagName.toLowerCase());
|
|
808
|
+
}
|
|
809
|
+
function firstCSSNumber(style, ...properties) {
|
|
810
|
+
for (const property of properties) {
|
|
811
|
+
const parsed = parseCSSNumber(pickStyle(style, property));
|
|
812
|
+
if (parsed !== null) return parsed;
|
|
813
|
+
}
|
|
814
|
+
return null;
|
|
815
|
+
}
|
|
816
|
+
function fillsFromStyle(style, property) {
|
|
817
|
+
return colorToFillFromCSS(pickStyle(style, property));
|
|
818
|
+
}
|
|
819
|
+
function aspectRatioFromCSS(value) {
|
|
820
|
+
if (!value || value === "auto") return null;
|
|
821
|
+
const parts = value.split("/").map((part) => Number.parseFloat(part.trim())).filter(Number.isFinite);
|
|
822
|
+
if (parts.length === 1 && parts[0] > 0) return parts[0];
|
|
823
|
+
if (parts.length === 2 && parts[0] > 0 && parts[1] > 0) return parts[0] / parts[1];
|
|
824
|
+
return null;
|
|
825
|
+
}
|
|
826
|
+
function setNodeBox(node, style) {
|
|
827
|
+
const width = firstCSSNumber(style, "width");
|
|
828
|
+
const height = firstCSSNumber(style, "height");
|
|
829
|
+
const minWidth = firstCSSNumber(style, "min-width");
|
|
830
|
+
const maxWidth = firstCSSNumber(style, "max-width");
|
|
831
|
+
const minHeight = firstCSSNumber(style, "min-height");
|
|
832
|
+
const maxHeight = firstCSSNumber(style, "max-height");
|
|
833
|
+
const aspectRatio = aspectRatioFromCSS(pickStyle(style, "aspect-ratio"));
|
|
834
|
+
if (width !== null) node.width = width;
|
|
835
|
+
if (height !== null) node.height = height;
|
|
836
|
+
if (height === null && width !== null && aspectRatio !== null) node.height = width / aspectRatio;
|
|
837
|
+
if (width === null && height !== null && aspectRatio !== null) node.width = height * aspectRatio;
|
|
838
|
+
if (minWidth !== null) node.minWidth = minWidth;
|
|
839
|
+
if (maxWidth !== null) node.maxWidth = maxWidth;
|
|
840
|
+
if (minHeight !== null) node.minHeight = minHeight;
|
|
841
|
+
if (maxHeight !== null) node.maxHeight = maxHeight;
|
|
842
|
+
}
|
|
843
|
+
function firstStrokeColor(style) {
|
|
844
|
+
return pickStyle(style, "border-color") ?? pickStyle(style, "border-top-color") ?? pickStyle(style, "border-right-color") ?? pickStyle(style, "border-bottom-color") ?? pickStyle(style, "border-left-color");
|
|
845
|
+
}
|
|
846
|
+
function strokeWeightFromStyle(style) {
|
|
847
|
+
const borderWidth = firstCSSNumber(style, "border-width");
|
|
848
|
+
if (borderWidth !== null) return borderWidth;
|
|
849
|
+
const sideWeights = [
|
|
850
|
+
firstCSSNumber(style, "border-top-width"),
|
|
851
|
+
firstCSSNumber(style, "border-right-width"),
|
|
852
|
+
firstCSSNumber(style, "border-bottom-width"),
|
|
853
|
+
firstCSSNumber(style, "border-left-width")
|
|
854
|
+
].filter((weight) => weight !== null);
|
|
855
|
+
if (sideWeights.length === 0) return null;
|
|
856
|
+
return Math.max(...sideWeights);
|
|
857
|
+
}
|
|
858
|
+
function borderStyleFromCSS(style) {
|
|
859
|
+
return pickStyle(style, "border-style") ?? pickStyle(style, "border-top-style") ?? pickStyle(style, "border-right-style") ?? pickStyle(style, "border-bottom-style") ?? pickStyle(style, "border-left-style");
|
|
860
|
+
}
|
|
861
|
+
function dashPatternFromCSS(style, strokeWeight) {
|
|
862
|
+
const borderStyle = borderStyleFromCSS(style);
|
|
863
|
+
if (borderStyle === "dashed") return [strokeWeight * 3, strokeWeight * 2];
|
|
864
|
+
if (borderStyle === "dotted") return [strokeWeight, strokeWeight];
|
|
865
|
+
return [];
|
|
866
|
+
}
|
|
867
|
+
function setBorderWeights(node, style, stroke) {
|
|
868
|
+
const top = firstCSSNumber(style, "border-top-width") ?? stroke.weight;
|
|
869
|
+
const right = firstCSSNumber(style, "border-right-width") ?? stroke.weight;
|
|
870
|
+
const bottom = firstCSSNumber(style, "border-bottom-width") ?? stroke.weight;
|
|
871
|
+
const left = firstCSSNumber(style, "border-left-width") ?? stroke.weight;
|
|
872
|
+
node.borderTopWeight = top;
|
|
873
|
+
node.borderRightWeight = right;
|
|
874
|
+
node.borderBottomWeight = bottom;
|
|
875
|
+
node.borderLeftWeight = left;
|
|
876
|
+
node.independentStrokeWeights = [
|
|
877
|
+
top,
|
|
878
|
+
right,
|
|
879
|
+
bottom,
|
|
880
|
+
left
|
|
881
|
+
].some((weight) => weight !== stroke.weight);
|
|
882
|
+
}
|
|
883
|
+
function applyCornerRadii(node, style) {
|
|
884
|
+
const cornerRadius = firstCSSNumber(style, "border-radius");
|
|
885
|
+
const topLeft = firstCSSNumber(style, "border-top-left-radius");
|
|
886
|
+
const topRight = firstCSSNumber(style, "border-top-right-radius");
|
|
887
|
+
const bottomRight = firstCSSNumber(style, "border-bottom-right-radius");
|
|
888
|
+
const bottomLeft = firstCSSNumber(style, "border-bottom-left-radius");
|
|
889
|
+
if (cornerRadius !== null) node.cornerRadius = cornerRadius;
|
|
890
|
+
if (topLeft === null && topRight === null && bottomRight === null && bottomLeft === null) return;
|
|
891
|
+
const fallback = cornerRadius ?? 0;
|
|
892
|
+
node.topLeftRadius = topLeft ?? fallback;
|
|
893
|
+
node.topRightRadius = topRight ?? fallback;
|
|
894
|
+
node.bottomRightRadius = bottomRight ?? fallback;
|
|
895
|
+
node.bottomLeftRadius = bottomLeft ?? fallback;
|
|
896
|
+
node.independentCorners = [
|
|
897
|
+
node.topLeftRadius,
|
|
898
|
+
node.topRightRadius,
|
|
899
|
+
node.bottomRightRadius,
|
|
900
|
+
node.bottomLeftRadius
|
|
901
|
+
].some((radius) => radius !== fallback);
|
|
902
|
+
}
|
|
903
|
+
function applyFlexGap(node, style) {
|
|
904
|
+
const gap = firstCSSNumber(style, "gap");
|
|
905
|
+
const rowGap = firstCSSNumber(style, "row-gap");
|
|
906
|
+
const columnGap = firstCSSNumber(style, "column-gap");
|
|
907
|
+
const isHorizontal = node.layoutMode === "HORIZONTAL";
|
|
908
|
+
const isWrapped = node.layoutWrap === "WRAP";
|
|
909
|
+
node.itemSpacing = (isHorizontal ? columnGap : rowGap) ?? gap ?? 0;
|
|
910
|
+
node.counterAxisSpacing = (isHorizontal ? rowGap : columnGap) ?? (isWrapped ? gap ?? 0 : 0);
|
|
911
|
+
}
|
|
912
|
+
function applyPositioning(node, style) {
|
|
913
|
+
const position = pickStyle(style, "position");
|
|
914
|
+
if (position === "absolute" || position === "fixed") node.layoutPositioning = "ABSOLUTE";
|
|
915
|
+
const left = firstCSSNumber(style, "left");
|
|
916
|
+
const top = firstCSSNumber(style, "top");
|
|
917
|
+
if (left !== null) node.x = left;
|
|
918
|
+
if (top !== null) node.y = top;
|
|
919
|
+
}
|
|
920
|
+
/**
|
|
921
|
+
* What CSS adds outside an explicit size: padding and borders, unless `box-sizing: border-box`
|
|
922
|
+
* puts them inside it as auto layout does.
|
|
923
|
+
*/
|
|
924
|
+
function contentBoxExtras(style) {
|
|
925
|
+
if (pickStyle(style, "box-sizing")?.trim() === "border-box") return {
|
|
926
|
+
width: 0,
|
|
927
|
+
height: 0
|
|
928
|
+
};
|
|
929
|
+
const borderStyle = borderStyleFromCSS(style);
|
|
930
|
+
const bordered = borderStyle !== void 0 && borderStyle !== "none" && borderStyle !== "hidden";
|
|
931
|
+
const side = (name, axis) => (firstCSSNumber(style, `padding-${name}`, `padding-${axis}`, "padding") ?? 0) + (bordered ? firstCSSNumber(style, `border-${name}-width`, "border-width") ?? 0 : 0);
|
|
932
|
+
return {
|
|
933
|
+
width: side("left", "inline") + side("right", "inline"),
|
|
934
|
+
height: side("top", "block") + side("bottom", "block")
|
|
935
|
+
};
|
|
936
|
+
}
|
|
937
|
+
/** Explicit sizes count the padding and borders CSS places outside them. */
|
|
938
|
+
function applyBoxSizing(node, style) {
|
|
939
|
+
const extras = contentBoxExtras(style);
|
|
940
|
+
const width = firstCSSNumber(style, "width");
|
|
941
|
+
const height = firstCSSNumber(style, "height");
|
|
942
|
+
const ratio = aspectRatioFromCSS(pickStyle(style, "aspect-ratio")) !== null;
|
|
943
|
+
if (extras.width > 0) {
|
|
944
|
+
if (width !== null || ratio && height !== null) node.width += extras.width;
|
|
945
|
+
if (node.minWidth !== null) node.minWidth += extras.width;
|
|
946
|
+
if (node.maxWidth !== null) node.maxWidth += extras.width;
|
|
947
|
+
}
|
|
948
|
+
if (extras.height > 0) {
|
|
949
|
+
if (height !== null || ratio && width !== null) node.height += extras.height;
|
|
950
|
+
if (node.minHeight !== null) node.minHeight += extras.height;
|
|
951
|
+
if (node.maxHeight !== null) node.maxHeight += extras.height;
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
function applyPadding(node, style) {
|
|
955
|
+
node.paddingTop = firstCSSNumber(style, "padding-top", "padding-block", "padding") ?? 0;
|
|
956
|
+
node.paddingRight = firstCSSNumber(style, "padding-right", "padding-inline", "padding") ?? 0;
|
|
957
|
+
node.paddingBottom = firstCSSNumber(style, "padding-bottom", "padding-block", "padding") ?? 0;
|
|
958
|
+
node.paddingLeft = firstCSSNumber(style, "padding-left", "padding-inline", "padding") ?? 0;
|
|
959
|
+
}
|
|
960
|
+
function imageScaleModeFromObjectFit(value) {
|
|
961
|
+
if (value === "contain" || value === "scale-down") return "FIT";
|
|
962
|
+
if (value === "cover") return "FILL";
|
|
963
|
+
return null;
|
|
964
|
+
}
|
|
965
|
+
function bytesFromDataURL(value) {
|
|
966
|
+
if (!value?.startsWith("data:")) return null;
|
|
967
|
+
const commaIndex = value.indexOf(",");
|
|
968
|
+
if (commaIndex === -1) return null;
|
|
969
|
+
const metadata = value.slice(0, commaIndex);
|
|
970
|
+
const body = value.slice(commaIndex + 1);
|
|
971
|
+
if (!metadata.endsWith(";base64") || !isValid(body)) return null;
|
|
972
|
+
return toUint8Array(body);
|
|
973
|
+
}
|
|
974
|
+
function applyImageFill(graph, node, element, style) {
|
|
975
|
+
if (element.tagName.toLowerCase() !== "img") return;
|
|
976
|
+
const source = element.attrs.src;
|
|
977
|
+
const bytes = bytesFromDataURL(source);
|
|
978
|
+
if (!bytes) {
|
|
979
|
+
if (source) node.pluginData.push({
|
|
980
|
+
pluginId: DOM_CSS_PLUGIN_ID,
|
|
981
|
+
key: IMAGE_SOURCE_URL_KEY,
|
|
982
|
+
value: source
|
|
983
|
+
});
|
|
984
|
+
return;
|
|
985
|
+
}
|
|
986
|
+
const imageHash = computeImageHash(bytes);
|
|
987
|
+
graph.images.set(imageHash, bytes);
|
|
988
|
+
node.fills = [{
|
|
989
|
+
type: "IMAGE",
|
|
990
|
+
imageHash,
|
|
991
|
+
imageScaleMode: imageScaleModeFromObjectFit(pickStyle(style, "object-fit")) ?? "FILL",
|
|
992
|
+
color: TRANSPARENT,
|
|
993
|
+
opacity: 1,
|
|
994
|
+
visible: true
|
|
995
|
+
}];
|
|
996
|
+
}
|
|
997
|
+
function applyElementStyle(graph, node, element, style) {
|
|
998
|
+
setNodeBox(node, style);
|
|
999
|
+
applyBoxSizing(node, style);
|
|
1000
|
+
applyPositioning(node, style);
|
|
1001
|
+
applyPadding(node, style);
|
|
1002
|
+
const fills = fillsFromStyle(style, "background-color");
|
|
1003
|
+
if (fills.length > 0) node.fills = fills;
|
|
1004
|
+
applyImageFill(graph, node, element, style);
|
|
1005
|
+
const strokes = colorToStrokeFromCSS(firstStrokeColor(style), strokeWeightFromStyle(style)?.toString());
|
|
1006
|
+
if (strokes.length > 0) {
|
|
1007
|
+
const dashPattern = dashPatternFromCSS(style, strokes[0].weight);
|
|
1008
|
+
if (dashPattern.length > 0) {
|
|
1009
|
+
strokes[0].dashPattern = dashPattern;
|
|
1010
|
+
node.dashPattern = dashPattern;
|
|
1011
|
+
}
|
|
1012
|
+
node.strokes = strokes;
|
|
1013
|
+
setBorderWeights(node, style, strokes[0]);
|
|
1014
|
+
}
|
|
1015
|
+
const effects = parseCSSShadows(pickStyle(style, "box-shadow"));
|
|
1016
|
+
if (effects.length > 0) node.effects = effects;
|
|
1017
|
+
const opacity = parseCSSNumber(pickStyle(style, "opacity"));
|
|
1018
|
+
if (opacity !== null) node.opacity = opacity;
|
|
1019
|
+
applyCornerRadii(node, style);
|
|
1020
|
+
const overflow = pickStyle(style, "overflow");
|
|
1021
|
+
if (overflow === "hidden" || overflow === "clip") node.clipsContent = true;
|
|
1022
|
+
const alignSelf = parseCSSAlignSelf(pickStyle(style, "align-self")) ?? "AUTO";
|
|
1023
|
+
if (alignSelf !== "AUTO") node.layoutAlignSelf = alignSelf;
|
|
1024
|
+
}
|
|
1025
|
+
const CHARACTER_FIELDS = [
|
|
1026
|
+
"fontFamily",
|
|
1027
|
+
"fontSize",
|
|
1028
|
+
"fontWeight",
|
|
1029
|
+
"italic",
|
|
1030
|
+
"letterSpacing",
|
|
1031
|
+
"lineHeight",
|
|
1032
|
+
"fills",
|
|
1033
|
+
"textDecoration",
|
|
1034
|
+
"textDecorationStyle",
|
|
1035
|
+
"textDecorationThickness",
|
|
1036
|
+
"textDecorationFills",
|
|
1037
|
+
"textDecorationSkipInk",
|
|
1038
|
+
"textUnderlineOffset",
|
|
1039
|
+
"textCase"
|
|
1040
|
+
];
|
|
1041
|
+
function applyTextStyle(node, style) {
|
|
1042
|
+
setNodeBox(node, style);
|
|
1043
|
+
applyPositioning(node, style);
|
|
1044
|
+
Object.assign(node, characterStyleFromCSS(style, pick(node, CHARACTER_FIELDS)));
|
|
1045
|
+
const opacity = parseCSSNumber(pickStyle(style, "opacity"));
|
|
1046
|
+
if (opacity !== null) node.opacity = opacity;
|
|
1047
|
+
const effects = parseCSSShadows(pickStyle(style, "text-shadow"));
|
|
1048
|
+
if (effects.length > 0) node.effects = effects;
|
|
1049
|
+
const textAlign = pickStyle(style, "text-align")?.toUpperCase();
|
|
1050
|
+
if (textAlign === "CENTER" || textAlign === "RIGHT" || textAlign === "JUSTIFIED") node.textAlignHorizontal = textAlign;
|
|
1051
|
+
if (pickStyle(style, "white-space") === "nowrap") node.maxLines = 1;
|
|
1052
|
+
}
|
|
1053
|
+
/**
|
|
1054
|
+
* Text and inline elements, or blocks of text, as one text layer: spans become runs, blocks
|
|
1055
|
+
* paragraphs, and lists list paragraphs, as Figma holds rich text.
|
|
1056
|
+
*/
|
|
1057
|
+
function createRichTextNode(graph, parentId, element, style, display, parentFlow) {
|
|
1058
|
+
const node = graph.createNode("TEXT", parentId, { name: "Text" });
|
|
1059
|
+
applyTextStyle(node, style);
|
|
1060
|
+
const blocks = holdsTextBlocks(element, isPlainElement, isInlineElement);
|
|
1061
|
+
const base = pick(node, CHARACTER_FIELDS);
|
|
1062
|
+
const rich = blocks ? blockRichText(element, style, base) : inlineRichText(element, style, base);
|
|
1063
|
+
node.text = rich.text;
|
|
1064
|
+
node.name = rich.text.slice(0, 32) || "Text";
|
|
1065
|
+
node.styleRuns = rich.styleRuns;
|
|
1066
|
+
if (blocks) applyParagraphs(node, rich);
|
|
1067
|
+
applyFlowSizing(graph, node, style, display, parentFlow);
|
|
1068
|
+
return node;
|
|
1069
|
+
}
|
|
1070
|
+
/** Whether an element draws nothing of its own, so its text can join the text around it. */
|
|
1071
|
+
function isPlainElement(element) {
|
|
1072
|
+
return !hasBoxStyle(mergedStyle(element));
|
|
1073
|
+
}
|
|
1074
|
+
function isInlineElement(element) {
|
|
1075
|
+
const style = mergedStyle(element);
|
|
1076
|
+
return elementDisplay(element, style) === "inline" && !hasBoxStyle(style);
|
|
1077
|
+
}
|
|
1078
|
+
/** Whether the element's children are text in one layer: inline content, or text blocks. */
|
|
1079
|
+
function holdsRichText(element) {
|
|
1080
|
+
if (!element.children.some((child) => child.type === "element")) return false;
|
|
1081
|
+
if (holdsTextBlocks(element, isPlainElement, isInlineElement)) return true;
|
|
1082
|
+
return isInlineContent(element, isInlineElement);
|
|
1083
|
+
}
|
|
1084
|
+
function createTextNode(graph, parentId, text, style, display, parentFlow) {
|
|
1085
|
+
const node = graph.createNode("TEXT", parentId, {
|
|
1086
|
+
name: text.slice(0, 32) || "Text",
|
|
1087
|
+
text
|
|
1088
|
+
});
|
|
1089
|
+
applyTextStyle(node, style);
|
|
1090
|
+
applyFlowSizing(graph, node, style, display, parentFlow);
|
|
1091
|
+
return node;
|
|
1092
|
+
}
|
|
1093
|
+
/** A link of text alone links all of it. */
|
|
1094
|
+
function linkText(node, element) {
|
|
1095
|
+
const hyperlink = elementLink(element);
|
|
1096
|
+
if (hyperlink && node.text.length > 0) node.styleRuns = [{
|
|
1097
|
+
start: 0,
|
|
1098
|
+
length: node.text.length,
|
|
1099
|
+
style: { hyperlink }
|
|
1100
|
+
}];
|
|
1101
|
+
}
|
|
1102
|
+
function hasBoxStyle(style) {
|
|
1103
|
+
return [
|
|
1104
|
+
"aspect-ratio",
|
|
1105
|
+
"background-color",
|
|
1106
|
+
"border-color",
|
|
1107
|
+
"border-style",
|
|
1108
|
+
"border-width",
|
|
1109
|
+
"border-top-style",
|
|
1110
|
+
"border-top-width",
|
|
1111
|
+
"border-right-style",
|
|
1112
|
+
"border-right-width",
|
|
1113
|
+
"border-bottom-style",
|
|
1114
|
+
"border-bottom-width",
|
|
1115
|
+
"border-left-style",
|
|
1116
|
+
"border-left-width",
|
|
1117
|
+
"border-radius",
|
|
1118
|
+
"box-shadow",
|
|
1119
|
+
"display",
|
|
1120
|
+
"height",
|
|
1121
|
+
"padding",
|
|
1122
|
+
"padding-top",
|
|
1123
|
+
"padding-right",
|
|
1124
|
+
"padding-bottom",
|
|
1125
|
+
"padding-left",
|
|
1126
|
+
"padding-block",
|
|
1127
|
+
"padding-inline",
|
|
1128
|
+
"width",
|
|
1129
|
+
"min-width",
|
|
1130
|
+
"max-width",
|
|
1131
|
+
"min-height",
|
|
1132
|
+
"max-height",
|
|
1133
|
+
"object-fit",
|
|
1134
|
+
"overflow",
|
|
1135
|
+
"position",
|
|
1136
|
+
"top",
|
|
1137
|
+
"right",
|
|
1138
|
+
"bottom",
|
|
1139
|
+
"left",
|
|
1140
|
+
"flex-wrap",
|
|
1141
|
+
"align-self"
|
|
1142
|
+
].some((property) => pickStyle(style, property) !== void 0);
|
|
1143
|
+
}
|
|
1144
|
+
function hasContent(element) {
|
|
1145
|
+
return element.children.some((child) => child.type !== "text" || child.text.trim().length > 0);
|
|
1146
|
+
}
|
|
1147
|
+
function createElementNode(graph, parentId, element, parentFlow) {
|
|
1148
|
+
const style = mergedStyle(element);
|
|
1149
|
+
const display = elementDisplay(element, style);
|
|
1150
|
+
if (display === "none") return null;
|
|
1151
|
+
if (isTextLikeElement(element) && !hasBoxStyle(style) && element.children.every((child) => child.type === "text")) {
|
|
1152
|
+
const text = createTextNode(graph, parentId, textContent(element), style, display, parentFlow);
|
|
1153
|
+
linkText(text, element);
|
|
1154
|
+
return text;
|
|
1155
|
+
}
|
|
1156
|
+
if (!hasBoxStyle(style) && holdsRichText(element)) return createRichTextNode(graph, parentId, element, style, display, parentFlow);
|
|
1157
|
+
const node = graph.createNode("FRAME", parentId, {
|
|
1158
|
+
name: element.attrs.id || element.attrs.class || element.tagName,
|
|
1159
|
+
clipsContent: false
|
|
1160
|
+
});
|
|
1161
|
+
applyElementStyle(graph, node, element, style);
|
|
1162
|
+
const flow = hasContent(element) || flowOf(display) === "flex" ? flowOf(display) : null;
|
|
1163
|
+
if (flow) {
|
|
1164
|
+
applyContainerLayout(node, element, style, flow);
|
|
1165
|
+
if (flow === "flex") applyFlexGap(node, style);
|
|
1166
|
+
}
|
|
1167
|
+
applyFlowSizing(graph, node, style, display, parentFlow);
|
|
1168
|
+
if (flow !== "flex" && holdsRichText(element)) {
|
|
1169
|
+
const textStyle = inheritedTextStyle(element, style);
|
|
1170
|
+
const textDisplay = flow === "inline" ? "inline" : "block";
|
|
1171
|
+
createRichTextNode(graph, node.id, element, textStyle, textDisplay, flow ?? "none");
|
|
1172
|
+
return node;
|
|
1173
|
+
}
|
|
1174
|
+
for (const child of element.children) createDesignNode(graph, node.id, child, flow ?? "none", inheritedTextStyle(element, style));
|
|
1175
|
+
return node;
|
|
1176
|
+
}
|
|
1177
|
+
function createDesignNode(graph, parentId, node, parentFlow, inheritedStyle = {}) {
|
|
1178
|
+
if (node.type === "text") {
|
|
1179
|
+
if (node.text.trim().length === 0) return null;
|
|
1180
|
+
const display = parentFlow === "inline" ? "inline" : "block";
|
|
1181
|
+
return createTextNode(graph, parentId, node.text, inheritedStyle, display, parentFlow);
|
|
1182
|
+
}
|
|
1183
|
+
return createElementNode(graph, parentId, node, parentFlow);
|
|
1184
|
+
}
|
|
1185
|
+
function fitPageToChildren(page, graph) {
|
|
1186
|
+
const children = graph.getChildren(page.id);
|
|
1187
|
+
if (children.length === 0) return;
|
|
1188
|
+
page.width = Math.max(...children.map((child) => child.x + child.width));
|
|
1189
|
+
page.height = Math.max(...children.map((child) => child.y + child.height));
|
|
1190
|
+
}
|
|
1191
|
+
function designDocumentToSceneGraph(document, options = {}) {
|
|
1192
|
+
const graph = new SceneGraph();
|
|
1193
|
+
const page = graph.getPages().find((node) => node.type === "CANVAS") ?? graph.addPage("DesignDOM");
|
|
1194
|
+
page.name = options.pageName ?? "DesignDOM";
|
|
1195
|
+
for (const child of document.children) createDesignNode(graph, page.id, child, "none");
|
|
1196
|
+
fitPageToChildren(page, graph);
|
|
1197
|
+
return graph;
|
|
1198
|
+
}
|
|
1199
|
+
//#endregion
|
|
1200
|
+
//#region src/import/jsx/core.ts
|
|
1201
|
+
const Fragment = Symbol.for("pairdraw.dom-css.fragment");
|
|
1202
|
+
function cssPropertyName(name) {
|
|
1203
|
+
if (name.startsWith("--")) return name;
|
|
1204
|
+
return name.replace(/[A-Z]/g, (character) => `-${character.toLowerCase()}`);
|
|
1205
|
+
}
|
|
1206
|
+
function styleObjectToDeclaration(style) {
|
|
1207
|
+
const entries = Object.entries(style).filter((entry) => entry[1] !== null && entry[1] !== void 0).map(([property, value]) => [cssPropertyName(property), String(value)]).filter(([, value]) => value.length > 0);
|
|
1208
|
+
if (entries.length === 0) return void 0;
|
|
1209
|
+
return Object.fromEntries(entries);
|
|
1210
|
+
}
|
|
1211
|
+
function styleStringToDeclaration(style) {
|
|
1212
|
+
const entries = style.split(";").map((declaration) => declaration.split(":")).filter((parts) => parts.length >= 2).map(([property, ...value]) => [property.trim().toLowerCase(), value.join(":").trim()]).filter(([property, value]) => property.length > 0 && value.length > 0);
|
|
1213
|
+
if (entries.length === 0) return void 0;
|
|
1214
|
+
return Object.fromEntries(entries);
|
|
1215
|
+
}
|
|
1216
|
+
function styleToDeclaration(style) {
|
|
1217
|
+
if (typeof style === "string") return styleStringToDeclaration(style);
|
|
1218
|
+
if (style) return styleObjectToDeclaration(style);
|
|
1219
|
+
}
|
|
1220
|
+
function attributeValue(value) {
|
|
1221
|
+
if (value === null || value === void 0 || value === false) return void 0;
|
|
1222
|
+
if (value === true) return "";
|
|
1223
|
+
if (typeof value === "string") return value;
|
|
1224
|
+
if (typeof value === "number" || typeof value === "bigint") return String(value);
|
|
1225
|
+
}
|
|
1226
|
+
function propsToAttrs(props) {
|
|
1227
|
+
const attrs = {};
|
|
1228
|
+
const classValue = props.class ?? props.className;
|
|
1229
|
+
if (classValue) attrs.class = classValue;
|
|
1230
|
+
for (const [name, value] of Object.entries(props)) {
|
|
1231
|
+
if (name === "children" || name === "class" || name === "className" || name === "style") continue;
|
|
1232
|
+
if (name === "key" || name.startsWith("on")) continue;
|
|
1233
|
+
const attr = attributeValue(value);
|
|
1234
|
+
if (attr !== void 0) attrs[name] = attr;
|
|
1235
|
+
}
|
|
1236
|
+
return attrs;
|
|
1237
|
+
}
|
|
1238
|
+
function normalizeChild(child, nodes) {
|
|
1239
|
+
if (child === null || child === void 0 || typeof child === "boolean") return;
|
|
1240
|
+
if (Array.isArray(child)) {
|
|
1241
|
+
for (const item of child) normalizeChild(item, nodes);
|
|
1242
|
+
return;
|
|
1243
|
+
}
|
|
1244
|
+
if (typeof child === "string" || typeof child === "number") {
|
|
1245
|
+
nodes.push({
|
|
1246
|
+
type: "text",
|
|
1247
|
+
text: String(child)
|
|
1248
|
+
});
|
|
1249
|
+
return;
|
|
1250
|
+
}
|
|
1251
|
+
nodes.push(child);
|
|
1252
|
+
}
|
|
1253
|
+
function normalizeJSXChildren(children) {
|
|
1254
|
+
const nodes = [];
|
|
1255
|
+
normalizeChild(children, nodes);
|
|
1256
|
+
return nodes;
|
|
1257
|
+
}
|
|
1258
|
+
function jsx(tag, props = {}) {
|
|
1259
|
+
if (tag === Fragment) return normalizeJSXChildren(props.children);
|
|
1260
|
+
if (typeof tag === "function") return normalizeJSXChildren(tag(props));
|
|
1261
|
+
return {
|
|
1262
|
+
type: "element",
|
|
1263
|
+
tagName: tag,
|
|
1264
|
+
attrs: propsToAttrs(props),
|
|
1265
|
+
inlineStyle: styleToDeclaration(props.style),
|
|
1266
|
+
children: normalizeJSXChildren(props.children)
|
|
1267
|
+
};
|
|
1268
|
+
}
|
|
1269
|
+
const jsxs = jsx;
|
|
1270
|
+
function jsxToDesignDocumentCore(input) {
|
|
1271
|
+
return {
|
|
1272
|
+
type: "document",
|
|
1273
|
+
children: normalizeJSXChildren(input)
|
|
1274
|
+
};
|
|
1275
|
+
}
|
|
1276
|
+
//#endregion
|
|
1277
|
+
//#region src/browser.ts
|
|
1278
|
+
function createRuntime(options) {
|
|
1279
|
+
return createBrowserCSSRuntime({
|
|
1280
|
+
sandbox: "iframe",
|
|
1281
|
+
...options
|
|
1282
|
+
});
|
|
1283
|
+
}
|
|
1284
|
+
async function compileBrowserTailwindCSS(candidates, options) {
|
|
1285
|
+
const { compileTailwindCSS } = await import("./import/html.js").then((n) => n.tailwind_exports);
|
|
1286
|
+
return compileTailwindCSS(candidates, options);
|
|
1287
|
+
}
|
|
1288
|
+
function resolveBrowserDocument(documentOverride) {
|
|
1289
|
+
if (documentOverride) return documentOverride;
|
|
1290
|
+
if (typeof document === "undefined") throw new TypeError("Browser DOM/CSS helpers require a DOM document");
|
|
1291
|
+
return document;
|
|
1292
|
+
}
|
|
1293
|
+
function extractEmbeddedCSSText(html, browserDocument) {
|
|
1294
|
+
const Parser = browserDocument.defaultView?.DOMParser;
|
|
1295
|
+
if (!Parser) throw new TypeError("Browser DOM/CSS helpers require DOMParser");
|
|
1296
|
+
const parsed = new Parser().parseFromString(html, "text/html");
|
|
1297
|
+
const styles = Array.from(parsed.querySelectorAll("style")).map((style) => style.textContent ? style.textContent.trim() : "").filter((css) => !!css);
|
|
1298
|
+
return styles.length > 0 ? styles.join("\n") : void 0;
|
|
1299
|
+
}
|
|
1300
|
+
async function browserHTMLToDesignDocument(html, options = {}) {
|
|
1301
|
+
options.signal?.throwIfAborted();
|
|
1302
|
+
const browserDocument = resolveBrowserDocument(options.document);
|
|
1303
|
+
const runtime = createRuntime({
|
|
1304
|
+
...options,
|
|
1305
|
+
document: browserDocument
|
|
1306
|
+
});
|
|
1307
|
+
const document = runtime.parseHTML(html);
|
|
1308
|
+
const cssText = mergeCSSText(extractEmbeddedCSSText(html, browserDocument), options.cssText);
|
|
1309
|
+
return runtime.computeStyles(document, cssText, options.compute);
|
|
1310
|
+
}
|
|
1311
|
+
async function browserHTMLToSceneGraph(html, options = {}) {
|
|
1312
|
+
const document = await browserHTMLToDesignDocument(html, options);
|
|
1313
|
+
options.signal?.throwIfAborted();
|
|
1314
|
+
const graph = designDocumentToSceneGraph(document, options);
|
|
1315
|
+
options.signal?.throwIfAborted();
|
|
1316
|
+
return graph;
|
|
1317
|
+
}
|
|
1318
|
+
async function browserTailwindHTMLToDesignDocument(html, candidates, options = {}) {
|
|
1319
|
+
const cssText = await compileBrowserTailwindCSS(candidates, options);
|
|
1320
|
+
return browserHTMLToDesignDocument(html, {
|
|
1321
|
+
...options,
|
|
1322
|
+
cssText
|
|
1323
|
+
});
|
|
1324
|
+
}
|
|
1325
|
+
async function browserTailwindHTMLToSceneGraph(html, candidates, options = {}) {
|
|
1326
|
+
const document = await browserTailwindHTMLToDesignDocument(html, candidates, options);
|
|
1327
|
+
options.signal?.throwIfAborted();
|
|
1328
|
+
const graph = designDocumentToSceneGraph(document, options);
|
|
1329
|
+
options.signal?.throwIfAborted();
|
|
1330
|
+
return graph;
|
|
1331
|
+
}
|
|
1332
|
+
async function browserJSXToDesignDocument(input, options = {}) {
|
|
1333
|
+
const document = jsxToDesignDocumentCore(input);
|
|
1334
|
+
return createRuntime(options).computeStyles(document, options.cssText, options.compute);
|
|
1335
|
+
}
|
|
1336
|
+
async function browserJSXToSceneGraph(input, options = {}) {
|
|
1337
|
+
return designDocumentToSceneGraph(await browserJSXToDesignDocument(input, options), options);
|
|
1338
|
+
}
|
|
1339
|
+
async function browserTailwindJSXToDesignDocument(input, candidates, options = {}) {
|
|
1340
|
+
const cssText = await compileBrowserTailwindCSS(candidates, options);
|
|
1341
|
+
return browserJSXToDesignDocument(input, {
|
|
1342
|
+
...options,
|
|
1343
|
+
cssText
|
|
1344
|
+
});
|
|
1345
|
+
}
|
|
1346
|
+
async function browserTailwindJSXToSceneGraph(input, candidates, options = {}) {
|
|
1347
|
+
return designDocumentToSceneGraph(await browserTailwindJSXToDesignDocument(input, candidates, options), options);
|
|
1348
|
+
}
|
|
1349
|
+
//#endregion
|
|
1350
|
+
export { Fragment, INLINE_TAGS, browserHTMLToDesignDocument, browserHTMLToSceneGraph, browserJSXToDesignDocument, browserJSXToSceneGraph, browserTailwindHTMLToDesignDocument, browserTailwindHTMLToSceneGraph, browserTailwindJSXToDesignDocument, browserTailwindJSXToSceneGraph, createBrowserCSSRuntime, designDocumentToSceneGraph, jsx, jsxToDesignDocumentCore, jsxs, mergeCSSText };
|
|
1351
|
+
|
|
1352
|
+
//# sourceMappingURL=browser.js.map
|