脚本函数
画布脚本是传给 canvases.execute 的同步 JavaScript 字符串。全局 document 的值是 "document",作为顶层容器;读写在同一次调用中立即可见。脚本没有 fetch、定时器、文件 API 或模块导入,网络和文件操作放在外层 codemode 程序。
函数参考
Node 是节点参考中的对象;VariableSet 是变量名到 {type,value} 的映射。所有函数名区分大小写。返回 void 的函数在脚本中得到 undefined;execute 的外层结果不采用脚本最后一个表达式,用 Print 输出。
Insert
Insert(parent: string, node: Node): string
在 document 或 frame/group 尾部插入节点树,递归分配新 ID,返回根 ID。node.type 必需;负数尺寸及百分比/viewport/calc 尺寸拒绝。不可向实例后代插入。
Copy
Copy(path: string, parent: string, overrides?: object): string
组件源创建 ref;普通源深拷贝。overrides 合并根属性,descendants 可按唯一名称或源 ID 覆盖/替换。返回新 ID;不支持复制实例子路径。
Update
Update(path: string, patch: object): void
浅合并普通节点属性;null 删除属性。id/type/ref 不可修改,children 不可传入。实例子路径合并到实例的 descendants 中;ref 根的 descendants 与已有键合并,同一键的新对象替换旧对象。
Replace
Replace(path: string, node: Node): string
普通节点删除整棵旧树,在原父级/顺序插入新树并返回新 ID,计为两条操作。实例子路径改成 descendants 替换,计一条操作;替换节点以新 ID 单独寻址。
Move
Move(path: string, parent?: string, index?: number): void
parent 省略保留父级;index 为移除自身后的兄弟序列的 0 起点位置,省略移到尾部,越界夹到有效范围。拒绝循环与实例子路径。
Delete
Delete(path: string): void
删除节点及全部后代;document 受保护。不能删除实例后代,改用 Update(path,{enabled:false}) 隐藏。
SetVariables
SetVariables(variables: VariableSet, replace?: boolean): void
默认按变量名合并,replace:true 替换整个变量集合;已有主题轴值仍保留。变量名不可带 $,每个定义需有 type/value;主题轴从 value 数组收集。
GetVariables
GetVariables(): {variables: VariableSet, themes: object}
返回原始变量定义与主题轴,不是当前主题下解析后的标量。
Get
Get(path?: string, visitor?: function, options?: object): Node / unknown[]
无 visitor 时必须指定单个节点,返回子树;全 document 必须用 visitor。参数依类型识别,可省略 path 对全图遍历。详见下节。
FindEmptySpace
FindEmptySpace({width, height, direction?, padding?, nodeId?}): {x:number,y:number}
基于顶层节点并集或 nodeId 的世界坐标边界,在指定方向返回位置。direction 默认 right,padding 默认 0;没有节点时 {x:0,y:0}。不是递归空洞搜索;width/height 用于向左/上偏移。
Print
Print(...values: unknown[]): void
字符串原样输出,其余 JSON 序列化并以空格连接,进入结果 prints;累计上限 65536,超出抛错。
TakeScreenshot
TakeScreenshot(nodeIds: string[]): void
登记最多 4 个 ID,超出的静默忽略,不去重也不渲染。execute 结果 screenshots 中的 ID 传给 images.list。
Export 和 Generate 虽然有全局名字,调用会报不可用;没有导出 .pen 或生成图片的脚本函数。
Get 的 visitor
Get(path, visitor, options) 深度优先、先访问父节点。visitor 非 undefined 返回值按顺序组成数组;没有返回值的节点不进入结果数组。节点对象是浅层快照,不带 children,修改它不会写入画布;用 Update 修改。
| options | 默认 | 行为 |
|---|---|---|
| depth | 不限 | 根为 0,限制后代层级;无 visitor 时超过深度的 children 显示为 ”…” |
| resolveInstances | false | visitor 中 ref 默认是叶节点;true 展开实例后代 |
| resolveVariables | false | 原始单节点读取默认保留引用;true 走展开读取 |
| includePathGeometry | 未设置 | 当前实现不按此值过滤 geometry,不依赖它减少结果 |
visitor 总是使用展开后的节点和已解析的变量;resolveVariables:false 不会让 visitor 还原原始变量引用。单节点读取走展开路径时(resolveInstances/resolveVariables 为 true 或实例子路径),同样返回已解析值;没有独立的两阶段解析开关。
| ctx 字段 | 内容 |
|---|---|
| node | 与 visitor 第一个参数相同 |
| parentCtx | 所访问父节点的 ctx;遍历根为 undefined |
| depth | 从本次遍历根起算的深度 |
| index | 在当前访问兄弟中的索引;每个顶层根为 0 |
| bounds | 按需计算的 {x,y,width,height};相对父节点,不是世界坐标 |
| problems | “partially clipped”、“fully clipped” 或 undefined;不含 FIT_FILL_CYCLE |
| skipChildren() | 跳过当前节点的后代 |
Get(document) 没有 visitor 会失败。读取整个画布要用 Get(document, visitor) 或 Get(visitor)。实例中的节点 ID 是路径,深层实例包含多个 /。对实例替换节点的新 ID,visitor 的 bounds 当前可能是全 0;不要把它当作已存储的独立节点 ID 传给 nodes.list。
下面脚本执行增、拷贝、改、移、替换、删,并读取边界。Area 是 320 × 160,Copied 是 80 × 32,Changed 是 40 × 40;空位在 {x:340,y:0}。
const area = Insert(document, { type: "frame", name: "Area", layout: "none", width: 320, height: 160, fill: "#FFFFFF"});const original = Insert(area, { type: "rectangle", name: "Original", x: 16, y: 16, width: 64, height: 32, fill: "#3366FF"});const copied = Copy(original, area, { name: "Copied", x: 96 });Update(copied, { width: 80 });Move(copied, area, 0);const changed = Replace(original, { type: "ellipse", name: "Changed", x: 16, y: 64, width: 40, height: 40, fill: "#00AA66"});const temporary = Insert(area, { type: "rectangle", width: 1, height: 1 });Delete(temporary);Get(area, (node, ctx) => { if (ctx.problems) Print(node.id, ctx.problems); return { id: node.id, bounds: ctx.bounds, depth: ctx.depth, index: ctx.index };}).forEach(item => Print(item));Print(FindEmptySpace({ width: 100, height: 80, direction: "right", padding: 20 }));TakeScreenshot([area]);跨调用变量
同一画布的成功调用会保存全局对象属性,下一次调用恢复。可保存能 JSON 往返的 null、字符串、有限数字、布尔值、普通对象和任意嵌套数组。函数、undefined、Symbol、BigInt、循环引用和内置对象(如 Date、Map)不会保留;对象或数组内包含这些值也不会保留。每个变量序列化后最多 256 KiB,所有变量保存后的 JSON(含变量名)合计最多 1 MiB,按 UTF-8 字节计数。值本身是函数的全局变量会静默跳过,不产生 warning;函数不会保留,每次 execute 需要重新定义。其余不保留的变量会出现在 warnings 中,code 为 GLOBAL_NOT_KEPT,message 包含变量名与原因;响应的 globals 只列出实际保存的名字。未声明的赋值(如 savedFrame = ...)、var 或 globalThis 属性可保留;顶层 const/let 是本次调用的局部绑定,不会保留。失败不保存新变量。
第一次调用:
savedFrame = Insert(document, { type: "frame", name: "Saved", width: 200, height: 80, fill: "#FFFFFF"});var savedCount = 1;const localOnly = "not persisted";Get(savedFrame, (node, ctx) => Print(node.name, ctx.bounds));同一画布的下一次调用(输出 2 undefined,宽度改成 240):
savedCount += 1;Update(savedFrame, { width: 240 });Print(savedCount, typeof localOnly);Get(savedFrame, (node, ctx) => Print(node.name, ctx.bounds));回滚、修补与限制
未捕获的异常或操作拒绝会使整次 execute 回滚,包括之前的写入与全局变量。错误响应为 {error:{code,message,details}};脚本错误 details 含 edit_id,可包含 line、column、rejection。单个操作拒绝可抛出带 rejection 的异常;不要吞掉异常后误以为整次仍失败。
失败后十分钟内,用 edit_id 和 edits 重新执行修好的完整脚本。edits 是按顺序应用的 {find,replace,all?} 文本替换:默认 all:false 要求 find 在当时的源码中恰好出现一次;未找到或多次匹配都报 SCRIPT_ERROR。all:true 替换所有匹配,零匹配仍失败。修补后的脚本从头运行,不会接着失败位置执行。edit_id 未知或过期则发送 corrected script。
调用形状(failed_edit_id 是本次错误返回的值,先用 describe 确认 SDK 抛错对象如何暴露响应):
{ "edit_id": "failed_edit_id", "edits": [{ "find": "fontSize: 320", "replace": "fontSize: 32" }]}每次最多录制 5000 条操作,Print 累计 65536 个 JavaScript 字符单位,TakeScreenshot 最多 4 个 ID。脚本大小上限 65536 个 JavaScript 字符单位,VM 内存 32 MiB、栈 1 MiB、中断轮询最多 20000 次;字符计数不等于 UTF-8 字节。完整错误及重试规则见限制与错误。