@pairdraw/dom-css 0.19.1 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.d.ts +16 -1
- package/dist/browser.js +2 -2
- package/dist/chunks/behaviours/controls.js +34 -2
- package/dist/chunks/behaviours/controls.js.map +1 -1
- package/dist/chunks/behaviours/states/layers.js +110 -5
- package/dist/chunks/behaviours/states/layers.js.map +1 -1
- package/dist/chunks/behaviours/states/tailwind.js +17 -2
- package/dist/chunks/behaviours/states/tailwind.js.map +1 -1
- package/dist/chunks/browser.js +208 -53
- package/dist/chunks/browser.js.map +1 -1
- package/dist/chunks/export/components/groups.js +3 -2
- package/dist/chunks/export/components/groups.js.map +1 -1
- package/dist/chunks/export/components/layers.js +50 -8
- package/dist/chunks/export/components/layers.js.map +1 -1
- package/dist/export.d.ts +9 -0
- package/dist/scene-graph2.d.ts +6 -0
- package/package.json +3 -3
- package/src/behaviours/controls.ts +49 -2
- package/src/behaviours/states/tailwind.ts +4 -1
- package/src/browser.ts +1 -0
- package/src/export/components/model.ts +60 -7
- package/src/export/components/react/index.ts +4 -0
- package/src/export/components/repeats.ts +7 -2
- package/src/export/components/vue.ts +38 -9
- package/src/export/css.ts +3 -2
- package/src/export/font-size-class.ts +13 -0
- package/src/export/html.ts +5 -1
- package/src/export/projection.ts +121 -10
- package/src/import/flow.ts +15 -0
- package/src/import/fonts.ts +40 -0
- package/src/import/scene-graph.ts +199 -55
package/dist/browser.d.ts
CHANGED
|
@@ -3,6 +3,21 @@ import { CompileTailwindCSSOptions, Fragment, JSXChild, JSXElementProps, JSXStyl
|
|
|
3
3
|
import { DesignDocumentToSceneGraphOptions } from "./scene-graph2.js";
|
|
4
4
|
import { BrowserCSSRuntimeOptions } from "./runtime.js";
|
|
5
5
|
import { SceneGraph } from "@pairdraw/scene-graph";
|
|
6
|
+
//#region src/import/fonts.d.ts
|
|
7
|
+
interface FontStackOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Whether the document can draw a family, such as the font service answering. Without it every
|
|
10
|
+
* concrete family is taken as available, as a headless import has no faces to ask.
|
|
11
|
+
*/
|
|
12
|
+
isAvailable?: (family: string) => boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The family a CSS `font-family` stack asks a document to draw: the first concrete family the
|
|
16
|
+
* font service can provide; a generic keyword maps to a concrete face of its class. Null when the
|
|
17
|
+
* list has no family.
|
|
18
|
+
*/
|
|
19
|
+
declare function resolveFontFamilyStack(value: string | undefined, options?: FontStackOptions): string | null;
|
|
20
|
+
//#endregion
|
|
6
21
|
//#region src/browser.d.ts
|
|
7
22
|
interface BrowserToDesignDocumentOptions extends BrowserCSSRuntimeOptions {
|
|
8
23
|
cssText?: string;
|
|
@@ -25,5 +40,5 @@ declare function browserJSXToSceneGraph(input: JSXChild, options?: BrowserToScen
|
|
|
25
40
|
declare function browserTailwindJSXToDesignDocument(input: JSXChild, candidates: string | Iterable<string>, options?: BrowserTailwindToDesignDocumentOptions): Promise<DesignDocument>;
|
|
26
41
|
declare function browserTailwindJSXToSceneGraph(input: JSXChild, candidates: string | Iterable<string>, options?: BrowserTailwindToSceneGraphOptions): Promise<SceneGraph>;
|
|
27
42
|
//#endregion
|
|
28
|
-
export { BrowserHTMLToDesignDocumentOptions, BrowserHTMLToSceneGraphOptions, BrowserTailwindHTMLToDesignDocumentOptions, BrowserTailwindHTMLToSceneGraphOptions, BrowserTailwindToDesignDocumentOptions, BrowserTailwindToSceneGraphOptions, BrowserToDesignDocumentOptions, BrowserToSceneGraphOptions, Fragment, type JSXChild, type JSXElementProps, type JSXStyleInput, type JSXTag, browserHTMLToDesignDocument, browserHTMLToSceneGraph, browserJSXToDesignDocument, browserJSXToSceneGraph, browserTailwindHTMLToDesignDocument, browserTailwindHTMLToSceneGraph, browserTailwindJSXToDesignDocument, browserTailwindJSXToSceneGraph, jsx, jsxs };
|
|
43
|
+
export { BrowserHTMLToDesignDocumentOptions, BrowserHTMLToSceneGraphOptions, BrowserTailwindHTMLToDesignDocumentOptions, BrowserTailwindHTMLToSceneGraphOptions, BrowserTailwindToDesignDocumentOptions, BrowserTailwindToSceneGraphOptions, BrowserToDesignDocumentOptions, BrowserToSceneGraphOptions, type FontStackOptions, Fragment, type JSXChild, type JSXElementProps, type JSXStyleInput, type JSXTag, browserHTMLToDesignDocument, browserHTMLToSceneGraph, browserJSXToDesignDocument, browserJSXToSceneGraph, browserTailwindHTMLToDesignDocument, browserTailwindHTMLToSceneGraph, browserTailwindJSXToDesignDocument, browserTailwindJSXToSceneGraph, jsx, jsxs, resolveFontFamilyStack };
|
|
29
44
|
//# sourceMappingURL=browser.d.ts.map
|
package/dist/browser.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Fragment, browserHTMLToDesignDocument, browserHTMLToSceneGraph, browserJSXToDesignDocument, browserJSXToSceneGraph, browserTailwindHTMLToDesignDocument, browserTailwindHTMLToSceneGraph, browserTailwindJSXToDesignDocument, browserTailwindJSXToSceneGraph, jsx, jsxs } from "./chunks/browser.js";
|
|
2
|
-
export { Fragment, browserHTMLToDesignDocument, browserHTMLToSceneGraph, browserJSXToDesignDocument, browserJSXToSceneGraph, browserTailwindHTMLToDesignDocument, browserTailwindHTMLToSceneGraph, browserTailwindJSXToDesignDocument, browserTailwindJSXToSceneGraph, jsx, jsxs };
|
|
1
|
+
import { Fragment, browserHTMLToDesignDocument, browserHTMLToSceneGraph, browserJSXToDesignDocument, browserJSXToSceneGraph, browserTailwindHTMLToDesignDocument, browserTailwindHTMLToSceneGraph, browserTailwindJSXToDesignDocument, browserTailwindJSXToSceneGraph, jsx, jsxs, resolveFontFamilyStack } from "./chunks/browser.js";
|
|
2
|
+
export { Fragment, browserHTMLToDesignDocument, browserHTMLToSceneGraph, browserJSXToDesignDocument, browserJSXToSceneGraph, browserTailwindHTMLToDesignDocument, browserTailwindHTMLToSceneGraph, browserTailwindJSXToDesignDocument, browserTailwindJSXToSceneGraph, jsx, jsxs, resolveFontFamilyStack };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { behaviourContract, behaviourOwner, behaviourProperties, booleanBinding, hasBehaviour, layerPath, partBinding, readBehaviour, textBinding } from "@pairdraw/scene-graph/behaviours";
|
|
2
2
|
import { instanceMainComponent } from "@pairdraw/scene-graph/instances";
|
|
3
|
-
import {
|
|
3
|
+
import { slotPropertyId } from "@pairdraw/scene-graph/slots";
|
|
4
4
|
import { findComponentPropertyTargets } from "@pairdraw/scene-graph/components";
|
|
5
5
|
//#region src/behaviours/controls.ts
|
|
6
6
|
function designedBoolean(graph, instance, definition, on) {
|
|
@@ -22,6 +22,23 @@ function statesModel(graph, instance, owner, behaviour) {
|
|
|
22
22
|
designedDisabled: !!states.disabled && authored === states.disabled
|
|
23
23
|
};
|
|
24
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* The frames an instance binds to slot properties, which its parts are. A slot may sit inside
|
|
27
|
+
* another, as a slider's range in its track: Figma keeps both in the component and makes the
|
|
28
|
+
* inner one part of the outer one's content in an instance, so the search goes through slots
|
|
29
|
+
* but not into nested instances, which are controls of their own.
|
|
30
|
+
*/
|
|
31
|
+
function partFrames(graph, instance) {
|
|
32
|
+
const frames = [];
|
|
33
|
+
const visit = (node) => {
|
|
34
|
+
for (const child of graph.getChildren(node.id)) {
|
|
35
|
+
if (slotPropertyId(child)) frames.push(child);
|
|
36
|
+
if (child.type !== "INSTANCE") visit(child);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
visit(instance);
|
|
40
|
+
return frames;
|
|
41
|
+
}
|
|
25
42
|
function controlModel(graph, rootId, instance) {
|
|
26
43
|
const component = instanceMainComponent(graph, instance);
|
|
27
44
|
const owner = component && behaviourOwner(graph, component);
|
|
@@ -41,7 +58,8 @@ function controlModel(graph, rootId, instance) {
|
|
|
41
58
|
designed: designedBoolean(graph, instance, definition, binding.on)
|
|
42
59
|
};
|
|
43
60
|
}
|
|
44
|
-
const
|
|
61
|
+
const choice = choiceModel(behaviour, properties, component);
|
|
62
|
+
const frames = partFrames(graph, instance);
|
|
45
63
|
const parts = Object.fromEntries(Object.entries(behaviour.parts).flatMap(([partId, propertyId]) => {
|
|
46
64
|
const frame = frames.find((item) => slotPropertyId(item) === propertyId);
|
|
47
65
|
return frame ? [[partId, layerPath(graph, rootId, frame.id)]] : [];
|
|
@@ -72,6 +90,7 @@ function controlModel(graph, rootId, instance) {
|
|
|
72
90
|
kind: behaviour.kind,
|
|
73
91
|
behaviour,
|
|
74
92
|
booleans,
|
|
93
|
+
choice,
|
|
75
94
|
states: statesModel(graph, instance, owner, behaviour),
|
|
76
95
|
parts,
|
|
77
96
|
texts,
|
|
@@ -81,6 +100,19 @@ function controlModel(graph, rootId, instance) {
|
|
|
81
100
|
triggers: behaviour.kind === "tabs" ? childPaths(frameOf("list")) : []
|
|
82
101
|
};
|
|
83
102
|
}
|
|
103
|
+
/** The variant property a tabs' choice binds, its options, and the drawn option's index. */
|
|
104
|
+
function choiceModel(behaviour, properties, component) {
|
|
105
|
+
const binding = behaviour.kind === "tabs" ? booleanBinding(behaviour, "value") : void 0;
|
|
106
|
+
const definition = properties.find((item) => item.id === binding?.propertyId && item.type === "VARIANT");
|
|
107
|
+
const options = definition?.variantOptions ?? [];
|
|
108
|
+
if (!definition || options.length === 0) return null;
|
|
109
|
+
const designed = component?.componentPropertyValues[definition.name];
|
|
110
|
+
return {
|
|
111
|
+
definition,
|
|
112
|
+
options,
|
|
113
|
+
designed: Math.max(0, options.indexOf(designed ?? ""))
|
|
114
|
+
};
|
|
115
|
+
}
|
|
84
116
|
/**
|
|
85
117
|
* The controls below a root layer, by layer path, outermost first. Items of a group are listed
|
|
86
118
|
* both in the group's `items` and on their own, since each keeps its own state. Preview runs
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"controls.js","names":[],"sources":["../../../src/behaviours/controls.ts"],"sourcesContent":["import type { ComponentPropertyDefinition, SceneGraph, SceneNode } from '@pairdraw/scene-graph'\nimport {\n behaviourContract,\n behaviourOwner,\n behaviourProperties,\n booleanBinding,\n hasBehaviour,\n layerPath,\n partBinding,\n readBehaviour,\n textBinding,\n type Behaviour\n} from '@pairdraw/scene-graph/behaviours'\nimport { findComponentPropertyTargets } from '@pairdraw/scene-graph/components'\nimport { instanceMainComponent } from '@pairdraw/scene-graph/instances'\nimport { instanceSlotFrames, slotPropertyId } from '@pairdraw/scene-graph/slots'\n\n/** A boolean value of a control and the property that draws it. */\nexport interface BooleanModel {\n definition: ComponentPropertyDefinition\n on?: string\n off?: string\n /** The value as the document's instance draws it. */\n designed: boolean\n}\n\n/** The variant property that draws a control's interaction states. */\nexport interface StatesModel {\n name: string\n values: NonNullable<Behaviour['states']>\n /** The value the instance shows at rest. */\n rest: string | undefined\n /** Whether the document's instance is drawn disabled. */\n designedDisabled: boolean\n}\n\n/** An instance with a behaviour below a root layer: its values, states, and bound layers. */\nexport interface ControlModel {\n /** The instance's layer path below the root. */\n path: string\n kind: Behaviour['kind']\n behaviour: Behaviour\n booleans: Record<string, BooleanModel>\n states: StatesModel | null\n /** Each bound part's slot frame, by part id, as a layer path below the root. */\n parts: Record<string, string>\n /** Each bound text value's text layer, by value id, as a layer path below the root. */\n texts: Record<string, { path: string; designed: string }>\n /** The children of the items slot that are controls themselves, in order. */\n items: ControlModel[]\n /**\n * Layers the control decides to show, which the design may hide: tab panels, or a\n * collapsible's content, as layer paths below the root.\n */\n reveal: string[]\n /** For tabs, each panel's layer path, in order. */\n panels: string[]\n /** For tabs, each trigger's layer path, in order. */\n triggers: string[]\n}\n\nfunction designedBoolean(\n graph: SceneGraph,\n instance: SceneNode,\n definition: ComponentPropertyDefinition,\n on: string | undefined\n): boolean {\n if (definition.type === 'BOOLEAN') {\n const assignments = instance.componentPropertyAssignments\n const value = Object.hasOwn(assignments, definition.id)\n ? assignments[definition.id]\n : definition.defaultValue\n return value === 'true'\n }\n return instanceMainComponent(graph, instance)?.componentPropertyValues[definition.name] === on\n}\n\nfunction statesModel(\n graph: SceneGraph,\n instance: SceneNode,\n owner: SceneNode,\n behaviour: Behaviour\n): StatesModel | null {\n const states = behaviour.states\n const definition = states\n ? behaviourProperties(graph, owner).find(\n (item) => item.id === states.propertyId && item.type === 'VARIANT'\n )\n : undefined\n if (!states || !definition) return null\n const authored = instanceMainComponent(graph, instance)?.componentPropertyValues[definition.name]\n return {\n name: definition.name,\n values: states,\n rest: states.rest ?? authored,\n designedDisabled: !!states.disabled && authored === states.disabled\n }\n}\n\nfunction controlModel(graph: SceneGraph, rootId: string, instance: SceneNode): ControlModel | null {\n const component = instanceMainComponent(graph, instance)\n const owner = component && behaviourOwner(graph, component)\n const behaviour = owner && readBehaviour(owner)\n if (!owner || !behaviour) return null\n const path = layerPath(graph, rootId, instance.id)\n const properties = behaviourProperties(graph, owner)\n const booleans: Record<string, BooleanModel> = {}\n for (const valueId of Object.keys(behaviour.booleans)) {\n const binding = booleanBinding(behaviour, valueId)\n const definition = properties.find((item) => item.id === binding?.propertyId)\n if (!binding || !definition) continue\n booleans[valueId] = {\n definition,\n on: binding.on,\n off: binding.off,\n designed: designedBoolean(graph, instance, definition, binding.on)\n }\n }\n const frames = instanceSlotFrames(graph, instance)\n const parts = Object.fromEntries(\n Object.entries(behaviour.parts).flatMap(([partId, propertyId]) => {\n const frame = frames.find((item) => slotPropertyId(item) === propertyId)\n return frame ? [[partId, layerPath(graph, rootId, frame.id)]] : []\n })\n )\n const texts = Object.fromEntries(\n Object.keys(behaviour.texts).flatMap((valueId) => {\n const propertyId = textBinding(behaviour, valueId)\n const target = propertyId\n ? findComponentPropertyTargets(graph, instance, propertyId).find(\n (item) => item.node.type === 'TEXT'\n )\n : undefined\n return target\n ? [\n [\n valueId,\n { path: layerPath(graph, rootId, target.node.id), designed: target.node.text }\n ]\n ]\n : []\n })\n )\n const frameOf = (partId: string) => {\n const propertyId = partBinding(behaviour, partId)\n return propertyId ? frames.find((item) => slotPropertyId(item) === propertyId) : undefined\n }\n const itemsPart = behaviourContract(behaviour.kind).parts.find((part) => part.items)\n const itemsFrame = itemsPart ? frameOf(itemsPart.id) : undefined\n const items = (itemsFrame ? graph.getChildren(itemsFrame.id) : []).flatMap((child) => {\n const item = controlModel(graph, rootId, child)\n return item ? [item] : []\n })\n const childPaths = (frame: SceneNode | undefined) =>\n (frame ? graph.getChildren(frame.id) : []).map((child) => layerPath(graph, rootId, child.id))\n const panels = behaviour.kind === 'tabs' ? childPaths(frameOf('panels')) : []\n const content = behaviour.kind === 'collapsible' ? frameOf('content') : undefined\n return {\n path,\n kind: behaviour.kind,\n behaviour,\n booleans,\n states: statesModel(graph, instance, owner, behaviour),\n parts,\n texts,\n items,\n reveal: content ? [layerPath(graph, rootId, content.id)] : panels,\n panels,\n triggers: behaviour.kind === 'tabs' ? childPaths(frameOf('list')) : []\n }\n}\n\n/**\n * The controls below a root layer, by layer path, outermost first. Items of a group are listed\n * both in the group's `items` and on their own, since each keeps its own state. Preview runs\n * them as live islands; export generates components from them.\n */\nexport function behaviourControls(graph: SceneGraph, rootId: string): Map<string, ControlModel> {\n const controls = new Map<string, ControlModel>()\n const visit = (node: SceneNode) => {\n if (hasBehaviour(graph, node)) {\n const control = controlModel(graph, rootId, node)\n if (control) controls.set(control.path, control)\n }\n for (const child of graph.getChildren(node.id)) visit(child)\n }\n const root = graph.getNode(rootId)\n if (root) visit(root)\n return controls\n}\n\n/** A control's boolean value, if the behaviour binds it. */\nexport function booleanOf(control: ControlModel, valueId: string): BooleanModel | undefined {\n return Object.hasOwn(control.booleans, valueId) ? control.booleans[valueId] : undefined\n}\n"],"mappings":";;;;;AA6DA,SAAS,gBACP,OACA,UACA,YACA,IACS;CACT,IAAI,WAAW,SAAS,WAAW;EACjC,MAAM,cAAc,SAAS;EAI7B,QAHc,OAAO,OAAO,aAAa,WAAW,EAAE,IAClD,YAAY,WAAW,MACvB,WAAW,kBACE;CACnB;CACA,OAAO,sBAAsB,OAAO,QAAQ,CAAC,EAAE,wBAAwB,WAAW,UAAU;AAC9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,OAAO;EACL;EACA,MAAM,UAAU;EAChB;EACA;EACA,QAAQ,YAAY,OAAO,UAAU,OAAO,SAAS;EACrD;EACA;EACA;EACA,QAAQ,UAAU,CAAC,UAAU,OAAO,QAAQ,QAAQ,EAAE,CAAC,IAAI;EAC3D;EACA,UAAU,UAAU,SAAS,SAAS,WAAW,QAAQ,MAAM,CAAC,IAAI,CAAC;CACvE;AACF;;;;;;AAOA,SAAgB,kBAAkB,OAAmB,QAA2C;CAC9F,MAAM,2BAAW,IAAI,IAA0B;CAC/C,MAAM,SAAS,SAAoB;EACjC,IAAI,aAAa,OAAO,IAAI,GAAG;GAC7B,MAAM,UAAU,aAAa,OAAO,QAAQ,IAAI;GAChD,IAAI,SAAS,SAAS,IAAI,QAAQ,MAAM,OAAO;EACjD;EACA,KAAK,MAAM,SAAS,MAAM,YAAY,KAAK,EAAE,GAAG,MAAM,KAAK;CAC7D;CACA,MAAM,OAAO,MAAM,QAAQ,MAAM;CACjC,IAAI,MAAM,MAAM,IAAI;CACpB,OAAO;AACT;;AAGA,SAAgB,UAAU,SAAuB,SAA2C;CAC1F,OAAO,OAAO,OAAO,QAAQ,UAAU,OAAO,IAAI,QAAQ,SAAS,WAAW,KAAA;AAChF"}
|
|
1
|
+
{"version":3,"file":"controls.js","names":[],"sources":["../../../src/behaviours/controls.ts"],"sourcesContent":["import type { ComponentPropertyDefinition, SceneGraph, SceneNode } from '@pairdraw/scene-graph'\nimport {\n behaviourContract,\n behaviourOwner,\n behaviourProperties,\n booleanBinding,\n hasBehaviour,\n layerPath,\n partBinding,\n readBehaviour,\n textBinding,\n type Behaviour\n} from '@pairdraw/scene-graph/behaviours'\nimport { findComponentPropertyTargets } from '@pairdraw/scene-graph/components'\nimport { instanceMainComponent } from '@pairdraw/scene-graph/instances'\nimport { slotPropertyId } from '@pairdraw/scene-graph/slots'\n\n/** A boolean value of a control and the property that draws it. */\nexport interface BooleanModel {\n definition: ComponentPropertyDefinition\n on?: string\n off?: string\n /** The value as the document's instance draws it. */\n designed: boolean\n}\n\n/** The variant property a tabs' choice binds, its options, and the one the instance shows. */\nexport interface ChoiceControlModel {\n definition: ComponentPropertyDefinition\n options: string[]\n /** The option's index as the document's instance draws it, 0 when none does. */\n designed: number\n}\n\n/** The variant property that draws a control's interaction states. */\nexport interface StatesModel {\n name: string\n values: NonNullable<Behaviour['states']>\n /** The value the instance shows at rest. */\n rest: string | undefined\n /** Whether the document's instance is drawn disabled. */\n designedDisabled: boolean\n}\n\n/** An instance with a behaviour below a root layer: its values, states, and bound layers. */\nexport interface ControlModel {\n /** The instance's layer path below the root. */\n path: string\n kind: Behaviour['kind']\n behaviour: Behaviour\n booleans: Record<string, BooleanModel>\n /** For tabs bound to a variant property, the tabs the triggers choose among. */\n choice: ChoiceControlModel | null\n states: StatesModel | null\n /** Each bound part's slot frame, by part id, as a layer path below the root. */\n parts: Record<string, string>\n /** Each bound text value's text layer, by value id, as a layer path below the root. */\n texts: Record<string, { path: string; designed: string }>\n /** The children of the items slot that are controls themselves, in order. */\n items: ControlModel[]\n /**\n * Layers the control decides to show, which the design may hide: tab panels, or a\n * collapsible's content, as layer paths below the root.\n */\n reveal: string[]\n /** For tabs, each panel's layer path, in order. */\n panels: string[]\n /** For tabs, each trigger's layer path, in order. */\n triggers: string[]\n}\n\nfunction designedBoolean(\n graph: SceneGraph,\n instance: SceneNode,\n definition: ComponentPropertyDefinition,\n on: string | undefined\n): boolean {\n if (definition.type === 'BOOLEAN') {\n const assignments = instance.componentPropertyAssignments\n const value = Object.hasOwn(assignments, definition.id)\n ? assignments[definition.id]\n : definition.defaultValue\n return value === 'true'\n }\n return instanceMainComponent(graph, instance)?.componentPropertyValues[definition.name] === on\n}\n\nfunction statesModel(\n graph: SceneGraph,\n instance: SceneNode,\n owner: SceneNode,\n behaviour: Behaviour\n): StatesModel | null {\n const states = behaviour.states\n const definition = states\n ? behaviourProperties(graph, owner).find(\n (item) => item.id === states.propertyId && item.type === 'VARIANT'\n )\n : undefined\n if (!states || !definition) return null\n const authored = instanceMainComponent(graph, instance)?.componentPropertyValues[definition.name]\n return {\n name: definition.name,\n values: states,\n rest: states.rest ?? authored,\n designedDisabled: !!states.disabled && authored === states.disabled\n }\n}\n\n/**\n * The frames an instance binds to slot properties, which its parts are. A slot may sit inside\n * another, as a slider's range in its track: Figma keeps both in the component and makes the\n * inner one part of the outer one's content in an instance, so the search goes through slots\n * but not into nested instances, which are controls of their own.\n */\nfunction partFrames(graph: SceneGraph, instance: SceneNode): SceneNode[] {\n const frames: SceneNode[] = []\n const visit = (node: SceneNode): void => {\n for (const child of graph.getChildren(node.id)) {\n if (slotPropertyId(child)) frames.push(child)\n if (child.type !== 'INSTANCE') visit(child)\n }\n }\n visit(instance)\n return frames\n}\n\nfunction controlModel(graph: SceneGraph, rootId: string, instance: SceneNode): ControlModel | null {\n const component = instanceMainComponent(graph, instance)\n const owner = component && behaviourOwner(graph, component)\n const behaviour = owner && readBehaviour(owner)\n if (!owner || !behaviour) return null\n const path = layerPath(graph, rootId, instance.id)\n const properties = behaviourProperties(graph, owner)\n const booleans: Record<string, BooleanModel> = {}\n for (const valueId of Object.keys(behaviour.booleans)) {\n const binding = booleanBinding(behaviour, valueId)\n const definition = properties.find((item) => item.id === binding?.propertyId)\n if (!binding || !definition) continue\n booleans[valueId] = {\n definition,\n on: binding.on,\n off: binding.off,\n designed: designedBoolean(graph, instance, definition, binding.on)\n }\n }\n const choice = choiceModel(behaviour, properties, component)\n\n const frames = partFrames(graph, instance)\n const parts = Object.fromEntries(\n Object.entries(behaviour.parts).flatMap(([partId, propertyId]) => {\n const frame = frames.find((item) => slotPropertyId(item) === propertyId)\n return frame ? [[partId, layerPath(graph, rootId, frame.id)]] : []\n })\n )\n const texts = Object.fromEntries(\n Object.keys(behaviour.texts).flatMap((valueId) => {\n const propertyId = textBinding(behaviour, valueId)\n const target = propertyId\n ? findComponentPropertyTargets(graph, instance, propertyId).find(\n (item) => item.node.type === 'TEXT'\n )\n : undefined\n return target\n ? [\n [\n valueId,\n { path: layerPath(graph, rootId, target.node.id), designed: target.node.text }\n ]\n ]\n : []\n })\n )\n const frameOf = (partId: string) => {\n const propertyId = partBinding(behaviour, partId)\n return propertyId ? frames.find((item) => slotPropertyId(item) === propertyId) : undefined\n }\n const itemsPart = behaviourContract(behaviour.kind).parts.find((part) => part.items)\n const itemsFrame = itemsPart ? frameOf(itemsPart.id) : undefined\n const items = (itemsFrame ? graph.getChildren(itemsFrame.id) : []).flatMap((child) => {\n const item = controlModel(graph, rootId, child)\n return item ? [item] : []\n })\n const childPaths = (frame: SceneNode | undefined) =>\n (frame ? graph.getChildren(frame.id) : []).map((child) => layerPath(graph, rootId, child.id))\n const panels = behaviour.kind === 'tabs' ? childPaths(frameOf('panels')) : []\n const content = behaviour.kind === 'collapsible' ? frameOf('content') : undefined\n return {\n path,\n kind: behaviour.kind,\n behaviour,\n booleans,\n choice,\n states: statesModel(graph, instance, owner, behaviour),\n parts,\n texts,\n items,\n reveal: content ? [layerPath(graph, rootId, content.id)] : panels,\n panels,\n triggers: behaviour.kind === 'tabs' ? childPaths(frameOf('list')) : []\n }\n}\n\n/** The variant property a tabs' choice binds, its options, and the drawn option's index. */\nfunction choiceModel(\n behaviour: Behaviour,\n properties: readonly ComponentPropertyDefinition[],\n component: SceneNode | undefined\n): ChoiceControlModel | null {\n const binding = behaviour.kind === 'tabs' ? booleanBinding(behaviour, 'value') : undefined\n const definition = properties.find(\n (item) => item.id === binding?.propertyId && item.type === 'VARIANT'\n )\n const options = definition?.variantOptions ?? []\n if (!definition || options.length === 0) return null\n const designed = component?.componentPropertyValues[definition.name]\n return { definition, options, designed: Math.max(0, options.indexOf(designed ?? '')) }\n}\n\n/**\n * The controls below a root layer, by layer path, outermost first. Items of a group are listed\n * both in the group's `items` and on their own, since each keeps its own state. Preview runs\n * them as live islands; export generates components from them.\n */\nexport function behaviourControls(graph: SceneGraph, rootId: string): Map<string, ControlModel> {\n const controls = new Map<string, ControlModel>()\n const visit = (node: SceneNode) => {\n if (hasBehaviour(graph, node)) {\n const control = controlModel(graph, rootId, node)\n if (control) controls.set(control.path, control)\n }\n for (const child of graph.getChildren(node.id)) visit(child)\n }\n const root = graph.getNode(rootId)\n if (root) visit(root)\n return controls\n}\n\n/** A control's boolean value, if the behaviour binds it. */\nexport function booleanOf(control: ControlModel, valueId: string): BooleanModel | undefined {\n return Object.hasOwn(control.booleans, valueId) ? control.booleans[valueId] : undefined\n}\n"],"mappings":";;;;;AAuEA,SAAS,gBACP,OACA,UACA,YACA,IACS;CACT,IAAI,WAAW,SAAS,WAAW;EACjC,MAAM,cAAc,SAAS;EAI7B,QAHc,OAAO,OAAO,aAAa,WAAW,EAAE,IAClD,YAAY,WAAW,MACvB,WAAW,kBACE;CACnB;CACA,OAAO,sBAAsB,OAAO,QAAQ,CAAC,EAAE,wBAAwB,WAAW,UAAU;AAC9F;AAEA,SAAS,YACP,OACA,UACA,OACA,WACoB;CACpB,MAAM,SAAS,UAAU;CACzB,MAAM,aAAa,SACf,oBAAoB,OAAO,KAAK,CAAC,CAAC,MAC/B,SAAS,KAAK,OAAO,OAAO,cAAc,KAAK,SAAS,SAC3D,IACA,KAAA;CACJ,IAAI,CAAC,UAAU,CAAC,YAAY,OAAO;CACnC,MAAM,WAAW,sBAAsB,OAAO,QAAQ,CAAC,EAAE,wBAAwB,WAAW;CAC5F,OAAO;EACL,MAAM,WAAW;EACjB,QAAQ;EACR,MAAM,OAAO,QAAQ;EACrB,kBAAkB,CAAC,CAAC,OAAO,YAAY,aAAa,OAAO;CAC7D;AACF;;;;;;;AAQA,SAAS,WAAW,OAAmB,UAAkC;CACvE,MAAM,SAAsB,CAAC;CAC7B,MAAM,SAAS,SAA0B;EACvC,KAAK,MAAM,SAAS,MAAM,YAAY,KAAK,EAAE,GAAG;GAC9C,IAAI,eAAe,KAAK,GAAG,OAAO,KAAK,KAAK;GAC5C,IAAI,MAAM,SAAS,YAAY,MAAM,KAAK;EAC5C;CACF;CACA,MAAM,QAAQ;CACd,OAA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AAW,MAC3B,SAAS,KAAK,OAAO,SAAS,cAAc,KAAK,SAAS,SAC7D;CACA,MAAM,UAAU,YAAY,kBAAkB,CAAC;CAC/C,IAAI,CAAC,cAAc,QAAQ,WAAW,GAAG,OAAO;CAChD,MAAM,WAAW,WAAW,wBAAwB,WAAW;CAC/D,OAAO;EAAE;EAAY;EAAS,UAAU,KAAK,IAAI,GAAG,QAAQ,QAAQ,YAAY,EAAE,CAAC;CAAE;AACvF;;;;;;AAOA,SAAgB,kBAAkB,OAAmB,QAA2C;CAC9F,MAAM,2BAAW,IAAI,IAA0B;CAC/C,MAAM,SAAS,SAAoB;EACjC,IAAI,aAAa,OAAO,IAAI,GAAG;GAC7B,MAAM,UAAU,aAAa,OAAO,QAAQ,IAAI;GAChD,IAAI,SAAS,SAAS,IAAI,QAAQ,MAAM,OAAO;EACjD;EACA,KAAK,MAAM,SAAS,MAAM,YAAY,KAAK,EAAE,GAAG,MAAM,KAAK;CAC7D;CACA,MAAM,OAAO,MAAM,QAAQ,MAAM;CACjC,IAAI,MAAM,MAAM,IAAI;CACpB,OAAO;AACT;;AAGA,SAAgB,UAAU,SAAuB,SAA2C;CAC1F,OAAO,OAAO,OAAO,QAAQ,UAAU,OAAO,IAAI,QAAQ,SAAS,WAAW,KAAA;AAChF"}
|
|
@@ -233,14 +233,15 @@ function gradientFillsToCSS(fills, width, height) {
|
|
|
233
233
|
* border insets the box its absolutely positioned children are placed in, where a stroke does not.
|
|
234
234
|
*/
|
|
235
235
|
function borderWidths(node) {
|
|
236
|
-
|
|
236
|
+
const stroke = node.strokes.find((item) => item.visible);
|
|
237
|
+
if (!strokeColorToCSS(stroke)) return {
|
|
237
238
|
top: 0,
|
|
238
239
|
right: 0,
|
|
239
240
|
bottom: 0,
|
|
240
241
|
left: 0
|
|
241
242
|
};
|
|
242
243
|
if (!node.independentStrokeWeights) {
|
|
243
|
-
const weight =
|
|
244
|
+
const weight = stroke?.weight ?? 0;
|
|
244
245
|
return {
|
|
245
246
|
top: weight,
|
|
246
247
|
right: weight,
|
|
@@ -1559,7 +1560,7 @@ function addCornerRadii(style, node, css) {
|
|
|
1559
1560
|
if (node.cornerRadius > 0) style["border-radius"] = css("cornerRadius", px(node.cornerRadius));
|
|
1560
1561
|
}
|
|
1561
1562
|
function addStroke(style, node, css) {
|
|
1562
|
-
const stroke = node.strokes.
|
|
1563
|
+
const stroke = node.strokes.find((item) => item.visible);
|
|
1563
1564
|
const literalColor = strokeColorToCSS(stroke);
|
|
1564
1565
|
if (!literalColor || !stroke) return;
|
|
1565
1566
|
const color = css("strokes/0/color", literalColor);
|
|
@@ -1576,6 +1577,33 @@ function addStroke(style, node, css) {
|
|
|
1576
1577
|
style["border-bottom-width"] = css("borderBottomWeight", px(node.borderBottomWeight));
|
|
1577
1578
|
style["border-left-width"] = css("borderLeftWeight", px(node.borderLeftWeight));
|
|
1578
1579
|
}
|
|
1580
|
+
/**
|
|
1581
|
+
* CSS gives an element one border, so the visible strokes after the first become rings in
|
|
1582
|
+
* `box-shadow`, in layer order. An outside ring grows from the border's outer edge, an inside one
|
|
1583
|
+
* from its inner edge, and a centred stroke gets a ring on each side of the border. Rings cannot
|
|
1584
|
+
* dash or take per-side weights, and outer and inner rings paint in separate CSS layers, so a
|
|
1585
|
+
* stroke order that alternates them is approximated.
|
|
1586
|
+
*/
|
|
1587
|
+
function extraStrokeShadows(strokes, first) {
|
|
1588
|
+
const outer = [];
|
|
1589
|
+
const inner = [];
|
|
1590
|
+
let outside = 0;
|
|
1591
|
+
let inside = first.weight;
|
|
1592
|
+
for (const stroke of strokes) {
|
|
1593
|
+
if (!stroke.visible) continue;
|
|
1594
|
+
const color = strokeColorToCSS(stroke);
|
|
1595
|
+
if (!color) continue;
|
|
1596
|
+
if (stroke.align !== "OUTSIDE") {
|
|
1597
|
+
inside += stroke.align === "CENTER" ? stroke.weight / 2 : stroke.weight;
|
|
1598
|
+
inner.push(`inset 0 0 0 ${px(inside)} ${color}`);
|
|
1599
|
+
}
|
|
1600
|
+
if (stroke.align !== "INSIDE") {
|
|
1601
|
+
outside += stroke.align === "CENTER" ? stroke.weight / 2 : stroke.weight;
|
|
1602
|
+
outer.push(`0 0 0 ${px(outside)} ${color}`);
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
return [...outer, ...inner];
|
|
1606
|
+
}
|
|
1579
1607
|
const PADDING_FIELDS = [
|
|
1580
1608
|
"paddingTop",
|
|
1581
1609
|
"paddingRight",
|
|
@@ -1686,6 +1714,29 @@ function addBoxSizing(style) {
|
|
|
1686
1714
|
const boxed = Object.keys(style).some((property) => property.startsWith("padding") || property.startsWith("border") && !property.endsWith("radius"));
|
|
1687
1715
|
if (sized && boxed) style["box-sizing"] = "border-box";
|
|
1688
1716
|
}
|
|
1717
|
+
/**
|
|
1718
|
+
* The CSS keyword for a blend mode, where one paints the same mode. Linear burn would need
|
|
1719
|
+
* `plus-darker`, which Chrome, Edge, and Firefox do not implement, so it gains no keyword and
|
|
1720
|
+
* exports flat. A normal layer or an unscoped pass-through needs none either.
|
|
1721
|
+
*/
|
|
1722
|
+
const CSS_BLEND_MODES = {
|
|
1723
|
+
DARKEN: "darken",
|
|
1724
|
+
MULTIPLY: "multiply",
|
|
1725
|
+
COLOR_BURN: "color-burn",
|
|
1726
|
+
LIGHTEN: "lighten",
|
|
1727
|
+
SCREEN: "screen",
|
|
1728
|
+
LINEAR_DODGE: "plus-lighter",
|
|
1729
|
+
COLOR_DODGE: "color-dodge",
|
|
1730
|
+
OVERLAY: "overlay",
|
|
1731
|
+
SOFT_LIGHT: "soft-light",
|
|
1732
|
+
HARD_LIGHT: "hard-light",
|
|
1733
|
+
DIFFERENCE: "difference",
|
|
1734
|
+
EXCLUSION: "exclusion",
|
|
1735
|
+
HUE: "hue",
|
|
1736
|
+
SATURATION: "saturation",
|
|
1737
|
+
COLOR: "color",
|
|
1738
|
+
LUMINOSITY: "luminosity"
|
|
1739
|
+
};
|
|
1689
1740
|
function styleFromSceneNode(node, parent, css) {
|
|
1690
1741
|
const style = {};
|
|
1691
1742
|
addSize(style, node, parent, css);
|
|
@@ -1698,7 +1749,14 @@ function styleFromSceneNode(node, parent, css) {
|
|
|
1698
1749
|
addImageStyle(style, node);
|
|
1699
1750
|
addStroke(style, node, css);
|
|
1700
1751
|
Object.assign(style, effectsToCSS(node.effects));
|
|
1752
|
+
const visibleStrokes = node.strokes.filter((stroke) => stroke.visible);
|
|
1753
|
+
if (visibleStrokes.length > 1) {
|
|
1754
|
+
const rings = extraStrokeShadows(visibleStrokes.slice(1), visibleStrokes[0]);
|
|
1755
|
+
if (rings.length > 0) style["box-shadow"] = [rings.join(", "), style["box-shadow"]].filter((shadow) => Boolean(shadow)).join(", ");
|
|
1756
|
+
}
|
|
1701
1757
|
if (node.opacity < 1) style.opacity = css("opacity", String(node.opacity));
|
|
1758
|
+
const blend = CSS_BLEND_MODES[node.blendMode];
|
|
1759
|
+
if (blend) style["mix-blend-mode"] = blend;
|
|
1702
1760
|
addCornerRadii(style, node, css);
|
|
1703
1761
|
if (node.clipsContent) style.overflow = "hidden";
|
|
1704
1762
|
const alignSelf = alignToCSS(node.layoutAlignSelf);
|
|
@@ -1802,6 +1860,53 @@ function vectorDesignNode(context, node, attrs, style) {
|
|
|
1802
1860
|
sourceSceneNode: node
|
|
1803
1861
|
};
|
|
1804
1862
|
}
|
|
1863
|
+
/** The outline a vector mask clips siblings to, in the sibling's own box; null where CSS cannot. */
|
|
1864
|
+
function vectorMaskClip(mask, sibling) {
|
|
1865
|
+
if (mask.maskType !== "VECTOR" || mask.childIds.length > 0 || mask.rotation !== 0) return null;
|
|
1866
|
+
const left = mask.x - sibling.x;
|
|
1867
|
+
const top = mask.y - sibling.y;
|
|
1868
|
+
if (mask.type === "RECTANGLE") {
|
|
1869
|
+
const right = left + mask.width;
|
|
1870
|
+
const bottom = top + mask.height;
|
|
1871
|
+
return `polygon(${px(left)} ${px(top)}, ${px(right)} ${px(top)}, ${px(right)} ${px(bottom)}, ${px(left)} ${px(bottom)})`;
|
|
1872
|
+
}
|
|
1873
|
+
if (mask.type === "ELLIPSE") {
|
|
1874
|
+
const rx = mask.width / 2;
|
|
1875
|
+
const ry = mask.height / 2;
|
|
1876
|
+
return `ellipse(${px(rx)} ${px(ry)} at ${px(left + rx)} ${px(top + ry)})`;
|
|
1877
|
+
}
|
|
1878
|
+
return null;
|
|
1879
|
+
}
|
|
1880
|
+
/**
|
|
1881
|
+
* Projects a parent's children as the canvas draws them: a mask layer is never a layer of its
|
|
1882
|
+
* own, and one vector mask clips the siblings after it. Alpha and luminance masks have no
|
|
1883
|
+
* faithful CSS form over several elements, and two masks combine, so those stay unclipped.
|
|
1884
|
+
*/
|
|
1885
|
+
function projectChildren(context, parent, modes, root) {
|
|
1886
|
+
const projected = [];
|
|
1887
|
+
let masks = [];
|
|
1888
|
+
let afterMask = false;
|
|
1889
|
+
for (const child of nodeChildren(context.graph, parent)) {
|
|
1890
|
+
if (child.visible && child.isMask) {
|
|
1891
|
+
masks = afterMask ? [...masks, child] : [child];
|
|
1892
|
+
afterMask = true;
|
|
1893
|
+
continue;
|
|
1894
|
+
}
|
|
1895
|
+
afterMask = false;
|
|
1896
|
+
const node = sceneNodeToDesignNode(context, child, modes, root);
|
|
1897
|
+
if (!node) continue;
|
|
1898
|
+
const mask = masks.length === 1 ? masks[0] : null;
|
|
1899
|
+
if (mask && node.type === "element") {
|
|
1900
|
+
const clip = vectorMaskClip(mask, child);
|
|
1901
|
+
if (clip) node.inlineStyle = {
|
|
1902
|
+
...node.inlineStyle,
|
|
1903
|
+
"clip-path": clip
|
|
1904
|
+
};
|
|
1905
|
+
}
|
|
1906
|
+
projected.push(node);
|
|
1907
|
+
}
|
|
1908
|
+
return projected;
|
|
1909
|
+
}
|
|
1805
1910
|
function sceneNodeToDesignNode(context, node, inherited, root) {
|
|
1806
1911
|
if (!node.visible && !context.shown?.has(node.id) || node.internalOnly) return null;
|
|
1807
1912
|
const { graph, tokens } = context;
|
|
@@ -1829,7 +1934,7 @@ function sceneNodeToDesignNode(context, node, inherited, root) {
|
|
|
1829
1934
|
const vector = vectorDesignNode(context, node, attrs, styleFromSceneNode(node, parent, css));
|
|
1830
1935
|
if (vector) return vector;
|
|
1831
1936
|
}
|
|
1832
|
-
const children =
|
|
1937
|
+
const children = projectChildren(context, node, modes, false);
|
|
1833
1938
|
if (node.type === "CANVAS") return {
|
|
1834
1939
|
type: "element",
|
|
1835
1940
|
tagName: "main",
|
|
@@ -1866,7 +1971,7 @@ function sceneGraphToDesignDocument(graph, options = {}) {
|
|
|
1866
1971
|
const root = graph.getNode(options.rootId ?? graph.rootId);
|
|
1867
1972
|
const context = projectionContext(graph, options);
|
|
1868
1973
|
const modes = context.tokens?.defaultModes() ?? /* @__PURE__ */ new Map();
|
|
1869
|
-
const children = root ?
|
|
1974
|
+
const children = root ? projectChildren(context, root, modes, true) : [];
|
|
1870
1975
|
return {
|
|
1871
1976
|
type: "document",
|
|
1872
1977
|
sourceGraph: graph,
|