@pairdraw/core 0.19.1 → 0.19.2
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/canvas/images/previews.d.ts +1 -1
- package/dist/canvas/overlays/index.d.ts +2 -2
- package/dist/canvas/overlays/index.js +2 -2
- package/dist/canvas/overlays/selection.d.ts +1 -2
- package/dist/canvas/overlays/selection.d.ts.map +1 -1
- package/dist/canvas/overlays/selection.js +2 -9
- package/dist/canvas/overlays/selection.js.map +1 -1
- package/dist/canvas/renderer/fonts.d.ts.map +1 -1
- package/dist/canvas/renderer/fonts.js +2 -0
- package/dist/canvas/renderer/fonts.js.map +1 -1
- package/dist/canvas/renderer/methods.d.ts +0 -1
- package/dist/canvas/renderer/methods.d.ts.map +1 -1
- package/dist/canvas/renderer/methods.js +1 -4
- package/dist/canvas/renderer/methods.js.map +1 -1
- package/dist/canvas/renderer/overlay-pass.d.ts.map +1 -1
- package/dist/canvas/renderer/overlay-pass.js +0 -1
- package/dist/canvas/renderer/overlay-pass.js.map +1 -1
- package/dist/canvas/renderer/pipeline.d.ts.map +1 -1
- package/dist/canvas/renderer/pipeline.js +0 -1
- package/dist/canvas/renderer/pipeline.js.map +1 -1
- package/dist/canvas/renderer/types.d.ts +0 -1
- package/dist/canvas/renderer/types.d.ts.map +1 -1
- package/dist/canvas/renderer.d.ts +0 -1
- package/dist/canvas/renderer.d.ts.map +1 -1
- package/dist/canvas/renderer.js.map +1 -1
- package/dist/design-jsx/index.d.ts +2 -2
- package/dist/design-jsx/index.js +2 -2
- package/dist/design-jsx/renderer.d.ts +3 -2
- package/dist/design-jsx/renderer.d.ts.map +1 -1
- package/dist/design-jsx/renderer.js +2 -2
- package/dist/design-jsx/renderer.js.map +1 -1
- package/dist/editor/clipboard/snapshot.d.ts.map +1 -1
- package/dist/editor/clipboard/snapshot.js +12 -4
- package/dist/editor/clipboard/snapshot.js.map +1 -1
- package/dist/editor/clipboard.d.ts.map +1 -1
- package/dist/editor/clipboard.js +2 -7
- package/dist/editor/clipboard.js.map +1 -1
- package/dist/editor/color-space.d.ts.map +1 -1
- package/dist/editor/color-space.js +33 -38
- package/dist/editor/color-space.js.map +1 -1
- package/dist/editor/create.d.ts +9 -5
- package/dist/editor/create.d.ts.map +1 -1
- package/dist/editor/create.js +10 -1
- package/dist/editor/create.js.map +1 -1
- package/dist/editor/history/edit.d.ts.map +1 -1
- package/dist/editor/history/edit.js +2 -1
- package/dist/editor/history/edit.js.map +1 -1
- package/dist/editor/nudge.js +1 -1
- package/dist/editor/nudge.js.map +1 -1
- package/dist/editor/read-only/actions.js +2 -0
- package/dist/editor/read-only/actions.js.map +1 -1
- package/dist/editor/selection/container.d.ts +2 -0
- package/dist/editor/selection/container.d.ts.map +1 -1
- package/dist/editor/selection/container.js +17 -1
- package/dist/editor/selection/container.js.map +1 -1
- package/dist/editor/selection/navigation.js +7 -4
- package/dist/editor/selection/navigation.js.map +1 -1
- package/dist/editor/selection/read.d.ts +2 -2
- package/dist/editor/selection.d.ts +4 -2
- package/dist/editor/selection.d.ts.map +1 -1
- package/dist/editor/shapes/with-text.d.ts.map +1 -1
- package/dist/editor/shapes/with-text.js +2 -5
- package/dist/editor/shapes/with-text.js.map +1 -1
- package/dist/editor/structure/auto-layout-wrap.js +1 -1
- package/dist/editor/structure/auto-layout-wrap.js.map +1 -1
- package/dist/editor/structure/boolean.d.ts +2 -3
- package/dist/editor/structure/boolean.d.ts.map +1 -1
- package/dist/editor/structure/boolean.js.map +1 -1
- package/dist/editor/structure/group/bounds.d.ts.map +1 -1
- package/dist/editor/structure/group/bounds.js +1 -3
- package/dist/editor/structure/group/bounds.js.map +1 -1
- package/dist/editor/structure/outline-stroke.js +3 -3
- package/dist/editor/structure/outline-stroke.js.map +1 -1
- package/dist/editor/structure/reorder.d.ts.map +1 -1
- package/dist/editor/structure/reorder.js +7 -14
- package/dist/editor/structure/reorder.js.map +1 -1
- package/dist/editor/structure/state.js +1 -1
- package/dist/editor/structure/state.js.map +1 -1
- package/dist/editor/structure.d.ts +1 -2
- package/dist/editor/structure.d.ts.map +1 -1
- package/dist/editor/structure.js +3 -3
- package/dist/editor/structure.js.map +1 -1
- package/dist/editor/types.d.ts +4 -1
- package/dist/editor/types.d.ts.map +1 -1
- package/dist/editor/variables.d.ts.map +1 -1
- package/dist/editor/variables.js +57 -1
- package/dist/editor/variables.js.map +1 -1
- package/dist/figma-api/accessors/text/index.d.ts.map +1 -1
- package/dist/figma-api/accessors/text/index.js +4 -1
- package/dist/figma-api/accessors/text/index.js.map +1 -1
- package/dist/figma-api/fonts/loaded.d.ts +3 -1
- package/dist/figma-api/fonts/loaded.d.ts.map +1 -1
- package/dist/figma-api/fonts/loaded.js +8 -1
- package/dist/figma-api/fonts/loaded.js.map +1 -1
- package/dist/figma-api/index.d.ts +13 -0
- package/dist/figma-api/index.d.ts.map +1 -1
- package/dist/figma-api/index.js +13 -0
- package/dist/figma-api/index.js.map +1 -1
- package/dist/figma-api/serialization.d.ts.map +1 -1
- package/dist/figma-api/serialization.js +2 -1
- package/dist/figma-api/serialization.js.map +1 -1
- package/dist/figma-api/shape-details.d.ts +26 -0
- package/dist/figma-api/shape-details.d.ts.map +1 -0
- package/dist/figma-api/shape-details.js +28 -0
- package/dist/figma-api/shape-details.js.map +1 -0
- package/dist/figma-api/shape-with-text.js +2 -5
- package/dist/figma-api/shape-with-text.js.map +1 -1
- package/dist/figma-api/surface.d.ts +1 -1
- package/dist/figma-api/surface.d.ts.map +1 -1
- package/dist/figma-api/surface.js +1 -0
- package/dist/figma-api/surface.js.map +1 -1
- package/dist/icons/svg.js +1 -1
- package/dist/icons/svg.js.map +1 -1
- package/dist/io/formats/drawio/index.d.ts.map +1 -1
- package/dist/io/formats/drawio/index.js +1 -1
- package/dist/io/formats/drawio/index.js.map +1 -1
- package/dist/io/formats/html/layers.d.ts +7 -4
- package/dist/io/formats/html/layers.d.ts.map +1 -1
- package/dist/io/formats/html/layers.js +4 -1
- package/dist/io/formats/html/layers.js.map +1 -1
- package/dist/io/formats/pdf/dom.d.ts +6 -1
- package/dist/io/formats/pdf/dom.d.ts.map +1 -1
- package/dist/io/formats/pdf/dom.js +9 -22
- package/dist/io/formats/pdf/dom.js.map +1 -1
- package/dist/io/formats/pdf/export.js +1 -1
- package/dist/io/formats/pdf/export.js.map +1 -1
- package/dist/io/formats/svg/defs.d.ts.map +1 -1
- package/dist/io/formats/svg/defs.js +3 -1
- package/dist/io/formats/svg/defs.js.map +1 -1
- package/dist/io/formats/svg/export.d.ts.map +1 -1
- package/dist/io/formats/svg/export.js +52 -16
- package/dist/io/formats/svg/export.js.map +1 -1
- package/dist/layout/apply.d.ts.map +1 -1
- package/dist/layout/apply.js +50 -12
- package/dist/layout/apply.js.map +1 -1
- package/dist/layout/derived.d.ts +7 -1
- package/dist/layout/derived.d.ts.map +1 -1
- package/dist/layout/derived.js +11 -1
- package/dist/layout/derived.js.map +1 -1
- package/dist/layout/grid.d.ts +7 -2
- package/dist/layout/grid.d.ts.map +1 -1
- package/dist/layout/grid.js +27 -9
- package/dist/layout/grid.js.map +1 -1
- package/dist/layout/mutations.d.ts +1 -0
- package/dist/layout/mutations.d.ts.map +1 -1
- package/dist/layout/mutations.js +10 -0
- package/dist/layout/mutations.js.map +1 -1
- package/dist/layout/strokes.d.ts +9 -1
- package/dist/layout/strokes.d.ts.map +1 -1
- package/dist/layout/strokes.js +12 -1
- package/dist/layout/strokes.js.map +1 -1
- package/dist/layout/yoga-helpers.d.ts +3 -1
- package/dist/layout/yoga-helpers.d.ts.map +1 -1
- package/dist/layout/yoga-helpers.js +16 -2
- package/dist/layout/yoga-helpers.js.map +1 -1
- package/dist/layout.d.ts.map +1 -1
- package/dist/layout.js +71 -38
- package/dist/layout.js.map +1 -1
- package/dist/lint/linter.d.ts +1 -0
- package/dist/lint/linter.d.ts.map +1 -1
- package/dist/lint/linter.js +13 -1
- package/dist/lint/linter.js.map +1 -1
- package/dist/lint/rules/effect-style-required.js +1 -0
- package/dist/lint/rules/effect-style-required.js.map +1 -1
- package/dist/lint/rules/text-style-required.js +1 -0
- package/dist/lint/rules/text-style-required.js.map +1 -1
- package/dist/lint/types.d.ts +7 -0
- package/dist/lint/types.d.ts.map +1 -1
- package/dist/rpc/commands.d.ts +2 -2
- package/dist/rpc/index.d.ts +2 -2
- package/dist/rpc/read-commands.d.ts +18 -8
- package/dist/rpc/read-commands.d.ts.map +1 -1
- package/dist/rpc/read-commands.js +18 -9
- package/dist/rpc/read-commands.js.map +1 -1
- package/dist/text/font/faces.d.ts +12 -1
- package/dist/text/font/faces.d.ts.map +1 -1
- package/dist/text/font/faces.js +15 -1
- package/dist/text/font/faces.js.map +1 -1
- package/dist/text/font/manager.d.ts.map +1 -1
- package/dist/text/font/manager.js +6 -11
- package/dist/text/font/manager.js.map +1 -1
- package/dist/text/index.d.ts +2 -2
- package/dist/text/index.js +2 -2
- package/dist/tools/modify/layout.d.ts.map +1 -1
- package/dist/tools/modify/layout.js +1 -0
- package/dist/tools/modify/layout.js.map +1 -1
- package/dist/tools/read/nodes.d.ts.map +1 -1
- package/dist/tools/read/nodes.js.map +1 -1
- package/dist/tools/structure/batch.d.ts.map +1 -1
- package/dist/tools/structure/batch.js +14 -0
- package/dist/tools/structure/batch.js.map +1 -1
- package/dist/tools/vector/path.d.ts.map +1 -1
- package/dist/tools/vector/path.js +39 -7
- package/dist/tools/vector/path.js.map +1 -1
- package/dist/xpath.d.ts +3 -0
- package/dist/xpath.d.ts.map +1 -1
- package/dist/xpath.js +3 -1
- package/dist/xpath.js.map +1 -1
- package/package.json +9 -9
- package/src/canvas/overlays/index.ts +0 -1
- package/src/canvas/overlays/selection.ts +1 -12
- package/src/canvas/renderer/fonts.ts +2 -0
- package/src/canvas/renderer/methods.ts +0 -9
- package/src/canvas/renderer/overlay-pass.ts +0 -1
- package/src/canvas/renderer/pipeline.ts +0 -1
- package/src/canvas/renderer/types.ts +0 -1
- package/src/canvas/renderer.ts +0 -1
- package/src/design-jsx/index.ts +1 -1
- package/src/design-jsx/renderer.ts +1 -1
- package/src/editor/clipboard/snapshot.ts +21 -4
- package/src/editor/clipboard.ts +4 -14
- package/src/editor/color-space.ts +47 -38
- package/src/editor/create.ts +14 -2
- package/src/editor/history/edit.ts +5 -1
- package/src/editor/nudge.ts +1 -1
- package/src/editor/read-only/actions.ts +2 -0
- package/src/editor/selection/container.ts +24 -1
- package/src/editor/selection/navigation.ts +8 -6
- package/src/editor/shapes/with-text.ts +2 -5
- package/src/editor/structure/auto-layout-wrap.ts +1 -1
- package/src/editor/structure/boolean.ts +1 -3
- package/src/editor/structure/group/bounds.ts +1 -5
- package/src/editor/structure/outline-stroke.ts +3 -3
- package/src/editor/structure/reorder.ts +8 -14
- package/src/editor/structure/state.ts +1 -1
- package/src/editor/structure.ts +5 -8
- package/src/editor/types.ts +4 -1
- package/src/editor/variables.ts +74 -2
- package/src/figma-api/accessors/text/index.ts +10 -1
- package/src/figma-api/fonts/loaded.ts +9 -0
- package/src/figma-api/index.ts +25 -0
- package/src/figma-api/serialization.ts +2 -1
- package/src/figma-api/shape-details.ts +50 -0
- package/src/figma-api/shape-with-text.ts +2 -5
- package/src/figma-api/surface.ts +1 -0
- package/src/global.d.ts +5 -0
- package/src/icons/svg.ts +3 -1
- package/src/io/formats/drawio/index.ts +3 -1
- package/src/io/formats/html/layers.ts +11 -2
- package/src/io/formats/pdf/dom.ts +10 -18
- package/src/io/formats/pdf/export.ts +1 -1
- package/src/io/formats/svg/defs.ts +10 -1
- package/src/io/formats/svg/export.ts +94 -19
- package/src/layout/apply.ts +87 -21
- package/src/layout/derived.ts +14 -0
- package/src/layout/grid.ts +33 -9
- package/src/layout/mutations.ts +13 -1
- package/src/layout/strokes.ts +12 -0
- package/src/layout/yoga-helpers.ts +18 -1
- package/src/layout.ts +94 -49
- package/src/lint/linter.ts +17 -2
- package/src/lint/rules/effect-style-required.ts +2 -0
- package/src/lint/rules/text-style-required.ts +2 -0
- package/src/lint/types.ts +8 -0
- package/src/rpc/index.ts +1 -0
- package/src/rpc/read-commands.ts +40 -23
- package/src/text/font/faces.ts +22 -0
- package/src/text/font/manager.ts +9 -11
- package/src/tools/modify/layout.ts +5 -0
- package/src/tools/read/nodes.ts +0 -1
- package/src/tools/structure/batch.ts +22 -0
- package/src/tools/vector/path.ts +30 -6
- package/src/xpath.ts +6 -1
package/dist/layout/derived.d.ts
CHANGED
|
@@ -13,6 +13,12 @@ declare function markFlowChanged(graph: SceneGraph, parentId: string): void;
|
|
|
13
13
|
*/
|
|
14
14
|
declare function keepsSavedLayout(graph: SceneGraph, frame: SceneNode): boolean;
|
|
15
15
|
declare function usesDetachedDerivedLayout(graph: SceneGraph, child: SceneNode): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Whether layout holds a frame's saved size exactly: a frame read from a file whose flow and
|
|
18
|
+
* layout are still untouched by the user or a tool. Growing it to its padding belongs to an
|
|
19
|
+
* edit, so opening or re-laying out a file leaves the saved size alone.
|
|
20
|
+
*/
|
|
21
|
+
declare function keepsSavedFrameSize(graph: SceneGraph, frame: SceneNode): boolean;
|
|
16
22
|
//#endregion
|
|
17
|
-
export { keepsSavedLayout, markFlowChanged, usesDetachedDerivedLayout };
|
|
23
|
+
export { keepsSavedFrameSize, keepsSavedLayout, markFlowChanged, usesDetachedDerivedLayout };
|
|
18
24
|
//# sourceMappingURL=derived.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"derived.d.ts","names":[],"sources":["../../src/layout/derived.ts"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"derived.d.ts","names":[],"sources":["../../src/layout/derived.ts"],"mappings":";;;;;;iBAoDgB,gBAAgB,OAAO,YAAY;;;;;;;iBAUnC,iBAAiB,OAAO,YAAY,OAAO;iBAwB3C,0BAA0B,OAAO,YAAY,OAAO;;;;;;iBAkBpD,oBAAoB,OAAO,YAAY,OAAO"}
|
package/dist/layout/derived.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { takesOwnSize } from "@pairdraw/scene-graph/instances";
|
|
1
2
|
import { LAYOUT_AFFECTING_KEYS } from "@pairdraw/scene-graph/fields";
|
|
2
3
|
//#region src/layout/derived.ts
|
|
3
4
|
/** Edits that make saved geometry stale: layout fields, visibility, and text laid out again. */
|
|
@@ -75,7 +76,16 @@ function usesDetachedDerivedLayout(graph, child) {
|
|
|
75
76
|
const heightSizing = isRow ? child.counterAxisSizing : child.primaryAxisSizing;
|
|
76
77
|
return (widthSizing === "HUG" && derived.width !== void 0 || heightSizing === "HUG" && derived.height !== void 0) && keepsSavedLayout(graph, child);
|
|
77
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Whether layout holds a frame's saved size exactly: a frame read from a file whose flow and
|
|
81
|
+
* layout are still untouched by the user or a tool. Growing it to its padding belongs to an
|
|
82
|
+
* edit, so opening or re-laying out a file leaves the saved size alone.
|
|
83
|
+
*/
|
|
84
|
+
function keepsSavedFrameSize(graph, frame) {
|
|
85
|
+
if (frame.type === "INSTANCE" || !takesOwnSize(graph, frame)) return true;
|
|
86
|
+
return (frame.source.format === "fig" || frame.derivedLayout != null) && keepsSavedLayout(graph, frame);
|
|
87
|
+
}
|
|
78
88
|
//#endregion
|
|
79
|
-
export { keepsSavedLayout, markFlowChanged, usesDetachedDerivedLayout };
|
|
89
|
+
export { keepsSavedFrameSize, keepsSavedLayout, markFlowChanged, usesDetachedDerivedLayout };
|
|
80
90
|
|
|
81
91
|
//# sourceMappingURL=derived.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"derived.js","names":[],"sources":["../../src/layout/derived.ts"],"sourcesContent":["import type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'\nimport { LAYOUT_AFFECTING_KEYS } from '@pairdraw/scene-graph/fields'\n\n/** Edits that make saved geometry stale: layout fields, visibility, and text laid out again. */\nconst LAYOUT_EDIT_FIELDS: ReadonlySet<string> = new Set([\n ...LAYOUT_AFFECTING_KEYS,\n 'visible',\n 'derivedLayout'\n])\n\nfunction hasEditedLayout(node: SceneNode): boolean {\n return node.source.editedFields.some((field) => LAYOUT_EDIT_FIELDS.has(field))\n}\n\n/** The layers in each .fig frame's flow when layout first saw them, as its page opened. */\nconst savedFlows = new WeakMap<SceneGraph, Map<string, string>>()\n/** Frames whose flow, or a flow inside it, lost or gained a layer since; kept until reload. */\nconst restructured = new WeakMap<SceneGraph, Set<string>>()\n/** Frames that last laid out without their saved geometry, for the frames around them. */\nconst reflowed = new WeakMap<SceneGraph, Set<string>>()\n\nfunction graphState<T>(states: WeakMap<SceneGraph, T>, graph: SceneGraph, create: () => T): T {\n let state = states.get(graph)\n if (!state) {\n state = create()\n states.set(graph, state)\n }\n return state\n}\n\nfunction flowOf(graph: SceneGraph, node: SceneNode): SceneNode[] {\n return graph.getChildren(node.id).filter((child) => child.layoutPositioning !== 'ABSOLUTE')\n}\n\n/**\n * Loading a file adds, moves, and removes layers too, so structure edits are told from it by\n * comparing the layers in a frame's flow, in order, with those layout first saw. A layer that\n * ignores auto layout, or stops ignoring it, leaves or joins the flow.\n */\nfunction flowChanged(graph: SceneGraph, node: SceneNode, flow = flowOf(graph, node)): boolean {\n const seen = graphState(savedFlows, graph, () => new Map<string, string>())\n const ids = flow.map((child) => child.id).join(',')\n const saved = seen.get(node.id)\n if (saved === undefined) seen.set(node.id, ids)\n return saved !== undefined && saved !== ids\n}\n\n/**\n * Records that a layer joined, left, or moved in `parentId`'s children, as an editor sees an edit\n * happen, so the frame stops keeping saved geometry even if layout never ran on it before.\n */\nexport function markFlowChanged(graph: SceneGraph, parentId: string): void {\n graphState(restructured, graph, () => new Set<string>()).add(parentId)\n}\n\n/**\n * Whether layout still keeps the geometry saved for `frame` and its flow. A frame stops once a\n * layer joins, leaves, or moves in its flow or a flow inside it, or once it or a layer in its flow\n * has its layout edited, such as by adding auto layout or a gap or restyling text. That holds for\n * layers read without a source format too, as a .fig file's components and instances are.\n */\nexport function keepsSavedLayout(graph: SceneGraph, frame: SceneNode): boolean {\n const fromFig = frame.source.format === 'fig'\n const flow = flowOf(graph, frame)\n // Nothing saved to keep: the usual case outside documents read from files.\n if (!fromFig && !frame.derivedLayout && !flow.some((child) => child.derivedLayout)) return true\n const stale = graphState(restructured, graph, () => new Set<string>())\n if (stale.has(frame.id)) return false\n // Layout runs inner frames first, so a changed flow deep inside is already marked.\n if (\n flowChanged(graph, frame, flow) ||\n flow.some((child) => stale.has(child.id) || flowChanged(graph, child))\n ) {\n stale.add(frame.id)\n return false\n }\n // A flow inside edited by now resizes this one, but a cancelled preview gives it back.\n const last = graphState(reflowed, graph, () => new Set<string>())\n const keeps =\n !hasEditedLayout(frame) && !flow.some((child) => hasEditedLayout(child) || last.has(child.id))\n if (keeps) last.delete(frame.id)\n else last.add(frame.id)\n return keeps\n}\n\nexport function usesDetachedDerivedLayout(graph: SceneGraph, child: SceneNode): boolean {\n const derived = child.derivedLayout\n if (!derived || child.layoutMode === 'NONE' || child.layoutGrow > 0) return false\n const isRow = child.layoutMode === 'HORIZONTAL'\n const widthSizing = isRow ? child.primaryAxisSizing : child.counterAxisSizing\n const heightSizing = isRow ? child.counterAxisSizing : child.primaryAxisSizing\n return (\n ((widthSizing === 'HUG' && derived.width !== undefined) ||\n (heightSizing === 'HUG' && derived.height !== undefined)) &&\n keepsSavedLayout(graph, child)\n )\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"derived.js","names":[],"sources":["../../src/layout/derived.ts"],"sourcesContent":["import type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'\nimport { LAYOUT_AFFECTING_KEYS } from '@pairdraw/scene-graph/fields'\nimport { takesOwnSize } from '@pairdraw/scene-graph/instances'\n\n/** Edits that make saved geometry stale: layout fields, visibility, and text laid out again. */\nconst LAYOUT_EDIT_FIELDS: ReadonlySet<string> = new Set([\n ...LAYOUT_AFFECTING_KEYS,\n 'visible',\n 'derivedLayout'\n])\n\nfunction hasEditedLayout(node: SceneNode): boolean {\n return node.source.editedFields.some((field) => LAYOUT_EDIT_FIELDS.has(field))\n}\n\n/** The layers in each .fig frame's flow when layout first saw them, as its page opened. */\nconst savedFlows = new WeakMap<SceneGraph, Map<string, string>>()\n/** Frames whose flow, or a flow inside it, lost or gained a layer since; kept until reload. */\nconst restructured = new WeakMap<SceneGraph, Set<string>>()\n/** Frames that last laid out without their saved geometry, for the frames around them. */\nconst reflowed = new WeakMap<SceneGraph, Set<string>>()\n\nfunction graphState<T>(states: WeakMap<SceneGraph, T>, graph: SceneGraph, create: () => T): T {\n let state = states.get(graph)\n if (!state) {\n state = create()\n states.set(graph, state)\n }\n return state\n}\n\nfunction flowOf(graph: SceneGraph, node: SceneNode): SceneNode[] {\n return graph.getChildren(node.id).filter((child) => child.layoutPositioning !== 'ABSOLUTE')\n}\n\n/**\n * Loading a file adds, moves, and removes layers too, so structure edits are told from it by\n * comparing the layers in a frame's flow, in order, with those layout first saw. A layer that\n * ignores auto layout, or stops ignoring it, leaves or joins the flow.\n */\nfunction flowChanged(graph: SceneGraph, node: SceneNode, flow = flowOf(graph, node)): boolean {\n const seen = graphState(savedFlows, graph, () => new Map<string, string>())\n const ids = flow.map((child) => child.id).join(',')\n const saved = seen.get(node.id)\n if (saved === undefined) seen.set(node.id, ids)\n return saved !== undefined && saved !== ids\n}\n\n/**\n * Records that a layer joined, left, or moved in `parentId`'s children, as an editor sees an edit\n * happen, so the frame stops keeping saved geometry even if layout never ran on it before.\n */\nexport function markFlowChanged(graph: SceneGraph, parentId: string): void {\n graphState(restructured, graph, () => new Set<string>()).add(parentId)\n}\n\n/**\n * Whether layout still keeps the geometry saved for `frame` and its flow. A frame stops once a\n * layer joins, leaves, or moves in its flow or a flow inside it, or once it or a layer in its flow\n * has its layout edited, such as by adding auto layout or a gap or restyling text. That holds for\n * layers read without a source format too, as a .fig file's components and instances are.\n */\nexport function keepsSavedLayout(graph: SceneGraph, frame: SceneNode): boolean {\n const fromFig = frame.source.format === 'fig'\n const flow = flowOf(graph, frame)\n // Nothing saved to keep: the usual case outside documents read from files.\n if (!fromFig && !frame.derivedLayout && !flow.some((child) => child.derivedLayout)) return true\n const stale = graphState(restructured, graph, () => new Set<string>())\n if (stale.has(frame.id)) return false\n // Layout runs inner frames first, so a changed flow deep inside is already marked.\n if (\n flowChanged(graph, frame, flow) ||\n flow.some((child) => stale.has(child.id) || flowChanged(graph, child))\n ) {\n stale.add(frame.id)\n return false\n }\n // A flow inside edited by now resizes this one, but a cancelled preview gives it back.\n const last = graphState(reflowed, graph, () => new Set<string>())\n const keeps =\n !hasEditedLayout(frame) && !flow.some((child) => hasEditedLayout(child) || last.has(child.id))\n if (keeps) last.delete(frame.id)\n else last.add(frame.id)\n return keeps\n}\n\nexport function usesDetachedDerivedLayout(graph: SceneGraph, child: SceneNode): boolean {\n const derived = child.derivedLayout\n if (!derived || child.layoutMode === 'NONE' || child.layoutGrow > 0) return false\n const isRow = child.layoutMode === 'HORIZONTAL'\n const widthSizing = isRow ? child.primaryAxisSizing : child.counterAxisSizing\n const heightSizing = isRow ? child.counterAxisSizing : child.primaryAxisSizing\n return (\n ((widthSizing === 'HUG' && derived.width !== undefined) ||\n (heightSizing === 'HUG' && derived.height !== undefined)) &&\n keepsSavedLayout(graph, child)\n )\n}\n\n/**\n * Whether layout holds a frame's saved size exactly: a frame read from a file whose flow and\n * layout are still untouched by the user or a tool. Growing it to its padding belongs to an\n * edit, so opening or re-laying out a file leaves the saved size alone.\n */\nexport function keepsSavedFrameSize(graph: SceneGraph, frame: SceneNode): boolean {\n // An instance shows the size its main or an override gives it, as do the layers inside it; their\n // own padding is not an edit this layout may grow them from.\n if (frame.type === 'INSTANCE' || !takesOwnSize(graph, frame)) return true\n const readFromFile = frame.source.format === 'fig' || frame.derivedLayout != null\n return readFromFile && keepsSavedLayout(graph, frame)\n}\n"],"mappings":";;;;AAKA,MAAM,qCAA0C,IAAI,IAAI;CACtD,GAAG;CACH;CACA;AACF,CAAC;AAED,SAAS,gBAAgB,MAA0B;CACjD,OAAO,KAAK,OAAO,aAAa,MAAM,UAAU,mB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AO,OAAO,IAAI,KAC9B,KAAK,MAAM,UAAU,MAAM,IAAI,MAAM,EAAE,KAAK,YAAY,OAAO,KAAK,CAAC,GACrE;EACA,MAAM,IAAI,MAAM,EAAE;EAClB,OAAO;CACT;CAEA,MAAM,OAAO,WAAW,UAAU,6BAAa,IAAI,IAAY,CAAC;CAChE,MAAM,QACJ,CAAC,gBAAgB,KAAK,KAAK,CAAC,KAAK,MAAM,UAAU,gBAAgB,KAAK,KAAK,KAAK,IAAI,MAAM,EAAE,CAAC;CAC/F,IAAI,OAAO,KAAK,OAAO,MAAM,EAAE;MAC1B,KAAK,IAAI,MAAM,EAAE;CACtB,OAAO;AACT;AAEA,SAAgB,0BAA0B,OAAmB,OAA2B;CACtF,MAAM,UAAU,MAAM;CACtB,IAAI,CAAC,WAAW,MAAM,eAAe,UAAU,MAAM,aAAa,GAAG,OAAO;CAC5E,MAAM,QAAQ,MAAM,eAAe;CACnC,MAAM,cAAc,QAAQ,MAAM,oBAAoB,MAAM;CAC5D,MAAM,eAAe,QAAQ,MAAM,oBAAoB,MAAM;CAC7D,QACI,gBAAgB,SAAS,QAAQ,UAAU,KAAA,KAC1C,iBAAiB,SAAS,QAAQ,WAAW,KAAA,MAChD,iBAAiB,OAAO,KAAK;AAEjC;;;;;;AAOA,SAAgB,oBAAoB,OAAmB,OAA2B;CAGhF,IAAI,MAAM,SAAS,cAAc,CAAC,aAAa,OAAO,KAAK,GAAG,OAAO;CAErE,QADqB,MAAM,OAAO,WAAW,SAAS,MAAM,iBAAiB,SACtD,iBAAiB,OAAO,KAAK;AACtD"}
|
package/dist/layout/grid.d.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
import { SceneGraph, SceneNode } from "@pairdraw/scene-graph";
|
|
1
|
+
import { GridTrack, SceneGraph, SceneNode } from "@pairdraw/scene-graph";
|
|
2
2
|
import { Node } from "yoga-layout";
|
|
3
3
|
//#region src/layout/grid.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* A grid frame that hugs an axis has no space to share out, so its fractional tracks take their
|
|
6
|
+
* content's size there and the frame keeps the smallest box around them, as Figma hugs a grid.
|
|
7
|
+
*/
|
|
8
|
+
declare function gridTracksForAxis(node: SceneNode, axis: 'width' | 'height'): GridTrack[];
|
|
4
9
|
declare function createGridChildNode(graph: SceneGraph, child: SceneNode): Node;
|
|
5
10
|
declare function buildGridTree(graph: SceneGraph, frame: SceneNode, inheritedDirection: 'LTR' | 'RTL'): Node;
|
|
6
11
|
//#endregion
|
|
7
|
-
export { buildGridTree, createGridChildNode };
|
|
12
|
+
export { buildGridTree, createGridChildNode, gridTracksForAxis };
|
|
8
13
|
//# sourceMappingURL=grid.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grid.d.ts","names":[],"sources":["../../src/layout/grid.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"grid.d.ts","names":[],"sources":["../../src/layout/grid.ts"],"mappings":";;;;;;;iBAoBgB,kBAAkB,MAAM,WAAW,2BAA2B;iBAsC9D,oBAAoB,OAAO,YAAY,OAAO,YAAY;iBA+B1D,cACd,OAAO,YACP,OAAO,WACP,oCACC"}
|
package/dist/layout/grid.js
CHANGED
|
@@ -1,19 +1,37 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { keepsSavedFrameSize } from "./derived.js";
|
|
2
|
+
import { applyMinMaxConstraints, configureAbsoluteChild, createYogaNode, mapGridTrack } from "./yoga-helpers.js";
|
|
3
|
+
import { applyContentInsets, applyStrokeMargins, contentInsetsSize } from "./strokes.js";
|
|
4
|
+
import { ownAxisSizing } from "./axis-sizing.js";
|
|
3
5
|
import { resolveNodeLayoutDirection } from "@pairdraw/scene-graph/text/direction";
|
|
4
6
|
import { layoutSizing } from "@pairdraw/scene-graph/layout";
|
|
5
7
|
import { Direction, Display, Gutter } from "yoga-layout";
|
|
6
8
|
//#region src/layout/grid.ts
|
|
7
|
-
|
|
9
|
+
/**
|
|
10
|
+
* A grid frame that hugs an axis has no space to share out, so its fractional tracks take their
|
|
11
|
+
* content's size there and the frame keeps the smallest box around them, as Figma hugs a grid.
|
|
12
|
+
*/
|
|
13
|
+
function gridTracksForAxis(node, axis) {
|
|
14
|
+
const tracks = axis === "width" ? node.gridTemplateColumns : node.gridTemplateRows;
|
|
15
|
+
if (ownAxisSizing(node, axis) !== "HUG") return tracks;
|
|
16
|
+
return tracks.map((track) => track.sizing === "FR" ? {
|
|
17
|
+
sizing: "AUTO",
|
|
18
|
+
value: 0
|
|
19
|
+
} : track);
|
|
20
|
+
}
|
|
21
|
+
function configureAsGrid(graph, yogaNode, node, direction) {
|
|
8
22
|
yogaNode.setDisplay(Display.Grid);
|
|
9
23
|
yogaNode.setDirection(direction === "RTL" ? Direction.RTL : Direction.LTR);
|
|
10
|
-
|
|
11
|
-
if (node
|
|
12
|
-
if (node
|
|
13
|
-
|
|
24
|
+
const insets = keepsSavedFrameSize(graph, node) ? null : contentInsetsSize(node);
|
|
25
|
+
if (ownAxisSizing(node, "width") !== "HUG") yogaNode.setWidth(insets ? Math.max(node.width, insets.width) : node.width);
|
|
26
|
+
if (ownAxisSizing(node, "height") !== "HUG") {
|
|
27
|
+
if (node.gridTemplateRows.length > 0 || node.height > 0) yogaNode.setHeight(insets ? Math.max(node.height, insets.height) : node.height);
|
|
28
|
+
}
|
|
29
|
+
if (node.gridTemplateColumns.length > 0) yogaNode.setGridTemplateColumns(gridTracksForAxis(node, "width").map(mapGridTrack));
|
|
30
|
+
if (node.gridTemplateRows.length > 0) yogaNode.setGridTemplateRows(gridTracksForAxis(node, "height").map(mapGridTrack));
|
|
14
31
|
yogaNode.setGap(Gutter.Column, node.gridColumnGap);
|
|
15
32
|
yogaNode.setGap(Gutter.Row, node.gridRowGap);
|
|
16
33
|
applyContentInsets(yogaNode, node);
|
|
34
|
+
applyMinMaxConstraints(yogaNode, node);
|
|
17
35
|
}
|
|
18
36
|
function createGridChildNode(graph, child) {
|
|
19
37
|
const yogaChild = createYogaNode();
|
|
@@ -40,7 +58,7 @@ function createGridChildNode(graph, child) {
|
|
|
40
58
|
function buildGridTree(graph, frame, inheritedDirection) {
|
|
41
59
|
const root = createYogaNode();
|
|
42
60
|
const direction = resolveNodeLayoutDirection(frame, inheritedDirection);
|
|
43
|
-
configureAsGrid(root, frame, direction);
|
|
61
|
+
configureAsGrid(graph, root, frame, direction);
|
|
44
62
|
const children = graph.getChildren(frame.id);
|
|
45
63
|
for (const child of children) if (child.layoutPositioning === "ABSOLUTE") {
|
|
46
64
|
const yogaChild = createYogaNode();
|
|
@@ -58,6 +76,6 @@ function buildGridTree(graph, frame, inheritedDirection) {
|
|
|
58
76
|
return root;
|
|
59
77
|
}
|
|
60
78
|
//#endregion
|
|
61
|
-
export { buildGridTree, createGridChildNode };
|
|
79
|
+
export { buildGridTree, createGridChildNode, gridTracksForAxis };
|
|
62
80
|
|
|
63
81
|
//# sourceMappingURL=grid.js.map
|
package/dist/layout/grid.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grid.js","names":[],"sources":["../../src/layout/grid.ts"],"sourcesContent":["import { Direction, Display, Gutter, type Node as YogaNode } from 'yoga-layout'\n\nimport type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'\nimport { layoutSizing } from '@pairdraw/scene-graph/layout'\nimport { resolveNodeLayoutDirection } from '@pairdraw/scene-graph/text/direction'\n\nimport { applyContentInsets, applyStrokeMargins } from './strokes'\nimport {
|
|
1
|
+
{"version":3,"file":"grid.js","names":[],"sources":["../../src/layout/grid.ts"],"sourcesContent":["import { Direction, Display, Gutter, type Node as YogaNode } from 'yoga-layout'\n\nimport type { GridTrack, SceneGraph, SceneNode } from '@pairdraw/scene-graph'\nimport { layoutSizing } from '@pairdraw/scene-graph/layout'\nimport { resolveNodeLayoutDirection } from '@pairdraw/scene-graph/text/direction'\n\nimport { ownAxisSizing } from './axis-sizing'\nimport { keepsSavedFrameSize } from './derived'\nimport { applyContentInsets, applyStrokeMargins, contentInsetsSize } from './strokes'\nimport {\n applyMinMaxConstraints,\n configureAbsoluteChild,\n createYogaNode,\n mapGridTrack\n} from './yoga-helpers'\n\n/**\n * A grid frame that hugs an axis has no space to share out, so its fractional tracks take their\n * content's size there and the frame keeps the smallest box around them, as Figma hugs a grid.\n */\nexport function gridTracksForAxis(node: SceneNode, axis: 'width' | 'height'): GridTrack[] {\n const tracks = axis === 'width' ? node.gridTemplateColumns : node.gridTemplateRows\n if (ownAxisSizing(node, axis) !== 'HUG') return tracks\n return tracks.map((track) => (track.sizing === 'FR' ? { sizing: 'AUTO', value: 0 } : track))\n}\n\nfunction configureAsGrid(\n graph: SceneGraph,\n yogaNode: YogaNode,\n node: SceneNode,\n direction: Exclude<SceneNode['layoutDirection'], 'AUTO'>\n): void {\n yogaNode.setDisplay(Display.Grid)\n yogaNode.setDirection(direction === 'RTL' ? Direction.RTL : Direction.LTR)\n const insets = keepsSavedFrameSize(graph, node) ? null : contentInsetsSize(node)\n if (ownAxisSizing(node, 'width') !== 'HUG') {\n yogaNode.setWidth(insets ? Math.max(node.width, insets.width) : node.width)\n }\n if (ownAxisSizing(node, 'height') !== 'HUG') {\n if (node.gridTemplateRows.length > 0 || node.height > 0) {\n yogaNode.setHeight(insets ? Math.max(node.height, insets.height) : node.height)\n }\n }\n\n if (node.gridTemplateColumns.length > 0) {\n yogaNode.setGridTemplateColumns(gridTracksForAxis(node, 'width').map(mapGridTrack))\n }\n if (node.gridTemplateRows.length > 0) {\n yogaNode.setGridTemplateRows(gridTracksForAxis(node, 'height').map(mapGridTrack))\n }\n\n yogaNode.setGap(Gutter.Column, node.gridColumnGap)\n yogaNode.setGap(Gutter.Row, node.gridRowGap)\n\n applyContentInsets(yogaNode, node)\n applyMinMaxConstraints(yogaNode, node)\n}\n\nexport function createGridChildNode(graph: SceneGraph, child: SceneNode): YogaNode {\n const yogaChild = createYogaNode()\n if (!child.visible) {\n yogaChild.setDisplay(Display.None)\n } else {\n const pos = child.gridPosition\n if (pos) {\n // A span from an explicit line ends N lines on; from an auto line it is CSS\n // `grid-column: span N`, which Yoga places by itself.\n if (pos.column > 0) {\n yogaChild.setGridColumnStart(pos.column)\n yogaChild.setGridColumnEndSpan(pos.columnSpan)\n } else if (pos.columnSpan > 1) {\n yogaChild.setGridColumnStartSpan(pos.columnSpan)\n }\n if (pos.row > 0) {\n yogaChild.setGridRowStart(pos.row)\n yogaChild.setGridRowEndSpan(pos.rowSpan)\n } else if (pos.rowSpan > 1) {\n yogaChild.setGridRowStartSpan(pos.rowSpan)\n }\n }\n // Each axis fills its cell on its own, as in Figma: grow for width, stretch for height.\n if (layoutSizing(graph, child, 'HORIZONTAL') === 'FILL') yogaChild.setWidthStretch()\n else yogaChild.setWidth(child.width)\n if (layoutSizing(graph, child, 'VERTICAL') === 'FILL') yogaChild.setHeightStretch()\n else yogaChild.setHeight(child.height)\n }\n return yogaChild\n}\n\nexport function buildGridTree(\n graph: SceneGraph,\n frame: SceneNode,\n inheritedDirection: 'LTR' | 'RTL'\n): YogaNode {\n const root = createYogaNode()\n const direction = resolveNodeLayoutDirection(frame, inheritedDirection)\n configureAsGrid(graph, root, frame, direction)\n\n const children = graph.getChildren(frame.id)\n for (const child of children) {\n if (child.layoutPositioning === 'ABSOLUTE') {\n const yogaChild = createYogaNode()\n configureAbsoluteChild(yogaChild, child)\n root.insertChild(yogaChild, root.getChildCount())\n } else {\n const yogaChild = createGridChildNode(graph, child)\n applyStrokeMargins(yogaChild, child, frame)\n if (\n child.layoutMode === 'GRID' ||\n child.layoutMode === 'HORIZONTAL' ||\n child.layoutMode === 'VERTICAL'\n ) {\n const childDirection = resolveNodeLayoutDirection(child, direction)\n yogaChild.setDirection(childDirection === 'RTL' ? Direction.RTL : Direction.LTR)\n }\n root.insertChild(yogaChild, root.getChildCount())\n }\n }\n\n return root\n}\n"],"mappings":";;;;;;;;;;;;AAoBA,SAAgB,kBAAkB,MAAiB,MAAuC;CACxF,MAAM,SAAS,SAAS,UAAU,KAAK,sBAAsB,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B,IAAI,UAAU;GAC/C,OAAO,IAAI,IAAI,aAAa,GAC1B,UAAU,uBAAuB,IAAI,UAAU;GAEjD,IAAI,IAAI,MAAM,GAAG;IACf,UAAU,gBAAgB,IAAI,GAAG;IACjC,UAAU,kBAAkB,IAAI,OAAO;GACzC,OAAO,IAAI,IAAI,UAAU,GACvB,UAAU,oBAAoB,IAAI,OAAO;EAE7C;EAEA,IAAI,aAAa,OAAO,OAAO,YAAY,MAAM,QAAQ,UAAU,gBAAgB;OAC9E,UAAU,SAAS,MAAM,KAAK;EACnC,IAAI,aAAa,OAAO,OAAO,UAAU,MAAM,QAAQ,UAAU,iBAAiB;OAC7E,UAAU,UAAU,MAAM,MAAM;CACvC;CACA,OAAO;AACT;AAEA,SAAgB,cACd,OACA,OACA,oBACU;CACV,MAAM,OAAO,eAAe;CAC5B,MAAM,YAAY,2BAA2B,OAAO,kBAAkB;CACtE,gBAAgB,OAAO,MAAM,OAAO,SAAS;CAE7C,MAAM,WAAW,MAAM,YAAY,MAAM,EAAE;CAC3C,KAAK,MAAM,SAAS,UAClB,IAAI,MAAM,sBAAsB,YAAY;EAC1C,MAAM,YAAY,eAAe;EACjC,uBAAuB,WAAW,KAAK;EACvC,KAAK,YAAY,WAAW,KAAK,cAAc,CAAC;CAClD,OAAO;EACL,MAAM,YAAY,oBAAoB,OAAO,KAAK;EAClD,mBAAmB,WAAW,OAAO,KAAK;EAC1C,IACE,MAAM,eAAe,UACrB,MAAM,eAAe,gBACrB,MAAM,eAAe,YACrB;GACA,MAAM,iBAAiB,2BAA2B,OAAO,SAAS;GAClE,UAAU,aAAa,mBAAmB,QAAQ,UAAU,MAAM,UAAU,GAAG;EACjF;EACA,KAAK,YAAY,WAAW,KAAK,cAAc,CAAC;CAClD;CAGF,OAAO;AACT"}
|
|
@@ -3,6 +3,7 @@ import { SceneMutationImpact } from "@pairdraw/scene-graph/mutation-impact";
|
|
|
3
3
|
//#region src/layout/mutations.d.ts
|
|
4
4
|
declare function createLayoutRunner(getGraph: () => SceneGraph): {
|
|
5
5
|
runLayoutForNode: (id: string) => void;
|
|
6
|
+
runLayoutForParents: (parentIds: Iterable<string>) => void;
|
|
6
7
|
runLayoutForImpact: (impact: SceneMutationImpact) => boolean;
|
|
7
8
|
runMutationWithLayout: <T>(operation: () => T | Promise<T>, fallbackId?: string, beforeLayout?: (result: T, impact: SceneMutationImpact) => Promise<void> | void) => Promise<T>;
|
|
8
9
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutations.d.ts","names":[],"sources":["../../src/layout/mutations.ts"],"mappings":";;;iBAYgB,mBAAmB,gBAAgB;EACnB,mBAAA;
|
|
1
|
+
{"version":3,"file":"mutations.d.ts","names":[],"sources":["../../src/layout/mutations.ts"],"mappings":";;;iBAYgB,mBAAmB,gBAAgB;EACnB,mBAAA;EAqBU,sBAAA,WAAA;EAoCJ,qBAAA,QAAA;EAhBC,wBAAA,GAAC,iBACnB,IAAI,QAAQ,IAAE,qBACZ,gBACH,QAAQ,GAAG,QAAQ,wBAAwB,yBAC1D,QAAQ"}
|
package/dist/layout/mutations.js
CHANGED
|
@@ -15,6 +15,15 @@ function createLayoutRunner(getGraph) {
|
|
|
15
15
|
parent = parent.parentId ? graph.getNode(parent.parentId) : void 0;
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Lays out the parents whose children were added, removed, reordered, shown, or hidden. Only an
|
|
20
|
+
* auto layout places its children, so other parents are skipped: laying out a page reaches every
|
|
21
|
+
* frame on it. Hugging auto layout ancestors still follow, as `runLayoutForNode` walks up.
|
|
22
|
+
*/
|
|
23
|
+
function runLayoutForParents(parentIds) {
|
|
24
|
+
const graph = getGraph();
|
|
25
|
+
for (const id of uniq([...parentIds])) if (graph.getNode(id)?.layoutMode !== "NONE") runLayoutForNode(id);
|
|
26
|
+
}
|
|
18
27
|
function compactLayoutScope(impact) {
|
|
19
28
|
const graph = getGraph();
|
|
20
29
|
const candidates = new Set(mutationLayoutScopeIds(impact).filter((id) => graph.getNode(id)));
|
|
@@ -63,6 +72,7 @@ function createLayoutRunner(getGraph) {
|
|
|
63
72
|
}
|
|
64
73
|
return {
|
|
65
74
|
runLayoutForNode,
|
|
75
|
+
runLayoutForParents,
|
|
66
76
|
runLayoutForImpact,
|
|
67
77
|
runMutationWithLayout
|
|
68
78
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutations.js","names":[],"sources":["../../src/layout/mutations.ts"],"sourcesContent":["import { uniq } from 'es-toolkit'\n\nimport type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'\nimport { connectorsAffectedBy, layoutConnector } from '@pairdraw/scene-graph/connectors'\nimport {\n collectSceneMutation,\n mutationLayoutScopeIds,\n type SceneMutationImpact\n} from '@pairdraw/scene-graph/mutation-impact'\n\nimport { computeAllLayouts, computeLayout } from '#core/layout'\n\nexport function createLayoutRunner(getGraph: () => SceneGraph) {\n function runLayoutForNode(id: string) {\n const graph = getGraph()\n const node = graph.getNode(id)\n if (!node) return\n\n computeAllLayouts(graph, id)\n\n let parent = node.parentId ? graph.getNode(node.parentId) : undefined\n while (parent) {\n if (parent.layoutMode !== 'NONE') {\n computeLayout(graph, parent.id)\n }\n parent = parent.parentId ? graph.getNode(parent.parentId) : undefined\n }\n }\n\n function compactLayoutScope(impact: SceneMutationImpact): string[] {\n const graph = getGraph()\n const candidates = new Set(mutationLayoutScopeIds(impact).filter((id) => graph.getNode(id)))\n return [...candidates].filter((id) => {\n let parentId = graph.getNode(id)?.parentId ?? null\n while (parentId) {\n if (candidates.has(parentId)) return false\n parentId = graph.getNode(parentId)?.parentId ?? null\n }\n return true\n })\n }\n\n async function runMutationWithLayout<T>(\n operation: () => T | Promise<T>,\n fallbackId?: string,\n beforeLayout?: (result: T, impact: SceneMutationImpact) => Promise<void> | void\n ): Promise<T> {\n const graph = getGraph()\n const { result, impact } = await collectSceneMutation(graph, operation)\n await beforeLayout?.(result, impact)\n if (!runLayoutForImpact(impact) && fallbackId) computeAllLayouts(graph, fallbackId)\n return result\n }\n\n /**\n * Lays out what an edit touched, and routes again the connectors it moved; returns whether\n * there was anything to lay out.\n */\n function runLayoutForImpact(impact: SceneMutationImpact): boolean {\n const scopeIds = compactLayoutScope(impact)\n for (const id of scopeIds) runLayoutForNode(id)\n routeConnectors(impact)\n return scopeIds.length > 0\n }\n\n /**\n * Routes the connectors an edit moved. One created together with the layers it joins, as a\n * paste makes it, keeps the route it came with; one that joins layers already there routes to\n * them.\n */\n function routeConnectors(impact: SceneMutationImpact) {\n const graph = getGraph()\n const created = impact.createdNodeIds\n const joinsExisting = (connector: SceneNode) =>\n [connector.connectorStart?.nodeId, connector.connectorEnd?.nodeId].some(\n (id) => id && !created.has(id) && graph.getNode(id)\n )\n const connectors = [\n ...connectorsAffectedBy(graph, impact.changedNodeIds),\n ...[...created].flatMap((id) => {\n const node = graph.getNode(id)\n return node?.type === 'CONNECTOR' && joinsExisting(node) ? [node] : []\n })\n ]\n graph.withLayoutMutations(() => {\n for (const connector of uniq(connectors)) layoutConnector(graph, connector)\n })\n }\n\n return { runLayoutForNode, runLayoutForImpact, runMutationWithLayout }\n}\n"],"mappings":";;;;;AAYA,SAAgB,mBAAmB,UAA4B;CAC7D,SAAS,iBAAiB,IAAY;EACpC,MAAM,QAAQ,SAAS;EACvB,MAAM,OAAO,MAAM,QAAQ,EAAE;EAC7B,IAAI,CAAC,MAAM;EAEX,kBAAkB,OAAO,EAAE;EAE3B,IAAI,SAAS,KAAK,WAAW,MAAM,QAAQ,KAAK,QAAQ,IAAI,KAAA;EAC5D,OAAO,QAAQ;GACb,IAAI,OAAO,eAAe,QACxB,cAAc,OAAO,OAAO,EAAE;GAEhC,SAAS,OAAO,WAAW,MAAM,QAAQ,OAAO,QAAQ,IAAI,KAAA;EAC9D;CACF;CAEA,SAAS,mBAAmB,QAAuC;EACjE,MAAM,QAAQ,SAAS;EACvB,MAAM,aAAa,IAAI,IAAI,uBAAuB,MAAM,CAAC,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AU,GAAG,gBAAgB,OAAO,SAAS;EAC5E,CAAC;CACH;CAEA,OAAO;EAAE;EAAkB;EAAoB;CAAsB;
|
|
1
|
+
{"version":3,"file":"mutations.js","names":[],"sources":["../../src/layout/mutations.ts"],"sourcesContent":["import { uniq } from 'es-toolkit'\n\nimport type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'\nimport { connectorsAffectedBy, layoutConnector } from '@pairdraw/scene-graph/connectors'\nimport {\n collectSceneMutation,\n mutationLayoutScopeIds,\n type SceneMutationImpact\n} from '@pairdraw/scene-graph/mutation-impact'\n\nimport { computeAllLayouts, computeLayout } from '#core/layout'\n\nexport function createLayoutRunner(getGraph: () => SceneGraph) {\n function runLayoutForNode(id: string) {\n const graph = getGraph()\n const node = graph.getNode(id)\n if (!node) return\n\n computeAllLayouts(graph, id)\n\n let parent = node.parentId ? graph.getNode(node.parentId) : undefined\n while (parent) {\n if (parent.layoutMode !== 'NONE') {\n computeLayout(graph, parent.id)\n }\n parent = parent.parentId ? graph.getNode(parent.parentId) : undefined\n }\n }\n\n /**\n * Lays out the parents whose children were added, removed, reordered, shown, or hidden. Only an\n * auto layout places its children, so other parents are skipped: laying out a page reaches every\n * frame on it. Hugging auto layout ancestors still follow, as `runLayoutForNode` walks up.\n */\n function runLayoutForParents(parentIds: Iterable<string>) {\n const graph = getGraph()\n for (const id of uniq([...parentIds])) {\n if (graph.getNode(id)?.layoutMode !== 'NONE') runLayoutForNode(id)\n }\n }\n\n function compactLayoutScope(impact: SceneMutationImpact): string[] {\n const graph = getGraph()\n const candidates = new Set(mutationLayoutScopeIds(impact).filter((id) => graph.getNode(id)))\n return [...candidates].filter((id) => {\n let parentId = graph.getNode(id)?.parentId ?? null\n while (parentId) {\n if (candidates.has(parentId)) return false\n parentId = graph.getNode(parentId)?.parentId ?? null\n }\n return true\n })\n }\n\n async function runMutationWithLayout<T>(\n operation: () => T | Promise<T>,\n fallbackId?: string,\n beforeLayout?: (result: T, impact: SceneMutationImpact) => Promise<void> | void\n ): Promise<T> {\n const graph = getGraph()\n const { result, impact } = await collectSceneMutation(graph, operation)\n await beforeLayout?.(result, impact)\n if (!runLayoutForImpact(impact) && fallbackId) computeAllLayouts(graph, fallbackId)\n return result\n }\n\n /**\n * Lays out what an edit touched, and routes again the connectors it moved; returns whether\n * there was anything to lay out.\n */\n function runLayoutForImpact(impact: SceneMutationImpact): boolean {\n const scopeIds = compactLayoutScope(impact)\n for (const id of scopeIds) runLayoutForNode(id)\n routeConnectors(impact)\n return scopeIds.length > 0\n }\n\n /**\n * Routes the connectors an edit moved. One created together with the layers it joins, as a\n * paste makes it, keeps the route it came with; one that joins layers already there routes to\n * them.\n */\n function routeConnectors(impact: SceneMutationImpact) {\n const graph = getGraph()\n const created = impact.createdNodeIds\n const joinsExisting = (connector: SceneNode) =>\n [connector.connectorStart?.nodeId, connector.connectorEnd?.nodeId].some(\n (id) => id && !created.has(id) && graph.getNode(id)\n )\n const connectors = [\n ...connectorsAffectedBy(graph, impact.changedNodeIds),\n ...[...created].flatMap((id) => {\n const node = graph.getNode(id)\n return node?.type === 'CONNECTOR' && joinsExisting(node) ? [node] : []\n })\n ]\n graph.withLayoutMutations(() => {\n for (const connector of uniq(connectors)) layoutConnector(graph, connector)\n })\n }\n\n return { runLayoutForNode, runLayoutForParents, runLayoutForImpact, runMutationWithLayout }\n}\n"],"mappings":";;;;;AAYA,SAAgB,mBAAmB,UAA4B;CAC7D,SAAS,iBAAiB,IAAY;EACpC,MAAM,QAAQ,SAAS;EACvB,MAAM,OAAO,MAAM,QAAQ,EAAE;EAC7B,IAAI,CAAC,MAAM;EAEX,kBAAkB,OAAO,EAAE;EAE3B,IAAI,SAAS,KAAK,WAAW,MAAM,QAAQ,KAAK,QAAQ,IAAI,KAAA;EAC5D,OAAO,QAAQ;GACb,IAAI,OAAO,eAAe,QACxB,cAAc,OAAO,OAAO,EAAE;GAEhC,SAAS,OAAO,WAAW,MAAM,QAAQ,OAAO,QAAQ,IAAI,KAAA;EAC9D;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,CACpD;EACF,MAAM,aAAa,CACjB,GAAG,qBAAqB,OAAO,OAAO,cAAc,GACpD,GAAG,CAAC,GAAG,OAAO,CAAC,CAAC,SAAS,OAAO;GAC9B,MAAM,OAAO,MAAM,QAAQ,EAAE;GAC7B,OAAO,MAAM,SAAS,eAAe,cAAc,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC;EACvE,CAAC,CACH;EACA,MAAM,0BAA0B;GAC9B,KAAK,MAAM,aAAa,KAAK,UAAU,GAAG,gBAAgB,OAAO,SAAS;EAC5E,CAAC;CACH;CAEA,OAAO;EAAE;EAAkB;EAAqB;EAAoB;CAAsB;AAC5F"}
|
package/dist/layout/strokes.d.ts
CHANGED
|
@@ -3,6 +3,14 @@ import { Node } from "yoga-layout";
|
|
|
3
3
|
//#region src/layout/strokes.d.ts
|
|
4
4
|
/** Insets a layout container's content by its padding and, with strokes included, its stroke. */
|
|
5
5
|
declare function applyContentInsets(yogaNode: Node, node: SceneNode): void;
|
|
6
|
+
/**
|
|
7
|
+
* The room a horizontal and vertical layout frame's padding and included inside strokes take,
|
|
8
|
+
* which is the smallest size it holds even when set smaller, as Figma grows such a frame.
|
|
9
|
+
*/
|
|
10
|
+
declare function contentInsetsSize(node: SceneNode): {
|
|
11
|
+
width: number;
|
|
12
|
+
height: number;
|
|
13
|
+
};
|
|
6
14
|
/**
|
|
7
15
|
* With strokes included in its parent's layout, the part of a child's stroke that falls outside
|
|
8
16
|
* its box takes room beside it, so neighbours and a hugging parent clear the stroke.
|
|
@@ -14,5 +22,5 @@ declare function applyStrokeMargins(yogaChild: Node, child: SceneNode, parent: S
|
|
|
14
22
|
*/
|
|
15
23
|
declare function strokeRoom(parent: SceneNode, children: readonly SceneNode[], axis: 'width' | 'height'): number;
|
|
16
24
|
//#endregion
|
|
17
|
-
export { applyContentInsets, applyStrokeMargins, strokeRoom };
|
|
25
|
+
export { applyContentInsets, applyStrokeMargins, contentInsetsSize, strokeRoom };
|
|
18
26
|
//# sourceMappingURL=strokes.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"strokes.d.ts","names":[],"sources":["../../src/layout/strokes.ts"],"mappings":";;;;iBAmEgB,mBAAmB,UAAU,MAAU,MAAM;;;;;iBAY7C,mBAAmB,WAAW,MAAU,OAAO,WAAW,QAAQ;;;;;iBAWlE,WACd,QAAQ,WACR,mBAAmB,aACnB"}
|
|
1
|
+
{"version":3,"file":"strokes.d.ts","names":[],"sources":["../../src/layout/strokes.ts"],"mappings":";;;;iBAmEgB,mBAAmB,UAAU,MAAU,MAAM;;;;;iBAY7C,kBAAkB,MAAM;EAAc;EAAe;;;;;;iBAYrD,mBAAmB,WAAW,MAAU,OAAO,WAAW,QAAQ;;;;;iBAWlE,WACd,QAAQ,WACR,mBAAmB,aACnB"}
|
package/dist/layout/strokes.js
CHANGED
|
@@ -69,6 +69,17 @@ function applyContentInsets(yogaNode, node) {
|
|
|
69
69
|
applyStrokeBorders(yogaNode, node);
|
|
70
70
|
}
|
|
71
71
|
/**
|
|
72
|
+
* The room a horizontal and vertical layout frame's padding and included inside strokes take,
|
|
73
|
+
* which is the smallest size it holds even when set smaller, as Figma grows such a frame.
|
|
74
|
+
*/
|
|
75
|
+
function contentInsetsSize(node) {
|
|
76
|
+
const inside = node.strokesIncludedInLayout ? strokeExtent(node, "inside") : null;
|
|
77
|
+
return {
|
|
78
|
+
width: node.paddingLeft + node.paddingRight + (inside ? inside[1] + inside[3] : 0),
|
|
79
|
+
height: node.paddingTop + node.paddingBottom + (inside ? inside[0] + inside[2] : 0)
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
72
83
|
* With strokes included in its parent's layout, the part of a child's stroke that falls outside
|
|
73
84
|
* its box takes room beside it, so neighbours and a hugging parent clear the stroke.
|
|
74
85
|
*/
|
|
@@ -91,6 +102,6 @@ function strokeRoom(parent, children, axis) {
|
|
|
91
102
|
return room;
|
|
92
103
|
}
|
|
93
104
|
//#endregion
|
|
94
|
-
export { applyContentInsets, applyStrokeMargins, strokeRoom };
|
|
105
|
+
export { applyContentInsets, applyStrokeMargins, contentInsetsSize, strokeRoom };
|
|
95
106
|
|
|
96
107
|
//# sourceMappingURL=strokes.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"strokes.js","names":[],"sources":["../../src/layout/strokes.ts"],"sourcesContent":["import { Edge, type Node as YogaNode } from 'yoga-layout'\n\nimport type { SceneNode, Stroke } from '@pairdraw/scene-graph'\n\n/** Widths on each side of a layer, in Yoga's edge order: top, right, bottom, left. */\ntype Sides = [number, number, number, number]\n\nconst EDGES = [Edge.Top, Edge.Right, Edge.Bottom, Edge.Left] as const\n\n/** The share of a stroke's weight that lies outside the layer it outlines. */\nfunction outsideShare(align: Stroke['align']): number {\n if (align === 'OUTSIDE') return 1\n if (align === 'CENTER') return 0.5\n return 0\n}\n\n/**\n * How far a layer's visible strokes reach into (`inside`) or out of (`outside`) its box on each\n * side, the widest stroke deciding. Sides with weights of their own use them for every stroke.\n */\nfunction strokeExtent(node: SceneNode, part: 'inside' | 'outside'): Sides | null {\n const strokes = node.strokes.filter((stroke) => stroke.visible)\n if (strokes.length === 0) return null\n const extent: Sides = [0, 0, 0, 0]\n for (const stroke of strokes) {\n const outside = outsideShare(stroke.align)\n const share = part === 'outside' ? outside : 1 - outside\n const weights = node.independentStrokeWeights\n ? [\n node.borderTopWeight,\n node.borderRightWeight,\n node.borderBottomWeight,\n node.borderLeftWeight\n ]\n : [stroke.weight, stroke.weight, stroke.weight, stroke.weight]\n for (const [side, weight] of weights.entries()) {\n extent[side] = Math.max(extent[side], Math.max(0, weight) * share)\n }\n }\n return extent.some((width) => width > 0) ? extent : null\n}\n\n/**\n * Whether a child's strokes take room in its parent's auto layout. Text strokes follow its glyphs\n * rather than its box, and a child out of the flow is placed where it is.\n */\nfunction strokeTakesRoom(child: SceneNode, parent: SceneNode): boolean {\n return (\n parent.strokesIncludedInLayout &&\n child.type !== 'TEXT' &&\n child.visible &&\n child.layoutPositioning !== 'ABSOLUTE'\n )\n}\n\n/**\n * With strokes included in its layout, an auto layout frame lays its content out inside the part\n * of its own stroke that falls within its box, as CSS `box-sizing: border-box` does with a border.\n */\nfunction applyStrokeBorders(yogaNode: YogaNode, node: SceneNode): void {\n if (!node.strokesIncludedInLayout) return\n const inside = strokeExtent(node, 'inside')\n if (!inside) return\n for (const [side, edge] of EDGES.entries()) yogaNode.setBorder(edge, inside[side])\n}\n\n/** Insets a layout container's content by its padding and, with strokes included, its stroke. */\nexport function applyContentInsets(yogaNode: YogaNode, node: SceneNode): void {\n yogaNode.setPadding(Edge.Top, node.paddingTop)\n yogaNode.setPadding(Edge.Right, node.paddingRight)\n yogaNode.setPadding(Edge.Bottom, node.paddingBottom)\n yogaNode.setPadding(Edge.Left, node.paddingLeft)\n applyStrokeBorders(yogaNode, node)\n}\n\n/**\n * With strokes included in its parent's layout, the part of a child's stroke that falls outside\n * its box takes room beside it, so neighbours and a hugging parent clear the stroke.\n */\nexport function applyStrokeMargins(yogaChild: YogaNode, child: SceneNode, parent: SceneNode): void {\n if (!strokeTakesRoom(child, parent)) return\n const outside = strokeExtent(child, 'outside')\n if (!outside) return\n for (const [side, edge] of EDGES.entries()) yogaChild.setMargin(edge, outside[side])\n}\n\n/**\n * The room strokes take along one axis of a frame laid out with them included: its own stroke\n * inside its box on both ends, and the stroke outside each child's box.\n */\nexport function strokeRoom(\n parent: SceneNode,\n children: readonly SceneNode[],\n axis: 'width' | 'height'\n): number {\n if (!parent.strokesIncludedInLayout) return 0\n const [start, end] = axis === 'width' ? [3, 1] : [0, 2]\n const across = (sides: Sides | null) => (sides ? sides[start] + sides[end] : 0)\n let room = across(strokeExtent(parent, 'inside'))\n for (const child of children) {\n if (strokeTakesRoom(child, parent)) room += across(strokeExtent(child, 'outside'))\n }\n return room\n}\n"],"mappings":";;AAOA,MAAM,QAAQ;CAAC,KAAK;CAAK,KAAK;CAAO,KAAK;CAAQ,KAAK;AAAI;;AAG3D,SAAS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CACnD,SAAS,WAAW,KAAK,MAAM,KAAK,WAAW;CAC/C,mBAAmB,UAAU,IAAI;AACnC;;;;;AAMA,SAAgB,mBAAmB,WAAqB,OAAkB,QAAyB;CACjG,IAAI,CAAC,gBAAgB,OAAO,MAAM,GAAG;CACrC,MAAM,UAAU,aAAa,OAAO,SAAS;CAC7C,IAAI,CAAC,SAAS;CACd,KAAK,MAAM,CAAC,MAAM,SAAS,MAAM,QAAQ,GAAG,UAAU,UAAU,MAAM,QAAQ,KAAK;AACrF;;;;;AAMA,SAAgB,WACd,QACA,UACA,MACQ;CACR,IAAI,CAAC,OAAO,yBAAyB,OAAO;CAC5C,MAAM,CAAC,OAAO,OAAO,SAAS,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC;CACtD,MAAM,UAAU,UAAyB,QAAQ,MAAM,SAAS,MAAM,OAAO;CAC7E,IAAI,OAAO,OAAO,aAAa,QAAQ,QAAQ,CAAC;CAChD,KAAK,MAAM,SAAS,UAClB,IAAI,gBAAgB,OAAO,MAAM,GAAG,QAAQ,OAAO,aAAa,OAAO,SAAS,CAAC;CAEnF,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"strokes.js","names":[],"sources":["../../src/layout/strokes.ts"],"sourcesContent":["import { Edge, type Node as YogaNode } from 'yoga-layout'\n\nimport type { SceneNode, Stroke } from '@pairdraw/scene-graph'\n\n/** Widths on each side of a layer, in Yoga's edge order: top, right, bottom, left. */\ntype Sides = [number, number, number, number]\n\nconst EDGES = [Edge.Top, Edge.Right, Edge.Bottom, Edge.Left] as const\n\n/** The share of a stroke's weight that lies outside the layer it outlines. */\nfunction outsideShare(align: Stroke['align']): number {\n if (align === 'OUTSIDE') return 1\n if (align === 'CENTER') return 0.5\n return 0\n}\n\n/**\n * How far a layer's visible strokes reach into (`inside`) or out of (`outside`) its box on each\n * side, the widest stroke deciding. Sides with weights of their own use them for every stroke.\n */\nfunction strokeExtent(node: SceneNode, part: 'inside' | 'outside'): Sides | null {\n const strokes = node.strokes.filter((stroke) => stroke.visible)\n if (strokes.length === 0) return null\n const extent: Sides = [0, 0, 0, 0]\n for (const stroke of strokes) {\n const outside = outsideShare(stroke.align)\n const share = part === 'outside' ? outside : 1 - outside\n const weights = node.independentStrokeWeights\n ? [\n node.borderTopWeight,\n node.borderRightWeight,\n node.borderBottomWeight,\n node.borderLeftWeight\n ]\n : [stroke.weight, stroke.weight, stroke.weight, stroke.weight]\n for (const [side, weight] of weights.entries()) {\n extent[side] = Math.max(extent[side], Math.max(0, weight) * share)\n }\n }\n return extent.some((width) => width > 0) ? extent : null\n}\n\n/**\n * Whether a child's strokes take room in its parent's auto layout. Text strokes follow its glyphs\n * rather than its box, and a child out of the flow is placed where it is.\n */\nfunction strokeTakesRoom(child: SceneNode, parent: SceneNode): boolean {\n return (\n parent.strokesIncludedInLayout &&\n child.type !== 'TEXT' &&\n child.visible &&\n child.layoutPositioning !== 'ABSOLUTE'\n )\n}\n\n/**\n * With strokes included in its layout, an auto layout frame lays its content out inside the part\n * of its own stroke that falls within its box, as CSS `box-sizing: border-box` does with a border.\n */\nfunction applyStrokeBorders(yogaNode: YogaNode, node: SceneNode): void {\n if (!node.strokesIncludedInLayout) return\n const inside = strokeExtent(node, 'inside')\n if (!inside) return\n for (const [side, edge] of EDGES.entries()) yogaNode.setBorder(edge, inside[side])\n}\n\n/** Insets a layout container's content by its padding and, with strokes included, its stroke. */\nexport function applyContentInsets(yogaNode: YogaNode, node: SceneNode): void {\n yogaNode.setPadding(Edge.Top, node.paddingTop)\n yogaNode.setPadding(Edge.Right, node.paddingRight)\n yogaNode.setPadding(Edge.Bottom, node.paddingBottom)\n yogaNode.setPadding(Edge.Left, node.paddingLeft)\n applyStrokeBorders(yogaNode, node)\n}\n\n/**\n * The room a horizontal and vertical layout frame's padding and included inside strokes take,\n * which is the smallest size it holds even when set smaller, as Figma grows such a frame.\n */\nexport function contentInsetsSize(node: SceneNode): { width: number; height: number } {\n const inside = node.strokesIncludedInLayout ? strokeExtent(node, 'inside') : null\n return {\n width: node.paddingLeft + node.paddingRight + (inside ? inside[1] + inside[3] : 0),\n height: node.paddingTop + node.paddingBottom + (inside ? inside[0] + inside[2] : 0)\n }\n}\n\n/**\n * With strokes included in its parent's layout, the part of a child's stroke that falls outside\n * its box takes room beside it, so neighbours and a hugging parent clear the stroke.\n */\nexport function applyStrokeMargins(yogaChild: YogaNode, child: SceneNode, parent: SceneNode): void {\n if (!strokeTakesRoom(child, parent)) return\n const outside = strokeExtent(child, 'outside')\n if (!outside) return\n for (const [side, edge] of EDGES.entries()) yogaChild.setMargin(edge, outside[side])\n}\n\n/**\n * The room strokes take along one axis of a frame laid out with them included: its own stroke\n * inside its box on both ends, and the stroke outside each child's box.\n */\nexport function strokeRoom(\n parent: SceneNode,\n children: readonly SceneNode[],\n axis: 'width' | 'height'\n): number {\n if (!parent.strokesIncludedInLayout) return 0\n const [start, end] = axis === 'width' ? [3, 1] : [0, 2]\n const across = (sides: Sides | null) => (sides ? sides[start] + sides[end] : 0)\n let room = across(strokeExtent(parent, 'inside'))\n for (const child of children) {\n if (strokeTakesRoom(child, parent)) room += across(strokeExtent(child, 'outside'))\n }\n return room\n}\n"],"mappings":";;AAOA,MAAM,QAAQ;CAAC,KAAK;CAAK,KAAK;CAAO,KAAK;CAAQ,KAAK;AAAI;;AAG3D,SAAS,aAAa,OAAgC;CACpD,IAAI,UAAU,WAAW,OAAO;CAChC,IAAI,UAAU,UAAU,OAAO;CAC/B,OAAO;AACT;;;;;AAMA,SAAS,aAAa,MAAiB,MAA0C;CAC/E,MAAM,UAAU,KAAK,QAAQ,QAAQ,WAAW,OAAO,OAAO;CAC9D,IAAI,QAAQ,WAAW,GAAG,OAAO;CACjC,MAAM,SAAgB;EAAC;EAAG;EAAG;EAAG;CAAC;CACjC,KAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,UAAU,aAAa,OAAO,KAAK;EACzC,MAAM,QAAQ,SAAS,YAAY,UAAU,IAAI;EACjD,MAAM,UA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B,OAAO,MAAM,GAAG;CACrC,MAAM,UAAU,aAAa,OAAO,SAAS;CAC7C,IAAI,CAAC,SAAS;CACd,KAAK,MAAM,CAAC,MAAM,SAAS,MAAM,QAAQ,GAAG,UAAU,UAAU,MAAM,QAAQ,KAAK;AACrF;;;;;AAMA,SAAgB,WACd,QACA,UACA,MACQ;CACR,IAAI,CAAC,OAAO,yBAAyB,OAAO;CAC5C,MAAM,CAAC,OAAO,OAAO,SAAS,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC;CACtD,MAAM,UAAU,UAAyB,QAAQ,MAAM,SAAS,MAAM,OAAO;CAC7E,IAAI,OAAO,OAAO,aAAa,QAAQ,QAAQ,CAAC;CAChD,KAAK,MAAM,SAAS,UAClB,IAAI,gBAAgB,OAAO,MAAM,GAAG,QAAQ,OAAO,aAAa,OAAO,SAAS,CAAC;CAEnF,OAAO;AACT"}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { GridTrack, SceneGraph, SceneNode } from "@pairdraw/scene-graph";
|
|
2
2
|
import { Align, GridTrackType, Justify, Node } from "yoga-layout";
|
|
3
3
|
//#region src/layout/yoga-helpers.d.ts
|
|
4
|
+
/** Yoga nodes created and not yet freed by `freeYogaTree`. */
|
|
5
|
+
declare function liveYogaNodeCount(): number;
|
|
4
6
|
declare function createYogaNode(): Node;
|
|
5
7
|
declare function configureAbsoluteChild(yogaChild: Node, child: SceneNode): void;
|
|
6
8
|
declare function configureNonTextLeaf(yogaChild: Node, child: SceneNode, isRow: boolean, stretchCross: boolean): void;
|
|
@@ -22,5 +24,5 @@ declare function mapAlignSelf(alignSelf: string): Align | null;
|
|
|
22
24
|
/** The axis a frame's auto layout parent stretches it along, if any. */
|
|
23
25
|
declare function parentStretchedAxis(graph: SceneGraph, frame: SceneNode): 'width' | 'height' | null;
|
|
24
26
|
//#endregion
|
|
25
|
-
export { applyLockedAspectRatio, applyMinMaxConstraints, configureAbsoluteChild, configureNonTextLeaf, createYogaNode, freeYogaTree, mapAlign, mapAlignSelf, mapGridTrack, mapJustify, parentStretchedAxis };
|
|
27
|
+
export { applyLockedAspectRatio, applyMinMaxConstraints, configureAbsoluteChild, configureNonTextLeaf, createYogaNode, freeYogaTree, liveYogaNodeCount, mapAlign, mapAlignSelf, mapGridTrack, mapJustify, parentStretchedAxis };
|
|
26
28
|
//# sourceMappingURL=yoga-helpers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"yoga-helpers.d.ts","names":[],"sources":["../../src/layout/yoga-helpers.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"yoga-helpers.d.ts","names":[],"sources":["../../src/layout/yoga-helpers.ts"],"mappings":";;;;iBAuBgB;iBAIA,kBAAkB;iBAKlB,uBAAuB,WAAW,MAAU,OAAO;iBAQnD,qBACd,WAAW,MACX,OAAO,WACP,gBACA;;;;;;iBA2Bc,uBACd,UAAU,MACV,MAAM,WACN,QAAQ;iBAmBM,uBAAuB,UAAU,MAAU,MAAM;iBAOjD,aAAa,OAAO;EAAc,MAAM;EAAe;;iBAWvD,aAAa,MAAM;iBAWnB,WAAW,gBAAgB;iBAe3B,SAAS,gBAAgB;iBAezB,aAAa,oBAAoB;;iBAkBjC,oBACd,OAAO,YACP,OAAO"}
|
|
@@ -3,7 +3,18 @@ import Yoga, { Align, Edge, GridTrackType, Justify, PositionType } from "yoga-la
|
|
|
3
3
|
//#region src/layout/yoga-helpers.ts
|
|
4
4
|
const yogaConfig = Yoga.Config.create();
|
|
5
5
|
yogaConfig.setPointScaleFactor(0);
|
|
6
|
+
/**
|
|
7
|
+
* Yoga nodes built and not freed yet. Yoga's nodes live in WebAssembly memory the caller owns, so a
|
|
8
|
+
* pass that leaves its tree behind holds that memory and the nodes it holds; a test reads this to
|
|
9
|
+
* see one freed on every path.
|
|
10
|
+
*/
|
|
11
|
+
let liveYogaNodes = 0;
|
|
12
|
+
/** Yoga nodes created and not yet freed by `freeYogaTree`. */
|
|
13
|
+
function liveYogaNodeCount() {
|
|
14
|
+
return liveYogaNodes;
|
|
15
|
+
}
|
|
6
16
|
function createYogaNode() {
|
|
17
|
+
liveYogaNodes += 1;
|
|
7
18
|
return Yoga.Node.create(yogaConfig);
|
|
8
19
|
}
|
|
9
20
|
function configureAbsoluteChild(yogaChild, child) {
|
|
@@ -76,7 +87,10 @@ function mapGridTrack(track) {
|
|
|
76
87
|
}
|
|
77
88
|
function freeYogaTree(node) {
|
|
78
89
|
for (let i = node.getChildCount() - 1; i >= 0; i--) freeYogaTree(node.getChild(i));
|
|
79
|
-
if ("free" in node
|
|
90
|
+
if ("free" in node && typeof node.free === "function") {
|
|
91
|
+
node.free();
|
|
92
|
+
liveYogaNodes -= 1;
|
|
93
|
+
}
|
|
80
94
|
}
|
|
81
95
|
function mapJustify(align) {
|
|
82
96
|
switch (align) {
|
|
@@ -115,6 +129,6 @@ function parentStretchedAxis(graph, frame) {
|
|
|
115
129
|
return parent.layoutMode === "VERTICAL" ? "width" : "height";
|
|
116
130
|
}
|
|
117
131
|
//#endregion
|
|
118
|
-
export { applyLockedAspectRatio, applyMinMaxConstraints, configureAbsoluteChild, configureNonTextLeaf, createYogaNode, freeYogaTree, mapAlign, mapAlignSelf, mapGridTrack, mapJustify, parentStretchedAxis };
|
|
132
|
+
export { applyLockedAspectRatio, applyMinMaxConstraints, configureAbsoluteChild, configureNonTextLeaf, createYogaNode, freeYogaTree, liveYogaNodeCount, mapAlign, mapAlignSelf, mapGridTrack, mapJustify, parentStretchedAxis };
|
|
119
133
|
|
|
120
134
|
//# sourceMappingURL=yoga-helpers.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"yoga-helpers.js","names":[],"sources":["../../src/layout/yoga-helpers.ts"],"sourcesContent":["import Yoga, {\n Align,\n Edge,\n GridTrackType,\n Justify,\n PositionType,\n type Node as YogaNode\n} from 'yoga-layout'\n\nimport type { GridTrack, SceneGraph, SceneNode } from '@pairdraw/scene-graph'\nimport { enforcedAspectRatio, layoutSizingInParent } from '@pairdraw/scene-graph/layout'\n\nconst yogaConfig = Yoga.Config.create()\nyogaConfig.setPointScaleFactor(0)\n\nexport function createYogaNode(): YogaNode {\n return Yoga.Node.create(yogaConfig)\n}\n\nexport function configureAbsoluteChild(yogaChild: YogaNode, child: SceneNode): void {\n yogaChild.setPositionType(PositionType.Absolute)\n yogaChild.setPosition(Edge.Left, child.x)\n yogaChild.setPosition(Edge.Top, child.y)\n yogaChild.setWidth(child.width)\n yogaChild.setHeight(child.height)\n}\n\nexport function configureNonTextLeaf(\n yogaChild: YogaNode,\n child: SceneNode,\n isRow: boolean,\n stretchCross: boolean\n): void {\n const w = child.width\n const h = child.height\n\n if (child.layoutGrow > 0) {\n yogaChild.setFlexGrow(child.layoutGrow)\n if (!stretchCross) {\n if (isRow) yogaChild.setHeight(h)\n else yogaChild.setWidth(w)\n }\n } else {\n if (isRow) {\n yogaChild.setWidth(w)\n if (!stretchCross) yogaChild.setHeight(h)\n } else {\n yogaChild.setHeight(h)\n if (!stretchCross) yogaChild.setWidth(w)\n }\n }\n}\n\n/**\n * A locked aspect ratio sizes the axis Fill does not drive, as Figma lays it out: Fill along the\n * parent's primary axis drives when set, else Fill across it, and the other axis follows even past\n * the parent's edge. An axis that hugs keeps its own size.\n */\nexport function applyLockedAspectRatio(\n yogaNode: YogaNode,\n node: SceneNode,\n parent: SceneNode\n): void {\n const ratio = enforcedAspectRatio(node)\n if (ratio === null) return\n const width = layoutSizingInParent(parent, node, 'HORIZONTAL')\n const height = layoutSizingInParent(parent, node, 'VERTICAL')\n const primaryIsWidth = parent.layoutMode !== 'VERTICAL'\n const primary = primaryIsWidth ? width : height\n const cross = primaryIsWidth ? height : width\n let drivesWidth: boolean\n if (primary === 'FILL') drivesWidth = primaryIsWidth\n else if (cross === 'FILL') drivesWidth = !primaryIsWidth\n else return\n if ((drivesWidth ? height : width) === 'HUG') return\n yogaNode.setAspectRatio(ratio)\n if (drivesWidth) yogaNode.setHeight('auto')\n else yogaNode.setWidth('auto')\n}\n\nexport function applyMinMaxConstraints(yogaNode: YogaNode, node: SceneNode): void {\n if (node.minWidth != null) yogaNode.setMinWidth(node.minWidth)\n if (node.maxWidth != null) yogaNode.setMaxWidth(node.maxWidth)\n if (node.minHeight != null) yogaNode.setMinHeight(node.minHeight)\n if (node.maxHeight != null) yogaNode.setMaxHeight(node.maxHeight)\n}\n\nexport function mapGridTrack(track: GridTrack): { type: GridTrackType; value: number } {\n switch (track.sizing) {\n case 'FR':\n return { type: GridTrackType.Fr, value: track.value }\n case 'FIXED':\n return { type: GridTrackType.Points, value: track.value }\n default:\n return { type: GridTrackType.Auto, value: 0 }\n }\n}\n\nexport function freeYogaTree(node: YogaNode): void {\n for (let i = node.getChildCount() - 1; i >= 0; i--) {\n freeYogaTree(node.getChild(i))\n }\n if ('free' in node
|
|
1
|
+
{"version":3,"file":"yoga-helpers.js","names":[],"sources":["../../src/layout/yoga-helpers.ts"],"sourcesContent":["import Yoga, {\n Align,\n Edge,\n GridTrackType,\n Justify,\n PositionType,\n type Node as YogaNode\n} from 'yoga-layout'\n\nimport type { GridTrack, SceneGraph, SceneNode } from '@pairdraw/scene-graph'\nimport { enforcedAspectRatio, layoutSizingInParent } from '@pairdraw/scene-graph/layout'\n\nconst yogaConfig = Yoga.Config.create()\nyogaConfig.setPointScaleFactor(0)\n\n/**\n * Yoga nodes built and not freed yet. Yoga's nodes live in WebAssembly memory the caller owns, so a\n * pass that leaves its tree behind holds that memory and the nodes it holds; a test reads this to\n * see one freed on every path.\n */\nlet liveYogaNodes = 0\n\n/** Yoga nodes created and not yet freed by `freeYogaTree`. */\nexport function liveYogaNodeCount(): number {\n return liveYogaNodes\n}\n\nexport function createYogaNode(): YogaNode {\n liveYogaNodes += 1\n return Yoga.Node.create(yogaConfig)\n}\n\nexport function configureAbsoluteChild(yogaChild: YogaNode, child: SceneNode): void {\n yogaChild.setPositionType(PositionType.Absolute)\n yogaChild.setPosition(Edge.Left, child.x)\n yogaChild.setPosition(Edge.Top, child.y)\n yogaChild.setWidth(child.width)\n yogaChild.setHeight(child.height)\n}\n\nexport function configureNonTextLeaf(\n yogaChild: YogaNode,\n child: SceneNode,\n isRow: boolean,\n stretchCross: boolean\n): void {\n const w = child.width\n const h = child.height\n\n if (child.layoutGrow > 0) {\n yogaChild.setFlexGrow(child.layoutGrow)\n if (!stretchCross) {\n if (isRow) yogaChild.setHeight(h)\n else yogaChild.setWidth(w)\n }\n } else {\n if (isRow) {\n yogaChild.setWidth(w)\n if (!stretchCross) yogaChild.setHeight(h)\n } else {\n yogaChild.setHeight(h)\n if (!stretchCross) yogaChild.setWidth(w)\n }\n }\n}\n\n/**\n * A locked aspect ratio sizes the axis Fill does not drive, as Figma lays it out: Fill along the\n * parent's primary axis drives when set, else Fill across it, and the other axis follows even past\n * the parent's edge. An axis that hugs keeps its own size.\n */\nexport function applyLockedAspectRatio(\n yogaNode: YogaNode,\n node: SceneNode,\n parent: SceneNode\n): void {\n const ratio = enforcedAspectRatio(node)\n if (ratio === null) return\n const width = layoutSizingInParent(parent, node, 'HORIZONTAL')\n const height = layoutSizingInParent(parent, node, 'VERTICAL')\n const primaryIsWidth = parent.layoutMode !== 'VERTICAL'\n const primary = primaryIsWidth ? width : height\n const cross = primaryIsWidth ? height : width\n let drivesWidth: boolean\n if (primary === 'FILL') drivesWidth = primaryIsWidth\n else if (cross === 'FILL') drivesWidth = !primaryIsWidth\n else return\n if ((drivesWidth ? height : width) === 'HUG') return\n yogaNode.setAspectRatio(ratio)\n if (drivesWidth) yogaNode.setHeight('auto')\n else yogaNode.setWidth('auto')\n}\n\nexport function applyMinMaxConstraints(yogaNode: YogaNode, node: SceneNode): void {\n if (node.minWidth != null) yogaNode.setMinWidth(node.minWidth)\n if (node.maxWidth != null) yogaNode.setMaxWidth(node.maxWidth)\n if (node.minHeight != null) yogaNode.setMinHeight(node.minHeight)\n if (node.maxHeight != null) yogaNode.setMaxHeight(node.maxHeight)\n}\n\nexport function mapGridTrack(track: GridTrack): { type: GridTrackType; value: number } {\n switch (track.sizing) {\n case 'FR':\n return { type: GridTrackType.Fr, value: track.value }\n case 'FIXED':\n return { type: GridTrackType.Points, value: track.value }\n default:\n return { type: GridTrackType.Auto, value: 0 }\n }\n}\n\nexport function freeYogaTree(node: YogaNode): void {\n for (let i = node.getChildCount() - 1; i >= 0; i--) {\n freeYogaTree(node.getChild(i))\n }\n // Wasm-backed nodes carry `free`, which `yoga-layout`'s type leaves out.\n if ('free' in node && typeof node.free === 'function') {\n node.free()\n liveYogaNodes -= 1\n }\n}\n\nexport function mapJustify(align: string): Justify {\n switch (align) {\n case 'CENTER':\n return Justify.Center\n case 'MAX':\n return Justify.FlexEnd\n case 'SPACE_BETWEEN':\n return Justify.SpaceBetween\n case 'SPACE_AROUND':\n return Justify.SpaceAround\n default:\n return Justify.FlexStart\n }\n}\n\nexport function mapAlign(align: string): Align {\n switch (align) {\n case 'CENTER':\n return Align.Center\n case 'MAX':\n return Align.FlexEnd\n case 'STRETCH':\n return Align.Stretch\n case 'BASELINE':\n return Align.Baseline\n default:\n return Align.FlexStart\n }\n}\n\nexport function mapAlignSelf(alignSelf: string): Align | null {\n switch (alignSelf) {\n case 'MIN':\n return Align.FlexStart\n case 'CENTER':\n return Align.Center\n case 'MAX':\n return Align.FlexEnd\n case 'STRETCH':\n return Align.Stretch\n case 'BASELINE':\n return Align.Baseline\n default:\n return null\n }\n}\n\n/** The axis a frame's auto layout parent stretches it along, if any. */\nexport function parentStretchedAxis(\n graph: SceneGraph,\n frame: SceneNode\n): 'width' | 'height' | null {\n const parent = frame.parentId ? graph.getNode(frame.parentId) : undefined\n if (!parent || frame.layoutPositioning === 'ABSOLUTE') return null\n if (parent.layoutMode !== 'HORIZONTAL' && parent.layoutMode !== 'VERTICAL') return null\n const stretched =\n frame.layoutAlignSelf === 'STRETCH' ||\n (frame.layoutAlignSelf === 'AUTO' && parent.counterAxisAlign === 'STRETCH')\n if (!stretched) return null\n return parent.layoutMode === 'VERTICAL' ? 'width'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AAM;EACf,SACE,OAAO,MAAM;CACjB;AACF;AAEA,SAAgB,aAAa,WAAiC;CAC5D,QAAQ,WAAR;EACE,KAAK,OACH,OAAO,MAAM;EACf,KAAK,UACH,OAAO,MAAM;EACf,KAAK,OACH,OAAO,MAAM;EACf,KAAK,WACH,OAAO,MAAM;EACf,KAAK,YACH,OAAO,MAAM;EACf,SACE,OAAO;CACX;AACF;;AAGA,SAAgB,oBACd,OACA,OAC2B;CAC3B,MAAM,SAAS,MAAM,WAAW,MAAM,QAAQ,MAAM,QAAQ,IAAI,KAAA;CAChE,IAAI,CAAC,UAAU,MAAM,sBAAsB,YAAY,OAAO;CAC9D,IAAI,OAAO,eAAe,gBAAgB,OAAO,eAAe,YAAY,OAAO;CAInF,IAAI,EAFF,MAAM,oBAAoB,aACzB,MAAM,oBAAoB,UAAU,OAAO,qBAAqB,YACnD,OAAO;CACvB,OAAO,OAAO,eAAe,aAAa,UAAU;AACtD"}
|
package/dist/layout.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"layout.d.ts","names":[],"sources":["../src/layout.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"layout.d.ts","names":[],"sources":["../src/layout.ts"],"mappings":";;;;iBAgEgB,cAAc,OAAO,YAAY;iBA2CjC,kBAAkB,OAAO,YAAY;;;;;iBAgBrC,wBAAwB,OAAO,YAAY;;;;;;iBAe3C,oBAAoB,OAAO,YAAY,SAAS"}
|