diff --git a/code.js b/code.js index f0b740a..0bc1b56 100644 --- a/code.js +++ b/code.js @@ -155,11 +155,19 @@ codegenResultTemplates.push(...matchingCodegenResults); } } - if (globalTemplates) { - const componentTemplates = "key" in snippetNode && globalTemplates.components ? globalTemplates.components[snippetNode.key] || [] : []; - const typeTemplates = globalTemplates.types ? globalTemplates.types[snippetNode.type] || [] : []; + if (globalTemplates.components) { + const componentTemplates = "key" in snippetNode ? globalTemplates.components[snippetNode.key] || [] : []; codegenResultTemplates.push(...matchingTemplates(componentTemplates)); - codegenResultTemplates.push(...matchingTemplates(typeTemplates)); + } + if (!Object.keys(seenSnippetTemplates).length && !codegenResultTemplates.length && globalTemplates.types) { + const typeTemplates = globalTemplates.types[snippetNode.type] || []; + const seenKey = JSON.stringify(typeTemplates); + if (!seenSnippetTemplates[seenKey]) { + seenSnippetTemplates[seenKey] = 1; + const defaultTemplates = !typeTemplates.length && globalTemplates.types.DEFAULT ? globalTemplates.types.DEFAULT : []; + codegenResultTemplates.push(...matchingTemplates(typeTemplates)); + codegenResultTemplates.push(...matchingTemplates(defaultTemplates)); + } } const children = "children" in node ? node.children : []; const nodeSnippetTemplateData = await hydrateSnippets( @@ -173,17 +181,17 @@ ); nodeSnippetTemplateDataArray.push(nodeSnippetTemplateData); } - await processSnippetTemplatesForNode(node); - if (node.type === "INSTANCE") { + if (node.type === "COMPONENT" && node.parent && node.parent.type === "COMPONENT_SET") { + await processSnippetTemplatesForNode(node.parent); + } else if (node.type === "INSTANCE") { if (node.mainComponent) { - await processSnippetTemplatesForNode(node.mainComponent); if (node.mainComponent.parent && node.mainComponent.parent.type === "COMPONENT_SET") { await processSnippetTemplatesForNode(node.mainComponent.parent); } + await processSnippetTemplatesForNode(node.mainComponent); } - } else if (node.type === "COMPONENT" && node.parent && node.parent.type === "COMPONENT_SET") { - await processSnippetTemplatesForNode(node.parent); } + await processSnippetTemplatesForNode(node); return nodeSnippetTemplateDataArray; } function transformStringWithFilter(string, rawString, filter = "hyphen") { @@ -237,16 +245,16 @@ } else if (param === "figma.children" && recursionIndex < MAX_RECURSION) { const indentMatch = line.match(/^[ \t]+/); const indent2 = indentMatch ? indentMatch[0] : ""; - const value = await findChildrenSnippets( + const childrenValue = await findChildrenSnippets( codegenResult, nodeChildren, indent2, recursionIndex + 1, globalTemplates ); - if (value) { + if (childrenValue) { line = line.replace(/^[ \t]+/, ""); - line = line.replace(match, value); + line = line.replace(match, childrenValue); } else { succeeded = false; } diff --git a/src/index.d.ts b/src/index.d.ts index 81931b6..7b6d22c 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -5,7 +5,7 @@ */ type CodeSnippetGlobalTemplates = { components?: CodegenResultTemplatesByComponentKey; - types?: { [K in NodeType]?: CodegenResult[] }; + types?: { [K in NodeType | "DEFAULT"]?: CodegenResult[] }; }; /** diff --git a/src/snippets.ts b/src/snippets.ts index 7a9e8d7..a72d9d2 100644 --- a/src/snippets.ts +++ b/src/snippets.ts @@ -104,16 +104,30 @@ export async function nodeSnippetTemplateDataArrayFromNode( codegenResultTemplates.push(...matchingCodegenResults); } } - if (globalTemplates) { + if (globalTemplates.components) { const componentTemplates = - "key" in snippetNode && globalTemplates.components + "key" in snippetNode ? globalTemplates.components[snippetNode.key] || [] : []; - const typeTemplates = globalTemplates.types - ? globalTemplates.types[snippetNode.type] || [] - : []; codegenResultTemplates.push(...matchingTemplates(componentTemplates)); - codegenResultTemplates.push(...matchingTemplates(typeTemplates)); + } + + if ( + !Object.keys(seenSnippetTemplates).length && + !codegenResultTemplates.length && + globalTemplates.types + ) { + const typeTemplates = globalTemplates.types[snippetNode.type] || []; + const seenKey = JSON.stringify(typeTemplates); + if (!seenSnippetTemplates[seenKey]) { + seenSnippetTemplates[seenKey] = 1; + const defaultTemplates = + !typeTemplates.length && globalTemplates.types.DEFAULT + ? globalTemplates.types.DEFAULT + : []; + codegenResultTemplates.push(...matchingTemplates(typeTemplates)); + codegenResultTemplates.push(...matchingTemplates(defaultTemplates)); + } } const children = "children" in node ? node.children : []; const nodeSnippetTemplateData = await hydrateSnippets( @@ -129,31 +143,32 @@ export async function nodeSnippetTemplateDataArrayFromNode( } /** - * Templates on the given node - */ - await processSnippetTemplatesForNode(node); - - /** - * Templates via inheritance from component lineage + * Templates via inheritance from component lineage. + * Starting at the top with component sets, then components, then instances. */ - if (node.type === "INSTANCE") { + if ( + node.type === "COMPONENT" && + node.parent && + node.parent.type === "COMPONENT_SET" + ) { + await processSnippetTemplatesForNode(node.parent); + } else if (node.type === "INSTANCE") { if (node.mainComponent) { - await processSnippetTemplatesForNode(node.mainComponent); if ( node.mainComponent.parent && node.mainComponent.parent.type === "COMPONENT_SET" ) { await processSnippetTemplatesForNode(node.mainComponent.parent); } + await processSnippetTemplatesForNode(node.mainComponent); } - } else if ( - node.type === "COMPONENT" && - node.parent && - node.parent.type === "COMPONENT_SET" - ) { - await processSnippetTemplatesForNode(node.parent); } + /** + * Templates on the given node + */ + await processSnippetTemplatesForNode(node); + return nodeSnippetTemplateDataArray; } @@ -247,16 +262,16 @@ export async function hydrateSnippets( ) { const indentMatch = line.match(/^[ \t]+/); const indent = indentMatch ? indentMatch[0] : ""; - const value = await findChildrenSnippets( + const childrenValue = await findChildrenSnippets( codegenResult, nodeChildren, indent, recursionIndex + 1, globalTemplates ); - if (value) { + if (childrenValue) { line = line.replace(/^[ \t]+/, ""); - line = line.replace(match, value); + line = line.replace(match, childrenValue); } else { succeeded = false; } diff --git a/test.js b/test.js index 7339c91..dff5322 100644 --- a/test.js +++ b/test.js @@ -343,11 +343,19 @@ codegenResultTemplates.push(...matchingCodegenResults); } } - if (globalTemplates) { - const componentTemplates = "key" in snippetNode && globalTemplates.components ? globalTemplates.components[snippetNode.key] || [] : []; - const typeTemplates = globalTemplates.types ? globalTemplates.types[snippetNode.type] || [] : []; + if (globalTemplates.components) { + const componentTemplates = "key" in snippetNode ? globalTemplates.components[snippetNode.key] || [] : []; codegenResultTemplates.push(...matchingTemplates(componentTemplates)); - codegenResultTemplates.push(...matchingTemplates(typeTemplates)); + } + if (!Object.keys(seenSnippetTemplates).length && !codegenResultTemplates.length && globalTemplates.types) { + const typeTemplates = globalTemplates.types[snippetNode.type] || []; + const seenKey = JSON.stringify(typeTemplates); + if (!seenSnippetTemplates[seenKey]) { + seenSnippetTemplates[seenKey] = 1; + const defaultTemplates = !typeTemplates.length && globalTemplates.types.DEFAULT ? globalTemplates.types.DEFAULT : []; + codegenResultTemplates.push(...matchingTemplates(typeTemplates)); + codegenResultTemplates.push(...matchingTemplates(defaultTemplates)); + } } const children = "children" in node ? node.children : []; const nodeSnippetTemplateData = await hydrateSnippets( @@ -361,17 +369,17 @@ ); nodeSnippetTemplateDataArray.push(nodeSnippetTemplateData); } - await processSnippetTemplatesForNode(node); - if (node.type === "INSTANCE") { + if (node.type === "COMPONENT" && node.parent && node.parent.type === "COMPONENT_SET") { + await processSnippetTemplatesForNode(node.parent); + } else if (node.type === "INSTANCE") { if (node.mainComponent) { - await processSnippetTemplatesForNode(node.mainComponent); if (node.mainComponent.parent && node.mainComponent.parent.type === "COMPONENT_SET") { await processSnippetTemplatesForNode(node.mainComponent.parent); } + await processSnippetTemplatesForNode(node.mainComponent); } - } else if (node.type === "COMPONENT" && node.parent && node.parent.type === "COMPONENT_SET") { - await processSnippetTemplatesForNode(node.parent); } + await processSnippetTemplatesForNode(node); return nodeSnippetTemplateDataArray; } function transformStringWithFilter(string, rawString, filter = "hyphen") { @@ -425,16 +433,16 @@ } else if (param === "figma.children" && recursionIndex < MAX_RECURSION) { const indentMatch = line.match(/^[ \t]+/); const indent2 = indentMatch ? indentMatch[0] : ""; - const value = await findChildrenSnippets( + const childrenValue = await findChildrenSnippets( codegenResult, nodeChildren, indent2, recursionIndex + 1, globalTemplates ); - if (value) { + if (childrenValue) { line = line.replace(/^[ \t]+/, ""); - line = line.replace(match, value); + line = line.replace(match, childrenValue); } else { succeeded = false; }