编程语言/sketch/getSelectedDocument:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
第135行: 第135行:
symbol.name 图标/业务图标/应用图标/定位
symbol.name 图标/业务图标/应用图标/定位
symbol.name 图标/业务图标/设备图标/智能机器人
symbol.name 图标/业务图标/设备图标/智能机器人
</pre>}}}}
== 选中画板(图层) ==
在 sketch 中,选中画板和在 photoshop 中选中图层的逻辑是一样的,单选中一个图层,则命中当前选中图层的信息,但如果是连选(按住 shift 选中),则命中父级图层及其之间的所有子图层。
[[文件:Sketch-selectedmorelayers.png|600px|无框|无|sketch-selectedmorelayers]]
{{leftrightflex|<syntaxhighlight lang="javascript">
/**
* 获取用户选择的画板(图层)
*/
export function getSelectedArtboards(): Artboard[] {
  const document = sketch.getSelectedDocument();
  if (!document) return [];
  const { selectedLayers } = document;
  const artboards = selectedLayers.layers
    .filter(isArtboard)
    .map(layer => layer as Artboard);
  if (artboards.length) return artboards;
  // 如果只是选中了画板中的图层,则获取所属画板
  const parentArtboards = selectedLayers
    .map(layer => layer.getParentArtboard?.())
    .filter(Boolean);
  return [...new Set(parentArtboards as Artboard[])];
}</syntaxhighlight>|{{consoleout|<pre>[ Artboard {
    type: 'Artboard',
    id: 'BAEC35CC-E9EB-4C4B-95A0-CD8885EC4000',
    frame: { x: -3, y: 859, width: 1440, height: 1726 },
    name: '1.2专题2-编辑任务',
    selected: true,
    exportFormats: [ [ExportFormat] ],
    sharedStyleId: null,
    layers:
    [ [ShapePath],
      [Group],
      [Group],
      [Group],
      [Group],
      [Group],
      [Group],
      [Group],
      [Group],
      [ShapePath],
      [Group],
      [Group],
      [Group],
      [Group],
      [Group],
      [Group],
      [Group],
      [Group],
      [Group] ],
    flowStartPoint: false,
    background: { enabled: true, includedInExport: true, color: '#e5ebf1ff' } },
  Artboard {
    type: 'Artboard',
    id: 'FBF92504-7CB9-4B33-AC97-DA1A4112BFE3',
    frame: { x: -3, y: -883, width: 1440, height: 1540 },
    name: '1.1专题1-创建任务',
    selected: true,
    exportFormats: [ [ExportFormat] ],
    sharedStyleId: null,
    layers: [ [Group], [Group], [Group], [Group], [Group] ],
    flowStartPoint: false,
    background: { enabled: true, includedInExport: true, color: '#ffffffff' } } ]
</pre>}}}}
</pre>}}}}

2021年6月27日 (日) 08:25的版本

引言

初始化获取

执行方法。 获取当前选中的 Sketch 文件 模板:Leftrightflex 其中 swatches、sharedLayerStyles、sharedTextStyles 分别是控件中的颜色、图层、字符的内容:

Sketch-getSelectedDocument打印的内容
Sketch-getSelectedDocument打印的内容

控件

如果想要获取组件中具体的控件信息,需要进一步处理。 模板:Leftrightflex

选中画板(图层)

在 sketch 中,选中画板和在 photoshop 中选中图层的逻辑是一样的,单选中一个图层,则命中当前选中图层的信息,但如果是连选(按住 shift 选中),则命中父级图层及其之间的所有子图层。

sketch-selectedmorelayers
sketch-selectedmorelayers

模板:Leftrightflex