|
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 | +} |
19 | 10 |
|
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); |
33 | 17 | }
|
34 | 18 |
|
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); |
44 | 24 | }
|
45 | 25 | });
|
46 | 26 | }
|
47 | 27 | });
|
| 28 | + |
48 | 29 | return imageNodes;
|
49 | 30 | }
|
50 | 31 |
|
@@ -96,18 +77,8 @@ function createAltTextAnnotations(selection, label) {
|
96 | 77 | showAnnotationNotification(count, skipped);
|
97 | 78 | }
|
98 | 79 |
|
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 | +} |
0 commit comments