📙笔记 - 使用 Figma 的插件 Scripter 自动化完成任务(一)

用户头像
深圳/设计爱好者/5年前/540浏览
📙笔记 - 使用 Figma 的插件 Scripter 自动化完成任务(一)

读了关于使用 Scripter 完成自动化操作的文章,必须写点东西消化一下。

读了关于使用 Scripter 完成自动化操作的文章,必须写点东西消化一下。翻译的那篇文章,我亲自试验了一下。对代码不了解,不知道是不是版本的问题,有些测试不出来。因为那篇文章写的时候都是很早之前的版本了。不过其实有些操作可以找到相应的插件完成,而且更加方便和可视化。你也可以理解为装了 Scripter 就可以不要少装几个插件……当然 Figma 的插件管理比其他软件好太多了,所以根本不用在乎插件的数量。

下面一步步来:(防杠:gif 图有些颜色识别不出来,见谅。)

生成颜色样式的描述:

生成16进制的代码,方便设计人员查看和复制。不一定是必须的,但现实情况复杂多样包括跨软件操作,所以增加一点便利也是可以考虑的。

1.直接生成颜色样式和描述。

选择有填充色的元素,需要命名好元素。执行脚本,如下。创建 Design system 的时候非常有用。


Create Fill Styles from Selected Layers

for (let node of figma.currentPage.selection) {
  let fill = (node as GeometryMixin).fills[0];
  if (fill.type === 'SOLID') {
    let name = node.name;
    let description = '#' + Object.values(fill.color).map((component: number) => {
      let hex: string = Math.round(component * 255).toString(16).toUpperCase();
      return (hex.length === 1 ? '0' : '') + hex;
    }).join('');
    if (fill.opacity !== 1) {
      description += ' ' + Math.round(fill.opacity * 100) + '%';
    }
    let style = figma.createPaintStyle();
    style.name = name;
    style.description = description;
    style.paints = [fill];
    (node as GeometryMixin).fillStyleId = style.id;
  }


2.已有颜色样式,缺少描述。

已有颜色样式,缺少描述的情况下,如果你一个个点开输入很费时间,使用脚本执行补救行为就变得非常必要。无需选择任何元素,执行脚本。如下。


Generates style descriptions for each color style in the current Figma file.

// Get the list of color styles in the current Figma file.
const colorStyles = figma.getLocalPaintStyles();

const updatedColorStyles = colorStyles.filter(style => {
  const { paints } = style;
  if (paints.length !== 1) {
    // Skip styles containing multiple colors.
    return false;
  }
  const [paint] = paints;
  if (paint.type !== 'SOLID') {
    // Skip styles containing gradients, image fills, etc.
    return false;
  }
  const hexColor = rgbToHex(paint.color);
  if (style.description === hexColor) {
    // Skip styles that already have the correct description.
    return false;
  }
  // Set the style description equal to the color's hex code.
  style.description = hexColor;
  return true;
});

// Show a toast indicating the number of style descriptions that were updated.
figma.notify(`✅ Updated ${updatedColorStyles.length} style description(s)`);

/**
 * Converts an RGB object to a hex color string. For example:
 * { r: 0.5, g: 0.5, b: 0.5 } => '#808080'
 */
function rgbToHex(rgb: RGB) {
  const r = ratioToHex(rgb.r);
  const g = ratioToHex(rgb.g);
  const b = ratioToHex(rgb.b);
  return `#${r}${g}${b}`;
}

/**
 * Converts a ratio between [0,1] to a 2-character hex string. For example:
 * 0.5 => '80'
 */
function ratioToHex(ratio: number) {
  const hex = Math.round(ratio * 255).toString(16).toUpperCase();
  return `${hex.length === 1 ? '0' : ''}${hex}`;


拼合图标:

首先要知道的是:拼合(flatten)图标是为了加快 Figma 的渲染效率,尤其是创建了成千上万的图标的时候。小系统则没必要拼合,毕竟保留必要的零散部件是为了方便日后改动。大体量的图标库发布之前有必要拼合图标,加快渲染效率。所以会有一种情况需要考虑:多人团队,可能是设计人员A复杂一块或多块的图标集,设计人员A自己保留并维护自己图标库,而设计系统发布之前,而设计人员A则需要拼合图标后复制到设计系统。注意:该脚本会拼合所有组件,所以不要在设计系统执行该脚本。至此,仅作为“抛砖引玉”,大家需要辩证地看待一件事。

如下图,执行脚本。组件都拼合了,包括进度条组件。危险!一个含有几百个图标的图标库,执行该脚本,轻松搞定了需要几个小时甚至几天才能完成的工作。



Flatten Icon

// Create a list of all component nodes in the Figma file.
const componentNodes = figma.root.children.flatMap(pageNode => {
  return pageNode.findAll(node => node.type === 'COMPONENT');
}) as readonly ComponentNode[];

// Create a list of component nodes that have more than one child node.
const unflattenedComponentNodes = componentNodes.filter(componentNode => {
  const childrenNodes = componentNode.findAll(() => true);
  return childrenNodes.length > 1;
});

// Flatten each component node's children into a single node.
unflattenedComponentNodes.forEach(node => figma.flatten(node.children, node));


删除私有组件:

设计系统的更新和维护的过程中会淘汰弃用一些旧组件。工作太忙,你是否遗忘了一些应当淘汰的私有组件?当你回过神来,如何辨别它是否私有呢?运行以下脚本则能很好地解决问题。

我这里的代码跟原作者的代码不一样,删除了“检测组件是否发布”那段代码,因为组件都是“发布”的状态,没必要检测,删除了这段代码后反而能运行下去,不然一直卡在那里。

这里提一下:其实可以使用 Safely Delete Components 这个插件实现全局删除私有组件,这样是最方便最快的,推荐。还有,也可以用 Unused Components 检测实例的数量,可视化而且方便。感兴趣的应该去试试。

选择组件,执行脚本,检测组件有无其他实例(Instances)有则提示实例的数量;无则属于私有组件,删除组件。


Deleting unused private components

if (figma.currentPage.selection.length !== 1) {
  figma.notify("🚫 Select a component");
  return;
}

const [componentNode] = figma.currentPage.selection;
if (componentNode.type !== "COMPONENT") {
  figma.notify("🚫 Select a component");
  return;
}

// Create a list of all instances of the selected component node.
const instanceNodes = figma.root.children.flatMap(pageNode => {
  return pageNode.findAll(node => {
    if (node.type !== 'INSTANCE') {
      // Ignore non-instance nodes.
      return false;
    }
    // The node is an instance of the selected component node
    // if its master component ID matches the component node's ID.
    return node.masterComponent.id === componentNode.id;
  });
}) as readonly InstanceNode[];

if (instanceNodes.length === 0) {
  // The component is unused, so delete it.
  componentNode.remove();
  figma.notify(`🗑️ Deleted unused component`);
} else {
  // The component is still in use, so don't delete it.
  figma.notify(`✅ Found ${instanceNodes.length} instance(s) of the selected component`);


清理多余空格:

该脚本清理组件的多余空格,仅保留该有的空格。仔细看下图。


Cleans up Figma component and style names

const components = figma.root.children.flatMap(pageNode => {
  return pageNode.findAll(node => node.type === 'COMPONENT');
});

const styles = [
  ...figma.getLocalEffectStyles(),
  ...figma.getLocalGridStyles(),
  ...figma.getLocalPaintStyles(),
  ...figma.getLocalTextStyles(),
];

const updatedObjs = [...components, ...styles].filter(obj => {
  let updated = false;
  if (obj.name !== obj.name.trim()) {
      obj.name = obj.name.trim();
      updated = true;
    }
    if (obj.name !== obj.name.replace(/\\s{2,}/g, ' ')) {
      obj.name = obj.name.replace(/\\s{2,}/g, ' ');
      updated = true;
    }
    if (obj.name.startsWith('.')) {
      obj.name = obj.name.replace('.', '_');
      updated = true;
    }
    return updated;
});


自定义选择:

该脚本可以选择包括:Frame(框架)、Rectangle(矩形)、Line(线)、Arrow(箭头)、Ellipse(椭圆)、Polygon(多边形)、Star(星形)、Vector(矢量)等。那如果我要 Image(图片) 呢?在 Figma 中没有严格意义上的 Image(图片),所以你可以这么理解:Image 以填充(fill)的形式填充到矩形、椭圆、星形或者框架等等的里面。所以你只能选择所说的名词去选择,你打上 Image 执行脚本也没反应。

这里输入你想要选择的类型。


如图,选择矩形。包括组件、实例中的矩形都选择了。填充了图片的矩形也是一样。


Select all of type

setSelection(await find(n => n.type == "RECTANGLE" && n))


5
举报
|
2
分享
相关推荐
评论
用户头像
评论你的想法~
表情
喜欢TA的作品吗?喜欢就快来夸夸TA吧!
推荐素材
新能源APP应用UIKit
【新年UI图标】30个图标
高级表盘系列UI源文件
【新年UI图标】秒杀icon
3D渐变流体抽象矢量UI背景图
盲盒APP UI设计
3D卡通UI界面图标可爱插画免扣素
【新年UI图标】体育icon
UI通用设计素材1
UI 登录界面设计模板包
科技医疗透明柜UI界面设计
原创UIUX交互橙红渐变炫酷视觉平面设计作品集模板PSD
【新年UI图标】汽车icon
智能家居中心 简约 UI设计组件库
我的钱包-UI界面设计-app
手表表盘UI系列
新拟态风格 UI设计组件库
【新年UI图标】钱包icon
户外旅行飞机情侣假日出行
【新年UI图标】会员icon
APP/小程序商业项目UI模板|99个高保真UI+79个原型
拟物风质感写实UI卡片合集源文件
【新年UI图标】旅行icon
你可能喜欢
相关收藏夹
大家都在看
登录注册