|
| 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 | + } |
| 19 | + |
| 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 | + } |
| 33 | + } |
| 34 | + |
| 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 | + }); |
| 44 | + } |
| 45 | + }); |
| 46 | + } |
| 47 | + }); |
| 48 | + return imageNodes; |
| 49 | +} |
| 50 | + |
| 51 | +// helper function for creating an annotation |
| 52 | +function createImageAnnotation(node, customLabel) { |
| 53 | + let DEFAULT_TEMPLATE = `🔵 **ALT TEXT**\n${node.name}`; |
| 54 | + let markdown = customLabel || DEFAULT_TEMPLATE; |
| 55 | + node.annotations = [ |
| 56 | + { |
| 57 | + labelMarkdown: markdown, |
| 58 | + properties: [{ type: "fills" }], |
| 59 | + }, |
| 60 | + ]; |
| 61 | +} |
| 62 | + |
| 63 | +// helper function for notifying after annotations are created |
| 64 | +function showAnnotationNotification(count, skipped) { |
| 65 | + let msg = ""; |
| 66 | + if (count > 0) { |
| 67 | + msg += `Created ${count} annotation${count > 1 ? "s" : ""}.`; |
| 68 | + } |
| 69 | + if (skipped > 0) { |
| 70 | + msg += ` Skipped ${skipped} annotation${skipped > 1 ? "s" : ""}.`; |
| 71 | + } |
| 72 | + figma.notify(msg); |
| 73 | +} |
| 74 | + |
| 75 | +// function to create annotations in selection |
| 76 | +// selection => selection can be figma.currentPage.selection or figma.currentPage |
| 77 | +// label (optional) => custom label to be annotated, |
| 78 | +// if not provided will be labeled using image name |
| 79 | +function createAltTextAnnotations(selection, label) { |
| 80 | + let imageNodes = getImageNodes(selection); |
| 81 | + |
| 82 | + let count = 0; // number of annotations we'll create |
| 83 | + let skipped = 0; // number of annotations we'll skip |
| 84 | + |
| 85 | + imageNodes.forEach((node) => { |
| 86 | + // if an annotations already exists, we don't want to overwrite it |
| 87 | + if (node.annotations.length > 0) { |
| 88 | + skipped++; |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + createImageAnnotation(node, label); |
| 93 | + count++; |
| 94 | + }); |
| 95 | + |
| 96 | + showAnnotationNotification(count, skipped); |
| 97 | +} |
| 98 | + |
| 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 | +}); |
0 commit comments