Skip to content

Commit f7dbfcc

Browse files
jake-figmaakbarbmirza
authored andcommitted
rough cut at some opportunities to simplify
1 parent 540678d commit f7dbfcc

File tree

2 files changed

+27
-60
lines changed

2 files changed

+27
-60
lines changed

annotations/code.js

+26-55
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,31 @@
1-
// a helper function to find nodes with image fills
2-
// accepts either figma.currentPage or an array of nodes
3-
function getImageNodes(selection) {
4-
let imageNodes = [];
5-
6-
// if selection is currentPage
7-
if (selection == figma.currentPage) {
8-
figma.currentPage.findAll((node) => {
9-
if ("fills" in node) {
10-
node.fills.find((paint) => {
11-
if (paint.type === "IMAGE") {
12-
imageNodes.push(node);
13-
}
14-
});
15-
}
16-
});
17-
return imageNodes;
18-
}
1+
// Whether or not a node has a visible image fill on it.
2+
// Ignores "figma.mixed" fill values.
3+
function nodeHasImageFill(node) {
4+
return (
5+
"fills" in node &&
6+
Array.isArray(node.fills) &&
7+
Boolean(node.fills.find((paint) => paint.visible && paint.type === "IMAGE"))
8+
);
9+
}
1910

20-
// otherwise, selection is an Array
21-
selection.forEach((element) => {
22-
// if our current element has an image fill, annotate it
23-
if ("fills" in element) {
24-
// ignore if fills is figma.mixed
25-
if (Array.isArray(element.fills)) {
26-
element.fills.find((paint) => {
27-
if (paint.type === "IMAGE") {
28-
imageNodes.push(element);
29-
return;
30-
}
31-
});
32-
}
11+
// Returns all Figma nodes and descendants with image fills for an array of nodes
12+
function getImageNodes(nodes) {
13+
const imageNodes = [];
14+
nodes.forEach((node) => {
15+
if (nodeHasImageFills(node)) {
16+
imageNodes.push(node);
3317
}
3418

35-
// if our current element is a node that can be traversed further
36-
if ("findAll" in element) {
37-
element.findAll((node) => {
38-
if ("fills" in node) {
39-
node.fills.find((paint) => {
40-
if (paint.type === "IMAGE") {
41-
imageNodes.push(node);
42-
}
43-
});
19+
// Checking node descendants
20+
if ("findAll" in node) {
21+
node.findAll((descendant) => {
22+
if (nodeHasImageFill(descendant)) {
23+
imageNodes.push(descendant);
4424
}
4525
});
4626
}
4727
});
28+
4829
return imageNodes;
4930
}
5031

@@ -96,18 +77,8 @@ function createAltTextAnnotations(selection, label) {
9677
showAnnotationNotification(count, skipped);
9778
}
9879

99-
// runs plugin from menu commands
100-
figma.on("run", ({ command }) => {
101-
switch (command) {
102-
case "all-images":
103-
createAltTextAnnotations(figma.currentPage);
104-
figma.closePlugin();
105-
break;
106-
case "selection":
107-
createAltTextAnnotations(figma.currentPage.selection);
108-
figma.closePlugin();
109-
default:
110-
// do nothing
111-
break;
112-
}
113-
});
80+
if (figma.currentPage.selection.length) {
81+
createAltTextAnnotations(figma.currentPage.selection);
82+
} else {
83+
createAltTextAnnotations([figma.currentPage]);
84+
}

annotations/manifest.json

+1-5
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,5 @@
1010
"ui": "ui.html",
1111
"networkAccess": {
1212
"allowedDomains": ["none"]
13-
},
14-
"menu": [
15-
{ "name": "Annotate images in selection", "command": "selection" },
16-
{ "name": "Annotate all images on page", "command": "all-images" }
17-
]
13+
}
1814
}

0 commit comments

Comments
 (0)