编程语言/sketch:修订间差异
小 文字替换 -“{{Leftrightflex|”替换为“{{Double-columns|” |
|||
| 第169行: | 第169行: | ||
== 项目结构 == | == 项目结构 == | ||
要扩展插件,所以我重构了一下项目的结构,现如下(重构中,并不是最终结果)。 | 要扩展插件,所以我重构了一下项目的结构,现如下(重构中,并不是最终结果)。 | ||
{{ | {{Double-columns|<pre> | ||
. | . | ||
├── assets | ├── assets | ||
2026年11月26日 (四) 07:47的版本
引言
一个偶然的机会,开始接触设计软件的插件开发,在此记录 sketch 软件的插件开发过程。 2021-6-23 4:47 pm
API
Library: Library Style: Style Fill: Fill ImageData: ImageData Image: Image Shadow: Shadow Document: Document SharedStyle: SharedStyle Override: Override Group: Group Artboard: Artboard Border: Border BorderOptions: BorderOptions Shape: Shape Blur: Blur Gradient: Gradient SymbolMaster: SymbolMaster DataOverride: DataOverride Text: Text HotSpot: HotSpot Page: Page ImportableObject: ImportableObject Flow: Flow SymbolInstance: SymbolInstance ShapePath: ShapePath GradientStop: GradientStop
插件
在开发这个插件之前,我第一次知道有 sketch 这个软件,也才知道时间荏苒,设计同学们已经从 photoshop 过渡到了 sketch 和 figma。单纯从设计美学等角度上来说,我区分不出几款软件的差异,但这并不妨碍我们从软件层次去“解读” sketch 的部分设计思想。
从能够了解到的途径,sketch 开发有两种方式,一是 JavaScript 与 cocoascript 混合开发模式,二是 objective-c 或 swift 搭配 appkit。两种方式各有优异,大多数互联网前端与设计团队开发的相关的插件多用混合模式,以此降低难度并快速产出。在我过去的几年里,缺少移动端经历一直是我的短板,经此反而初窥到移动开发的一点理念,从本质上讲,各母体软件与子体插件之间关联的关系与通信,有些类似与移动硬件与移动软件之间的原型。
标准文件和工具
在开发前需要准备了解一些必备的内容,比如 skpm 和 manifest.json 等。
skpm
/home/kangkk
npm install -g skpm
skpm 是 sketch 官方提供的插件管理工具,类似于 npm 等。
manifest.json
清单 manifest.json 提供集成到 sketch 菜单栏中的信息。示例。
{
"$schema": "https://raw.githubusercontent.com/sketch-hq/SketchAPI/develop/docs/sketch-plugin-manifest-schema.json",
"icon": "icon.png",
"appcast": "<网址>/appcast.xml",
"description": "<描述>",
"compatibleVersion": "<版本>",
"commands": [
{
"name": "<菜单标签卡选项名称>",
"identifier": "medici.openPlugin",
"script": "<引入 js 文件入口的相对地址>",
"shortcut": "command u",
"handler": "openPlugin"
}
],
"menu": {
"title": "☁️设计云",
"items": [
"medici.openPlugin"
]
}
}
sketch-module-web-view
加载网页
Symbolic Link
sketch/dom
import sketch from "sketch/dom";
| getSelectedDocument | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
webview
/home/kangkk
npm install sketch-module-web-view --save-dev
通信
就像 photoshop 一样,ui 界面端通信 sketch 运行环境,需要使用 window.postMessage 方法传递数据。然后 sketch 运行环境通过 webViewContents.on 监听并接收数据。
| ui 界面端(发送数据) | sketch 运行环境(接收数据) |
|---|---|
window.postMessage('onMyMessage', 'item');
window.postMessage('onMyMessage', {
foo: 'item'
});
window.postMessage('onMyMessage', 'item', 'foo', 'biz');
|
webview.webContents.on('onMyMessage', item => {
Settings.setSettingForKey('key', JSON.stringify(item));
}
|
如果想在 sketch 运行环境中向 ui 界面端传递单向数据,也可使用 browserWindow.webContents.executeJavaScript 方法。executeJavaScript 方法可以参考 photoshop 中的 cs.evalScript 方法。大致上是相同的,区别在于 cs.evalScript 在是 ui 界面端 执行 photoshop 运行环境中的方法,而 browserWindow.webContents.executeJavaScript 是反过来的?
| ui 界面端(接收数据) | sketch 运行环境(发送数据) |
|---|---|
window.somefunc = function(arg) {
console.log(arg)
}
|
browserWindow.webContents.executeJavaScript('somefunc("hello")').then(res => {
// TODO
});
browserWindow.webContents.executeJavaScript(
`localStorage.setItem("${key}",'${value}')`
);
|
html
iframe
在 sketch 的 iframe 中,由于使用 iframe 并不能获取其 contentWindow,这与 photoshop 有着极大的区别。 模板:Leftrightflex
所以需要调整策略,改用 postmessage。
调试
sketch-dev-tools 用于调试 sketch。
调试日志的位置在:~/Library/Logs/com.bohemiancoding.sketch3/Plugin Log.log
更方便的做法是打开控制台,在日志报告的选项卡里我们可以直接找到 Plugin Log.log 这个文件。里面记录了在 sketch 插件运行环境的所有 console 的输出。
以上就是在 sketch 插件运行环境的 console 输出获取方式。ui 层的 console 输出获取可以直接在插件上点击右键审视元素,打开浏览器的开发者工具,里面有你所有想要的~~。
联调
为了方便,我单独设置了 symbolic link 在 npm 里面。
...
"scripts": {
...
"link": "skpm-link",
...
},
...
这样虽然多出来了一步操作,但是更方便我清晰知道哪一步的作用是什么,当执行 npm run link 之后,会在 sketch 的插件文件夹内生成一个被命名为插件名称的文件夹,文件夹内是一个项目内 npm start 构建出来的 sketch 插件的软连接。

cocoascript
cocoascript 环境。
编辑器
通知 sketch 使用一个编辑器编辑插件。
/home/kangkk
defaults write ~/Library/Preferences/com.bohemiancoding.sketch3.plist "Plugin Editor" "/usr/local/bin/atom"
项目结构
要扩展插件,所以我重构了一下项目的结构,现如下(重构中,并不是最终结果)。
. ├── assets │ ├── icon.png │ └── question.png ├── package.json ├── package-lock.json ├── README.md ├── scripts │ ├── build-test.js │ └── update-version.js ├── src │ ├── api │ │ ├── action │ │ │ ├── artboard.ts │ │ │ ├── assist.ts │ │ │ ├── cocoa.ts │ │ │ ├── config.ts │ │ │ ├── document.ts │ │ │ ├── export.ts │ │ │ ├── file.ts │ │ │ ├── info.ts │ │ │ ├── layer.ts │ │ │ ├── page.ts │ │ │ ├── slice.ts │ │ │ └── type.ts │ │ ├── communication │ │ │ ├── login │ │ │ │ ├── receive.ts │ │ │ │ └── send.ts │ │ │ └── plugin │ │ │ ├── receive.ts │ │ │ └── send.ts │ │ ├── controller │ │ │ ├── feedback │ │ │ │ └── feedback.ts │ │ │ ├── login │ │ │ │ └── index.ts │ │ │ └── plugin │ │ │ └── index.ts │ │ ├── extend │ │ │ └── cache.ts │ │ └── store │ │ ├── index.ts │ │ └── token.ts │ ├── client │ │ ├── communication │ │ │ └── plugin │ │ │ ├── receive.js │ │ │ └── send.ts │ │ ├── config │ │ │ ├── http.ts │ │ │ └── uuap.ts │ │ ├── extend │ │ │ └── ajax.ts │ │ ├── page │ │ │ ├── error │ │ │ │ ├── noNet.tsx │ │ │ │ └── type.ts │ │ │ ├── feedback │ │ │ │ └── feedback.ts │ │ │ ├── login │ │ │ │ └── index.ts │ │ │ └── plugin │ │ │ ├── index.tsx │ │ │ ├── root.tsx │ │ │ ├── style.less │ │ │ └── type.ts │ │ ├── service │ │ │ ├── artboard.ts │ │ │ ├── group.ts │ │ │ ├── project.ts │ │ │ ├── team.ts │ │ │ └── type.ts │ │ ├── store │ │ │ ├── index.ts │ │ │ └── token.ts │ │ ├── template │ │ │ ├── feedback │ │ │ │ └── index.html │ │ │ └── plugin │ │ │ └── index.html │ │ └── util │ │ ├── ajax.ts │ │ ├── file.ts │ │ └── type.ts │ ├── manifest.json │ └── types │ ├── sketch-module-web-view.d.ts │ ├── skpm-dialog.d.ts │ ├── skpm-fs.d.ts │ ├── skpm-path.d.ts │ └── window.d.ts ├── tsconfig.json ├── webpack.skpm.config.js └── yarn.lock
我将运行环境和 UI 分开,模拟前后端的概念,当插件加载的时候,会先寻找
package.json中的skpm.resources参数,打包的是模拟出来的前端client代码。... "skpm": { "name": "360设计云", "manifest": "src/manifest.json", "main": "medici.sketchplugin", "assets": [ "assets/**/*" ], "resources": [ "src/client/communication/plugin/receive.js" ] }, ...后端加载则通过打包
src/manifest.json文件中的commands.script,并调用commands.script引入的文件中的钩子函数commands.handler。... "commands": [ { "name": "上传画板", "identifier": "medici.openPlugin", "script": "./api/controller/plugin/index.ts", "shortcut": "command u", "handler": "openPlugin" } ], ...前后数据交互通过
communication文件夹内的文件进行。
在这个项目中,UI 层使用的是封装的 webview,这里有一个需要注意的地方是 webview 引入了页面模板(不管是 HTML 还是 EJS,或者其他),在模板中引入 JS 文件时,使用的是打包生成后的文件在 sketch 插件包中的相对路径。并且引入文件格式是.js,示例如下。src/client/template/plugin/index.html... <!-- 寻址路径 --> <script src="../src_client_communication_plugin_receive.js" ></script> </body> ...简单示意图。
Sketch-plugin-design-1
sketchObject
other
| roadhog | cross-env | concurrently |
| sketch-dev-tools | 示例 | 示例 |
sharedStyleId
const outOfSync = !!layer.sharedStyleId && layer.style.isOutOfSyncWithSharedStyle(layer.sharedStyle)
symbolId
获取 MSSymbolMaster。MSSymbolMaster 的示例。 模板:Leftrightflex
获取 symbol 样式。 模板:Leftrightflex
MSImmutableSymbolMaster
示例
| MagicMirror | Content-generator-sketch-plugin | sketch-measure |
参考
No categories
