diff --git a/package-lock.json b/package-lock.json index 6892935..d4a6c94 100755 --- a/package-lock.json +++ b/package-lock.json @@ -1075,11 +1075,6 @@ "resolved": "https://registry.npmjs.org/cyclist/-/cyclist-0.2.2.tgz", "integrity": "sha1-GzN5LhHpFKL9bW7WRHRkRE5fpkA=" }, - "dash-get": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/dash-get/-/dash-get-1.0.2.tgz", - "integrity": "sha512-4FbVrHDwfOASx7uQVxeiCTo7ggSdYZbqs8lH+WU6ViypPlDbe9y6IP5VVUDQBv9DcnyaiPT5XT0UWHgJ64zLeQ==" - }, "date-now": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/date-now/-/date-now-0.1.4.tgz", @@ -2812,6 +2807,11 @@ "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" }, + "lodash-es": { + "version": "4.17.15", + "resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.15.tgz", + "integrity": "sha512-rlrc3yU3+JNOpZ9zj5pQtxnx2THmvRykwL4Xlxoa8I9lHBlVbbyPhgyPMioxVZ4NqyxaVVtaJnzsyOidQIhyyQ==" + }, "lower-case": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/lower-case/-/lower-case-1.1.4.tgz", diff --git a/package.json b/package.json index d723453..195589a 100755 --- a/package.json +++ b/package.json @@ -10,9 +10,10 @@ "license": "ISC", "dependencies": { "css-loader": "^3.1.0", - "dash-get": "^1.0.2", "html-webpack-inline-source-plugin": "0.0.10", "html-webpack-plugin": "^3.2.0", + "lodash": "^4.17.15", + "lodash-es": "^4.17.15", "style-loader": "^0.23.1", "ts-loader": "^6.0.4", "typescript": "^3.5.3", diff --git a/src/code.ts b/src/code.ts index 31bba03..2542752 100755 --- a/src/code.ts +++ b/src/code.ts @@ -1,23 +1,50 @@ -import get from "dash-get"; figma.showUI(__html__); figma.ui.onmessage = async msg => { if (msg.type === "populate") { - const textNodes = figma.currentPage.findAll( - node => node.type === "TEXT" - ) as TextNode[]; - if (textNodes.length == 0) return; + const instanceNodes = figma.currentPage.findAll( + node => node.type === "INSTANCE" + ) as InstanceNode[]; - textNodes.map(node => replacePlaceholder(node, msg.body)); + // Keep a count of the number of instances per component + type componentIndexMap = Map; + const componentIndices: componentIndexMap = new Map(); + + instanceNodes.forEach(instanceNode => { + // Replace the empty `[]` in all instance nodes with incrementing indices + const currentVal = componentIndices.get(instanceNode.masterComponent) || 0; + const textNodes = findTextNodes(instanceNode); + textNodes.forEach(textNode => replacePlaceholder(textNode, msg.body, currentVal)); + + // Increment number in map + componentIndices.set(instanceNode.masterComponent, currentVal + 1); + }); + + // const textNodes = figma.currentPage.findAll( + // node => node.type === "TEXT" + // ) as TextNode[]; + // if (textNodes.length == 0) return; + + // textNodes.map(node => replacePlaceholder(node, msg.body)); } }; -// Replace placeholder text values preceeded with a `$` with the value at the -// equivalent json path -async function replacePlaceholder(node: TextNode, json: Object): Promise { +function findTextNodes(rootNode: ChildrenMixin) { + return rootNode.findAll( + node => node.type === "TEXT" + ) as TextNode[]; +} + +/** + * Replace placeholder text values preceeded with a `$` with the value at the + * associated json path. Replace square-brackets with incrementing indices + */ +async function replacePlaceholder(node: TextNode, json: Object, index: number = 0): Promise { if (!node.characters.startsWith("$")) return; // Remove "$" from start - const path = node.characters.slice(1); + let path = node.characters.slice(1); + // Populate empty square-brackets with the provided index + path = path.replace("[]", `[${index}]`); const objValue = get(json, path); if (objValue == null) return; @@ -25,3 +52,20 @@ async function replacePlaceholder(node: TextNode, json: Object): Promise { await figma.loadFontAsync(node.fontName as FontName); node.characters = String(objValue); } + +/** + * Equivalent to _.get from lodash + */ +function get(obj, path, def = null) { + var fullPath = path + .replace(/\[/g, '.') + .replace(/]/g, '') + .split('.') + .filter(Boolean); + + return fullPath.every(everyFunc) ? obj : def; + + function everyFunc(step) { + return !(step && (obj = obj[step]) === undefined); + } +} diff --git a/src/ui.html b/src/ui.html index ae137c9..847035a 100755 --- a/src/ui.html +++ b/src/ui.html @@ -3,7 +3,7 @@