-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathcode.js
94 lines (83 loc) · 2.47 KB
/
code.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Whether or not a node has a visible image fill on it.
// Ignores "figma.mixed" fill values.
function nodeHasImageFill(node) {
return (
"fills" in node &&
Array.isArray(node.fills) &&
Boolean(node.fills.find((paint) => paint.visible && paint.type === "IMAGE"))
);
}
// Returns all Figma nodes and descendants with image fills for an array of nodes
function getImageNodes(nodes) {
const imageNodes = [];
nodes.forEach((node) => {
if (nodeHasImageFills(node)) {
imageNodes.push(node);
}
// Checking node descendants
if ("findAll" in node) {
node.findAll((descendant) => {
if (nodeHasImageFill(descendant)) {
imageNodes.push(descendant);
}
});
}
});
return imageNodes;
}
// helper function for creating an annotation
function createImageAnnotation(node, customLabel) {
let DEFAULT_TEMPLATE = `🔵 **ALT TEXT**\n${node.name}`;
let markdown = customLabel || DEFAULT_TEMPLATE;
node.annotations = [
{
labelMarkdown: markdown,
properties: [{ type: "fills" }],
},
];
}
// helper function for notifying after annotations are created
function showAnnotationNotification(count, skipped) {
let msg = "";
if (count > 0) {
msg += `Created ${count} annotation${count > 1 ? "s" : ""}.`;
}
if (skipped > 0) {
msg += ` Skipped ${skipped} annotation${skipped > 1 ? "s" : ""}.`;
}
figma.notify(msg);
}
// function to create annotations in selection
// selection => selection can be figma.currentPage.selection or figma.currentPage
// label (optional) => custom label to be annotated,
// if not provided will be labeled using image name
function createAltTextAnnotations(selection, label) {
let imageNodes = getImageNodes(selection);
let count = 0; // number of annotations we'll create
let skipped = 0; // number of annotations we'll skip
imageNodes.forEach((node) => {
// if an annotations already exists, we don't want to overwrite it
if (node.annotations.length > 0) {
skipped++;
return;
}
createImageAnnotation(node, label);
count++;
});
showAnnotationNotification(count, skipped);
}
// runs plugin from menu commands
figma.on("run", ({ command }) => {
switch (command) {
case "all-images":
createAltTextAnnotations([figma.currentPage]);
figma.closePlugin();
break;
case "selection":
createAltTextAnnotations(figma.currentPage.selection);
figma.closePlugin();
default:
// do nothing
break;
}
});