@pairdraw/core 0.17.0 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/canvas/outline-stroke.d.ts.map +1 -1
- package/dist/canvas/outline-stroke.js.map +1 -1
- package/dist/canvas/overlays/prototype.d.ts +2 -5
- package/dist/canvas/overlays/prototype.d.ts.map +1 -1
- package/dist/canvas/overlays/prototype.js +3 -19
- package/dist/canvas/overlays/prototype.js.map +1 -1
- package/dist/canvas/renderer/fonts.js +2 -1
- package/dist/canvas/renderer/fonts.js.map +1 -1
- package/dist/canvas/renderer.d.ts +4 -0
- package/dist/canvas/renderer.d.ts.map +1 -1
- package/dist/canvas/renderer.js +4 -0
- package/dist/canvas/renderer.js.map +1 -1
- package/dist/canvas/scene.d.ts.map +1 -1
- package/dist/canvas/scene.js +61 -37
- package/dist/canvas/scene.js.map +1 -1
- package/dist/canvas/text/font-families.d.ts +4 -3
- package/dist/canvas/text/font-families.d.ts.map +1 -1
- package/dist/canvas/text/font-families.js +8 -7
- package/dist/canvas/text/font-families.js.map +1 -1
- package/dist/canvas/text/index.d.ts.map +1 -1
- package/dist/canvas/text/index.js +1 -1
- package/dist/canvas/text/index.js.map +1 -1
- package/dist/canvas/text/paragraph/index.js +2 -2
- package/dist/canvas/text/paragraph/index.js.map +1 -1
- package/dist/canvas/text/placed.js +2 -2
- package/dist/canvas/text/placed.js.map +1 -1
- package/dist/canvas/text/renderer.d.ts +2 -0
- package/dist/canvas/text/renderer.d.ts.map +1 -1
- package/dist/design-jsx/renderer.d.ts.map +1 -1
- package/dist/editor/bridges/undo.d.ts +1 -1
- package/dist/editor/create.d.ts +1 -1
- package/dist/figma-api/index.d.ts +43 -8
- package/dist/figma-api/index.d.ts.map +1 -1
- package/dist/figma-api/index.js +48 -6
- package/dist/figma-api/index.js.map +1 -1
- package/dist/figma-api/serialization.js +10 -0
- package/dist/figma-api/serialization.js.map +1 -1
- package/dist/geometry/connectors.js +2 -14
- package/dist/geometry/connectors.js.map +1 -1
- package/dist/geometry/index.d.ts +2 -2
- package/dist/geometry/scene.d.ts +3 -1
- package/dist/geometry/scene.d.ts.map +1 -1
- package/dist/geometry/scene.js +16 -1
- package/dist/geometry/scene.js.map +1 -1
- package/dist/geometry/types.d.ts +7 -1
- package/dist/geometry/types.d.ts.map +1 -1
- package/dist/io/formats/drawio/export.d.ts.map +1 -1
- package/dist/io/formats/drawio/export.js +10 -0
- package/dist/io/formats/drawio/export.js.map +1 -1
- package/dist/io/formats/drawio/index.d.ts.map +1 -1
- package/dist/io/formats/drawio/index.js +32 -1
- package/dist/io/formats/drawio/index.js.map +1 -1
- package/dist/io/formats/drawio/shapes.d.ts +10 -5
- package/dist/io/formats/drawio/shapes.d.ts.map +1 -1
- package/dist/io/formats/drawio/shapes.js +10 -6
- package/dist/io/formats/drawio/shapes.js.map +1 -1
- package/dist/io/formats/drawio/warnings.d.ts +11 -0
- package/dist/io/formats/drawio/warnings.d.ts.map +1 -0
- package/dist/io/formats/drawio/warnings.js +23 -0
- package/dist/io/formats/drawio/warnings.js.map +1 -0
- package/dist/io/formats/pdf/dom.d.ts +10 -1
- package/dist/io/formats/pdf/dom.d.ts.map +1 -1
- package/dist/io/formats/pdf/dom.js +31 -1
- package/dist/io/formats/pdf/dom.js.map +1 -1
- package/dist/io/formats/pdf/export.js +3 -2
- package/dist/io/formats/pdf/export.js.map +1 -1
- package/dist/io/formats/raster/headless.d.ts +2 -2
- package/dist/io/formats/raster/headless.d.ts.map +1 -1
- package/dist/io/formats/raster/headless.js +2 -2
- package/dist/io/formats/raster/headless.js.map +1 -1
- package/dist/io/formats/raster/render.d.ts +1 -1
- package/dist/io/formats/raster/render.d.ts.map +1 -1
- package/dist/io/formats/raster/render.js +2 -2
- package/dist/io/formats/raster/render.js.map +1 -1
- package/dist/io/formats.d.ts.map +1 -1
- package/dist/io/formats.js +3 -1
- package/dist/io/formats.js.map +1 -1
- package/dist/io/registry.d.ts +2 -0
- package/dist/io/registry.d.ts.map +1 -1
- package/dist/io/registry.js +5 -0
- package/dist/io/registry.js.map +1 -1
- package/dist/io/types.d.ts +2 -0
- package/dist/io/types.d.ts.map +1 -1
- package/dist/library/snapshot.d.ts.map +1 -1
- package/dist/library/snapshot.js +1 -0
- package/dist/library/snapshot.js.map +1 -1
- package/dist/lint/linter.d.ts.map +1 -1
- package/dist/lint/linter.js +11 -2
- package/dist/lint/linter.js.map +1 -1
- package/dist/rpc/variables-command.d.ts.map +1 -1
- package/dist/rpc/variables-command.js +4 -2
- package/dist/rpc/variables-command.js.map +1 -1
- package/dist/text/coverage.d.ts.map +1 -1
- package/dist/text/coverage.js +6 -3
- package/dist/text/coverage.js.map +1 -1
- package/dist/text/fallbacks.d.ts +8 -2
- package/dist/text/fallbacks.d.ts.map +1 -1
- package/dist/text/fallbacks.js +73 -7
- package/dist/text/fallbacks.js.map +1 -1
- package/dist/text/font/manager.d.ts +14 -6
- package/dist/text/font/manager.d.ts.map +1 -1
- package/dist/text/font/manager.js +40 -14
- 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/text/outlines.js +1 -1
- package/dist/text/outlines.js.map +1 -1
- package/dist/text/path/caret.d.ts +2 -4
- package/dist/text/path/caret.d.ts.map +1 -1
- package/dist/text/path/caret.js.map +1 -1
- package/dist/text/prepare.d.ts +5 -1
- package/dist/text/prepare.d.ts.map +1 -1
- package/dist/text/prepare.js +19 -2
- package/dist/text/prepare.js.map +1 -1
- package/dist/text/web-font/assets.d.ts.map +1 -1
- package/dist/text/web-font/assets.js +13 -21
- package/dist/text/web-font/assets.js.map +1 -1
- package/dist/text/web-font/resolver.d.ts.map +1 -1
- package/dist/text/web-font/resolver.js +4 -1
- package/dist/text/web-font/resolver.js.map +1 -1
- package/dist/tools/create/components.d.ts +2 -1
- package/dist/tools/create/components.d.ts.map +1 -1
- package/dist/tools/create/components.js +27 -1
- package/dist/tools/create/components.js.map +1 -1
- package/dist/tools/create/icons.d.ts.map +1 -1
- package/dist/tools/create/icons.js +11 -5
- package/dist/tools/create/icons.js.map +1 -1
- package/dist/tools/create/vector.d.ts.map +1 -1
- package/dist/tools/create/vector.js +9 -6
- package/dist/tools/create/vector.js.map +1 -1
- package/dist/tools/create.d.ts +2 -2
- package/dist/tools/create.js +2 -2
- package/dist/tools/modify/effects.d.ts.map +1 -1
- package/dist/tools/modify/effects.js +5 -3
- package/dist/tools/modify/effects.js.map +1 -1
- package/dist/tools/modify/matching.d.ts.map +1 -1
- package/dist/tools/modify/matching.js +9 -6
- package/dist/tools/modify/matching.js.map +1 -1
- package/dist/tools/modify/paint.d.ts.map +1 -1
- package/dist/tools/modify/paint.js +7 -5
- package/dist/tools/modify/paint.js.map +1 -1
- package/dist/tools/modify/text.d.ts.map +1 -1
- package/dist/tools/modify/text.js +28 -8
- package/dist/tools/modify/text.js.map +1 -1
- package/dist/tools/read/libraries.d.ts.map +1 -1
- package/dist/tools/read/libraries.js +16 -4
- package/dist/tools/read/libraries.js.map +1 -1
- package/dist/tools/read/pages.d.ts.map +1 -1
- package/dist/tools/read/pages.js.map +1 -1
- package/dist/tools/registry/extended.d.ts.map +1 -1
- package/dist/tools/registry/extended.js +2 -1
- package/dist/tools/registry/extended.js.map +1 -1
- package/dist/tools/schema.d.ts +24 -5
- package/dist/tools/schema.d.ts.map +1 -1
- package/dist/tools/schema.js +80 -6
- package/dist/tools/schema.js.map +1 -1
- package/dist/tools/structure/batch.d.ts.map +1 -1
- package/dist/tools/structure/batch.js +4 -3
- package/dist/tools/structure/batch.js.map +1 -1
- package/dist/tools/variables/values.d.ts.map +1 -1
- package/dist/tools/variables/values.js +4 -2
- package/dist/tools/variables/values.js.map +1 -1
- package/dist/tools/vector/export.d.ts.map +1 -1
- package/dist/tools/vector/export.js +5 -2
- package/dist/tools/vector/export.js.map +1 -1
- package/package.json +9 -9
- package/src/canvas/outline-stroke.ts +2 -6
- package/src/canvas/overlays/prototype.ts +5 -27
- package/src/canvas/renderer/fonts.ts +2 -1
- package/src/canvas/renderer.ts +4 -0
- package/src/canvas/scene.ts +92 -63
- package/src/canvas/text/font-families.ts +8 -7
- package/src/canvas/text/index.ts +3 -1
- package/src/canvas/text/paragraph/index.ts +2 -2
- package/src/canvas/text/placed.ts +2 -2
- package/src/canvas/text/renderer.ts +2 -0
- package/src/figma-api/index.ts +90 -9
- package/src/figma-api/serialization.ts +14 -0
- package/src/geometry/connectors.ts +2 -12
- package/src/geometry/index.ts +1 -1
- package/src/geometry/scene.ts +19 -0
- package/src/geometry/types.ts +8 -0
- package/src/io/formats/drawio/export.ts +11 -0
- package/src/io/formats/drawio/index.ts +36 -1
- package/src/io/formats/drawio/shapes.ts +13 -9
- package/src/io/formats/drawio/warnings.ts +27 -0
- package/src/io/formats/pdf/dom.ts +29 -0
- package/src/io/formats/pdf/export.ts +3 -2
- package/src/io/formats/raster/headless.ts +10 -3
- package/src/io/formats/raster/render.ts +4 -2
- package/src/io/formats.ts +3 -1
- package/src/io/registry.ts +12 -0
- package/src/io/types.ts +2 -0
- package/src/library/snapshot.ts +1 -0
- package/src/lint/linter.ts +12 -2
- package/src/rpc/variables-command.ts +6 -5
- package/src/text/coverage.ts +17 -2
- package/src/text/fallbacks.ts +81 -7
- package/src/text/font/manager.ts +50 -17
- package/src/text/outlines.ts +1 -1
- package/src/text/path/caret.ts +3 -4
- package/src/text/prepare.ts +34 -2
- package/src/text/web-font/assets.ts +11 -15
- package/src/text/web-font/resolver.ts +7 -1
- package/src/tools/create/components.ts +33 -0
- package/src/tools/create/icons.ts +13 -5
- package/src/tools/create/vector.ts +16 -6
- package/src/tools/create.ts +1 -0
- package/src/tools/modify/effects.ts +11 -4
- package/src/tools/modify/matching.ts +10 -6
- package/src/tools/modify/paint.ts +13 -5
- package/src/tools/modify/text.ts +53 -20
- package/src/tools/read/libraries.ts +21 -4
- package/src/tools/read/pages.ts +1 -0
- package/src/tools/registry/extended.ts +2 -0
- package/src/tools/schema.ts +109 -5
- package/src/tools/structure/batch.ts +10 -5
- package/src/tools/variables/values.ts +6 -3
- package/src/tools/vector/export.ts +5 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"effects.js","names":[],"sources":["../../../src/tools/modify/effects.ts"],"sourcesContent":["import * as v from 'valibot'\n\nimport {
|
|
1
|
+
{"version":3,"file":"effects.js","names":[],"sources":["../../../src/tools/modify/effects.ts"],"sourcesContent":["import * as v from 'valibot'\n\nimport { DEFAULT_SHADOW_COLOR } from '#core/constants'\nimport type { FigmaEffect } from '#core/figma-api/effects'\nimport {\n defineTool,\n nodeIdInputSchema,\n nodeNotFound,\n parseOptionalToolColor,\n toolNumber\n} from '#core/tools/schema'\n\nexport const setEffects = defineTool({\n name: 'set_effects',\n\n description:\n 'Set effects on a node (drop shadow, inner shadow, blur). Pass an array or a single effect.',\n execution: { kind: 'sync', mutation: 'properties' },\n input: v.strictObject({\n id: nodeIdInputSchema,\n type: v.pipe(\n v.picklist(['DROP_SHADOW', 'INNER_SHADOW', 'FOREGROUND_BLUR', 'BACKGROUND_BLUR']),\n v.description('Effect type')\n ),\n color: v.optional(v.pipe(v.string(), v.description('Shadow color (hex). Ignored for blur.'))),\n offset_x: v.optional(toolNumber(v.pipe(v.number(), v.description('Shadow X offset'))), 0),\n offset_y: v.optional(toolNumber(v.pipe(v.number(), v.description('Shadow Y offset'))), 4),\n radius: v.optional(\n toolNumber(v.pipe(v.number(), v.minValue(0), v.description('Blur radius'))),\n 4\n ),\n spread: v.optional(toolNumber(v.pipe(v.number(), v.description('Shadow spread'))), 0)\n }),\n execute: (figma, args) => {\n const node = figma.getNodeById(args.id)\n if (!node) return nodeNotFound(args.id)\n const parsedColor = parseOptionalToolColor(args.color)\n if ('error' in parsedColor) return parsedColor\n const shadowColor = parsedColor.color ?? { ...DEFAULT_SHADOW_COLOR }\n\n const effect: FigmaEffect =\n args.type === 'FOREGROUND_BLUR' || args.type === 'BACKGROUND_BLUR'\n ? {\n type: args.type === 'BACKGROUND_BLUR' ? 'BACKGROUND_BLUR' : 'LAYER_BLUR',\n radius: args.radius,\n visible: true,\n blurType: 'NORMAL'\n }\n : {\n type: args.type,\n color: shadowColor,\n offset: { x: args.offset_x, y: args.offset_y },\n radius: args.radius,\n spread: args.spread,\n visible: true,\n blendMode: 'NORMAL'\n }\n\n node.effects = [...node.effects, effect]\n return { id: args.id, effects: node.effects.length }\n }\n})\n"],"mappings":";;;;AAYA,MAAa,aAAa,WAAW;CACnC,MAAM;CAEN,aACE;CACF,WAAW;EAAE,MAAM;EAAQ,UAAU;CAAa;CAClD,OAAO,EAAE,aAAa;EACpB,IAAI;EACJ,MAAM,EAAE,KACN,EAAE,SAAS;GAAC;GAAe;GAAgB;GAAmB;EAAiB,CAAC,GAChF,EAAE,YAAY,aAAa,CAC7B;EACA,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,YAAY,uCAAuC,CAAC,CAAC;EAC5F,UAAU,EAAE,SAAS,WAAW,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,YAAY,iBAAiB,CAAC,CAAC,GAAG,CAAC;EACxF,UAAU,EAAE,SAAS,WAAW,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,YAAY,iBAAiB,CAAC,CAAC,GAAG,CAAC;EACxF,QAAQ,EAAE,SACR,WAAW,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,SAAS,CAAC,GAAG,EAAE,YAAY,aAAa,CAAC,CAAC,GAC1E,CACF;EACA,QAAQ,EAAE,SAAS,WAAW,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,YAAY,eAAe,CAAC,CAAC,GAAG,CAAC;CACtF,CAAC;CACD,UAAU,OAAO,SAAS;EACxB,MAAM,OAAO,MAAM,YAAY,KAAK,EAAE;EACtC,IAAI,CAAC,MAAM,OAAO,aAAa,KAAK,EAAE;EACtC,MAAM,cAAc,uBAAuB,KAAK,KAAK;EACrD,IAAI,WAAW,aAAa,OAAO;EACnC,MAAM,cAAc,YAAY,SAAS,EAAE,GAAG,qBAAqB;EAEnE,MAAM,SACJ,KAAK,SAAS,qBAAqB,KAAK,SAAS,oBAC7C;GACE,MAAM,KAAK,SAAS,oBAAoB,oBAAoB;GAC5D,QAAQ,KAAK;GACb,SAAS;GACT,UAAU;EACZ,IACA;GACE,MAAM,KAAK;GACX,OAAO;GACP,QAAQ;IAAE,GAAG,KAAK;IAAU,GAAG,KAAK;GAAS;GAC7C,QAAQ,KAAK;GACb,QAAQ,KAAK;GACb,SAAS;GACT,WAAW;EACb;EAEN,KAAK,UAAU,CAAC,GAAG,KAAK,SAAS,MAAM;EACvC,OAAO;GAAE,IAAI,KAAK;GAAI,SAAS,KAAK,QAAQ;EAAO;CACrD;AACF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"matching.d.ts","names":[],"sources":["../../../src/tools/modify/matching.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"matching.d.ts","names":[],"sources":["../../../src/tools/modify/matching.ts"],"mappings":";;cAoNa,2BAAA"}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { collectNodeFontFaces } from "../../text/requirements/collect.js";
|
|
2
2
|
import { getNodeEditCapability } from "../../editor/capabilities.js";
|
|
3
3
|
import { getStyledTextSegments } from "../../figma-api/text/style.js";
|
|
4
|
-
import { defineTool, toolFailure, toolNumber } from "../schema.js";
|
|
4
|
+
import { defineTool, parseToolColor, toolFailure, toolNumber } from "../schema.js";
|
|
5
5
|
import { editNode } from "../../figma-api/accessor-utils.js";
|
|
6
6
|
import { textFonts } from "./text.js";
|
|
7
7
|
import { copyStroke } from "@pairdraw/scene-graph/copy";
|
|
8
8
|
import { mapValues, pick } from "es-toolkit";
|
|
9
|
-
import { tryParseColor } from "@pairdraw/scene-graph/color";
|
|
10
9
|
import * as v from "valibot";
|
|
11
10
|
//#region src/tools/modify/matching.ts
|
|
12
11
|
const colorPairSchema = v.strictObject({
|
|
@@ -59,12 +58,16 @@ function channels(color) {
|
|
|
59
58
|
color.b
|
|
60
59
|
].map((value) => Math.round(value * 255)).join(",");
|
|
61
60
|
}
|
|
61
|
+
/** A colour as a tool reads it, so a bad value fails with the message other tools give. */
|
|
62
|
+
function colorOf(value) {
|
|
63
|
+
const parsed = parseToolColor(value);
|
|
64
|
+
if ("error" in parsed) throw new Error(parsed.error);
|
|
65
|
+
return parsed;
|
|
66
|
+
}
|
|
62
67
|
/** The swap a colour pair makes; throws on a value that is not a colour. */
|
|
63
68
|
function colorSwap(pair) {
|
|
64
|
-
const from =
|
|
65
|
-
const to =
|
|
66
|
-
if (!from) throw new Error(`"${pair.from}" is not a color`);
|
|
67
|
-
if (!to) throw new Error(`"${pair.to}" is not a color`);
|
|
69
|
+
const from = colorOf(pair.from);
|
|
70
|
+
const to = colorOf(pair.to);
|
|
68
71
|
const key = channels(from);
|
|
69
72
|
const percent = (value) => Math.round(value * 100);
|
|
70
73
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"matching.js","names":[],"sources":["../../../src/tools/modify/matching.ts"],"sourcesContent":["import { mapValues, pick } from 'es-toolkit'\nimport * as v from 'valibot'\n\nimport type { Fill, NodeType, SceneNode } from '@pairdraw/scene-graph'\nimport { tryParseColor } from '@pairdraw/scene-graph/color'\nimport { copyStroke } from '@pairdraw/scene-graph/copy'\nimport type { Color } from '@pairdraw/scene-graph/primitives'\n\nimport { getNodeEditCapability } from '#core/editor/capabilities'\nimport type { FigmaAPI } from '#core/figma-api'\nimport { editNode } from '#core/figma-api/accessor-utils'\nimport type { FigmaFontNameInput } from '#core/figma-api/fonts/names'\nimport { getStyledTextSegments } from '#core/figma-api/text/style'\nimport { collectNodeFontFaces } from '#core/text/requirements/collect'\nimport { defineTool, toolFailure, toolNumber } from '#core/tools/schema'\n\nimport { textFonts } from './text'\n\nconst colorPairSchema = v.strictObject({\n from: v.pipe(v.string(), v.description('Color to find (hex); with alpha, only that opacity')),\n to: v.pipe(v.string(), v.description('Color to use instead (hex); with alpha, its opacity'))\n})\nconst numberPairSchema = v.strictObject({\n from: toolNumber(v.pipe(v.number(), v.description('Value to find'))),\n to: toolNumber(v.pipe(v.number(), v.minValue(0), v.description('Value to use instead')))\n})\nconst textPairSchema = v.strictObject({\n from: v.pipe(v.string(), v.nonEmpty(), v.description('Value to find (case-insensitive)')),\n to: v.pipe(v.string(), v.nonEmpty(), v.description('Value to use instead'))\n})\nconst fontNameSchema = v.object({ family: v.string(), style: v.string() })\n\nconst pairs = <S extends v.GenericSchema>(schema: S, description: string) =>\n v.optional(v.pipe(v.array(schema), v.minLength(1), v.description(description)))\n\nconst replacementInputs = {\n fill_color: pairs(colorPairSchema, 'Solid fill colors of layers other than text'),\n text_color: pairs(colorPairSchema, 'Text colors, including colored ranges'),\n stroke_color: pairs(colorPairSchema, 'Solid stroke colors'),\n stroke_weight: pairs(numberPairSchema, 'Stroke weights of layers that have strokes'),\n corner_radius: pairs(\n numberPairSchema,\n 'Corner radii: a layer’s radius, or each corner on its own where they are set separately'\n ),\n font_family: pairs(textPairSchema, 'Font families, keeping each range’s style'),\n font_style: pairs(textPairSchema, 'Font styles such as Regular or Bold, keeping the family'),\n font_size: pairs(numberPairSchema, 'Font sizes')\n}\n\ntype ReplacementKind = keyof typeof replacementInputs\ntype ColorPair = v.InferOutput<typeof colorPairSchema>\ntype NumberPair = v.InferOutput<typeof numberPairSchema>\ntype TextPair = v.InferOutput<typeof textPairSchema>\ntype Replacements = {\n fill_color?: ColorPair[]\n text_color?: ColorPair[]\n stroke_color?: ColorPair[]\n stroke_weight?: NumberPair[]\n corner_radius?: NumberPair[]\n font_family?: TextPair[]\n font_style?: TextPair[]\n font_size?: NumberPair[]\n}\ntype Scope = { ids?: string[]; within: 'page' | 'selection' }\n\nconst CORNERS = [\n 'topLeftRadius',\n 'topRightRadius',\n 'bottomRightRadius',\n 'bottomLeftRadius'\n] as const\n/** Layers that draw their corners rounded; a radius on any other is not shown. */\nconst ROUNDED_TYPES: ReadonlySet<NodeType> = new Set([\n 'FRAME',\n 'RECTANGLE',\n 'ROUNDED_RECTANGLE',\n 'COMPONENT',\n 'COMPONENT_SET',\n 'INSTANCE'\n])\n\n/** Channels as 0–255 integers, so a hex written by hand matches the colour it names. */\nfunction channels(color: Color): string {\n return [color.r, color.g, color.b].map((value) => Math.round(value * 255)).join(',')\n}\n\ninterface ColorSwap {\n match: (paint: Fill) => boolean\n apply: (paint: Fill) => Fill\n}\n\n/** The swap a colour pair makes; throws on a value that is not a colour. */\nfunction colorSwap(pair: ColorPair): ColorSwap {\n const from = tryParseColor(pair.from)\n const to = tryParseColor(pair.to)\n if (!from) throw new Error(`\"${pair.from}\" is not a color`)\n if (!to) throw new Error(`\"${pair.to}\" is not a color`)\n const key = channels(from)\n const percent = (value: number) => Math.round(value * 100)\n return {\n match: (paint) =>\n paint.type === 'SOLID' &&\n channels(paint.color) === key &&\n (from.a >= 1 || percent(paint.opacity * paint.color.a) === percent(from.a)),\n // A colour's alpha is the paint's opacity, as Figma takes it; without one the paint keeps its own.\n apply: (paint) => ({\n ...paint,\n color: { ...to, a: 1 },\n opacity: to.a < 1 ? to.a : paint.opacity * paint.color.a\n })\n }\n}\n\n/** `paints` with every listed colour swapped, or null when none matched. */\nfunction swapPaints<T extends Fill>(\n paints: readonly T[],\n swaps: readonly ColorSwap[],\n bound: (index: number) => boolean\n): T[] | null {\n const next = paints.map((paint, index) => {\n if (bound(index)) return paint\n const swap = swaps.find((candidate) => candidate.match(paint))\n return swap ? { ...paint, ...swap.apply(paint) } : paint\n })\n return next.some((paint, index) => paint !== paints[index]) ? next : null\n}\n\nfunction swapNumber(value: number, swaps: readonly NumberPair[] | undefined): number | null {\n const swap = swaps?.find((pair) => pair.from === value)\n return swap && swap.to !== value ? swap.to : null\n}\n\nfunction swapText(value: string, swaps: readonly TextPair[] | undefined): string | null {\n const swap = swaps?.find((pair) => pair.from.toLowerCase() === value.toLowerCase())\n return swap && swap.to !== value ? swap.to : null\n}\n\n/** The layers a call searches: the given ones, the selection, or the page's, with descendants. */\nfunction searchedNodes(figma: FigmaAPI, scope: Scope): SceneNode[] | string {\n const graph = figma.graph\n let roots: string[]\n if (scope.ids && scope.ids.length > 0) {\n const missing = scope.ids.find((id) => !graph.getNode(id))\n if (missing) return `Node \"${missing}\" not found`\n roots = scope.ids\n } else if (scope.within === 'selection') {\n roots = figma.currentPage.selection.map((node) => node.id)\n if (roots.length === 0) return 'Nothing is selected'\n } else {\n roots = graph.getNode(figma.currentPage.id)?.childIds ?? []\n }\n const nodes: SceneNode[] = []\n const seen = new Set<string>()\n const walk = (id: string) => {\n const node = seen.has(id) ? undefined : graph.getNode(id)\n if (!node) return\n seen.add(id)\n nodes.push(node)\n for (const childId of node.childIds) walk(childId)\n }\n for (const id of roots) walk(id)\n return nodes\n}\n\ninterface TextEdit {\n start: number\n end: number\n fontName?: { family: string; style: string }\n fontSize?: number\n family: boolean\n style: boolean\n}\n\n/** The font and size changes one text layer takes, range by range. */\nfunction textEdits(node: SceneNode, replacements: Replacements): TextEdit[] {\n if (node.type !== 'TEXT') return []\n const { font_family, font_style, font_size } = replacements\n if (!font_family && !font_style && !font_size) return []\n const edit = (start: number, end: number, font: unknown, size: unknown): TextEdit | null => {\n if (!v.is(fontNameSchema, font)) return null\n const family = swapText(font.family, font_family)\n const style = swapText(font.style, font_style)\n const fontSize = typeof size === 'number' ? swapNumber(size, font_size) : null\n if (!family && !style && fontSize === null) return null\n const textEdit: TextEdit = { start, end, family: family !== null, style: style !== null }\n if (family || style) {\n textEdit.fontName = { family: family ?? font.family, style: style ?? font.style }\n }\n if (fontSize !== null) textEdit.fontSize = fontSize\n return textEdit\n }\n if (node.text.length === 0) {\n // An empty text has no ranges to read; its own face is the first one it lists.\n const face = collectNodeFontFaces(node).at(0)\n const whole = face && edit(0, 0, face, node.fontSize)\n return whole ? [whole] : []\n }\n return getStyledTextSegments(node, ['fontName', 'fontSize']).flatMap(\n (segment) => edit(segment.start, segment.end, segment.fontName, segment.fontSize) ?? []\n )\n}\n\nconst REPLACEMENT_KINDS = Object.keys(replacementInputs) as ReplacementKind[]\n\nfunction colorSwaps(pairs: readonly ColorPair[] | undefined): ColorSwap[] {\n return (pairs ?? []).map(colorSwap)\n}\n\nexport const replaceMatchingProperties = defineTool({\n name: 'replace_matching_properties',\n description:\n 'Swap one value for another on every layer that uses it, across the current page (default), the selection, or the given layers with their descendants: fill, text, and stroke colors, stroke weights, corner radii, font families, font styles, and font sizes. Each property takes a list of { from, to } pairs; colors match by hex, ignoring opacity unless from has alpha, and colors bound to variables are left alone. Layers of read-only library components are not changed and come back in read_only. Returns how many values changed per property and the layers changed.',\n execution: { kind: 'sync', mutation: 'document' },\n input: v.strictObject({\n ids: v.optional(\n v.pipe(\n v.array(v.string()),\n v.description('Layers to search with their descendants (default: see within)')\n )\n ),\n within: v.optional(\n v.pipe(\n v.picklist(['page', 'selection']),\n v.description('Without ids: the current page (default) or the selection')\n ),\n 'page'\n ),\n ...replacementInputs\n }),\n fonts: (figma, args) => {\n const nodes = searchedNodes(figma, args)\n if (typeof nodes === 'string') return []\n const fonts: FigmaFontNameInput[] = []\n for (const node of nodes) {\n const edits = textEdits(node, args)\n if (edits.length === 0) continue\n fonts.push(...textFonts(figma, node.id))\n for (const edit of edits) if (edit.fontName) fonts.push(edit.fontName)\n }\n return fonts\n },\n execute: (figma, args) => {\n const kinds = REPLACEMENT_KINDS.filter((kind) => args[kind] !== undefined)\n if (kinds.length === 0) return { error: 'Name at least one property to replace' }\n const nodes = searchedNodes(figma, args)\n if (typeof nodes === 'string') return { error: nodes }\n const counts = mapValues(replacementInputs, () => 0)\n const changed = new Set<string>()\n const readOnly: string[] = []\n try {\n const swaps = {\n fill: colorSwaps(args.fill_color),\n text: colorSwaps(args.text_color),\n stroke: colorSwaps(args.stroke_color)\n }\n for (const node of nodes) {\n const paint = paintAndGeometryChanges(node, args, swaps)\n const edits = textEdits(node, args)\n if (!paint && edits.length === 0) continue\n // A library's components change in that library, so their layers are reported instead.\n if (!getNodeEditCapability(figma.graph, node.id).editable) {\n readOnly.push(node.id)\n continue\n }\n if (paint) {\n // As the node setters write, so styles detach and instances keep the change.\n editNode(figma.graph, node.id, paint.changes, figma.groupFitOptions)\n for (const kind of REPLACEMENT_KINDS) counts[kind] += paint.counts[kind] ?? 0\n }\n if (edits.length > 0) changeText(figma, node, edits, counts)\n changed.add(node.id)\n }\n } catch (error) {\n return toolFailure(error)\n }\n return {\n replaced: pick(counts, kinds),\n changed: changed.size,\n ids: [...changed],\n ...(readOnly.length > 0 ? { read_only: readOnly } : {})\n }\n }\n})\n\ntype Counts = Record<ReplacementKind, number>\n\n/** What one layer's paints, stroke weight, and corners change to, with the values it changes. */\nfunction paintAndGeometryChanges(\n node: SceneNode,\n replacements: Replacements,\n swaps: { fill: ColorSwap[]; text: ColorSwap[]; stroke: ColorSwap[] }\n): { changes: Partial<SceneNode>; counts: Partial<Counts> } | null {\n const changes: Partial<SceneNode> = {}\n const counts: Partial<Counts> = {}\n const count = (kind: ReplacementKind, amount = 1) => {\n counts[kind] = (counts[kind] ?? 0) + amount\n }\n const isText = node.type === 'TEXT'\n const fillSwaps = isText ? swaps.text : swaps.fill\n const fillKind = isText ? 'text_color' : 'fill_color'\n const bound = (kind: 'fills' | 'strokes') => (index: number) =>\n !!node.boundVariables[`${kind}/${index}/color`]\n if (fillSwaps.length > 0) {\n const fills = swapPaints(node.fills, fillSwaps, bound('fills'))\n if (fills) {\n changes.fills = fills\n count(fillKind, fills.filter((paint, index) => paint !== node.fills[index]).length)\n }\n if (isText) {\n let runs = 0\n const styleRuns = node.styleRuns.map((run) => {\n const runFills = run.style.fills && swapPaints(run.style.fills, fillSwaps, () => false)\n if (!runFills) return run\n runs++\n return { ...run, style: { ...run.style, fills: runFills } }\n })\n if (runs > 0) {\n changes.styleRuns = styleRuns\n count('text_color', runs)\n }\n }\n }\n if (swaps.stroke.length > 0) {\n const strokes = swapPaints(node.strokes, swaps.stroke, bound('strokes'))\n if (strokes) {\n changes.strokes = strokes\n count('stroke_color', strokes.filter((paint, index) => paint !== node.strokes[index]).length)\n }\n }\n const weight =\n node.strokes.length > 0 ? swapNumber(node.strokeWeight, replacements.stroke_weight) : null\n if (weight !== null) {\n // Each stroke draws at its own weight, so they all take it, as the strokeWeight setter does.\n changes.strokeWeight = weight\n changes.strokes = (changes.strokes ?? node.strokes).map((stroke) => ({\n ...copyStroke(stroke),\n weight\n }))\n count('stroke_weight')\n }\n if (replacements.corner_radius && ROUNDED_TYPES.has(node.type)) {\n Object.assign(changes, cornerChanges(node, replacements.corner_radius, count))\n }\n return Object.keys(changes).length > 0 ? { changes, counts } : null\n}\n\n/**\n * The corner radii `swaps` give a layer. Corners set together draw `cornerRadius`, whatever the\n * corner fields hold, so only corners set one by one are matched each on its own.\n */\nfunction cornerChanges(\n node: SceneNode,\n swaps: readonly NumberPair[],\n count: (kind: ReplacementKind) => void\n): Partial<SceneNode> {\n if (!node.independentCorners) {\n const radius = swapNumber(node.cornerRadius, swaps)\n if (radius === null) return {}\n count('corner_radius')\n return {\n cornerRadius: radius,\n ...Object.fromEntries(CORNERS.map((corner) => [corner, radius])),\n independentCorners: false\n }\n }\n const changes: Partial<SceneNode> = {}\n for (const corner of CORNERS) {\n const radius = swapNumber(node[corner], swaps)\n if (radius === null) continue\n changes[corner] = radius\n count('corner_radius')\n }\n const radii = CORNERS.map((corner) => changes[corner] ?? node[corner])\n if (radii.every((radius, index) => radius === node[CORNERS[index]])) return {}\n const uniform = radii.every((radius) => radius === radii[0])\n changes.cornerRadius = uniform ? radii[0] : node.cornerRadius\n changes.independentCorners = !uniform\n return changes\n}\n\nfunction changeText(\n figma: FigmaAPI,\n node: SceneNode,\n edits: readonly TextEdit[],\n counts: Record<ReplacementKind, number>\n): void {\n const text = figma.getNodeById(node.id)\n if (!text) return\n for (const edit of edits) {\n // A range over all of the text sets the text's own font, as set_font does, not a style run.\n const ranged = edit.start > 0 || edit.end < node.text.length\n if (edit.fontName) {\n if (ranged) text.setRangeFontName(edit.start, edit.end, edit.fontName)\n else text.fontName = edit.fontName\n }\n // The size comes after the font, so it lays the text out in the font it ends up in.\n if (edit.fontSize !== undefined) {\n if (ranged) text.setRangeFontSize(edit.start, edit.end, edit.fontSize)\n else text.fontSize = edit.fontSize\n counts.font_size++\n }\n if (edit.family) counts.font_family++\n if (edit.style) counts.font_style++\n }\n}\n"],"mappings":";;;;;;;;;;;AAkBA,MAAM,kBAAkB,EAAE,aAAa;CACrC,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,YAAY,oDAAoD,CAAC;CAC5F,IAAI,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,YAAY,qDAAqD,CAAC;AAC7F,CAAC;AACD,MAAM,mBAAmB,EAAE,aAAa;CACtC,MAAM,WAAW,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,YAAY,eAAe,CAAC,CAAC;CACnE,IAAI,WAAW,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,SAAS,CAAC,GAAG,EAAE,YAAY,sBAAsB,CAAC,CAAC;AACzF,CAAC;AACD,MAAM,iBAAiB,EAAE,aAAa;CACpC,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,SAAS,GAAG,EAAE,YAAY,kCAAkC,CAAC;CACxF,IAAI,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,SAAS,GAAG,EAAE,YAAY,sBAAsB,CAAC;AAC5E,CAAC;AACD,MAAM,iBAAiB,EAAE,OAAO;CAAE,QAAQ,EAAE,OAAO;CAAG,OAAO,EAAE,OAAO;AAAE,CAAC;AAEzE,MAAM,SAAoC,QAAW,gBACnD,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,MAAM,GAAG,EAAE,UAAU,CAAC,GAAG,EAAE,YAAY,WAAW,CAAC,CAAC;AAEhF,MAAM,oBAAoB;CACxB,YAAY,MAAM,iBAAiB,6CAA6C;CAChF,YAAY,MAAM,iBAAiB,uCAAuC;CAC1E,cAAc,MAAM,iBAAiB,qBAAqB;CAC1D,eA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C7B,MAAM,eAAe;EACrB,OAAO;GACL,cAAc;GACd,GAAG,OAAO,YAAY,QAAQ,KAAK,WAAW,CAAC,QAAQ,MAAM,CAAC,CAAC;GAC/D,oBAAoB;EACtB;CACF;CACA,MAAM,UAA8B,CAAC;CACrC,KAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,SAAS,WAAW,KAAK,SAAS,KAAK;EAC7C,IAAI,WAAW,MAAM;EACrB,QAAQ,UAAU;EAClB,MAAM,eAAe;CACvB;CACA,MAAM,QAAQ,QAAQ,KAAK,WAAW,QAAQ,WAAW,KAAK,OAAO;CACrE,IAAI,MAAM,OAAO,QAAQ,UAAU,WAAW,KAAK,QAAQ,OAAO,GAAG,OAAO,CAAC;CAC7E,MAAM,UAAU,MAAM,OAAO,WAAW,WAAW,MAAM,EAAE;CAC3D,QAAQ,eAAe,UAAU,MAAM,KAAK,KAAK;CACjD,QAAQ,qBAAqB,CAAC;CAC9B,OAAO;AACT;AAEA,SAAS,WACP,OACA,MACA,OACA,QACM;CACN,MAAM,OAAO,MAAM,YAAY,KAAK,EAAE;CACtC,IAAI,CAAC,MAAM;CACX,KAAK,MAAM,QAAQ,OAAO;EAExB,MAAM,SAAS,KAAK,QAAQ,KAAK,KAAK,MAAM,KAAK,KAAK;EACtD,IAAI,KAAK,UAAU;GACjB,IAAI,QAAQ,KAAK,iBAAiB,KAAK,OAAO,KAAK,KAAK,KAAK,QAAQ;QAChE,KAAK,WAAW,KAAK;EAC5B;EAEA,IAAI,KAAK,aAAa,KAAA,GAAW;GAC/B,IAAI,QAAQ,KAAK,iBAAiB,KAAK,OAAO,KAAK,KAAK,KAAK,QAAQ;QAChE,KAAK,WAAW,KAAK;GAC1B,OAAO;EACT;EACA,IAAI,KAAK,QAAQ,OAAO;EACxB,IAAI,KAAK,OAAO,OAAO;CACzB;AACF"}
|
|
1
|
+
{"version":3,"file":"matching.js","names":[],"sources":["../../../src/tools/modify/matching.ts"],"sourcesContent":["import { mapValues, pick } from 'es-toolkit'\nimport * as v from 'valibot'\n\nimport type { Fill, NodeType, SceneNode } from '@pairdraw/scene-graph'\nimport { copyStroke } from '@pairdraw/scene-graph/copy'\nimport type { Color } from '@pairdraw/scene-graph/primitives'\n\nimport { getNodeEditCapability } from '#core/editor/capabilities'\nimport type { FigmaAPI } from '#core/figma-api'\nimport { editNode } from '#core/figma-api/accessor-utils'\nimport type { FigmaFontNameInput } from '#core/figma-api/fonts/names'\nimport { getStyledTextSegments } from '#core/figma-api/text/style'\nimport { collectNodeFontFaces } from '#core/text/requirements/collect'\nimport { defineTool, parseToolColor, toolFailure, toolNumber } from '#core/tools/schema'\n\nimport { textFonts } from './text'\n\nconst colorPairSchema = v.strictObject({\n from: v.pipe(v.string(), v.description('Color to find (hex); with alpha, only that opacity')),\n to: v.pipe(v.string(), v.description('Color to use instead (hex); with alpha, its opacity'))\n})\nconst numberPairSchema = v.strictObject({\n from: toolNumber(v.pipe(v.number(), v.description('Value to find'))),\n to: toolNumber(v.pipe(v.number(), v.minValue(0), v.description('Value to use instead')))\n})\nconst textPairSchema = v.strictObject({\n from: v.pipe(v.string(), v.nonEmpty(), v.description('Value to find (case-insensitive)')),\n to: v.pipe(v.string(), v.nonEmpty(), v.description('Value to use instead'))\n})\nconst fontNameSchema = v.object({ family: v.string(), style: v.string() })\n\nconst pairs = <S extends v.GenericSchema>(schema: S, description: string) =>\n v.optional(v.pipe(v.array(schema), v.minLength(1), v.description(description)))\n\nconst replacementInputs = {\n fill_color: pairs(colorPairSchema, 'Solid fill colors of layers other than text'),\n text_color: pairs(colorPairSchema, 'Text colors, including colored ranges'),\n stroke_color: pairs(colorPairSchema, 'Solid stroke colors'),\n stroke_weight: pairs(numberPairSchema, 'Stroke weights of layers that have strokes'),\n corner_radius: pairs(\n numberPairSchema,\n 'Corner radii: a layer’s radius, or each corner on its own where they are set separately'\n ),\n font_family: pairs(textPairSchema, 'Font families, keeping each range’s style'),\n font_style: pairs(textPairSchema, 'Font styles such as Regular or Bold, keeping the family'),\n font_size: pairs(numberPairSchema, 'Font sizes')\n}\n\ntype ReplacementKind = keyof typeof replacementInputs\ntype ColorPair = v.InferOutput<typeof colorPairSchema>\ntype NumberPair = v.InferOutput<typeof numberPairSchema>\ntype TextPair = v.InferOutput<typeof textPairSchema>\ntype Replacements = {\n fill_color?: ColorPair[]\n text_color?: ColorPair[]\n stroke_color?: ColorPair[]\n stroke_weight?: NumberPair[]\n corner_radius?: NumberPair[]\n font_family?: TextPair[]\n font_style?: TextPair[]\n font_size?: NumberPair[]\n}\ntype Scope = { ids?: string[]; within: 'page' | 'selection' }\n\nconst CORNERS = [\n 'topLeftRadius',\n 'topRightRadius',\n 'bottomRightRadius',\n 'bottomLeftRadius'\n] as const\n/** Layers that draw their corners rounded; a radius on any other is not shown. */\nconst ROUNDED_TYPES: ReadonlySet<NodeType> = new Set([\n 'FRAME',\n 'RECTANGLE',\n 'ROUNDED_RECTANGLE',\n 'COMPONENT',\n 'COMPONENT_SET',\n 'INSTANCE'\n])\n\n/** Channels as 0–255 integers, so a hex written by hand matches the colour it names. */\nfunction channels(color: Color): string {\n return [color.r, color.g, color.b].map((value) => Math.round(value * 255)).join(',')\n}\n\n/** A colour as a tool reads it, so a bad value fails with the message other tools give. */\nfunction colorOf(value: string): Color {\n const parsed = parseToolColor(value)\n if ('error' in parsed) throw new Error(parsed.error)\n return parsed\n}\n\ninterface ColorSwap {\n match: (paint: Fill) => boolean\n apply: (paint: Fill) => Fill\n}\n\n/** The swap a colour pair makes; throws on a value that is not a colour. */\nfunction colorSwap(pair: ColorPair): ColorSwap {\n const from = colorOf(pair.from)\n const to = colorOf(pair.to)\n const key = channels(from)\n const percent = (value: number) => Math.round(value * 100)\n return {\n match: (paint) =>\n paint.type === 'SOLID' &&\n channels(paint.color) === key &&\n (from.a >= 1 || percent(paint.opacity * paint.color.a) === percent(from.a)),\n // A colour's alpha is the paint's opacity, as Figma takes it; without one the paint keeps its own.\n apply: (paint) => ({\n ...paint,\n color: { ...to, a: 1 },\n opacity: to.a < 1 ? to.a : paint.opacity * paint.color.a\n })\n }\n}\n\n/** `paints` with every listed colour swapped, or null when none matched. */\nfunction swapPaints<T extends Fill>(\n paints: readonly T[],\n swaps: readonly ColorSwap[],\n bound: (index: number) => boolean\n): T[] | null {\n const next = paints.map((paint, index) => {\n if (bound(index)) return paint\n const swap = swaps.find((candidate) => candidate.match(paint))\n return swap ? { ...paint, ...swap.apply(paint) } : paint\n })\n return next.some((paint, index) => paint !== paints[index]) ? next : null\n}\n\nfunction swapNumber(value: number, swaps: readonly NumberPair[] | undefined): number | null {\n const swap = swaps?.find((pair) => pair.from === value)\n return swap && swap.to !== value ? swap.to : null\n}\n\nfunction swapText(value: string, swaps: readonly TextPair[] | undefined): string | null {\n const swap = swaps?.find((pair) => pair.from.toLowerCase() === value.toLowerCase())\n return swap && swap.to !== value ? swap.to : null\n}\n\n/** The layers a call searches: the given ones, the selection, or the page's, with descendants. */\nfunction searchedNodes(figma: FigmaAPI, scope: Scope): SceneNode[] | string {\n const graph = figma.graph\n let roots: string[]\n if (scope.ids && scope.ids.length > 0) {\n const missing = scope.ids.find((id) => !graph.getNode(id))\n if (missing) return `Node \"${missing}\" not found`\n roots = scope.ids\n } else if (scope.within === 'selection') {\n roots = figma.currentPage.selection.map((node) => node.id)\n if (roots.length === 0) return 'Nothing is selected'\n } else {\n roots = graph.getNode(figma.currentPage.id)?.childIds ?? []\n }\n const nodes: SceneNode[] = []\n const seen = new Set<string>()\n const walk = (id: string) => {\n const node = seen.has(id) ? undefined : graph.getNode(id)\n if (!node) return\n seen.add(id)\n nodes.push(node)\n for (const childId of node.childIds) walk(childId)\n }\n for (const id of roots) walk(id)\n return nodes\n}\n\ninterface TextEdit {\n start: number\n end: number\n fontName?: { family: string; style: string }\n fontSize?: number\n family: boolean\n style: boolean\n}\n\n/** The font and size changes one text layer takes, range by range. */\nfunction textEdits(node: SceneNode, replacements: Replacements): TextEdit[] {\n if (node.type !== 'TEXT') return []\n const { font_family, font_style, font_size } = replacements\n if (!font_family && !font_style && !font_size) return []\n const edit = (start: number, end: number, font: unknown, size: unknown): TextEdit | null => {\n if (!v.is(fontNameSchema, font)) return null\n const family = swapText(font.family, font_family)\n const style = swapText(font.style, font_style)\n const fontSize = typeof size === 'number' ? swapNumber(size, font_size) : null\n if (!family && !style && fontSize === null) return null\n const textEdit: TextEdit = { start, end, family: family !== null, style: style !== null }\n if (family || style) {\n textEdit.fontName = { family: family ?? font.family, style: style ?? font.style }\n }\n if (fontSize !== null) textEdit.fontSize = fontSize\n return textEdit\n }\n if (node.text.length === 0) {\n // An empty text has no ranges to read; its own face is the first one it lists.\n const face = collectNodeFontFaces(node).at(0)\n const whole = face && edit(0, 0, face, node.fontSize)\n return whole ? [whole] : []\n }\n return getStyledTextSegments(node, ['fontName', 'fontSize']).flatMap(\n (segment) => edit(segment.start, segment.end, segment.fontName, segment.fontSize) ?? []\n )\n}\n\nconst REPLACEMENT_KINDS = Object.keys(replacementInputs) as ReplacementKind[]\n\nfunction colorSwaps(pairs: readonly ColorPair[] | undefined): ColorSwap[] {\n return (pairs ?? []).map(colorSwap)\n}\n\nexport const replaceMatchingProperties = defineTool({\n name: 'replace_matching_properties',\n description:\n 'Swap one value for another on every layer that uses it, across the current page (default), the selection, or the given layers with their descendants: fill, text, and stroke colors, stroke weights, corner radii, font families, font styles, and font sizes. Each property takes a list of { from, to } pairs; colors match by hex, ignoring opacity unless from has alpha, and colors bound to variables are left alone. Layers of read-only library components are not changed and come back in read_only. Returns how many values changed per property and the layers changed.',\n execution: { kind: 'sync', mutation: 'document' },\n input: v.strictObject({\n ids: v.optional(\n v.pipe(\n v.array(v.string()),\n v.description('Layers to search with their descendants (default: see within)')\n )\n ),\n within: v.optional(\n v.pipe(\n v.picklist(['page', 'selection']),\n v.description('Without ids: the current page (default) or the selection')\n ),\n 'page'\n ),\n ...replacementInputs\n }),\n fonts: (figma, args) => {\n const nodes = searchedNodes(figma, args)\n if (typeof nodes === 'string') return []\n const fonts: FigmaFontNameInput[] = []\n for (const node of nodes) {\n const edits = textEdits(node, args)\n if (edits.length === 0) continue\n fonts.push(...textFonts(figma, node.id))\n for (const edit of edits) if (edit.fontName) fonts.push(edit.fontName)\n }\n return fonts\n },\n execute: (figma, args) => {\n const kinds = REPLACEMENT_KINDS.filter((kind) => args[kind] !== undefined)\n if (kinds.length === 0) return { error: 'Name at least one property to replace' }\n const nodes = searchedNodes(figma, args)\n if (typeof nodes === 'string') return { error: nodes }\n const counts = mapValues(replacementInputs, () => 0)\n const changed = new Set<string>()\n const readOnly: string[] = []\n try {\n const swaps = {\n fill: colorSwaps(args.fill_color),\n text: colorSwaps(args.text_color),\n stroke: colorSwaps(args.stroke_color)\n }\n for (const node of nodes) {\n const paint = paintAndGeometryChanges(node, args, swaps)\n const edits = textEdits(node, args)\n if (!paint && edits.length === 0) continue\n // A library's components change in that library, so their layers are reported instead.\n if (!getNodeEditCapability(figma.graph, node.id).editable) {\n readOnly.push(node.id)\n continue\n }\n if (paint) {\n // As the node setters write, so styles detach and instances keep the change.\n editNode(figma.graph, node.id, paint.changes, figma.groupFitOptions)\n for (const kind of REPLACEMENT_KINDS) counts[kind] += paint.counts[kind] ?? 0\n }\n if (edits.length > 0) changeText(figma, node, edits, counts)\n changed.add(node.id)\n }\n } catch (error) {\n return toolFailure(error)\n }\n return {\n replaced: pick(counts, kinds),\n changed: changed.size,\n ids: [...changed],\n ...(readOnly.length > 0 ? { read_only: readOnly } : {})\n }\n }\n})\n\ntype Counts = Record<ReplacementKind, number>\n\n/** What one layer's paints, stroke weight, and corners change to, with the values it changes. */\nfunction paintAndGeometryChanges(\n node: SceneNode,\n replacements: Replacements,\n swaps: { fill: ColorSwap[]; text: ColorSwap[]; stroke: ColorSwap[] }\n): { changes: Partial<SceneNode>; counts: Partial<Counts> } | null {\n const changes: Partial<SceneNode> = {}\n const counts: Partial<Counts> = {}\n const count = (kind: ReplacementKind, amount = 1) => {\n counts[kind] = (counts[kind] ?? 0) + amount\n }\n const isText = node.type === 'TEXT'\n const fillSwaps = isText ? swaps.text : swaps.fill\n const fillKind = isText ? 'text_color' : 'fill_color'\n const bound = (kind: 'fills' | 'strokes') => (index: number) =>\n !!node.boundVariables[`${kind}/${index}/color`]\n if (fillSwaps.length > 0) {\n const fills = swapPaints(node.fills, fillSwaps, bound('fills'))\n if (fills) {\n changes.fills = fills\n count(fillKind, fills.filter((paint, index) => paint !== node.fills[index]).length)\n }\n if (isText) {\n let runs = 0\n const styleRuns = node.styleRuns.map((run) => {\n const runFills = run.style.fills && swapPaints(run.style.fills, fillSwaps, () => false)\n if (!runFills) return run\n runs++\n return { ...run, style: { ...run.style, fills: runFills } }\n })\n if (runs > 0) {\n changes.styleRuns = styleRuns\n count('text_color', runs)\n }\n }\n }\n if (swaps.stroke.length > 0) {\n const strokes = swapPaints(node.strokes, swaps.stroke, bound('strokes'))\n if (strokes) {\n changes.strokes = strokes\n count('stroke_color', strokes.filter((paint, index) => paint !== node.strokes[index]).length)\n }\n }\n const weight =\n node.strokes.length > 0 ? swapNumber(node.strokeWeight, replacements.stroke_weight) : null\n if (weight !== null) {\n // Each stroke draws at its own weight, so they all take it, as the strokeWeight setter does.\n changes.strokeWeight = weight\n changes.strokes = (changes.strokes ?? node.strokes).map((stroke) => ({\n ...copyStroke(stroke),\n weight\n }))\n count('stroke_weight')\n }\n if (replacements.corner_radius && ROUNDED_TYPES.has(node.type)) {\n Object.assign(changes, cornerChanges(node, replacements.corner_radius, count))\n }\n return Object.keys(changes).length > 0 ? { changes, counts } : null\n}\n\n/**\n * The corner radii `swaps` give a layer. Corners set together draw `cornerRadius`, whatever the\n * corner fields hold, so only corners set one by one are matched each on its own.\n */\nfunction cornerChanges(\n node: SceneNode,\n swaps: readonly NumberPair[],\n count: (kind: ReplacementKind) => void\n): Partial<SceneNode> {\n if (!node.independentCorners) {\n const radius = swapNumber(node.cornerRadius, swaps)\n if (radius === null) return {}\n count('corner_radius')\n return {\n cornerRadius: radius,\n ...Object.fromEntries(CORNERS.map((corner) => [corner, radius])),\n independentCorners: false\n }\n }\n const changes: Partial<SceneNode> = {}\n for (const corner of CORNERS) {\n const radius = swapNumber(node[corner], swaps)\n if (radius === null) continue\n changes[corner] = radius\n count('corner_radius')\n }\n const radii = CORNERS.map((corner) => changes[corner] ?? node[corner])\n if (radii.every((radius, index) => radius === node[CORNERS[index]])) return {}\n const uniform = radii.every((radius) => radius === radii[0])\n changes.cornerRadius = uniform ? radii[0] : node.cornerRadius\n changes.independentCorners = !uniform\n return changes\n}\n\nfunction changeText(\n figma: FigmaAPI,\n node: SceneNode,\n edits: readonly TextEdit[],\n counts: Record<ReplacementKind, number>\n): void {\n const text = figma.getNodeById(node.id)\n if (!text) return\n for (const edit of edits) {\n // A range over all of the text sets the text's own font, as set_font does, not a style run.\n const ranged = edit.start > 0 || edit.end < node.text.length\n if (edit.fontName) {\n if (ranged) text.setRangeFontName(edit.start, edit.end, edit.fontName)\n else text.fontName = edit.fontName\n }\n // The size comes after the font, so it lays the text out in the font it ends up in.\n if (edit.fontSize !== undefined) {\n if (ranged) text.setRangeFontSize(edit.start, edit.end, edit.fontSize)\n else text.fontSize = edit.fontSize\n counts.font_size++\n }\n if (edit.family) counts.font_family++\n if (edit.style) counts.font_style++\n }\n}\n"],"mappings":";;;;;;;;;;AAiBA,MAAM,kBAAkB,EAAE,aAAa;CACrC,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,YAAY,oDAAoD,CAAC;CAC5F,IAAI,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,YAAY,qDAAqD,CAAC;AAC7F,CAAC;AACD,MAAM,mBAAmB,EAAE,aAAa;CACtC,MAAM,WAAW,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,YAAY,eAAe,CAAC,CAAC;CACnE,IAAI,WAAW,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,SAAS,CAAC,GAAG,EAAE,YAAY,sBAAsB,CAAC,CAAC;AACzF,CAAC;AACD,MAAM,iBAAiB,EAAE,aAAa;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CAAC,MAAM;CACX,KAAK,MAAM,QAAQ,OAAO;EAExB,MAAM,SAAS,KAAK,QAAQ,KAAK,KAAK,MAAM,KAAK,KAAK;EACtD,IAAI,KAAK,UAAU;GACjB,IAAI,QAAQ,KAAK,iBAAiB,KAAK,OAAO,KAAK,KAAK,KAAK,QAAQ;QAChE,KAAK,WAAW,KAAK;EAC5B;EAEA,IAAI,KAAK,aAAa,KAAA,GAAW;GAC/B,IAAI,QAAQ,KAAK,iBAAiB,KAAK,OAAO,KAAK,KAAK,KAAK,QAAQ;QAChE,KAAK,WAAW,KAAK;GAC1B,OAAO;EACT;EACA,IAAI,KAAK,QAAQ,OAAO;EACxB,IAAI,KAAK,OAAO,OAAO;CACzB;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paint.d.ts","names":[],"sources":["../../../src/tools/modify/paint.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"paint.d.ts","names":[],"sources":["../../../src/tools/modify/paint.ts"],"mappings":";;cAwBa,SAAA;cAoEA,WAAA;cA8BA,cAAA"}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { defineTool, nodeIdInputSchema, toolFailure, toolNumber } from "../schema.js";
|
|
1
|
+
import { defineTool, nodeIdInputSchema, parseToolColor, toolFailure, toolNumber } from "../schema.js";
|
|
2
2
|
import { storeImage } from "../../figma-api/images.js";
|
|
3
3
|
import { isValid, toUint8Array } from "js-base64";
|
|
4
4
|
import { gradientTransformRows, linearGradientTransform } from "@pairdraw/scene-graph/gradient";
|
|
5
|
-
import { parseColor } from "@pairdraw/scene-graph/color";
|
|
6
5
|
import * as v from "valibot";
|
|
7
6
|
//#region src/tools/modify/paint.ts
|
|
8
7
|
/**
|
|
@@ -39,9 +38,11 @@ const setFill = defineTool({
|
|
|
39
38
|
execute: (figma, { id, color, color_end, gradient }) => {
|
|
40
39
|
const node = figma.getNodeById(id);
|
|
41
40
|
if (!node) return { error: `Node "${id}" not found` };
|
|
42
|
-
const c =
|
|
41
|
+
const c = parseToolColor(color);
|
|
42
|
+
if ("error" in c) return c;
|
|
43
43
|
if (gradient && color_end) {
|
|
44
|
-
const cEnd =
|
|
44
|
+
const cEnd = parseToolColor(color_end);
|
|
45
|
+
if ("error" in cEnd) return cEnd;
|
|
45
46
|
const ends = {
|
|
46
47
|
"top-bottom": [{
|
|
47
48
|
x: .5,
|
|
@@ -121,7 +122,8 @@ const setStroke = defineTool({
|
|
|
121
122
|
execute: (figma, { id, color, weight, align }) => {
|
|
122
123
|
const node = figma.getNodeById(id);
|
|
123
124
|
if (!node) return { error: `Node "${id}" not found` };
|
|
124
|
-
const c =
|
|
125
|
+
const c = parseToolColor(color);
|
|
126
|
+
if ("error" in c) return c;
|
|
125
127
|
node.strokes = [{
|
|
126
128
|
type: "SOLID",
|
|
127
129
|
color: c
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paint.js","names":[],"sources":["../../../src/tools/modify/paint.ts"],"sourcesContent":["import { isValid, toUint8Array } from 'js-base64'\nimport * as v from 'valibot'\n\nimport {
|
|
1
|
+
{"version":3,"file":"paint.js","names":[],"sources":["../../../src/tools/modify/paint.ts"],"sourcesContent":["import { isValid, toUint8Array } from 'js-base64'\nimport * as v from 'valibot'\n\nimport { gradientTransformRows, linearGradientTransform } from '@pairdraw/scene-graph/gradient'\nimport type { Vector } from '@pairdraw/scene-graph/primitives'\n\nimport { storeImage } from '#core/figma-api/images'\nimport {\n defineTool,\n nodeIdInputSchema,\n parseToolColor,\n toolFailure,\n toolNumber\n} from '#core/tools/schema'\n\n/**\n * Stretch as Figma's API writes it: a crop whose window is the whole image, which the image fill\n * panel's Stretch and `.pen`'s `stretch` also store.\n */\nconst STRETCH_IMAGE_TRANSFORM: [[number, number, number], [number, number, number]] = [\n [1, 0, 0],\n [0, 1, 0]\n]\n\nexport const setFill = defineTool({\n name: 'set_fill',\n\n description:\n 'Set fill on a node. Solid: color=\"#ff0000\". Linear gradient: gradient=\"top-bottom\" or \"left-right\" with color (start) and color_end (end).',\n execution: { kind: 'sync', mutation: 'properties' },\n input: v.strictObject({\n id: nodeIdInputSchema,\n color: v.pipe(v.string(), v.description('Color (hex). For gradient: start color.')),\n color_end: v.optional(\n v.pipe(v.string(), v.description('End color for gradient (if omitted, solid fill)'))\n ),\n gradient: v.optional(\n v.pipe(\n v.picklist(['top-bottom', 'bottom-top', 'left-right', 'right-left']),\n v.description('Gradient direction')\n )\n )\n }),\n execute: (figma, { id, color, color_end, gradient }) => {\n const node = figma.getNodeById(id)\n if (!node) return { error: `Node \"${id}\" not found` }\n\n const c = parseToolColor(color)\n if ('error' in c) return c\n\n if (gradient && color_end) {\n const cEnd = parseToolColor(color_end)\n if ('error' in cEnd) return cEnd\n // Edge middles of the layer's unit square that each direction runs between.\n const ends: Record<string, [Vector, Vector]> = {\n 'top-bottom': [\n { x: 0.5, y: 0 },\n { x: 0.5, y: 1 }\n ],\n 'bottom-top': [\n { x: 0.5, y: 1 },\n { x: 0.5, y: 0 }\n ],\n 'left-right': [\n { x: 0, y: 0.5 },\n { x: 1, y: 0.5 }\n ],\n 'right-left': [\n { x: 1, y: 0.5 },\n { x: 0, y: 0.5 }\n ]\n }\n const [start, end] = ends[gradient] ?? ends['top-bottom']\n node.fills = [\n {\n type: 'GRADIENT_LINEAR',\n gradientStops: [\n { position: 0, color: c },\n { position: 1, color: cEnd }\n ],\n gradientTransform: gradientTransformRows(linearGradientTransform(start, end))\n }\n ]\n return { id, gradient, start: c, end: cEnd }\n }\n\n // A colour's alpha becomes the paint's opacity, as Figma takes it.\n node.fills = [{ type: 'SOLID', color: c }]\n return { id, color: c }\n }\n})\n\nexport const setStroke = defineTool({\n name: 'set_stroke',\n\n description: 'Set the stroke (border) of a node.',\n execution: { kind: 'sync', mutation: 'properties' },\n input: v.strictObject({\n id: nodeIdInputSchema,\n color: v.pipe(v.string(), v.description('Stroke color (hex)')),\n weight: v.optional(\n toolNumber(v.pipe(v.number(), v.minValue(0.1), v.description('Stroke weight'))),\n 1\n ),\n align: v.optional(\n v.pipe(v.picklist(['INSIDE', 'CENTER', 'OUTSIDE']), v.description('Stroke alignment')),\n 'INSIDE'\n )\n }),\n execute: (figma, { id, color, weight, align }) => {\n const node = figma.getNodeById(id)\n if (!node) return { error: `Node \"${id}\" not found` }\n\n const c = parseToolColor(color)\n if ('error' in c) return c\n node.strokes = [{ type: 'SOLID', color: c }]\n node.strokeWeight = weight\n node.strokeAlign = align\n return { id, color: c, weight: weight }\n }\n})\n\nexport const setImageFill = defineTool({\n name: 'set_image_fill',\n\n description:\n 'Set an image fill on a node from base64-encoded image data or an image URL (http, https, or data). A URL is fetched as figma.createImageAsync fetches it, up to 64 MB.',\n execution: { kind: 'async', mutation: 'document' },\n capabilities: ['document:write', 'network:access'],\n input: v.strictObject({\n id: nodeIdInputSchema,\n image_data: v.optional(\n v.pipe(v.string(), v.description('Base64-encoded image bytes (PNG, JPEG, or WEBP)'))\n ),\n url: v.optional(\n v.pipe(\n v.string(),\n v.nonEmpty(),\n v.description('Image URL to fetch (http, https, or data), instead of image_data')\n )\n ),\n scale_mode: v.optional(\n v.pipe(\n v.picklist(['FILL', 'FIT', 'CROP', 'TILE', 'STRETCH']),\n v.description(\n 'Image scale mode. STRETCH stretches the whole image to the layer, ignoring its aspect ratio, as the Stretch mode of the image fill panel does.'\n )\n ),\n 'FILL'\n )\n }),\n execute: async (figma, { id, image_data, url, scale_mode }) => {\n if (!figma.getNodeById(id)) return { error: `Node \"${id}\" not found` }\n if ((image_data === undefined) === (url === undefined)) {\n return { error: 'Provide either image_data or url' }\n }\n let imageHash: string\n if (url !== undefined) {\n try {\n imageHash = (await figma.createImageAsync(url)).hash\n } catch (error) {\n return toolFailure(error)\n }\n } else {\n if (!image_data || !isValid(image_data)) return { error: 'image_data is not valid Base64' }\n imageHash = storeImage(figma.graph, toUint8Array(image_data))\n }\n // The fetch may outlast the node: another edit can remove it meanwhile.\n const node = figma.getNodeById(id)\n if (!node) return { error: `Node \"${id}\" not found` }\n node.fills = [\n scale_mode === 'STRETCH'\n ? { type: 'IMAGE', imageHash, scaleMode: 'CROP', imageTransform: STRETCH_IMAGE_TRANSFORM }\n : { type: 'IMAGE', imageHash, scaleMode: scale_mode }\n ]\n return { id, imageHash, scaleMode: scale_mode }\n }\n})\n"],"mappings":";;;;;;;;;;AAmBA,MAAM,0BAAgF,CACpF;CAAC;CAAG;CAAG;AAAC,GACR;CAAC;CAAG;CAAG;AAAC,CACV;AAEA,MAAa,UAAU,WAAW;CAChC,MAAM;CAEN,aACE;CACF,WAAW;EAAE,MAAM;EAAQ,UAAU;CAAa;CAClD,OAAO,EAAE,aAAa;EACpB,IAAI;EACJ,OAAO,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,YAAY,yCAAyC,CAAC;EAClF,WAAW,EAAE,SACX,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,YAAY,iDAAiD,CAAC,CACrF;EACA,UAAU,EAAE,SACV,EAAE,KACA,EAAE,SAAS;GAAC;GAAc;GAAc;GAAc;EAAY,CAAC,GACnE,EAAE,YAAY,oBAAoB,CACpC,CACF;CACF,CAAC;CACD,UAAU,OAAO,EAAE,IAAI,OAAO,WAAW,eAAe;EACtD,MAAM,OAAO,MAAM,YAAY,EAAE;EACjC,IAAI,CAAC,MAAM,OAAO,EAAE,OAAO,SAAS,GAAG,aAAa;EAEpD,MAAM,IAAI,eAAe,KAAK;EAC9B,IAAI,WAAW,GAAG,OAAO;EAEzB,IAAI,YAAY,WAAW;GACzB,MAAM,OAAO,eAAe,SAAS;GACrC,IAAI,WAAW,MAAM,OAAO;GAE5B,MAAM,OAAyC;IAC7C,cAAc,CACZ;KAAE,GAAG;KAAK,GAAG;IAAE,GACf;KAAE,GAAG;KAAK,GAAG;IAAE,CACjB;IACA,cAAc,CACZ;KAAE,GAAG;KAAK,GAAG;IAAE,GACf;KAAE,GAAG;KAAK,GAAG;IAAE,CACjB;IACA,cAAc,CACZ;KAAE,GAAG;KAAG,GAAG;IAAI,G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E,MAAM;GAAS;GAAW,WAAW;GAAQ,gBAAgB;EAAwB,IACvF;GAAE,MAAM;GAAS;GAAW,WAAW;EAAW,CACxD;EACA,OAAO;GAAE;GAAI;GAAW,WAAW;EAAW;CAChD;AACF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text.d.ts","names":[],"sources":["../../../src/tools/modify/text.ts"],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"text.d.ts","names":[],"sources":["../../../src/tools/modify/text.ts"],"mappings":";;;;;iBAqEgB,UAAU,OAAO,UAAU,aAAa;cAK3C,SAAA;cAoEA,SAAA;cA0FA,cAAA;cAoDA,eAAA;cAqBA,mBAAA"}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { collectNodeFontFaces } from "../../text/requirements/collect.js";
|
|
2
2
|
import { normalizeLinkURL } from "../../text/hyperlink.js";
|
|
3
|
-
import { defineTool, nodeIdInputSchema, nodeNotFound, toolFailure, toolNumber } from "../schema.js";
|
|
3
|
+
import { defineTool, nodeIdInputSchema, nodeNotFound, parseOptionalToolColor, toolFailure, toolNumber } from "../schema.js";
|
|
4
4
|
import { PairdrawAPI } from "../../pairdraw-api/index.js";
|
|
5
|
-
import { parseColor } from "@pairdraw/scene-graph/color";
|
|
6
5
|
import * as v from "valibot";
|
|
7
6
|
//#region src/tools/modify/text.ts
|
|
8
7
|
/**
|
|
@@ -67,7 +66,7 @@ const setText = defineTool({
|
|
|
67
66
|
}
|
|
68
67
|
});
|
|
69
68
|
/** Variable font axes a font tool can set, as Figma's `variationSettings`. */
|
|
70
|
-
const variationsInputSchema = v.optional(v.pipe(v.
|
|
69
|
+
const variationsInputSchema = v.optional(v.pipe(v.objectWithRest({}, v.number()), v.description("Variable font axis values by OpenType tag (e.g. {\"wght\": 550, \"wdth\": 75}); axes left out keep the style's values. Without a style, the nearest named style is picked.")));
|
|
71
70
|
/**
|
|
72
71
|
* The font a font tool gives text: what it names, the rest from `current`. Axes without a style
|
|
73
72
|
* leave the style out, for the nearest named style to be picked.
|
|
@@ -122,6 +121,8 @@ const setFont = defineTool({
|
|
|
122
121
|
execute: (figma, args) => {
|
|
123
122
|
const node = figma.getNodeById(args.id);
|
|
124
123
|
if (!node) return nodeNotFound(args.id);
|
|
124
|
+
const badTag = badVariationTag(args.variations);
|
|
125
|
+
if (badTag) return badTag;
|
|
125
126
|
const font = requestedFont(node, args);
|
|
126
127
|
try {
|
|
127
128
|
if (font && "whole" in font) node.fontName = font.whole;
|
|
@@ -142,6 +143,23 @@ function rangeFont(node, args) {
|
|
|
142
143
|
const current = args.start < node.characters.length ? node.getRangeFontName(args.start, args.start + 1) : node.fontName;
|
|
143
144
|
return fontNameInput(args, typeof current === "symbol" ? null : current);
|
|
144
145
|
}
|
|
146
|
+
/** Why a variation setting cannot apply: an axis tag is not four letters. */
|
|
147
|
+
function badVariationTag(variations) {
|
|
148
|
+
const tag = Object.keys(variations ?? {}).find((key) => key.length !== 4);
|
|
149
|
+
return tag === void 0 ? null : { error: `"${tag}" is not a four-letter OpenType axis tag` };
|
|
150
|
+
}
|
|
151
|
+
/** What a range edit sets, or why it cannot run: nothing named, a bad colour, or a bad link. */
|
|
152
|
+
function rangeEdit(args) {
|
|
153
|
+
if (!args.family && !args.size && !args.style && !args.variations && !args.color && args.hyperlink === void 0) return { error: "set_font_range needs at least one of family, size, style, variations, color, or hyperlink" };
|
|
154
|
+
const hyperlink = requestedHyperlink(args.hyperlink);
|
|
155
|
+
if (hyperlink && "error" in hyperlink) return hyperlink;
|
|
156
|
+
const color = parseOptionalToolColor(args.color);
|
|
157
|
+
if ("error" in color) return color;
|
|
158
|
+
return {
|
|
159
|
+
hyperlink: hyperlink ?? null,
|
|
160
|
+
color: color.color
|
|
161
|
+
};
|
|
162
|
+
}
|
|
145
163
|
const setFontRange = defineTool({
|
|
146
164
|
name: "set_font_range",
|
|
147
165
|
description: "Set font properties, color, or a link for a text range.",
|
|
@@ -171,15 +189,17 @@ const setFontRange = defineTool({
|
|
|
171
189
|
const node = figma.getNodeById(args.id);
|
|
172
190
|
if (!node) return nodeNotFound(args.id);
|
|
173
191
|
const { start, end } = args;
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
192
|
+
const badTag = badVariationTag(args.variations);
|
|
193
|
+
if (badTag) return badTag;
|
|
194
|
+
const edit = rangeEdit(args);
|
|
195
|
+
if ("error" in edit) return edit;
|
|
196
|
+
const { hyperlink, color } = edit;
|
|
177
197
|
try {
|
|
178
198
|
if (args.size) node.setRangeFontSize(start, end, args.size);
|
|
179
199
|
if (args.family || args.style || args.variations) node.setRangeFontName(start, end, rangeFont(node, args));
|
|
180
|
-
if (
|
|
200
|
+
if (color) node.setRangeFills(start, end, [{
|
|
181
201
|
type: "SOLID",
|
|
182
|
-
color
|
|
202
|
+
color
|
|
183
203
|
}]);
|
|
184
204
|
if (hyperlink) node.setRangeHyperlink(start, end, hyperlink.link);
|
|
185
205
|
} catch (error) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text.js","names":[],"sources":["../../../src/tools/modify/text.ts"],"sourcesContent":["import * as v from 'valibot'\n\nimport { parseColor } from '@pairdraw/scene-graph/color'\n\nimport type { FigmaAPI, FigmaNodeProxy } from '#core/figma-api'\nimport type { FigmaFontName, FigmaFontNameInput } from '#core/figma-api/fonts/names'\nimport type { FigmaVariationSettings } from '#core/figma-api/fonts/variations'\nimport { PairdrawAPI } from '#core/pairdraw-api'\nimport { normalizeLinkURL } from '#core/text/hyperlink'\nimport { collectNodeFontFaces } from '#core/text/requirements/collect'\nimport {\n toolNumber,\n nodeIdInputSchema,\n defineTool,\n nodeNotFound,\n toolFailure\n} from '#core/tools/schema'\n\n/**\n * A link a text tool sets, as the editor's link field takes it: a web or mail address, with\n * `https:` or `mailto:` added when typed without a scheme. Null or blank removes the link.\n */\nconst hyperlinkInputSchema = v.optional(\n v.pipe(\n v.nullable(v.string()),\n v.description(\n 'Link the text opens: an http(s) or mailto address (\"example.com\" becomes https://example.com, \"hi@example.com\" becomes mailto:). null or \"\" removes the link. Links are not underlined automatically; set text_decoration for that.'\n )\n )\n)\n\n/**\n * A scheme at the start of a typed link, even with a digit after its colon (`tel:123`), unless it\n * reads as a host and port (`localhost:3000`, `example.com:8080`), which gets `https://`.\n */\nconst TYPED_SCHEME = /^([a-z][a-z\\d+.-]*):/i\n\n/** A web address in the form the Figma API's link setter takes: `http(s)://host…`. */\nconst WEB_URL = /^https?:\\/\\/[^\\s/?#]+\\S*$/\n\n/** Whether a typed link names a scheme other than the http, https, and mailto links take. */\nfunction hasForeignScheme(typed: string): boolean {\n const scheme = TYPED_SCHEME.exec(typed)?.[1]?.toLowerCase()\n if (!scheme || scheme.includes('.') || scheme === 'localhost') return false\n return !['http', 'https', 'mailto'].includes(scheme)\n}\n\n/** What setting `hyperlink` asks for: no change, a removal, a URL, or an address it refuses. */\nfunction requestedHyperlink(\n hyperlink: string | null | undefined\n): { link: { type: 'URL'; value: string } | null } | { error: string } | null {\n if (hyperlink === undefined) return null\n if (hyperlink === null || hyperlink.trim() === '') return { link: null }\n const url = hasForeignScheme(hyperlink.trim()) ? null : normalizeLinkURL(hyperlink)\n if (!url) {\n return {\n error: `\"${hyperlink}\" is not a link the editor opens: use an http, https, or mailto address`\n }\n }\n // Figma keeps schemes in lower case and web addresses as `scheme://host`, so a loosely typed\n // `http:example.com` is written as the URL it parses to.\n const lowered = url.replace(TYPED_SCHEME, (scheme) => scheme.toLowerCase())\n const value =\n lowered.startsWith('mailto:') || WEB_URL.test(lowered) ? lowered : new URL(lowered).href\n return { link: { type: 'URL', value } }\n}\n\n/** The fonts a text layer uses, which an edit that lays it out again needs loaded. */\nexport function textFonts(figma: FigmaAPI, id: string): FigmaFontNameInput[] {\n const node = figma.graph.getNode(id)\n return node ? collectNodeFontFaces(node) : []\n}\n\nexport const setText = defineTool({\n name: 'set_text',\n\n description: 'Set text content of a text node.',\n execution: { kind: 'sync', mutation: 'properties' },\n input: v.strictObject({\n id: nodeIdInputSchema,\n text: v.pipe(v.string(), v.description('Text content'))\n }),\n fonts: (figma, { id }) => textFonts(figma, id),\n execute: (figma, { id, text }) => {\n const node = figma.getNodeById(id)\n if (!node) return { error: `Node \"${id}\" not found` }\n node.characters = text\n return { id, text }\n }\n})\n\n/** Variable font axes a font tool can set, as Figma's `variationSettings`. */\nconst variationsInputSchema = v.optional(\n v.pipe(\n v.record(v.pipe(v.string(), v.length(4)), v.number()),\n v.description(\n 'Variable font axis values by OpenType tag (e.g. {\"wght\": 550, \"wdth\": 75}); axes left out keep the style\\'s values. Without a style, the nearest named style is picked.'\n )\n )\n)\n\n/**\n * The font a font tool gives text: what it names, the rest from `current`. Axes without a style\n * leave the style out, for the nearest named style to be picked.\n */\nfunction fontNameInput(\n args: { family?: string; style?: string; variations?: FigmaVariationSettings },\n current: FigmaFontName | null\n): FigmaFontNameInput {\n return {\n family: args.family ?? current?.family ?? '',\n style: args.style ?? (args.variations ? undefined : (current?.style ?? 'Regular')),\n variationSettings: args.variations\n }\n}\n\n/**\n * The font `set_font` gives the text, or null when it keeps the text's. Text set in several fonts\n * takes the given family or style for all of it, keeping the rest from its first character. Axes\n * without a style are set per range, each range taking its nearest named style, as in Figma.\n */\nfunction requestedFont(\n node: FigmaNodeProxy,\n args: { family?: string; style?: string; variations?: FigmaVariationSettings }\n): { whole: FigmaFontName } | { perRange: FigmaFontNameInput } | null {\n if (!args.family && !args.style && !args.variations) return null\n const fontName =\n typeof node.fontName === 'symbol' && node.characters.length > 0\n ? node.getRangeFontName(0, 1)\n : node.fontName\n const current = typeof fontName === 'symbol' ? null : fontName\n const family = args.family ?? current?.family ?? ''\n if (args.variations && !args.style && node.characters.length > 0) {\n return { perRange: { family, variationSettings: args.variations } }\n }\n const style = args.style ?? current?.style ?? 'Regular'\n return { whole: { family, style, variationSettings: args.variations } }\n}\n\nexport const setFont = defineTool({\n name: 'set_font',\n\n description: 'Set font properties of a text node.',\n execution: { kind: 'sync', mutation: 'properties' },\n input: v.strictObject({\n id: nodeIdInputSchema,\n family: v.optional(v.pipe(v.string(), v.description('Font family name'))),\n size: v.optional(toolNumber(v.pipe(v.number(), v.minValue(1), v.description('Font size')))),\n style: v.optional(\n v.pipe(v.string(), v.description('Font style (e.g. \"Bold\", \"Regular\", \"Bold Italic\")'))\n ),\n variations: variationsInputSchema\n }),\n // A new font replaces the text's, which then need not be loaded, so a missing font can be\n // replaced; a size or per-range axes lay out the text in its own fonts.\n fonts: (figma, args) => {\n const node = figma.getNodeById(args.id)\n const font = node && requestedFont(node, args)\n if (font && 'whole' in font) return [font.whole]\n return [...(font ? [font.perRange] : []), ...textFonts(figma, args.id)]\n },\n execute: (figma, args) => {\n const node = figma.getNodeById(args.id)\n if (!node) return nodeNotFound(args.id)\n const font = requestedFont(node, args)\n try {\n if (font && 'whole' in font) node.fontName = font.whole\n else if (font) node.setRangeFontName(0, node.characters.length, font.perRange)\n // The size comes after the font, so it lays the text out in the font it ends up in.\n if (args.size !== undefined) node.fontSize = args.size\n } catch (error) {\n return toolFailure(error)\n }\n return { id: args.id, fontName: node.fontName, fontSize: node.fontSize }\n }\n})\n\n/** The font `set_font_range` gives its range: what it names, the rest from the range's start. */\nfunction rangeFont(\n node: FigmaNodeProxy,\n args: { start: number; family?: string; style?: string; variations?: FigmaVariationSettings }\n): FigmaFontNameInput {\n const inText = args.start < node.characters.length\n const current = inText ? node.getRangeFontName(args.start, args.start + 1) : node.fontName\n return fontNameInput(args, typeof current === 'symbol' ? null : current)\n}\n\nexport const setFontRange = defineTool({\n name: 'set_font_range',\n\n description: 'Set font properties, color, or a link for a text range.',\n execution: { kind: 'sync', mutation: 'properties' },\n input: v.strictObject({\n id: nodeIdInputSchema,\n start: toolNumber(v.pipe(v.number(), v.minValue(0), v.description('Start character index'))),\n end: toolNumber(v.pipe(v.number(), v.minValue(0), v.description('End character index'))),\n family: v.optional(v.pipe(v.string(), v.description('Font family name'))),\n size: v.optional(toolNumber(v.pipe(v.number(), v.minValue(1), v.description('Font size')))),\n style: v.optional(v.pipe(v.string(), v.description('Font style'))),\n variations: variationsInputSchema,\n color: v.optional(v.pipe(v.string(), v.description('Text color (hex)'))),\n hyperlink: hyperlinkInputSchema\n }),\n fonts: (figma, args) => {\n const node = figma.getNodeById(args.id)\n if (!node) return []\n const font = args.family || args.style || args.variations ? rangeFont(node, args) : null\n // A colour alone is a paint and lays nothing out, so text in a missing font can still take it.\n const laysOut = font !== null || args.size !== undefined\n return [...(font ? [font] : []), ...(laysOut ? textFonts(figma, args.id) : [])]\n },\n execute: (figma, args) => {\n const node = figma.getNodeById(args.id)\n if (!node) return nodeNotFound(args.id)\n const { start, end } = args\n if (\n !args.family &&\n !args.size &&\n !args.style &&\n !args.variations &&\n !args.color &&\n args.hyperlink === undefined\n ) {\n return {\n error:\n 'set_font_range needs at least one of family, size, style, variations, color, or hyperlink'\n }\n }\n const hyperlink = requestedHyperlink(args.hyperlink)\n if (hyperlink && 'error' in hyperlink) return hyperlink\n // The range methods reject an empty range or one past the text, before changing anything.\n try {\n if (args.size) node.setRangeFontSize(start, end, args.size)\n if (args.family || args.style || args.variations) {\n node.setRangeFontName(start, end, rangeFont(node, args))\n }\n if (args.color) {\n node.setRangeFills(start, end, [{ type: 'SOLID', color: parseColor(args.color) }])\n }\n if (hyperlink) node.setRangeHyperlink(start, end, hyperlink.link)\n } catch (error) {\n return toolFailure(error)\n }\n return {\n id: args.id,\n range: { start, end },\n ...(hyperlink ? { hyperlink: hyperlink.link?.value ?? null } : {})\n }\n }\n})\n\nexport const setTextResize = defineTool({\n name: 'set_text_resize',\n\n description: 'Set text auto-resize mode.',\n execution: { kind: 'sync', mutation: 'properties' },\n input: v.strictObject({\n id: nodeIdInputSchema,\n mode: v.pipe(\n v.picklist(['NONE', 'WIDTH_AND_HEIGHT', 'HEIGHT', 'TRUNCATE']),\n v.description('Resize mode')\n )\n }),\n fonts: (figma, { id }) => textFonts(figma, id),\n execute: (figma, { id, mode }) => {\n const node = figma.getNodeById(id)\n if (!node) return { error: `Node \"${id}\" not found` }\n node.textAutoResize = mode\n return { id, textAutoResize: mode }\n }\n})\n\nexport const setTextProperties = defineTool({\n name: 'set_text_properties',\n\n description:\n 'Set text layout properties: alignment, auto-resize, text case, decoration, truncation, and a link for the whole text.',\n execution: { kind: 'sync', mutation: 'properties' },\n input: v.strictObject({\n id: v.pipe(v.string(), v.description('Text node ID')),\n align_horizontal: v.optional(\n v.pipe(\n v.picklist(['LEFT', 'CENTER', 'RIGHT', 'JUSTIFIED']),\n v.description('Horizontal text alignment')\n )\n ),\n align_vertical: v.optional(\n v.pipe(v.picklist(['TOP', 'CENTER', 'BOTTOM']), v.description('Vertical text alignment'))\n ),\n auto_resize: v.optional(\n v.pipe(\n v.picklist(['NONE', 'WIDTH_AND_HEIGHT', 'HEIGHT', 'TRUNCATE']),\n v.description('Text auto-resize mode')\n )\n ),\n direction: v.optional(\n v.pipe(v.picklist(['AUTO', 'LTR', 'RTL']), v.description('Text direction'))\n ),\n text_decoration: v.optional(\n v.pipe(v.picklist(['NONE', 'UNDERLINE', 'STRIKETHROUGH']), v.description('Text decoration'))\n ),\n hyperlink: hyperlinkInputSchema\n }),\n fonts: (figma, { id }) => textFonts(figma, id),\n execute: (figma, args) => {\n const node = figma.getNodeById(args.id)\n if (!node) return nodeNotFound(args.id)\n if (node.type !== 'TEXT') return { error: `Node \"${args.id}\" is not a TEXT node` }\n // Refused before anything changes, so a bad address leaves the text as it was.\n const hyperlink = requestedHyperlink(args.hyperlink)\n if (hyperlink && 'error' in hyperlink) return hyperlink\n const updated: string[] = []\n if (args.align_horizontal !== undefined) {\n node.textAlignHorizontal = args.align_horizontal\n updated.push('textAlignHorizontal')\n }\n if (args.align_vertical !== undefined) {\n node.textAlignVertical = args.align_vertical\n updated.push('textAlignVertical')\n }\n if (args.auto_resize !== undefined) {\n node.textAutoResize = args.auto_resize\n updated.push('textAutoResize')\n }\n if (args.direction !== undefined) {\n new PairdrawAPI(figma).setTextDirection(node, args.direction)\n updated.push('textDirection')\n }\n if (args.text_decoration !== undefined) {\n node.textDecoration = args.text_decoration\n updated.push('textDecoration')\n }\n if (hyperlink) {\n try {\n node.hyperlink = hyperlink.link\n } catch (error) {\n return toolFailure(error)\n }\n updated.push('hyperlink')\n }\n return { id: args.id, updated }\n }\n})\n"],"mappings":";;;;;;;;;;;AAsBA,MAAM,uBAAuB,EAAE,SAC7B,EAAE,KACA,EAAE,SAAS,EAAE,OAAO,CAAC,GACrB,EAAE,YACA,2OACF,CACF,CACF;;;;;AAMA,MAAM,eAAe;;AAGrB,MAAM,UAAU;;AAGhB,SAAS,iBAAiB,OAAwB;CAChD,MAAM,SAAS,aAAa,KAAK,KAAK,CAAC,GAAG,EAAE,EAAE,YAAY;CAC1D,IAAI,CAAC,UAAU,OAAO,SAAS,GAAG,KAAK,WAAW,aAAa,OAAO;CACtE,OAAO,CAAC;EAAC;EAAQ;EAAS;CAAQ,CAAC,CAAC,SAAS,MAAM;AACrD;;AAGA,SAAS,mBACP,WAC4E;CAC5E,IAAI,cAAc,KAAA,GAAW,OAAO;CACpC,IAAI,cAAc,QAAQ,UAAU,KAAK,MAAM,IAAI,OAAO,EAAE,MAAM,KAAK;CACvE,MAAM,MAAM,iBAAiB,UAAU,KAAK,CAAC,IAAI,OAAO,iBAAiB,SAAS;CAClF,IAAI,CAAC,KACH,OAAO,EACL,OAAO,IAAI,UAAU,yEACvB;CAIF,MAAM,UAAU,IAAI,QAAQ,eAAe,WAAW,OAAO,YAAY,CAAC;CAG1E,OAAO,EAAE,MAAM;EAAE,MAAM;EAAO,OAD5B,QAAQ,WAAW,SAAS,KAAK,QAAQ,KAAK,OAAO,IAAI,UAAU,IAAI,IAAI,OAAO,CAAC,CAAC;CAClD,EAAE;AACxC;;AAGA,SAAgB,UAAU,OAAiB,IAAkC;CAC3E,MAAM,OAAO,MAAM,MAAM,QAAQ,EAAE;CACnC,OAAO,OAAO,qBAAqB,IAAI,IAAI,CAAC;AAC9C;AAEA,MAAa,UAAU,WAAW;CAChC,MAAM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,IAAI,KAAK,SAAS,QAAQ,OAAO,EAAE,OAAO,SAAS,KAAK,GAAG,sBAAsB;EAEjF,MAAM,YAAY,mBAAmB,KAAK,SAAS;EACnD,IAAI,aAAa,WAAW,WAAW,OAAO;EAC9C,MAAM,UAAoB,CAAC;EAC3B,IAAI,KAAK,qBAAqB,KAAA,GAAW;GACvC,KAAK,sBAAsB,KAAK;GAChC,QAAQ,KAAK,qBAAqB;EACpC;EACA,IAAI,KAAK,mBAAmB,KAAA,GAAW;GACrC,KAAK,oBAAoB,KAAK;GAC9B,QAAQ,KAAK,mBAAmB;EAClC;EACA,IAAI,KAAK,gBAAgB,KAAA,GAAW;GAClC,KAAK,iBAAiB,KAAK;GAC3B,QAAQ,KAAK,gBAAgB;EAC/B;EACA,IAAI,KAAK,cAAc,KAAA,GAAW;GAChC,IAAI,YAAY,KAAK,CAAC,CAAC,iBAAiB,MAAM,KAAK,SAAS;GAC5D,QAAQ,KAAK,eAAe;EAC9B;EACA,IAAI,KAAK,oBAAoB,KAAA,GAAW;GACtC,KAAK,iBAAiB,KAAK;GAC3B,QAAQ,KAAK,gBAAgB;EAC/B;EACA,IAAI,WAAW;GACb,IAAI;IACF,KAAK,YAAY,UAAU;GAC7B,SAAS,OAAO;IACd,OAAO,YAAY,KAAK;GAC1B;GACA,QAAQ,KAAK,WAAW;EAC1B;EACA,OAAO;GAAE,IAAI,KAAK;GAAI;EAAQ;CAChC;AACF,CAAC"}
|
|
1
|
+
{"version":3,"file":"text.js","names":[],"sources":["../../../src/tools/modify/text.ts"],"sourcesContent":["import * as v from 'valibot'\n\nimport type { Color } from '@pairdraw/scene-graph/primitives'\n\nimport type { FigmaAPI, FigmaNodeProxy } from '#core/figma-api'\nimport type { FigmaFontName, FigmaFontNameInput } from '#core/figma-api/fonts/names'\nimport type { FigmaVariationSettings } from '#core/figma-api/fonts/variations'\nimport { PairdrawAPI } from '#core/pairdraw-api'\nimport { normalizeLinkURL } from '#core/text/hyperlink'\nimport { collectNodeFontFaces } from '#core/text/requirements/collect'\nimport {\n parseOptionalToolColor,\n toolNumber,\n nodeIdInputSchema,\n defineTool,\n nodeNotFound,\n toolFailure\n} from '#core/tools/schema'\n\n/**\n * A link a text tool sets, as the editor's link field takes it: a web or mail address, with\n * `https:` or `mailto:` added when typed without a scheme. Null or blank removes the link.\n */\nconst hyperlinkInputSchema = v.optional(\n v.pipe(\n v.nullable(v.string()),\n v.description(\n 'Link the text opens: an http(s) or mailto address (\"example.com\" becomes https://example.com, \"hi@example.com\" becomes mailto:). null or \"\" removes the link. Links are not underlined automatically; set text_decoration for that.'\n )\n )\n)\n\n/**\n * A scheme at the start of a typed link, even with a digit after its colon (`tel:123`), unless it\n * reads as a host and port (`localhost:3000`, `example.com:8080`), which gets `https://`.\n */\nconst TYPED_SCHEME = /^([a-z][a-z\\d+.-]*):/i\n\n/** A web address in the form the Figma API's link setter takes: `http(s)://host…`. */\nconst WEB_URL = /^https?:\\/\\/[^\\s/?#]+\\S*$/\n\n/** Whether a typed link names a scheme other than the http, https, and mailto links take. */\nfunction hasForeignScheme(typed: string): boolean {\n const scheme = TYPED_SCHEME.exec(typed)?.[1]?.toLowerCase()\n if (!scheme || scheme.includes('.') || scheme === 'localhost') return false\n return !['http', 'https', 'mailto'].includes(scheme)\n}\n\n/** What setting `hyperlink` asks for: no change, a removal, a URL, or an address it refuses. */\nfunction requestedHyperlink(\n hyperlink: string | null | undefined\n): { link: { type: 'URL'; value: string } | null } | { error: string } | null {\n if (hyperlink === undefined) return null\n if (hyperlink === null || hyperlink.trim() === '') return { link: null }\n const url = hasForeignScheme(hyperlink.trim()) ? null : normalizeLinkURL(hyperlink)\n if (!url) {\n return {\n error: `\"${hyperlink}\" is not a link the editor opens: use an http, https, or mailto address`\n }\n }\n // Figma keeps schemes in lower case and web addresses as `scheme://host`, so a loosely typed\n // `http:example.com` is written as the URL it parses to.\n const lowered = url.replace(TYPED_SCHEME, (scheme) => scheme.toLowerCase())\n const value =\n lowered.startsWith('mailto:') || WEB_URL.test(lowered) ? lowered : new URL(lowered).href\n return { link: { type: 'URL', value } }\n}\n\n/** The fonts a text layer uses, which an edit that lays it out again needs loaded. */\nexport function textFonts(figma: FigmaAPI, id: string): FigmaFontNameInput[] {\n const node = figma.graph.getNode(id)\n return node ? collectNodeFontFaces(node) : []\n}\n\nexport const setText = defineTool({\n name: 'set_text',\n\n description: 'Set text content of a text node.',\n execution: { kind: 'sync', mutation: 'properties' },\n input: v.strictObject({\n id: nodeIdInputSchema,\n text: v.pipe(v.string(), v.description('Text content'))\n }),\n fonts: (figma, { id }) => textFonts(figma, id),\n execute: (figma, { id, text }) => {\n const node = figma.getNodeById(id)\n if (!node) return { error: `Node \"${id}\" not found` }\n node.characters = text\n return { id, text }\n }\n})\n\n/** Variable font axes a font tool can set, as Figma's `variationSettings`. */\n// An object with a value for every key rather than a record: a record publishes `propertyNames`,\n// which OpenAI refuses, and a JSON object's keys are strings anyway.\nconst variationsInputSchema = v.optional(\n v.pipe(\n v.objectWithRest({}, v.number()),\n v.description(\n 'Variable font axis values by OpenType tag (e.g. {\"wght\": 550, \"wdth\": 75}); axes left out keep the style\\'s values. Without a style, the nearest named style is picked.'\n )\n )\n)\n\n/**\n * The font a font tool gives text: what it names, the rest from `current`. Axes without a style\n * leave the style out, for the nearest named style to be picked.\n */\nfunction fontNameInput(\n args: { family?: string; style?: string; variations?: FigmaVariationSettings },\n current: FigmaFontName | null\n): FigmaFontNameInput {\n return {\n family: args.family ?? current?.family ?? '',\n style: args.style ?? (args.variations ? undefined : (current?.style ?? 'Regular')),\n variationSettings: args.variations\n }\n}\n\n/**\n * The font `set_font` gives the text, or null when it keeps the text's. Text set in several fonts\n * takes the given family or style for all of it, keeping the rest from its first character. Axes\n * without a style are set per range, each range taking its nearest named style, as in Figma.\n */\nfunction requestedFont(\n node: FigmaNodeProxy,\n args: { family?: string; style?: string; variations?: FigmaVariationSettings }\n): { whole: FigmaFontName } | { perRange: FigmaFontNameInput } | null {\n if (!args.family && !args.style && !args.variations) return null\n const fontName =\n typeof node.fontName === 'symbol' && node.characters.length > 0\n ? node.getRangeFontName(0, 1)\n : node.fontName\n const current = typeof fontName === 'symbol' ? null : fontName\n const family = args.family ?? current?.family ?? ''\n if (args.variations && !args.style && node.characters.length > 0) {\n return { perRange: { family, variationSettings: args.variations } }\n }\n const style = args.style ?? current?.style ?? 'Regular'\n return { whole: { family, style, variationSettings: args.variations } }\n}\n\nexport const setFont = defineTool({\n name: 'set_font',\n\n description: 'Set font properties of a text node.',\n execution: { kind: 'sync', mutation: 'properties' },\n input: v.strictObject({\n id: nodeIdInputSchema,\n family: v.optional(v.pipe(v.string(), v.description('Font family name'))),\n size: v.optional(toolNumber(v.pipe(v.number(), v.minValue(1), v.description('Font size')))),\n style: v.optional(\n v.pipe(v.string(), v.description('Font style (e.g. \"Bold\", \"Regular\", \"Bold Italic\")'))\n ),\n variations: variationsInputSchema\n }),\n // A new font replaces the text's, which then need not be loaded, so a missing font can be\n // replaced; a size or per-range axes lay out the text in its own fonts.\n fonts: (figma, args) => {\n const node = figma.getNodeById(args.id)\n const font = node && requestedFont(node, args)\n if (font && 'whole' in font) return [font.whole]\n return [...(font ? [font.perRange] : []), ...textFonts(figma, args.id)]\n },\n execute: (figma, args) => {\n const node = figma.getNodeById(args.id)\n if (!node) return nodeNotFound(args.id)\n const badTag = badVariationTag(args.variations)\n if (badTag) return badTag\n const font = requestedFont(node, args)\n try {\n if (font && 'whole' in font) node.fontName = font.whole\n else if (font) node.setRangeFontName(0, node.characters.length, font.perRange)\n // The size comes after the font, so it lays the text out in the font it ends up in.\n if (args.size !== undefined) node.fontSize = args.size\n } catch (error) {\n return toolFailure(error)\n }\n return { id: args.id, fontName: node.fontName, fontSize: node.fontSize }\n }\n})\n\n/** The font `set_font_range` gives its range: what it names, the rest from the range's start. */\nfunction rangeFont(\n node: FigmaNodeProxy,\n args: { start: number; family?: string; style?: string; variations?: FigmaVariationSettings }\n): FigmaFontNameInput {\n const inText = args.start < node.characters.length\n const current = inText ? node.getRangeFontName(args.start, args.start + 1) : node.fontName\n return fontNameInput(args, typeof current === 'symbol' ? null : current)\n}\n\ntype RangeEdit = {\n hyperlink: { link: { type: 'URL'; value: string } | null } | null\n color: Color | undefined\n}\n\n/** Why a variation setting cannot apply: an axis tag is not four letters. */\nfunction badVariationTag(variations: FigmaVariationSettings | undefined): { error: string } | null {\n const tag = Object.keys(variations ?? {}).find((key) => key.length !== 4)\n return tag === undefined ? null : { error: `\"${tag}\" is not a four-letter OpenType axis tag` }\n}\n\n/** What a range edit sets, or why it cannot run: nothing named, a bad colour, or a bad link. */\nfunction rangeEdit(args: {\n family?: string\n size?: number\n style?: string\n variations?: unknown\n color?: string\n hyperlink?: string | null\n}): RangeEdit | { error: string } {\n if (\n !args.family &&\n !args.size &&\n !args.style &&\n !args.variations &&\n !args.color &&\n args.hyperlink === undefined\n ) {\n return {\n error:\n 'set_font_range needs at least one of family, size, style, variations, color, or hyperlink'\n }\n }\n const hyperlink = requestedHyperlink(args.hyperlink)\n if (hyperlink && 'error' in hyperlink) return hyperlink\n const color = parseOptionalToolColor(args.color)\n if ('error' in color) return color\n return { hyperlink: hyperlink ?? null, color: color.color }\n}\n\nexport const setFontRange = defineTool({\n name: 'set_font_range',\n\n description: 'Set font properties, color, or a link for a text range.',\n execution: { kind: 'sync', mutation: 'properties' },\n input: v.strictObject({\n id: nodeIdInputSchema,\n start: toolNumber(v.pipe(v.number(), v.minValue(0), v.description('Start character index'))),\n end: toolNumber(v.pipe(v.number(), v.minValue(0), v.description('End character index'))),\n family: v.optional(v.pipe(v.string(), v.description('Font family name'))),\n size: v.optional(toolNumber(v.pipe(v.number(), v.minValue(1), v.description('Font size')))),\n style: v.optional(v.pipe(v.string(), v.description('Font style'))),\n variations: variationsInputSchema,\n color: v.optional(v.pipe(v.string(), v.description('Text color (hex)'))),\n hyperlink: hyperlinkInputSchema\n }),\n fonts: (figma, args) => {\n const node = figma.getNodeById(args.id)\n if (!node) return []\n const font = args.family || args.style || args.variations ? rangeFont(node, args) : null\n // A colour alone is a paint and lays nothing out, so text in a missing font can still take it.\n const laysOut = font !== null || args.size !== undefined\n return [...(font ? [font] : []), ...(laysOut ? textFonts(figma, args.id) : [])]\n },\n execute: (figma, args) => {\n const node = figma.getNodeById(args.id)\n if (!node) return nodeNotFound(args.id)\n const { start, end } = args\n const badTag = badVariationTag(args.variations)\n if (badTag) return badTag\n const edit = rangeEdit(args)\n if ('error' in edit) return edit\n const { hyperlink, color } = edit\n // The range methods reject an empty range or one past the text, before changing anything.\n try {\n if (args.size) node.setRangeFontSize(start, end, args.size)\n if (args.family || args.style || args.variations) {\n node.setRangeFontName(start, end, rangeFont(node, args))\n }\n if (color) node.setRangeFills(start, end, [{ type: 'SOLID', color }])\n if (hyperlink) node.setRangeHyperlink(start, end, hyperlink.link)\n } catch (error) {\n return toolFailure(error)\n }\n return {\n id: args.id,\n range: { start, end },\n ...(hyperlink ? { hyperlink: hyperlink.link?.value ?? null } : {})\n }\n }\n})\n\nexport const setTextResize = defineTool({\n name: 'set_text_resize',\n\n description: 'Set text auto-resize mode.',\n execution: { kind: 'sync', mutation: 'properties' },\n input: v.strictObject({\n id: nodeIdInputSchema,\n mode: v.pipe(\n v.picklist(['NONE', 'WIDTH_AND_HEIGHT', 'HEIGHT', 'TRUNCATE']),\n v.description('Resize mode')\n )\n }),\n fonts: (figma, { id }) => textFonts(figma, id),\n execute: (figma, { id, mode }) => {\n const node = figma.getNodeById(id)\n if (!node) return { error: `Node \"${id}\" not found` }\n node.textAutoResize = mode\n return { id, textAutoResize: mode }\n }\n})\n\nexport const setTextProperties = defineTool({\n name: 'set_text_properties',\n\n description:\n 'Set text layout properties: alignment, auto-resize, text case, decoration, truncation, and a link for the whole text.',\n execution: { kind: 'sync', mutation: 'properties' },\n input: v.strictObject({\n id: v.pipe(v.string(), v.description('Text node ID')),\n align_horizontal: v.optional(\n v.pipe(\n v.picklist(['LEFT', 'CENTER', 'RIGHT', 'JUSTIFIED']),\n v.description('Horizontal text alignment')\n )\n ),\n align_vertical: v.optional(\n v.pipe(v.picklist(['TOP', 'CENTER', 'BOTTOM']), v.description('Vertical text alignment'))\n ),\n auto_resize: v.optional(\n v.pipe(\n v.picklist(['NONE', 'WIDTH_AND_HEIGHT', 'HEIGHT', 'TRUNCATE']),\n v.description('Text auto-resize mode')\n )\n ),\n direction: v.optional(\n v.pipe(v.picklist(['AUTO', 'LTR', 'RTL']), v.description('Text direction'))\n ),\n text_decoration: v.optional(\n v.pipe(v.picklist(['NONE', 'UNDERLINE', 'STRIKETHROUGH']), v.description('Text decoration'))\n ),\n hyperlink: hyperlinkInputSchema\n }),\n fonts: (figma, { id }) => textFonts(figma, id),\n execute: (figma, args) => {\n const node = figma.getNodeById(args.id)\n if (!node) return nodeNotFound(args.id)\n if (node.type !== 'TEXT') return { error: `Node \"${args.id}\" is not a TEXT node` }\n // Refused before anything changes, so a bad address leaves the text as it was.\n const hyperlink = requestedHyperlink(args.hyperlink)\n if (hyperlink && 'error' in hyperlink) return hyperlink\n const updated: string[] = []\n if (args.align_horizontal !== undefined) {\n node.textAlignHorizontal = args.align_horizontal\n updated.push('textAlignHorizontal')\n }\n if (args.align_vertical !== undefined) {\n node.textAlignVertical = args.align_vertical\n updated.push('textAlignVertical')\n }\n if (args.auto_resize !== undefined) {\n node.textAutoResize = args.auto_resize\n updated.push('textAutoResize')\n }\n if (args.direction !== undefined) {\n new PairdrawAPI(figma).setTextDirection(node, args.direction)\n updated.push('textDirection')\n }\n if (args.text_decoration !== undefined) {\n node.textDecoration = args.text_decoration\n updated.push('textDecoration')\n }\n if (hyperlink) {\n try {\n node.hyperlink = hyperlink.link\n } catch (error) {\n return toolFailure(error)\n }\n updated.push('hyperlink')\n }\n return { id: args.id, updated }\n }\n})\n"],"mappings":";;;;;;;;;;AAuBA,MAAM,uBAAuB,EAAE,SAC7B,EAAE,KACA,EAAE,SAAS,EAAE,OAAO,CAAC,GACrB,EAAE,YACA,2OACF,CACF,CACF;;;;;AAMA,M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EACA,WAAW;CACb,CAAC;CACD,QAAQ,OAAO,EAAE,SAAS,UAAU,OAAO,EAAE;CAC7C,UAAU,OAAO,SAAS;EACxB,MAAM,OAAO,MAAM,YAAY,KAAK,EAAE;EACtC,IAAI,CAAC,MAAM,OAAO,aAAa,KAAK,EAAE;EACtC,IAAI,KAAK,SAAS,QAAQ,OAAO,EAAE,OAAO,SAAS,KAAK,GAAG,sBAAsB;EAEjF,MAAM,YAAY,mBAAmB,KAAK,SAAS;EACnD,IAAI,aAAa,WAAW,WAAW,OAAO;EAC9C,MAAM,UAAoB,CAAC;EAC3B,IAAI,KAAK,qBAAqB,KAAA,GAAW;GACvC,KAAK,sBAAsB,KAAK;GAChC,QAAQ,KAAK,qBAAqB;EACpC;EACA,IAAI,KAAK,mBAAmB,KAAA,GAAW;GACrC,KAAK,oBAAoB,KAAK;GAC9B,QAAQ,KAAK,mBAAmB;EAClC;EACA,IAAI,KAAK,gBAAgB,KAAA,GAAW;GAClC,KAAK,iBAAiB,KAAK;GAC3B,QAAQ,KAAK,gBAAgB;EAC/B;EACA,IAAI,KAAK,cAAc,KAAA,GAAW;GAChC,IAAI,YAAY,KAAK,CAAC,CAAC,iBAAiB,MAAM,KAAK,SAAS;GAC5D,QAAQ,KAAK,eAAe;EAC9B;EACA,IAAI,KAAK,oBAAoB,KAAA,GAAW;GACtC,KAAK,iBAAiB,KAAK;GAC3B,QAAQ,KAAK,gBAAgB;EAC/B;EACA,IAAI,WAAW;GACb,IAAI;IACF,KAAK,YAAY,UAAU;GAC7B,SAAS,OAAO;IACd,OAAO,YAAY,KAAK;GAC1B;GACA,QAAQ,KAAK,WAAW;EAC1B;EACA,OAAO;GAAE,IAAI,KAAK;GAAI;EAAQ;CAChC;AACF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"libraries.d.ts","names":[],"sources":["../../../src/tools/read/libraries.ts"],"mappings":";;cAca,eAAA;
|
|
1
|
+
{"version":3,"file":"libraries.d.ts","names":[],"sources":["../../../src/tools/read/libraries.ts"],"mappings":";;cAca,eAAA;cA8BA,wBAAA"}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { defineTool, toolNumber } from "../schema.js";
|
|
2
2
|
import { getComponentCatalog } from "../component-catalog.js";
|
|
3
|
-
import { isPlainObject } from "es-toolkit";
|
|
3
|
+
import { groupBy, isPlainObject } from "es-toolkit";
|
|
4
4
|
import * as v from "valibot";
|
|
5
5
|
//#region src/tools/read/libraries.ts
|
|
6
6
|
const variantValuesSchema = v.pipe(v.string(), v.parseJson(), v.check(isPlainObject, "Expected a JSON object"), v.record(v.string(), v.string()));
|
|
7
7
|
const listLibraries = defineTool({
|
|
8
8
|
name: "list_libraries",
|
|
9
|
-
description: "List available published component libraries.",
|
|
9
|
+
description: "List available published component libraries and each component asset key, name, and type. Pass a key to insert_library_component. A library does not have to be enabled to be listed.",
|
|
10
10
|
execution: {
|
|
11
11
|
kind: "async",
|
|
12
12
|
mutation: "none"
|
|
@@ -15,10 +15,22 @@ const listLibraries = defineTool({
|
|
|
15
15
|
input: v.strictObject({}),
|
|
16
16
|
execute: async (figma) => {
|
|
17
17
|
const catalog = getComponentCatalog(figma.graph);
|
|
18
|
-
|
|
18
|
+
if (!catalog) return {
|
|
19
|
+
count: 0,
|
|
20
|
+
libraries: []
|
|
21
|
+
};
|
|
22
|
+
const [libraries, components] = await Promise.all([catalog.listLibraries(), catalog.listComponents({ enabledOnly: false })]);
|
|
23
|
+
const byLibrary = groupBy(components, (component) => component.libraryId);
|
|
19
24
|
return {
|
|
20
25
|
count: libraries.length,
|
|
21
|
-
libraries
|
|
26
|
+
libraries: libraries.map((library) => ({
|
|
27
|
+
...library,
|
|
28
|
+
assets: (byLibrary[library.libraryId] ?? []).map((component) => ({
|
|
29
|
+
key: component.asset.key,
|
|
30
|
+
name: component.asset.name,
|
|
31
|
+
type: component.asset.type
|
|
32
|
+
}))
|
|
33
|
+
}))
|
|
22
34
|
};
|
|
23
35
|
}
|
|
24
36
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"libraries.js","names":[],"sources":["../../../src/tools/read/libraries.ts"],"sourcesContent":["import { isPlainObject } from 'es-toolkit'\nimport * as v from 'valibot'\n\nimport { getComponentCatalog } from '#core/tools/component-catalog'\nimport { toolNumber, defineTool } from '#core/tools/schema'\n\nconst variantValuesSchema = v.pipe(\n v.string(),\n v.parseJson(),\n // A record also takes an array, as its indices.\n v.check(isPlainObject, 'Expected a JSON object'),\n v.record(v.string(), v.string())\n)\n\nexport const listLibraries = defineTool({\n name: 'list_libraries',\n description
|
|
1
|
+
{"version":3,"file":"libraries.js","names":[],"sources":["../../../src/tools/read/libraries.ts"],"sourcesContent":["import { groupBy, isPlainObject } from 'es-toolkit'\nimport * as v from 'valibot'\n\nimport { getComponentCatalog } from '#core/tools/component-catalog'\nimport { toolNumber, defineTool } from '#core/tools/schema'\n\nconst variantValuesSchema = v.pipe(\n v.string(),\n v.parseJson(),\n // A record also takes an array, as its indices.\n v.check(isPlainObject, 'Expected a JSON object'),\n v.record(v.string(), v.string())\n)\n\nexport const listLibraries = defineTool({\n name: 'list_libraries',\n description:\n 'List available published component libraries and each component asset key, name, and type. Pass a key to insert_library_component. A library does not have to be enabled to be listed.',\n execution: { kind: 'async', mutation: 'none' },\n exposure: { webmcp: false },\n input: v.strictObject({}),\n execute: async (figma) => {\n const catalog = getComponentCatalog(figma.graph)\n if (!catalog) return { count: 0, libraries: [] }\n const [libraries, components] = await Promise.all([\n catalog.listLibraries(),\n // Enabled-only would hide the keys of a library the document has not turned on.\n catalog.listComponents({ enabledOnly: false })\n ])\n const byLibrary = groupBy(components, (component) => component.libraryId)\n return {\n count: libraries.length,\n libraries: libraries.map((library) => ({\n ...library,\n assets: (byLibrary[library.libraryId] ?? []).map((component) => ({\n key: component.asset.key,\n name: component.asset.name,\n type: component.asset.type\n }))\n }))\n }\n }\n})\n\nexport const insertLibraryComponent = defineTool({\n name: 'insert_library_component',\n description:\n 'Insert a reusable component from an enabled library by stable library and asset identity.',\n\n execution: { kind: 'async', mutation: 'document' },\n input: v.strictObject({\n library_id: v.pipe(v.string(), v.description('Library ID')),\n revision_id: v.optional(v.pipe(v.string(), v.description('Pinned revision ID'))),\n asset_key: v.pipe(v.string(), v.description('Stable asset key')),\n parent_id: v.optional(v.pipe(v.string(), v.description('Optional parent node ID'))),\n x: v.optional(toolNumber(v.pipe(v.number(), v.description('X position')))),\n y: v.optional(toolNumber(v.pipe(v.number(), v.description('Y position')))),\n variant_values: v.optional(\n v.pipe(v.string(), v.description('Optional JSON object of variant property values'))\n )\n }),\n execute: async (figma, args) => {\n const catalog = getComponentCatalog(figma.graph)\n if (!catalog) throw new Error('No component library catalog is configured')\n let variantValues: Record<string, string> | undefined\n if (args.variant_values) {\n const parsed = v.safeParse(variantValuesSchema, args.variant_values)\n if (!parsed.success) {\n throw new TypeError(\n `variant_values must be a JSON object of strings: ${v.summarize(parsed.issues)}`\n )\n }\n variantValues = parsed.output\n }\n return catalog.insertComponent({\n libraryId: args.library_id,\n revisionId: args.revision_id,\n assetKey: args.asset_key,\n parentId: args.parent_id,\n x: args.x,\n y: args.y,\n variantValues\n })\n }\n})\n"],"mappings":";;;;;AAMA,MAAM,sBAAsB,EAAE,KAC5B,EAAE,OAAO,GACT,EAAE,UAAU,GAEZ,EAAE,MAAM,eAAe,wBAAwB,GAC/C,EAAE,OAAO,EAAE,OAAO,GAAG,EAAE,OAAO,CAAC,CACjC;AAEA,MAAa,gBAAgB,WAAW;CACtC,MAAM;CACN,aACE;CACF,WAAW;EAAE,MAAM;EAAS,UAAU;CAAO;CAC7C,UAAU,EAAE,QAAQ,MAAM;CAC1B,OAAO,EAAE,aAAa,CAAC,CAAC;CACxB,SAAS,OAAO,UAAU;EACxB,MAAM,UAAU,oBAAoB,MAAM,KAAK;EAC/C,IAAI,CAAC,SAAS,OAAO;GAAE,OAAO;GAAG,WAAW,CAAC;EAAE;EAC/C,MAAM,CAAC,WAAW,cAAc,M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EAC1E,IAAI;EACJ,IAAI,KAAK,gBAAgB;GACvB,MAAM,SAAS,EAAE,UAAU,qBAAqB,KAAK,cAAc;GACnE,IAAI,CAAC,OAAO,SACV,MAAM,IAAI,UACR,oDAAoD,EAAE,UAAU,OAAO,MAAM,GAC/E;GAEF,gBAAgB,OAAO;EACzB;EACA,OAAO,QAAQ,gBAAgB;GAC7B,WAAW,KAAK;GAChB,YAAY,KAAK;GACjB,UAAU,KAAK;GACf,UAAU,KAAK;GACf,GAAG,KAAK;GACR,GAAG,KAAK;GACR;EACF,CAAC;CACH;AACF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pages.d.ts","names":[],"sources":["../../../src/tools/read/pages.ts"],"mappings":";;cAMa,WAAA;cAcA,YAAA;
|
|
1
|
+
{"version":3,"file":"pages.d.ts","names":[],"sources":["../../../src/tools/read/pages.ts"],"mappings":";;cAMa,WAAA;cAcA,YAAA;cAkBA,gBAAA;cAUA,YAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pages.js","names":[],"sources":["../../../src/tools/read/pages.ts"],"sourcesContent":["import * as v from 'valibot'\n\nimport { computeBounds } from '@pairdraw/scene-graph/geometry'\n\nimport { defineTool } from '#core/tools/schema'\n\nexport const listPages = defineTool({\n name: 'list_pages',\n description: 'List all pages in the document.',\n execution: { kind: 'sync', mutation: 'none' },\n input: v.strictObject({}),\n execute: (figma) => {\n const pages = figma.root.children\n return {\n current: figma.currentPage.name,\n pages: pages.map((page) => ({ id: page.id, name: page.name }))\n }\n }\n})\n\nexport const switchPage = defineTool({\n name: 'switch_page',\n\n description: 'Switch to a different page by name or ID.',\n execution: { kind: 'sync', mutation: 'view' },\n input: v.strictObject({\n page: v.pipe(v.string(), v.description('Page name or ID'))\n }),\n execute: (figma, { page }) => {\n const target =\n figma.root.children.find((candidate) => candidate.name === page) ?? figma.getNodeById(page)\n if (!target) return { error: `Page \"${page}\" not found` }\n figma.currentPage = target\n return { page: target.name, id: target.id }\n }\n})\n\nexport const getCurrentPage = defineTool({\n name: 'get_current_page',\n description: 'Get the current page name and ID.',\n execution: { kind: 'sync', mutation: 'none' },\n input: v.strictObject({}),\n execute: (figma) => {\n return { id: figma.currentPage.id, name: figma.currentPage.name }\n }\n})\n\nexport const pageBounds = defineTool({\n name: 'page_bounds',\n description: 'Get bounding box of all objects on the current page.',\n execution: { kind: 'sync', mutation: 'none' },\n exposure: { webmcp: false },\n input: v.strictObject({}),\n execute: (figma) => {\n return computeBounds(figma.currentPage.children.map((child) => child.absoluteBoundingBox))\n }\n})\n"],"mappings":";;;;AAMA,MAAa,YAAY,WAAW;CAClC,MAAM;CACN,aAAa;CACb,WAAW;EAAE,MAAM;EAAQ,UAAU;CAAO;CAC5C,OAAO,EAAE,aAAa,CAAC,CAAC;CACxB,UAAU,UAAU;EAClB,MAAM,QAAQ,MAAM,KAAK;EACzB,OAAO;GACL,SAAS,MAAM,YAAY;GAC3B,OAAO,MAAM,KAAK,UAAU;IAAE,IAAI,KAAK;IAAI,MAAM,KAAK;GAAK,EAAE;EAC/D;CACF;AACF,CAAC;AAED,MAAa,aAAa,WAAW;CACnC,MAAM;CAEN,aAAa;CACb,WAAW;EAAE,MAAM;EAAQ,UAAU;CAAO;CAC5C,OAAO,EAAE,aAAa,EACpB,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,YAAY,iBAAiB,CAAC,EAC3D,CAAC;CACD,UAAU,OAAO,EAAE,WAAW;EAC5B,MAAM,SACJ,MAAM,KAAK,SAAS,MAAM,cAAc,UAAU,SAAS,IAAI,KAAK,MAAM,YAAY,IAAI;EAC5F,IAAI,CAAC,QAAQ,OAAO,EAAE,OAAO,SAAS,KAAK,aAAa;
|
|
1
|
+
{"version":3,"file":"pages.js","names":[],"sources":["../../../src/tools/read/pages.ts"],"sourcesContent":["import * as v from 'valibot'\n\nimport { computeBounds } from '@pairdraw/scene-graph/geometry'\n\nimport { defineTool } from '#core/tools/schema'\n\nexport const listPages = defineTool({\n name: 'list_pages',\n description: 'List all pages in the document.',\n execution: { kind: 'sync', mutation: 'none' },\n input: v.strictObject({}),\n execute: (figma) => {\n const pages = figma.root.children\n return {\n current: figma.currentPage.name,\n pages: pages.map((page) => ({ id: page.id, name: page.name }))\n }\n }\n})\n\nexport const switchPage = defineTool({\n name: 'switch_page',\n\n description: 'Switch to a different page by name or ID.',\n execution: { kind: 'sync', mutation: 'view' },\n input: v.strictObject({\n page: v.pipe(v.string(), v.description('Page name or ID'))\n }),\n execute: (figma, { page }) => {\n const target =\n figma.root.children.find((candidate) => candidate.name === page) ?? figma.getNodeById(page)\n if (!target) return { error: `Page \"${page}\" not found` }\n // The host shows the page from here, and reads it back on the next call.\n figma.currentPage = target\n return { page: target.name, id: target.id }\n }\n})\n\nexport const getCurrentPage = defineTool({\n name: 'get_current_page',\n description: 'Get the current page name and ID.',\n execution: { kind: 'sync', mutation: 'none' },\n input: v.strictObject({}),\n execute: (figma) => {\n return { id: figma.currentPage.id, name: figma.currentPage.name }\n }\n})\n\nexport const pageBounds = defineTool({\n name: 'page_bounds',\n description: 'Get bounding box of all objects on the current page.',\n execution: { kind: 'sync', mutation: 'none' },\n exposure: { webmcp: false },\n input: v.strictObject({}),\n execute: (figma) => {\n return computeBounds(figma.currentPage.children.map((child) => child.absoluteBoundingBox))\n }\n})\n"],"mappings":";;;;AAMA,MAAa,YAAY,WAAW;CAClC,MAAM;CACN,aAAa;CACb,WAAW;EAAE,MAAM;EAAQ,UAAU;CAAO;CAC5C,OAAO,EAAE,aAAa,CAAC,CAAC;CACxB,UAAU,UAAU;EAClB,MAAM,QAAQ,MAAM,KAAK;EACzB,OAAO;GACL,SAAS,MAAM,YAAY;GAC3B,OAAO,MAAM,KAAK,UAAU;IAAE,IAAI,KAAK;IAAI,MAAM,KAAK;GAAK,EAAE;EAC/D;CACF;AACF,CAAC;AAED,MAAa,aAAa,WAAW;CACnC,MAAM;CAEN,aAAa;CACb,WAAW;EAAE,MAAM;EAAQ,UAAU;CAAO;CAC5C,OAAO,EAAE,aAAa,EACpB,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,EAAE,YAAY,iBAAiB,CAAC,EAC3D,CAAC;CACD,UAAU,OAAO,EAAE,WAAW;EAC5B,MAAM,SACJ,MAAM,KAAK,SAAS,MAAM,cAAc,UAAU,SAAS,IAAI,KAAK,MAAM,YAAY,IAAI;EAC5F,IAAI,CAAC,QAAQ,OAAO,EAAE,OAAO,SAAS,KAAK,aAAa;EAExD,MAAM,cAAc;EACpB,OAAO;GAAE,MAAM,OAAO;GAAM,IAAI,OAAO;EAAG;CAC5C;AACF,CAAC;AAED,MAAa,iBAAiB,WAAW;CACvC,MAAM;CACN,aAAa;CACb,WAAW;EAAE,MAAM;EAAQ,UAAU;CAAO;CAC5C,OAAO,EAAE,aAAa,CAAC,CAAC;CACxB,UAAU,UAAU;EAClB,OAAO;GAAE,IAAI,MAAM,YAAY;GAAI,MAAM,MAAM,YAAY;EAAK;CAClE;AACF,CAAC;AAED,MAAa,aAAa,WAAW;CACnC,MAAM;CACN,aAAa;CACb,WAAW;EAAE,MAAM;EAAQ,UAAU;CAAO;CAC5C,UAAU,EAAE,QAAQ,MAAM;CAC1B,OAAO,EAAE,aAAa,CAAC,CAAC;CACxB,UAAU,UAAU;EAClB,OAAO,cAAc,MAAM,YAAY,SAAS,KAAK,UAAU,MAAM,mBAAmB,CAAC;CAC3F;AACF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extended.d.ts","names":[],"sources":["../../../src/tools/registry/extended.ts"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"extended.d.ts","names":[],"sources":["../../../src/tools/registry/extended.ts"],"mappings":";;;;;;cAkIa,gBAAgB"}
|
|
@@ -11,7 +11,7 @@ import { designToComponentMap } from "../codegen/component-map.js";
|
|
|
11
11
|
import { addComment, getComments, replyToComment, resolveComment } from "../comments.js";
|
|
12
12
|
import { createPage, createShape, createSlice } from "../create/basic.js";
|
|
13
13
|
import { createSlot, getBehaviour, setBehaviour } from "../create/behaviours.js";
|
|
14
|
-
import { combineAsVariants, createComponent, createInstance, exposeInstanceSwap } from "../create/components.js";
|
|
14
|
+
import { combineAsVariants, createComponent, createInstance, detachInstance, exposeInstanceSwap } from "../create/components.js";
|
|
15
15
|
import { fetchIconsTool, insertIcon, searchIconsTool } from "../create/icons.js";
|
|
16
16
|
import { script } from "../create/script.js";
|
|
17
17
|
import { importMermaid } from "../create/mermaid.js";
|
|
@@ -86,6 +86,7 @@ const EXTENDED_TOOLS = [
|
|
|
86
86
|
combineAsVariants,
|
|
87
87
|
createComponent,
|
|
88
88
|
createInstance,
|
|
89
|
+
detachInstance,
|
|
89
90
|
exposeInstanceSwap,
|
|
90
91
|
createSlot,
|
|
91
92
|
getBehaviour,
|