@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
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { DocumentFontStatus } from "../text/font/status.js";
|
|
2
2
|
import { RPCCommand } from "./types.js";
|
|
3
|
+
import { BooleanOperation, MaskType } from "@pairdraw/scene-graph";
|
|
3
4
|
//#region src/rpc/read-commands.d.ts
|
|
5
|
+
/** A page picked by its ID, `page_id`, or by its name, `page`; the ID wins when both are given. */
|
|
6
|
+
interface PageArgs {
|
|
7
|
+
page?: string;
|
|
8
|
+
page_id?: string;
|
|
9
|
+
}
|
|
4
10
|
interface InfoResult {
|
|
5
11
|
pages: number;
|
|
6
12
|
totalNodes: number;
|
|
@@ -16,8 +22,7 @@ interface PageItem {
|
|
|
16
22
|
nodes: number;
|
|
17
23
|
}
|
|
18
24
|
declare const pagesCommand: RPCCommand<void, PageItem[]>;
|
|
19
|
-
interface TreeArgs {
|
|
20
|
-
page?: string;
|
|
25
|
+
interface TreeArgs extends PageArgs {
|
|
21
26
|
depth?: number;
|
|
22
27
|
}
|
|
23
28
|
interface TreeNodeResult {
|
|
@@ -41,10 +46,9 @@ interface TreeResult {
|
|
|
41
46
|
declare const treeCommand: RPCCommand<TreeArgs, TreeResult | {
|
|
42
47
|
error: string;
|
|
43
48
|
}>;
|
|
44
|
-
interface FindArgs {
|
|
49
|
+
interface FindArgs extends PageArgs {
|
|
45
50
|
name?: string;
|
|
46
51
|
type?: string;
|
|
47
|
-
page?: string;
|
|
48
52
|
limit?: number;
|
|
49
53
|
}
|
|
50
54
|
interface FindNodeResult {
|
|
@@ -55,9 +59,8 @@ interface FindNodeResult {
|
|
|
55
59
|
height: number;
|
|
56
60
|
}
|
|
57
61
|
declare const findCommand: RPCCommand<FindArgs, FindNodeResult[]>;
|
|
58
|
-
interface QueryArgs {
|
|
62
|
+
interface QueryArgs extends PageArgs {
|
|
59
63
|
selector: string;
|
|
60
|
-
page?: string;
|
|
61
64
|
limit?: number;
|
|
62
65
|
}
|
|
63
66
|
interface QueryNodeResult {
|
|
@@ -90,7 +93,14 @@ interface NodeResult {
|
|
|
90
93
|
fills: unknown[];
|
|
91
94
|
strokes: unknown[];
|
|
92
95
|
effects: unknown[];
|
|
93
|
-
cornerRadius
|
|
96
|
+
cornerRadius?: number;
|
|
97
|
+
topLeftRadius?: number;
|
|
98
|
+
topRightRadius?: number;
|
|
99
|
+
bottomRightRadius?: number;
|
|
100
|
+
bottomLeftRadius?: number;
|
|
101
|
+
isMask?: boolean;
|
|
102
|
+
maskType?: MaskType;
|
|
103
|
+
booleanOperation?: BooleanOperation;
|
|
94
104
|
blendMode: string;
|
|
95
105
|
layoutMode: string;
|
|
96
106
|
layoutDirection: string;
|
|
@@ -111,5 +121,5 @@ declare const nodeCommand: RPCCommand<NodeArgs, NodeResult | {
|
|
|
111
121
|
error: string;
|
|
112
122
|
}>;
|
|
113
123
|
//#endregion
|
|
114
|
-
export { FindArgs, FindNodeResult, InfoResult, NodeArgs, NodeResult, PageItem, QueryArgs, QueryNodeResult, TreeArgs, TreeNodeResult, TreeResult, findCommand, fontStatusCommand, infoCommand, nodeCommand, pagesCommand, queryCommand, treeCommand };
|
|
124
|
+
export { FindArgs, FindNodeResult, InfoResult, NodeArgs, NodeResult, PageArgs, PageItem, QueryArgs, QueryNodeResult, TreeArgs, TreeNodeResult, TreeResult, findCommand, fontStatusCommand, infoCommand, nodeCommand, pagesCommand, queryCommand, treeCommand };
|
|
115
125
|
//# sourceMappingURL=read-commands.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"read-commands.d.ts","names":[],"sources":["../../src/rpc/read-commands.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"read-commands.d.ts","names":[],"sources":["../../src/rpc/read-commands.ts"],"mappings":";;;;;UAwBiB;EACf;EACA;;UA4Ce;EACf;EACA;EACA,OAAO;EACP;EACA,YAAY;;cAGD,aAAa,iBAAiB;cA4B9B,mBAAmB,iBAAiB;UAUhC;EACf;EACA;EACA;;cAGW,cAAc,iBAAiB;UAS3B,iBAAiB;EAChC;;UAGe;EACf;EACA;EACA;EACA;EACA;EACA;EACA;EACA,WAAW;;UAyBI;EACf;IAAQ;IAAY;IAAc;;EAClC,UAAU;;cAGC,aAAa,WAAW,UAAU;EAAe;;UAkB7C,iBAAiB;EAChC;EACA;EACA;;UAGe;EACf;EACA;EACA;EACA;EACA;;cAGW,aAAa,WAAW,UAAU;UAqC9B,kBAAkB;EACjC;EACA;;UAGe;EACf;EACA;EACA;EACA;EACA;EACA;EACA;;cAGW,cAAc,WAAW,WAAW;EAAsB;;UA0BtD;EACf;;UAGe;EACf;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,WAAW;EACX,mBAAmB;EACnB;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;IAAU;IAAY;IAAc;;EACpC;EACA,gBAAgB;;cAGL,aAAa,WAAW,UAAU;EAAe"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import "../text/ellipsis.js";
|
|
2
2
|
import { prepareGraphFonts } from "../text/prepare.js";
|
|
3
3
|
import { queryByXPath } from "../xpath.js";
|
|
4
|
+
import { shapeDetailFields } from "../figma-api/shape-details.js";
|
|
4
5
|
import { sumBy } from "es-toolkit";
|
|
5
6
|
//#region src/rpc/read-commands.ts
|
|
6
7
|
/** Walk descendants. Callback returns `false` to stop traversal. */
|
|
@@ -11,6 +12,18 @@ function walkNodes(graph, rootId, fn) {
|
|
|
11
12
|
for (const childId of node.childIds) if (!walkNodes(graph, childId, fn)) return false;
|
|
12
13
|
return true;
|
|
13
14
|
}
|
|
15
|
+
/** The pages `args` picks: the one it names, none when it names a missing page, else `fallback`. */
|
|
16
|
+
function pickedPages(graph, args, fallback) {
|
|
17
|
+
const pages = graph.getPages();
|
|
18
|
+
if (args.page_id) return pages.filter((page) => page.id === args.page_id);
|
|
19
|
+
if (args.page) return pages.filter((page) => page.name === args.page).slice(0, 1);
|
|
20
|
+
return fallback;
|
|
21
|
+
}
|
|
22
|
+
function missingPageError(graph, args) {
|
|
23
|
+
const pages = graph.getPages();
|
|
24
|
+
const [label, available] = args.page_id ? [args.page_id, pages.map((page) => page.id)] : [args.page ?? "", pages.map((page) => page.name)];
|
|
25
|
+
return `Page "${label}" not found. Available: ${available.join(", ")}`;
|
|
26
|
+
}
|
|
14
27
|
function countDescendants(graph, rootId) {
|
|
15
28
|
let count = 0;
|
|
16
29
|
walkNodes(graph, rootId, () => {
|
|
@@ -91,10 +104,9 @@ function buildTreeNode(graph, id, depth, maxDepth) {
|
|
|
91
104
|
const treeCommand = {
|
|
92
105
|
name: "tree",
|
|
93
106
|
execute: (graph, args) => {
|
|
94
|
-
const pages = graph.getPages();
|
|
95
107
|
const maxDepth = args.depth ?? Infinity;
|
|
96
|
-
const page = args
|
|
97
|
-
if (!page) return { error:
|
|
108
|
+
const page = pickedPages(graph, args, graph.getPages().slice(0, 1)).at(0);
|
|
109
|
+
if (!page) return { error: missingPageError(graph, args) };
|
|
98
110
|
return {
|
|
99
111
|
page: {
|
|
100
112
|
id: page.id,
|
|
@@ -108,7 +120,6 @@ const treeCommand = {
|
|
|
108
120
|
const findCommand = {
|
|
109
121
|
name: "find",
|
|
110
122
|
execute: (graph, args) => {
|
|
111
|
-
const pages = graph.getPages();
|
|
112
123
|
const max = args.limit ?? 100;
|
|
113
124
|
const namePattern = args.name?.toLowerCase();
|
|
114
125
|
const typeFilter = args.type?.toUpperCase();
|
|
@@ -128,10 +139,7 @@ const findCommand = {
|
|
|
128
139
|
return true;
|
|
129
140
|
})) break;
|
|
130
141
|
};
|
|
131
|
-
|
|
132
|
-
const page = pages.find((p) => p.name === args.page);
|
|
133
|
-
if (page) searchPage(page);
|
|
134
|
-
} else for (const page of pages) searchPage(page);
|
|
142
|
+
for (const page of pickedPages(graph, args, graph.getPages())) searchPage(page);
|
|
135
143
|
return results;
|
|
136
144
|
}
|
|
137
145
|
};
|
|
@@ -141,6 +149,7 @@ const queryCommand = {
|
|
|
141
149
|
try {
|
|
142
150
|
return (await queryByXPath(graph, args.selector, {
|
|
143
151
|
page: args.page,
|
|
152
|
+
pageId: args.page_id,
|
|
144
153
|
limit: args.limit
|
|
145
154
|
})).map((n) => ({
|
|
146
155
|
id: n.id,
|
|
@@ -176,7 +185,7 @@ const nodeCommand = {
|
|
|
176
185
|
fills: node.fills,
|
|
177
186
|
strokes: node.strokes,
|
|
178
187
|
effects: node.effects,
|
|
179
|
-
|
|
188
|
+
...shapeDetailFields(node),
|
|
180
189
|
blendMode: node.blendMode,
|
|
181
190
|
layoutMode: node.layoutMode,
|
|
182
191
|
layoutDirection: node.layoutDirection,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"read-commands.js","names":[],"sources":["../../src/rpc/read-commands.ts"],"sourcesContent":["import { sumBy } from 'es-toolkit'\n\nimport type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'\n\nimport { ELLIPSIS } from '#core/text/ellipsis'\nimport type { DocumentFontStatus } from '#core/text/font/status'\nimport { prepareGraphFonts } from '#core/text/prepare'\nimport { queryByXPath } from '#core/xpath'\n\nimport type { RPCCommand } from './types'\n\n/** Walk descendants. Callback returns `false` to stop traversal. */\nfunction walkNodes(graph: SceneGraph, rootId: string, fn: (node: SceneNode) => boolean): boolean {\n const node = graph.getNode(rootId)\n if (!node) return true\n if (!fn(node)) return false\n for (const childId of node.childIds) {\n if (!walkNodes(graph, childId, fn)) return false\n }\n return true\n}\n\nfunction countDescendants(graph: SceneGraph, rootId: string): number {\n let count = 0\n walkNodes(graph, rootId, () => {\n count++\n return true\n })\n return count\n}\n\nfunction countNodes(graph: SceneGraph, pageId: string): number {\n const page = graph.getNode(pageId)\n return page ? sumBy(page.childIds, (id) => countDescendants(graph, id)) : 0\n}\n\nfunction nodeFrame(node: SceneNode) {\n return {\n x: Math.round(node.x),\n y: Math.round(node.y),\n width: Math.round(node.width),\n height: Math.round(node.height)\n }\n}\n\n// ── info ──\n\nexport interface InfoResult {\n pages: number\n totalNodes: number\n types: Record<string, number>\n fonts: string[]\n pageCounts: Record<string, number>\n}\n\nexport const infoCommand: RPCCommand<void, InfoResult> = {\n name: 'info',\n execute: (graph) => {\n const pages = graph.getPages()\n let totalNodes = 0\n const types: Record<string, number> = {}\n const fonts = new Set<string>()\n const pageCounts: Record<string, number> = {}\n\n const countNode = (node: SceneNode) => {\n totalNodes++\n types[node.type] = (types[node.type] ?? 0) + 1\n if (node.fontFamily) fonts.add(node.fontFamily)\n return true\n }\n\n for (const page of pages) {\n const beforePage = totalNodes\n for (const cid of page.childIds) {\n walkNodes(graph, cid, countNode)\n }\n pageCounts[page.name] = totalNodes - beforePage\n }\n\n return { pages: pages.length, totalNodes, types, fonts: [...fonts].sort(), pageCounts }\n }\n}\n\nexport const fontStatusCommand: RPCCommand<void, DocumentFontStatus> = {\n name: 'font-status',\n execute: async (graph) => {\n const pageIds = graph.getPages().map((page) => page.id)\n return prepareGraphFonts(graph, pageIds.length > 0 ? pageIds : [graph.rootId])\n }\n}\n\n// ── pages ──\n\nexport interface PageItem {\n id: string\n name: string\n nodes: number\n}\n\nexport const pagesCommand: RPCCommand<void, PageItem[]> = {\n name: 'pages',\n execute: (graph) => {\n return graph.getPages().map((p) => ({ id: p.id, name: p.name, nodes: countNodes(graph, p.id) }))\n }\n}\n\n// ── tree ──\n\nexport interface TreeArgs {\n page?: string\n depth?: number\n}\n\nexport interface TreeNodeResult {\n id: string\n name: string\n type: string\n x: number\n y: number\n width: number\n height: number\n children?: TreeNodeResult[]\n}\n\nfunction buildTreeNode(\n graph: SceneGraph,\n id: string,\n depth: number,\n maxDepth: number\n): TreeNodeResult | null {\n const node = graph.getNode(id)\n if (!node) return null\n const result: TreeNodeResult = {\n id: node.id,\n name: node.name,\n type: node.type,\n ...nodeFrame(node)\n }\n if (node.childIds.length > 0 && depth < maxDepth) {\n result.children = node.childIds\n .map((cid) => buildTreeNode(graph, cid, depth + 1, maxDepth))\n .filter((n): n is TreeNodeResult => n !== null)\n }\n return result\n}\n\nexport interface TreeResult {\n page: { id: string; name: string; type: string }\n children: TreeNodeResult[]\n}\n\nexport const treeCommand: RPCCommand<TreeArgs, TreeResult | { error: string }> = {\n name: 'tree',\n execute: (graph, args) => {\n const pages = graph.getPages()\n const maxDepth = args.depth ?? Infinity\n const page = args.page ? pages.find((p) => p.name === args.page) : pages[0]\n if (!page)\n return {\n error: `Page \"${args.page}\" not found. Available: ${pages.map((p) => p.name).join(', ')}`\n }\n\n return {\n page: { id: page.id, name: page.name, type: page.type },\n children: page.childIds\n .map((cid) => buildTreeNode(graph, cid, 0, maxDepth))\n .filter((n): n is TreeNodeResult => n !== null)\n }\n }\n}\n\n// ── find ──\n\nexport interface FindArgs {\n name?: string\n type?: string\n page?: string\n limit?: number\n}\n\nexport interface FindNodeResult {\n id: string\n name: string\n type: string\n width: number\n height: number\n}\n\nexport const findCommand: RPCCommand<FindArgs, FindNodeResult[]> = {\n name: 'find',\n execute: (graph, args) => {\n const pages = graph.getPages()\n const max = args.limit ?? 100\n const namePattern = args.name?.toLowerCase()\n const typeFilter = args.type?.toUpperCase()\n const results: FindNodeResult[] = []\n\n const searchPage = (page: SceneNode) => {\n for (const cid of page.childIds) {\n const cont = walkNodes(graph, cid, (node) => {\n if (results.length >= max) return false\n const matchesName = !namePattern || node.name.toLowerCase().includes(namePattern)\n const matchesType = !typeFilter || node.type === typeFilter\n if (matchesName && matchesType) {\n results.push({\n id: node.id,\n name: node.name,\n type: node.type,\n width: Math.round(node.width),\n height: Math.round(node.height)\n })\n }\n return true\n })\n if (!cont) break\n }\n }\n\n if (args.page) {\n const page = pages.find((p) => p.name === args.page)\n if (page) searchPage(page)\n } else {\n for (const page of pages) searchPage(page)\n }\n\n return results\n }\n}\n\n// ── query (xpath) ──\n\nexport interface QueryArgs {\n selector: string\n page?: string\n limit?: number\n}\n\nexport interface QueryNodeResult {\n id: string\n name: string\n type: string\n x: number\n y: number\n width: number\n height: number\n}\n\nexport const queryCommand: RPCCommand<QueryArgs, QueryNodeResult[] | { error: string }> = {\n name: 'query',\n execute: async (graph, args) => {\n try {\n const nodes = await queryByXPath(graph, args.selector, {\n page: args.page,\n limit: args.limit\n })\n return nodes.map((n) => ({\n id: n.id,\n name: n.name,\n type: n.type,\n x: Math.round(n.x),\n y: Math.round(n.y),\n width: Math.round(n.width),\n height: Math.round(n.height)\n }))\n } catch (err) {\n return { error: `XPath error: ${err instanceof Error ? err.message : String(err)}` }\n }\n }\n}\n\n// ── node ──\n\nexport interface NodeArgs {\n id: string\n}\n\nexport interface NodeResult {\n id: string\n name: string\n type: string\n x: number\n y: number\n width: number\n height: number\n visible: boolean\n locked: boolean\n opacity: number\n rotation: number\n fills: unknown[]\n strokes: unknown[]\n effects: unknown[]\n cornerRadius: number\n blendMode: string\n layoutMode: string\n layoutDirection: string\n fontFamily: string\n fontSize: number\n fontWeight: number\n textDirection: string\n text: string | null\n parent: { id: string; name: string; type: string } | null\n children: number\n boundVariables: Record<string, string>\n}\n\nexport const nodeCommand: RPCCommand<NodeArgs, NodeResult | { error: string }> = {\n name: 'node',\n execute: (graph, args) => {\n const node = graph.getNode(args.id)\n if (!node) return { error: `Node \"${args.id}\" not found` }\n\n const parent = node.parentId ? graph.getNode(node.parentId) : undefined\n const boundVars: Record<string, string> = {}\n for (const [field, varId] of Object.entries(node.boundVariables)) {\n const variable = graph.variables.get(varId)\n boundVars[field] = variable?.name ?? varId\n }\n\n return {\n id: node.id,\n name: node.name,\n type: node.type,\n ...nodeFrame(node),\n visible: node.visible,\n locked: node.locked,\n opacity: node.opacity,\n rotation: node.rotation,\n fills: node.fills,\n strokes: node.strokes,\n effects: node.effects,\n cornerRadius: node.cornerRadius,\n blendMode: node.blendMode,\n layoutMode: node.layoutMode,\n layoutDirection: node.layoutDirection,\n fontFamily: node.fontFamily,\n fontSize: node.fontSize,\n fontWeight: node.fontWeight,\n textDirection: node.textDirection,\n text: (() => {\n if (!node.text.length) return null\n if (node.text.length > 200) return node.text.slice(0, 200) + ELLIPSIS\n return node.text\n })(),\n parent: parent ? { id: parent.id, name: parent.name, type: parent.type } : null,\n children: node.childIds.length,\n boundVariables: boundVars\n }\n }\n}\n"],"mappings":";;;;;;AAYA,SAAS,UAAU,OAAmB,QAAgB,IAA2C;CAC/F,MAAM,OAAO,MAAM,QAAQ,MAAM;CACjC,IAAI,CAAC,MAAM,OAAO;CAClB,IAAI,CAAC,GAAG,IAAI,GAAG,OAAO;CACtB,KAAK,MAAM,WAAW,KAAK,UACzB,IAAI,CAAC,UAAU,OAAO,SAAS,EAAE,GAAG,OAAO;CAE7C,OAAO;AACT;AAEA,SAAS,iBAAiB,OAAmB,QAAwB;CACnE,IAAI,QAAQ;CACZ,UAAU,OAAO,cAAc;EAC7B;EACA,OAAO;CACT,CAAC;CACD,OAAO;AACT;AAEA,SAAS,WAAW,OAAmB,QAAwB;CAC7D,MAAM,OAAO,MAAM,QAAQ,MAAM;CACjC,OAAO,OAAO,MAAM,KAAK,WAAW,OAAO,iBAAiB,OAAO,EAAE,CAAC,IAAI;AAC5E;AAEA,SAAS,UAAU,MAAiB;CAClC,OAAO;EACL,GAAG,KAAK,MAAM,KAAK,CAAC;EACpB,GAAG,KAAK,MAAM,KAAK,CAAC;EACpB,OAAO,KAAK,MAAM,KAAK,KAAK;EAC5B,QAAQ,KAAK,MAAM,KAAK,MAAM;CAChC;AACF;AAYA,MAAa,cAA4C;CACvD,MAAM;CACN,UAAU,UAAU;EAClB,MAAM,QAAQ,MAAM,SAAS;EAC7B,IAAI,aAAa;EACjB,MAAM,QAAgC,CAAC;EACvC,MAAM,wBAAQ,IAAI,IAAY;EAC9B,MAAM,aAAqC,CAAC;EAE5C,M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AAK;GACd,cAAc,KAAK;GACnB,WAAW,KAAK;GAChB,YAAY,KAAK;GACjB,iBAAiB,KAAK;GACtB,YAAY,KAAK;GACjB,UAAU,KAAK;GACf,YAAY,KAAK;GACjB,eAAe,KAAK;GACpB,aAAa;IACX,IAAI,CAAC,KAAK,KAAK,QAAQ,OAAO;IAC9B,IAAI,KAAK,KAAK,SAAS,KAAK,OAAO,KAAK,KAAK,MAAM,GAAG,GAAG,IAAA;IACzD,OAAO,KAAK;GACd,EAAA,CAAG;GACH,QAAQ,SAAS;IAAE,IAAI,OAAO;IAAI,MAAM,OAAO;IAAM,MAAM,OAAO;GAAK,IAAI;GAC3E,UAAU,KAAK,SAAS;GACxB,gBAAgB;EAClB;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"read-commands.js","names":[],"sources":["../../src/rpc/read-commands.ts"],"sourcesContent":["import { sumBy } from 'es-toolkit'\n\nimport type { BooleanOperation, MaskType, SceneGraph, SceneNode } from '@pairdraw/scene-graph'\n\nimport { shapeDetailFields } from '#core/figma-api/shape-details'\nimport { ELLIPSIS } from '#core/text/ellipsis'\nimport type { DocumentFontStatus } from '#core/text/font/status'\nimport { prepareGraphFonts } from '#core/text/prepare'\nimport { queryByXPath } from '#core/xpath'\n\nimport type { RPCCommand } from './types'\n\n/** Walk descendants. Callback returns `false` to stop traversal. */\nfunction walkNodes(graph: SceneGraph, rootId: string, fn: (node: SceneNode) => boolean): boolean {\n const node = graph.getNode(rootId)\n if (!node) return true\n if (!fn(node)) return false\n for (const childId of node.childIds) {\n if (!walkNodes(graph, childId, fn)) return false\n }\n return true\n}\n\n/** A page picked by its ID, `page_id`, or by its name, `page`; the ID wins when both are given. */\nexport interface PageArgs {\n page?: string\n page_id?: string\n}\n\n/** The pages `args` picks: the one it names, none when it names a missing page, else `fallback`. */\nfunction pickedPages(graph: SceneGraph, args: PageArgs, fallback: SceneNode[]): SceneNode[] {\n const pages = graph.getPages()\n if (args.page_id) return pages.filter((page) => page.id === args.page_id)\n if (args.page) return pages.filter((page) => page.name === args.page).slice(0, 1)\n return fallback\n}\n\nfunction missingPageError(graph: SceneGraph, args: PageArgs): string {\n const pages = graph.getPages()\n const [label, available] = args.page_id\n ? [args.page_id, pages.map((page) => page.id)]\n : [args.page ?? '', pages.map((page) => page.name)]\n return `Page \"${label}\" not found. Available: ${available.join(', ')}`\n}\n\nfunction countDescendants(graph: SceneGraph, rootId: string): number {\n let count = 0\n walkNodes(graph, rootId, () => {\n count++\n return true\n })\n return count\n}\n\nfunction countNodes(graph: SceneGraph, pageId: string): number {\n const page = graph.getNode(pageId)\n return page ? sumBy(page.childIds, (id) => countDescendants(graph, id)) : 0\n}\n\nfunction nodeFrame(node: SceneNode) {\n return {\n x: Math.round(node.x),\n y: Math.round(node.y),\n width: Math.round(node.width),\n height: Math.round(node.height)\n }\n}\n\n// ── info ──\n\nexport interface InfoResult {\n pages: number\n totalNodes: number\n types: Record<string, number>\n fonts: string[]\n pageCounts: Record<string, number>\n}\n\nexport const infoCommand: RPCCommand<void, InfoResult> = {\n name: 'info',\n execute: (graph) => {\n const pages = graph.getPages()\n let totalNodes = 0\n const types: Record<string, number> = {}\n const fonts = new Set<string>()\n const pageCounts: Record<string, number> = {}\n\n const countNode = (node: SceneNode) => {\n totalNodes++\n types[node.type] = (types[node.type] ?? 0) + 1\n if (node.fontFamily) fonts.add(node.fontFamily)\n return true\n }\n\n for (const page of pages) {\n const beforePage = totalNodes\n for (const cid of page.childIds) {\n walkNodes(graph, cid, countNode)\n }\n pageCounts[page.name] = totalNodes - beforePage\n }\n\n return { pages: pages.length, totalNodes, types, fonts: [...fonts].sort(), pageCounts }\n }\n}\n\nexport const fontStatusCommand: RPCCommand<void, DocumentFontStatus> = {\n name: 'font-status',\n execute: async (graph) => {\n const pageIds = graph.getPages().map((page) => page.id)\n return prepareGraphFonts(graph, pageIds.length > 0 ? pageIds : [graph.rootId])\n }\n}\n\n// ── pages ──\n\nexport interface PageItem {\n id: string\n name: string\n nodes: number\n}\n\nexport const pagesCommand: RPCCommand<void, PageItem[]> = {\n name: 'pages',\n execute: (graph) => {\n return graph.getPages().map((p) => ({ id: p.id, name: p.name, nodes: countNodes(graph, p.id) }))\n }\n}\n\n// ── tree ──\n\nexport interface TreeArgs extends PageArgs {\n depth?: number\n}\n\nexport interface TreeNodeResult {\n id: string\n name: string\n type: string\n x: number\n y: number\n width: number\n height: number\n children?: TreeNodeResult[]\n}\n\nfunction buildTreeNode(\n graph: SceneGraph,\n id: string,\n depth: number,\n maxDepth: number\n): TreeNodeResult | null {\n const node = graph.getNode(id)\n if (!node) return null\n const result: TreeNodeResult = {\n id: node.id,\n name: node.name,\n type: node.type,\n ...nodeFrame(node)\n }\n if (node.childIds.length > 0 && depth < maxDepth) {\n result.children = node.childIds\n .map((cid) => buildTreeNode(graph, cid, depth + 1, maxDepth))\n .filter((n): n is TreeNodeResult => n !== null)\n }\n return result\n}\n\nexport interface TreeResult {\n page: { id: string; name: string; type: string }\n children: TreeNodeResult[]\n}\n\nexport const treeCommand: RPCCommand<TreeArgs, TreeResult | { error: string }> = {\n name: 'tree',\n execute: (graph, args) => {\n const maxDepth = args.depth ?? Infinity\n const page = pickedPages(graph, args, graph.getPages().slice(0, 1)).at(0)\n if (!page) return { error: missingPageError(graph, args) }\n\n return {\n page: { id: page.id, name: page.name, type: page.type },\n children: page.childIds\n .map((cid) => buildTreeNode(graph, cid, 0, maxDepth))\n .filter((n): n is TreeNodeResult => n !== null)\n }\n }\n}\n\n// ── find ──\n\nexport interface FindArgs extends PageArgs {\n name?: string\n type?: string\n limit?: number\n}\n\nexport interface FindNodeResult {\n id: string\n name: string\n type: string\n width: number\n height: number\n}\n\nexport const findCommand: RPCCommand<FindArgs, FindNodeResult[]> = {\n name: 'find',\n execute: (graph, args) => {\n const max = args.limit ?? 100\n const namePattern = args.name?.toLowerCase()\n const typeFilter = args.type?.toUpperCase()\n const results: FindNodeResult[] = []\n\n const searchPage = (page: SceneNode) => {\n for (const cid of page.childIds) {\n const cont = walkNodes(graph, cid, (node) => {\n if (results.length >= max) return false\n const matchesName = !namePattern || node.name.toLowerCase().includes(namePattern)\n const matchesType = !typeFilter || node.type === typeFilter\n if (matchesName && matchesType) {\n results.push({\n id: node.id,\n name: node.name,\n type: node.type,\n width: Math.round(node.width),\n height: Math.round(node.height)\n })\n }\n return true\n })\n if (!cont) break\n }\n }\n\n for (const page of pickedPages(graph, args, graph.getPages())) searchPage(page)\n\n return results\n }\n}\n\n// ── query (xpath) ──\n\nexport interface QueryArgs extends PageArgs {\n selector: string\n limit?: number\n}\n\nexport interface QueryNodeResult {\n id: string\n name: string\n type: string\n x: number\n y: number\n width: number\n height: number\n}\n\nexport const queryCommand: RPCCommand<QueryArgs, QueryNodeResult[] | { error: string }> = {\n name: 'query',\n execute: async (graph, args) => {\n try {\n const nodes = await queryByXPath(graph, args.selector, {\n page: args.page,\n pageId: args.page_id,\n limit: args.limit\n })\n return nodes.map((n) => ({\n id: n.id,\n name: n.name,\n type: n.type,\n x: Math.round(n.x),\n y: Math.round(n.y),\n width: Math.round(n.width),\n height: Math.round(n.height)\n }))\n } catch (err) {\n return { error: `XPath error: ${err instanceof Error ? err.message : String(err)}` }\n }\n }\n}\n\n// ── node ──\n\nexport interface NodeArgs {\n id: string\n}\n\nexport interface NodeResult {\n id: string\n name: string\n type: string\n x: number\n y: number\n width: number\n height: number\n visible: boolean\n locked: boolean\n opacity: number\n rotation: number\n fills: unknown[]\n strokes: unknown[]\n effects: unknown[]\n cornerRadius?: number\n topLeftRadius?: number\n topRightRadius?: number\n bottomRightRadius?: number\n bottomLeftRadius?: number\n isMask?: boolean\n maskType?: MaskType\n booleanOperation?: BooleanOperation\n blendMode: string\n layoutMode: string\n layoutDirection: string\n fontFamily: string\n fontSize: number\n fontWeight: number\n textDirection: string\n text: string | null\n parent: { id: string; name: string; type: string } | null\n children: number\n boundVariables: Record<string, string>\n}\n\nexport const nodeCommand: RPCCommand<NodeArgs, NodeResult | { error: string }> = {\n name: 'node',\n execute: (graph, args) => {\n const node = graph.getNode(args.id)\n if (!node) return { error: `Node \"${args.id}\" not found` }\n\n const parent = node.parentId ? graph.getNode(node.parentId) : undefined\n const boundVars: Record<string, string> = {}\n for (const [field, varId] of Object.entries(node.boundVariables)) {\n const variable = graph.variables.get(varId)\n boundVars[field] = variable?.name ?? varId\n }\n return {\n id: node.id,\n name: node.name,\n type: node.type,\n ...nodeFrame(node),\n visible: node.visible,\n locked: node.locked,\n opacity: node.opacity,\n rotation: node.rotation,\n fills: node.fills,\n strokes: node.strokes,\n effects: node.effects,\n ...shapeDetailFields(node),\n blendMode: node.blendMode,\n layoutMode: node.layoutMode,\n layoutDirection: node.layoutDirection,\n fontFamily: node.fontFamily,\n fontSize: node.fontSize,\n fontWeight: node.fontWeight,\n textDirection: node.textDirection,\n text: (() => {\n if (!node.text.length) return null\n if (node.text.length > 200) return node.text.slice(0, 200) + ELLIPSIS\n return node.text\n })(),\n parent: parent ? { id: parent.id, name: parent.name, type: parent.type } : null,\n children: node.childIds.length,\n boundVariables: boundVars\n }\n }\n}\n"],"mappings":";;;;;;;AAaA,SAAS,UAAU,OAAmB,QAAgB,IAA2C;CAC/F,MAAM,OAAO,MAAM,QAAQ,MAAM;CACjC,IAAI,CAAC,MAAM,OAAO;CAClB,IAAI,CAAC,GAAG,IAAI,GAAG,OAAO;CACtB,KAAK,MAAM,WAAW,KAAK,UACzB,IAAI,CAAC,UAAU,OAAO,SAAS,EAAE,GAAG,OAAO;CAE7C,OAAO;AACT;;AASA,SAAS,YAAY,OAAmB,MAAgB,UAAoC;CAC1F,MAAM,QAAQ,MAAM,SAAS;CAC7B,IAAI,KAAK,SAAS,OAAO,MAAM,QAAQ,SAAS,KAAK,OAAO,KAAK,OAAO;CACxE,IAAI,KAAK,MAAM,OAAO,MAAM,QAAQ,SAAS,KAAK,SAAS,KAAK,IAAI,CAAC,CAAC,MAAM,GAAG,CAAC;CAChF,OAAO;AACT;AAEA,SAAS,iBAAiB,OAAmB,MAAwB;CACnE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,17 @@ declare function fontFaceFromFigmaFontName(fontName: {
|
|
|
38
38
|
postscript?: string;
|
|
39
39
|
}): FontFaceRef;
|
|
40
40
|
declare function fontFaceRenderFamily(family: string, style: string): string;
|
|
41
|
+
/** What a font file is registered with in the page's `document.fonts`. */
|
|
42
|
+
interface BrowserFaceDescriptors {
|
|
43
|
+
weight: string;
|
|
44
|
+
style: 'normal' | 'italic';
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The page face for a font file loaded as `style`. A variable font covers its whole `wght` range,
|
|
48
|
+
* so text at any weight draws from the font's own instance; registered at the one weight `style`
|
|
49
|
+
* names, every other weight would fall back to that instance, synthesized bold or not at all.
|
|
50
|
+
*/
|
|
51
|
+
declare function browserFaceDescriptors(style: string, data: ArrayBuffer): BrowserFaceDescriptors;
|
|
41
52
|
//#endregion
|
|
42
|
-
export { FaceWeight, FamilyFace, FontFaceRef, facesFromStyles, fontFaceFromFigmaFontName, fontFaceRenderFamily, hasFamilyFace, nearestFamilyFace, standardFace, standardFaces };
|
|
53
|
+
export { BrowserFaceDescriptors, FaceWeight, FamilyFace, FontFaceRef, browserFaceDescriptors, facesFromStyles, fontFaceFromFigmaFontName, fontFaceRenderFamily, hasFamilyFace, nearestFamilyFace, standardFace, standardFaces };
|
|
43
54
|
//# sourceMappingURL=faces.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"faces.d.ts","names":[],"sources":["../../../src/text/font/faces.ts"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"faces.d.ts","names":[],"sources":["../../../src/text/font/faces.ts"],"mappings":";;;UAYiB;EACf;EACA;;;UAIe,mBAAmB;EAClC;;;;;;;iBAQc,gBAAgB,4BAA4B;;iBAU5C,aAAa,gBAAgB,kBAAkB;;iBAK/C,cAAc,4BAA4B;iBAI1C,cAAc,gBAAgB,cAAc,gBAAgB;;;;;;iBAS5D,kBACd,gBAAgB,cAChB,gBACA,kBACC;;UAcc,oBAAoB;EACnC;EACA;EACA;;iBAGc,0BAA0B;EACxC;EACA;EACA;IACE;iBAUY,qBAAqB,gBAAgB;;UAKpC;EACf;EACA;;;;;;;iBAQc,uBAAuB,eAAe,MAAM,cAAc"}
|
package/dist/text/font/faces.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { fontAxes } from "./variation.js";
|
|
1
2
|
import { namesWidth, parseFontStyle, standardStyleName } from "@pairdraw/scene-graph/font-style";
|
|
2
3
|
import { partition, uniqBy } from "es-toolkit";
|
|
3
4
|
//#region src/text/font/faces.ts
|
|
@@ -58,7 +59,20 @@ function fontFaceFromFigmaFontName(fontName) {
|
|
|
58
59
|
function fontFaceRenderFamily(family, style) {
|
|
59
60
|
return `__pairdraw_font__${family.replace(/[^a-z0-9_-]+/giu, "_")}__${style.replace(/[^a-z0-9_-]+/giu, "_")}`;
|
|
60
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* The page face for a font file loaded as `style`. A variable font covers its whole `wght` range,
|
|
64
|
+
* so text at any weight draws from the font's own instance; registered at the one weight `style`
|
|
65
|
+
* names, every other weight would fall back to that instance, synthesized bold or not at all.
|
|
66
|
+
*/
|
|
67
|
+
function browserFaceDescriptors(style, data) {
|
|
68
|
+
const { weight, italic } = parseFontStyle(style);
|
|
69
|
+
const wght = fontAxes(data)?.find((axis) => axis.tag === "wght");
|
|
70
|
+
return {
|
|
71
|
+
weight: wght ? `${wght.min} ${wght.max}` : String(weight),
|
|
72
|
+
style: italic ? "italic" : "normal"
|
|
73
|
+
};
|
|
74
|
+
}
|
|
61
75
|
//#endregion
|
|
62
|
-
export { facesFromStyles, fontFaceFromFigmaFontName, fontFaceRenderFamily, hasFamilyFace, nearestFamilyFace, standardFace, standardFaces };
|
|
76
|
+
export { browserFaceDescriptors, facesFromStyles, fontFaceFromFigmaFontName, fontFaceRenderFamily, hasFamilyFace, nearestFamilyFace, standardFace, standardFaces };
|
|
63
77
|
|
|
64
78
|
//# sourceMappingURL=faces.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"faces.js","names":[],"sources":["../../../src/text/font/faces.ts"],"sourcesContent":["import { partition, uniqBy } from 'es-toolkit'\n\nimport {\n namesWidth,\n parseFontStyle,\n standardStyleName,\n type ParsedFontStyle\n} from '@pairdraw/scene-graph/font-style'\n\n/** A weight, upright or italic. */\nexport interface FaceWeight {\n weight: number\n italic: boolean\n}\n\n/** One face a font family has: a weight, upright or italic, and the name its font gives it. */\nexport interface FamilyFace extends FaceWeight {\n style: string\n}\n\n/**\n * The faces named by a family's style names, such as \"Bold Italic\" or \"Semi Bold\", each keeping\n * its name, in the order listed. Of two names for one weight and slant, the first listed is the\n * face's, and a normal-width name (\"Medium\") goes before a width name (\"Compressed Medium\").\n */\nexport function facesFromStyles(styles: readonly string[]): FamilyFace[] {\n const faces = styles.map((style) => ({ ...parseFontStyle(style), style }))\n const [normalWidth, otherWidth] = partition(faces, (face) => !namesWidth(face.style))\n const slotFaces = new Set(\n uniqBy([...normalWidth, ...otherWidth], (face) => `${face.weight}${face.italic ? 'i' : ''}`)\n )\n return faces.filter((face) => slotFaces.has(face))\n}\n\n/** A face named as Google Fonts names it, for a source that gives weights but no names. */\nexport function standardFace(weight: number, italic: boolean): FamilyFace {\n return { weight, italic, style: standardStyleName(weight, italic) }\n}\n\n/** The faces style keys stand for, named as Google Fonts names them. */\nexport function standardFaces(styles: readonly string[]): FamilyFace[] {\n return facesFromStyles(styles).map((face) => standardFace(face.weight, face.italic))\n}\n\nexport function hasFamilyFace(faces: readonly FaceWeight[], weight: number, italic: boolean) {\n return faces.some((face) => face.weight === weight && face.italic === italic)\n}\n\n/**\n * The face a text takes in a family, as Figma picks it when the family changes: the same weight\n * and slant where the family has it, otherwise the nearest weight with the same slant, or the\n * nearest weight at all in a family without that slant. Equally near weights take the lighter.\n */\nexport function nearestFamilyFace(\n faces: readonly FamilyFace[],\n weight: number,\n italic: boolean\n): FamilyFace | null {\n const sameSlant = faces.filter((face) => face.italic === italic)\n const candidates = sameSlant.length > 0 ? sameSlant : faces\n let nearest: FamilyFace | null = null\n for (const face of candidates) {\n const distance = Math.abs(face.weight - weight)\n const best = nearest ? Math.abs(nearest.weight - weight) : Infinity\n if (distance < best || (distance === best && nearest && face.weight < nearest.weight))\n nearest = face\n }\n return nearest\n}\n\n/** A face text asks the font manager for: its family, its style key, and its weight and slant. */\nexport interface FontFaceRef extends ParsedFontStyle {\n family: string\n style: string\n postscriptName?: string\n}\n\nexport function fontFaceFromFigmaFontName(fontName: {\n family?: string\n style?: string\n postscript?: string\n}): FontFaceRef {\n const style = fontName.style ?? 'Regular'\n return {\n family: fontName.family ?? 'Inter',\n style,\n postscriptName: fontName.postscript,\n ...parseFontStyle(style)\n }\n}\n\nexport function fontFaceRenderFamily(family: string, style: string): string {\n return `__pairdraw_font__${family.replace(/[^a-z0-9_-]+/giu, '_')}__${style.replace(/[^a-z0-9_-]+/giu, '_')}`\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"faces.js","names":[],"sources":["../../../src/text/font/faces.ts"],"sourcesContent":["import { partition, uniqBy } from 'es-toolkit'\n\nimport {\n namesWidth,\n parseFontStyle,\n standardStyleName,\n type ParsedFontStyle\n} from '@pairdraw/scene-graph/font-style'\n\nimport { fontAxes } from './variation'\n\n/** A weight, upright or italic. */\nexport interface FaceWeight {\n weight: number\n italic: boolean\n}\n\n/** One face a font family has: a weight, upright or italic, and the name its font gives it. */\nexport interface FamilyFace extends FaceWeight {\n style: string\n}\n\n/**\n * The faces named by a family's style names, such as \"Bold Italic\" or \"Semi Bold\", each keeping\n * its name, in the order listed. Of two names for one weight and slant, the first listed is the\n * face's, and a normal-width name (\"Medium\") goes before a width name (\"Compressed Medium\").\n */\nexport function facesFromStyles(styles: readonly string[]): FamilyFace[] {\n const faces = styles.map((style) => ({ ...parseFontStyle(style), style }))\n const [normalWidth, otherWidth] = partition(faces, (face) => !namesWidth(face.style))\n const slotFaces = new Set(\n uniqBy([...normalWidth, ...otherWidth], (face) => `${face.weight}${face.italic ? 'i' : ''}`)\n )\n return faces.filter((face) => slotFaces.has(face))\n}\n\n/** A face named as Google Fonts names it, for a source that gives weights but no names. */\nexport function standardFace(weight: number, italic: boolean): FamilyFace {\n return { weight, italic, style: standardStyleName(weight, italic) }\n}\n\n/** The faces style keys stand for, named as Google Fonts names them. */\nexport function standardFaces(styles: readonly string[]): FamilyFace[] {\n return facesFromStyles(styles).map((face) => standardFace(face.weight, face.italic))\n}\n\nexport function hasFamilyFace(faces: readonly FaceWeight[], weight: number, italic: boolean) {\n return faces.some((face) => face.weight === weight && face.italic === italic)\n}\n\n/**\n * The face a text takes in a family, as Figma picks it when the family changes: the same weight\n * and slant where the family has it, otherwise the nearest weight with the same slant, or the\n * nearest weight at all in a family without that slant. Equally near weights take the lighter.\n */\nexport function nearestFamilyFace(\n faces: readonly FamilyFace[],\n weight: number,\n italic: boolean\n): FamilyFace | null {\n const sameSlant = faces.filter((face) => face.italic === italic)\n const candidates = sameSlant.length > 0 ? sameSlant : faces\n let nearest: FamilyFace | null = null\n for (const face of candidates) {\n const distance = Math.abs(face.weight - weight)\n const best = nearest ? Math.abs(nearest.weight - weight) : Infinity\n if (distance < best || (distance === best && nearest && face.weight < nearest.weight))\n nearest = face\n }\n return nearest\n}\n\n/** A face text asks the font manager for: its family, its style key, and its weight and slant. */\nexport interface FontFaceRef extends ParsedFontStyle {\n family: string\n style: string\n postscriptName?: string\n}\n\nexport function fontFaceFromFigmaFontName(fontName: {\n family?: string\n style?: string\n postscript?: string\n}): FontFaceRef {\n const style = fontName.style ?? 'Regular'\n return {\n family: fontName.family ?? 'Inter',\n style,\n postscriptName: fontName.postscript,\n ...parseFontStyle(style)\n }\n}\n\nexport function fontFaceRenderFamily(family: string, style: string): string {\n return `__pairdraw_font__${family.replace(/[^a-z0-9_-]+/giu, '_')}__${style.replace(/[^a-z0-9_-]+/giu, '_')}`\n}\n\n/** What a font file is registered with in the page's `document.fonts`. */\nexport interface BrowserFaceDescriptors {\n weight: string\n style: 'normal' | 'italic'\n}\n\n/**\n * The page face for a font file loaded as `style`. A variable font covers its whole `wght` range,\n * so text at any weight draws from the font's own instance; registered at the one weight `style`\n * names, every other weight would fall back to that instance, synthesized bold or not at all.\n */\nexport function browserFaceDescriptors(style: string, data: ArrayBuffer): BrowserFaceDescriptors {\n const { weight, italic } = parseFontStyle(style)\n const wght = fontAxes(data)?.find((axis) => axis.tag === 'wght')\n return {\n weight: wght ? `${wght.min} ${wght.max}` : String(weight),\n style: italic ? 'italic' : 'normal'\n }\n}\n"],"mappings":";;;;;;;;;AA2BA,SAAgB,gBAAgB,QAAyC;CACvE,MAAM,QAAQ,OAAO,KAAK,WAAW;EAAE,GAAG,eAAe,KAAK;EAAG;CAAM,EAAE;CACzE,MAAM,CAAC,aAAa,cAAc,UAAU,QAAQ,SAAS,CAAC,WAAW,KAAK,KAAK,CAAC;CACpF,MAAM,YAAY,IAAI,IACpB,OAAO,CAAC,GAAG,aAAa,GAAG,UAAU,IAAI,SAAS,GAAG,KAAK,SAAS,KAAK,SAAS,MAAM,IAAI,CAC7F;CACA,OAAO,MAAM,QAAQ,SAAS,UAAU,IAAI,IAAI,CAAC;AACnD;;AAGA,SAAgB,aAAa,QAAgB,QAA6B;CACxE,OAAO;EAAE;EAAQ;EAAQ,OAAO,kBAAkB,QAAQ,MAAM;CAAE;AACpE;;AAGA,SAAgB,cAAc,QAAyC;CACrE,OAAO,gBAAgB,MAAM,CAAC,CAAC,KAAK,SAAS,aAAa,KAAK,QAAQ,KAAK,MAAM,CAAC;AACrF;AAEA,SAAgB,cAAc,OAA8B,QAAgB,QAAiB;CAC3F,OAAO,MAAM,MAAM,SAAS,KAAK,WAAW,UAAU,KAAK,WAAW,MAAM;AAC9E;;;;;;AAOA,SAAgB,kBACd,OACA,QACA,QACmB;CACnB,MAAM,YAAY,MAAM,QAAQ,SAAS,KAAK,WAAW,MAAM;CAC/D,MAAM,aAAa,UAAU,SAAS,IAAI,YAAY;CACtD,IAAI,UAA6B;CACjC,KAAK,MAAM,QAAQ,YAAY;EAC7B,MAAM,WAAW,KAAK,IAAI,KAAK,SAAS,MAAM;EAC9C,MAAM,OAAO,UAAU,KAAK,IAAI,QAAQ,SAAS,MAAM,IAAI;EAC3D,IAAI,WAAW,QAAS,aAAa,QAAQ,WAAW,KAAK,SAAS,QAAQ,QAC5E,UAAU;CACd;CACA,OAAO;AACT;AASA,SAAgB,0BAA0B,UAI1B;CACd,MAAM,QAAQ,SAAS,SAAS;CAChC,OAAO;EACL,QAAQ,SAAS,UAAU;EAC3B;EACA,gBAAgB,SAAS;EACzB,GAAG,eAAe,KAAK;CACzB;AACF;AAEA,SAAgB,qBAAqB,QAAgB,OAAuB;CAC1E,OAAO,oBAAoB,OAAO,QAAQ,mBAAmB,GAAG,EAAE,IAAI,MAAM,QAAQ,mBAAmB,GAAG;AAC5G;;;;;;AAaA,SAAgB,uBAAuB,OAAe,MAA2C;CAC/F,MAAM,EAAE,QAAQ,WAAW,eAAe,KAAK;CAC/C,MAAM,OAAO,SAAS,IAAI,CAAC,EAAE,MAAM,SAAS,KAAK,QAAQ,MAAM;CAC/D,OAAO;EACL,QAAQ,OAAO,GAAG,KAAK,IAAI,GAAG,KAAK,QAAQ,OAAO,MAAM;EACxD,OAAO,SAAS,WAAW;CAC7B;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manager.d.ts","names":[],"sources":["../../../src/text/font/manager.ts"],"mappings":";;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"manager.d.ts","names":[],"sources":["../../../src/text/font/manager.ts"],"mappings":";;;;;;;;;;cAyDa;UACH;;UAEA;UACA;UACA;UACA;;UAEA;;;;;;UAMA;;;;;;UAMA;UACA;UACA;UACA;UAIA;UACA;UACA;UACA;UACA;UACA;mBACS;UAIT;UACA;UACA;mBACS;UACT;mBACS;mBACA;mBACA;mBACA;mBACA;UAKT;UACA;;mBAES;mBAGA;;;;;;;;;EAUjB,gBAAgB,WAAW,YAAY;EA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BAAgB;;;;;;;;;;;;;EAwBxD,aAAa,gBAAgB;;;;;;EAgB7B,oBAAoB,gBAAgB;;EAOpC,UAAU,gBAAgB,yBAAyB;;UAK3C;;UAWA;EAMR,gBAAgB,OAAO,YAAY,oBAAoB;EAIjD,qBAAqB;EAQ3B;EAIA,qBAAqB;UAMb;UAWA;;UAUA;EAIR,mBACE,UAAS,sBACT,qBACA,SAAS,cACR,QAAQ,QAAQ,OAAO;UAIZ;;;;;EA8Bd;EAIA,wBAAwB,QAAQ,sBAAsB;;;;;;;;UAYxC;;UAwCA;;;;;;UA0BN;UAyBA;UAQM;UAwBA;UAcA;;UAeN;UAiBA;;;;;EAoCR,aAAa,WAAW,gBAAgB;UAKhC;UAQA;;EAwBR,sBAAsB,gBAAgB,eAAe,MAAM;;cAsBhD,aAAW"}
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import { DEFAULT_FONT_FAMILY, IS_BROWSER } from "../../constants.js";
|
|
2
2
|
import { LETTER_FALLBACK_SCRIPTS, fallbackDrawsCharacter, fontFallbackEntry } from "../fallbacks.js";
|
|
3
3
|
import { BundledFonts } from "./bundled.js";
|
|
4
|
-
import { fontFaceRenderFamily } from "./faces.js";
|
|
5
|
-
import { FamilyFaceCatalog } from "./catalog.js";
|
|
6
|
-
import { resetFontCollection } from "./collection.js";
|
|
7
4
|
import { fontAxes, hasNamedInstance, namedInstanceVariations, namedInstances } from "./variation.js";
|
|
5
|
+
import { browserFaceDescriptors, fontFaceRenderFamily } from "./faces.js";
|
|
6
|
+
import { FamilyFaceCatalog } from "./catalog.js";
|
|
7
|
+
import { makeParagraphBuilder, resetFontCollection } from "./collection.js";
|
|
8
8
|
import { loadHarfBuzz } from "./harfbuzz.js";
|
|
9
9
|
import { SharedReads, sameFontFile, withHarfBuzz } from "./reads.js";
|
|
10
10
|
import { LocalFonts } from "./local.js";
|
|
11
11
|
import { UnsupportedFontFormatError } from "./sources.js";
|
|
12
12
|
import { collectGraphFontKeys } from "../requirements/collect.js";
|
|
13
13
|
import { WebFontResolver, normalizedCoverageText } from "../web-font/resolver.js";
|
|
14
|
-
import { faceLoadKey, namesWeight, normalizeFontFamily, parseFontStyle
|
|
14
|
+
import { faceLoadKey, namesWeight, normalizeFontFamily, parseFontStyle } from "@pairdraw/scene-graph/font-style";
|
|
15
15
|
import { uniq } from "es-toolkit";
|
|
16
16
|
//#region src/text/font/manager.ts
|
|
17
17
|
var FontManager = class {
|
|
@@ -623,7 +623,7 @@ var FontManager = class {
|
|
|
623
623
|
fontFamilies: [...families],
|
|
624
624
|
fontSize: 16
|
|
625
625
|
} });
|
|
626
|
-
const builder = canvasKit
|
|
626
|
+
const builder = makeParagraphBuilder(canvasKit, style, provider);
|
|
627
627
|
try {
|
|
628
628
|
builder.addText(characters.join(""));
|
|
629
629
|
const paragraph = builder.build();
|
|
@@ -745,12 +745,7 @@ var FontManager = class {
|
|
|
745
745
|
/** Makes a face available to the page's own text, such as fields over canvas labels. */
|
|
746
746
|
registerFontInBrowser(family, style, data) {
|
|
747
747
|
if (!IS_BROWSER) return;
|
|
748
|
-
const
|
|
749
|
-
const italic = style.toLowerCase().includes("italic") ? "italic" : "normal";
|
|
750
|
-
const face = new FontFace(family, data, {
|
|
751
|
-
weight: String(weight),
|
|
752
|
-
style: italic
|
|
753
|
-
});
|
|
748
|
+
const face = new FontFace(family, data, browserFaceDescriptors(style, data));
|
|
754
749
|
face.load().then(() => document.fonts.add(face)).catch(() => {
|
|
755
750
|
console.warn(`Failed to load font "${family}" (${style})`);
|
|
756
751
|
});
|