编程语言/sketch:修订间差异
来自康健生活
小 文字替换 -“{{leftrightflex|”替换为“{{Double-columns|” |
|||
| (未显示同一用户的19个中间版本) | |||
| 第1行: | 第1行: | ||
== 引言 == | == 引言 == | ||
一个偶然的机会,开始接触设计软件的插件开发,在此记录 sketch 软件的插件开发过程。{{Timepoint|timepoint=2021-6-23 4:47 pm}} | 一个偶然的机会,开始接触设计软件的插件开发,在此记录 sketch 软件的插件开发过程。{{Timepoint|timepoint=2021-6-23 4:47 pm}} | ||
== API == | |||
<blockquote><pre> | |||
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 | |||
</pre></blockquote> | |||
== 插件 == | == 插件 == | ||
| 第96行: | 第125行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|} | |} | ||
=== html === | |||
==== iframe ==== | |||
在 sketch 的 iframe 中,由于使用 iframe 并不能获取其 contentWindow,这与 photoshop 有着极大的区别。 | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
console.log(iframe.contentDocument) | |||
console.log(iframe.contentWindow.document) | |||
</syntaxhighlight>|{{consoleout|<pre>Blocked a frame with origin "null" from accessing a frame with origin "https://login.ops.qihoo.net:4430". The frame requesting access has a protocol of "file", the frame being accessed has a protocol of "https". Protocols must match. | |||
null | |||
SecurityError: Blocked a frame with origin "null" from accessing a cross-origin frame. Protocols, domains, and ports must match. | |||
Error: A cross-origin error was thrown. React doesn't have access to the actual error object in development. See https://fb.me/react-crossorigin-error for more information. | |||
</pre>}}}} | |||
所以需要调整策略,改用 postmessage。 | |||
== 调试 == | == 调试 == | ||
[https://github.com/skpm/sketch-dev-tools sketch-dev-tools] 用于调试 sketch。 | [https://github.com/skpm/sketch-dev-tools sketch-dev-tools] 用于调试 sketch。 | ||
调试日志的位置在:<code>~/Library/Logs/com.bohemiancoding.sketch3/Plugin Log.log</code> | 调试日志的位置在:<code>~/Library/Logs/com.bohemiancoding.sketch3/Plugin Log.log</code> | ||
更方便的做法是打开控制台,在日志报告的选项卡里我们可以直接找到 <code>Plugin Log.log</code> 这个文件。里面记录了在 sketch 插件运行环境的所有 <code>console</code> 的输出。 | |||
<div class="pageBoxGreen">这里需要注意的是 sketch 插件运行环境,也就是 cocoascript 环境,我的理解是是一种 js 的变体 dsl。</div> | |||
以上就是在 sketch 插件运行环境的 <code>console</code> 输出获取方式。ui 层的 <code>console</code> 输出获取可以直接在插件上点击右键审视元素,打开浏览器的开发者工具,里面有你所有想要的~~。 | |||
=== 联调 === | === 联调 === | ||
为了方便,我单独设置了 <code>symbolic link</code> 在 npm 里面。 | 为了方便,我单独设置了 <code>symbolic link</code> 在 npm 里面。 | ||
| 第121行: | 第169行: | ||
== 项目结构 == | == 项目结构 == | ||
要扩展插件,所以我重构了一下项目的结构,现如下(重构中,并不是最终结果)。 | 要扩展插件,所以我重构了一下项目的结构,现如下(重构中,并不是最终结果)。 | ||
{{ | {{Double-columns|<pre> | ||
. | . | ||
├── assets | ├── assets | ||
| 第175行: | 第223行: | ||
│ │ │ └── uuap.ts | │ │ │ └── uuap.ts | ||
│ │ ├── extend | │ │ ├── extend | ||
│ │ │ | │ │ │ └── ajax.ts | ||
│ │ ├── page | │ │ ├── page | ||
│ │ │ ├── error | │ │ │ ├── error | ||
| 第253行: | 第300行: | ||
</body> | </body> | ||
...</syntaxhighlight> | ...</syntaxhighlight> | ||
}}</blockquote> | }} | ||
简单示意图。 | |||
[[文件:Sketch-plugin-design-1.png|600px|无框|无|Sketch-plugin-design-1]] | |||
</blockquote> | |||
}} | }} | ||
== sketchObject == | |||
[[/sketchObject|sketchObject]] | |||
== other == | == other == | ||
| 第263行: | 第316行: | ||
| sketch-dev-tools || 示例 || 示例 | | sketch-dev-tools || 示例 || 示例 | ||
|} | |} | ||
=== sharedStyleId === | |||
<syntaxhighlight lang="javascript"> | |||
const outOfSync = !!layer.sharedStyleId && layer.style.isOutOfSyncWithSharedStyle(layer.sharedStyle)</syntaxhighlight> | |||
=== symbolId === | |||
获取 MSSymbolMaster。[[/MSSymbolMaster|MSSymbolMaster]] 的示例。 | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
const document = context.document; | |||
log(document.documentData().symbolWithID(layer?.symbolId));</syntaxhighlight>|{{consoleout|<pre> | |||
<MSSymbolMaster: 0x7fe41b62d400> icon_using document (3C94663C-B213-4FCD-A7CB-43A6AA877859) | |||
</pre>}}}} | |||
获取 symbol 样式。 | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
if (layer?.symbolId) { | |||
const document = context.document; | |||
const immutableMaster = document.documentData().symbolWithID(layer?.symbolId); | |||
const layerItem = sketch.fromNative(immutableMaster); | |||
log(layerItem); | |||
}</syntaxhighlight>|{{consoleout|<pre> | |||
if (layer?.symbolId) { | |||
const document = context.document; | |||
const immutableMaster = document.documentData().symbolWithID(layer?.symbolId); | |||
const layerItem = sketch.fromNative(immutableMaster); | |||
log(layerItem); | |||
} | |||
... | |||
type = Style; | |||
verticalAlignment = "<MOUndefined: 0x6000025b8da0>"; | |||
}; | |||
symbolId = "9AD4358B-C5B7-4D59-BBA3-D451D757685E"; | |||
transform = { | |||
}; | |||
type = SymbolMaster; | |||
} | |||
</pre>}}}} | |||
=== MSImmutableSymbolMaster === | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
const document = context.document; | |||
const immutableMaster = document.documentData().symbolWithID(layer?.symbolId).immutableModelObject(); | |||
log(immutableMaster)</syntaxhighlight>|{{consoleout|<pre> | |||
MSImmutableSymbolMaster: 0x7fe942b0d7d0> icon_using document (3C94663C-B213-4FCD-A7CB-43A6AA877859) | |||
</pre>}}}} | |||
== 示例 == | == 示例 == | ||
| 第274行: | 第371行: | ||
* https://developer.sketch.com/plugins/debugging/ | * https://developer.sketch.com/plugins/debugging/ | ||
* https://www.sketch.com/extensions/plugins/ | * https://www.sketch.com/extensions/plugins/ | ||
* https://sketchplugins.com/ | |||
* https://github.com/sketch-hq/sketch-document/ | |||
* https://github.com/sketch-hq/SketchAPI | |||