From bd36496023bb6afead8180c77284fb4ee3df1f63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20S=C3=A1nchez?= Date: Fri, 28 Jun 2024 12:38:25 +0200 Subject: [PATCH] Improve registering component properties (#201) * improvements * changeset * fixes --- .changeset/dirty-jobs-begin.md | 5 +++++ plugin-src/registerComponentProperties.ts | 13 +++++++++++++ plugin-src/transformers/transformComponentNode.ts | 13 +++---------- plugin-src/transformers/transformFrameNode.ts | 12 ++---------- plugin-src/transformers/transformInstanceNode.ts | 2 +- 5 files changed, 24 insertions(+), 21 deletions(-) create mode 100644 .changeset/dirty-jobs-begin.md create mode 100644 plugin-src/registerComponentProperties.ts diff --git a/.changeset/dirty-jobs-begin.md b/.changeset/dirty-jobs-begin.md new file mode 100644 index 0000000..951e328 --- /dev/null +++ b/.changeset/dirty-jobs-begin.md @@ -0,0 +1,5 @@ +--- +"penpot-exporter": patch +--- + +Improve registering component properties diff --git a/plugin-src/registerComponentProperties.ts b/plugin-src/registerComponentProperties.ts new file mode 100644 index 0000000..3fcd124 --- /dev/null +++ b/plugin-src/registerComponentProperties.ts @@ -0,0 +1,13 @@ +import { componentProperties } from '@plugin/libraries'; + +export const registerComponentProperties = (node: ComponentSetNode | ComponentNode) => { + try { + Object.entries(node.componentPropertyDefinitions).forEach(([key, value]) => { + if (value.type === 'TEXT' || value.type === 'BOOLEAN') { + componentProperties.set(key, value); + } + }); + } catch (error) { + console.error('Error registering component properties', node, error); + } +}; diff --git a/plugin-src/transformers/transformComponentNode.ts b/plugin-src/transformers/transformComponentNode.ts index 2a7886f..3eb3eff 100644 --- a/plugin-src/transformers/transformComponentNode.ts +++ b/plugin-src/transformers/transformComponentNode.ts @@ -1,4 +1,5 @@ -import { componentProperties, components } from '@plugin/libraries'; +import { components } from '@plugin/libraries'; +import { registerComponentProperties } from '@plugin/registerComponentProperties'; import { transformAutoLayout, transformBlend, @@ -45,15 +46,7 @@ export const transformComponentNode = async (node: ComponentNode): Promise { - if (value.type === 'TEXT' || value.type === 'BOOLEAN') { - componentProperties.set(key, value); - } - }); - } catch (error) { - console.error('Error registering component properties', error); - } + registerComponentProperties(node); } return { diff --git a/plugin-src/transformers/transformFrameNode.ts b/plugin-src/transformers/transformFrameNode.ts index 39e4032..aa7e5e0 100644 --- a/plugin-src/transformers/transformFrameNode.ts +++ b/plugin-src/transformers/transformFrameNode.ts @@ -1,4 +1,4 @@ -import { componentProperties } from '@plugin/libraries'; +import { registerComponentProperties } from '@plugin/registerComponentProperties'; import { transformAutoLayout, transformBlend, @@ -37,15 +37,7 @@ export const transformFrameNode = async ( let referencePoint: Point = { x: node.absoluteTransform[0][2], y: node.absoluteTransform[1][2] }; if (isComponentSetNode(node)) { - try { - Object.entries(node.componentPropertyDefinitions).forEach(([key, value]) => { - if (value.type === 'TEXT' || value.type === 'BOOLEAN') { - componentProperties.set(key, value); - } - }); - } catch (error) { - console.error('Error registering component properties', error); - } + registerComponentProperties(node); } if (!isSectionNode(node)) { diff --git a/plugin-src/transformers/transformInstanceNode.ts b/plugin-src/transformers/transformInstanceNode.ts index d1c1648..ec9f192 100644 --- a/plugin-src/transformers/transformInstanceNode.ts +++ b/plugin-src/transformers/transformInstanceNode.ts @@ -35,7 +35,7 @@ export const transformInstanceNode = async ( nodeOverrides = transformOverrides(node); } - const fetchedOverrides = overrides.get(node.id) ?? []; + const fetchedOverrides = [...(overrides.get(node.id) ?? [])]; if (node.visible !== mainComponent.visible) { fetchedOverrides.push('visible'); }