-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnode-parser.ts
More file actions
80 lines (72 loc) · 2.82 KB
/
Copy pathnode-parser.ts
File metadata and controls
80 lines (72 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import FigmaRestAPI from "@figma/rest-api-spec";
import {
AdditionalData,
AdditionalDataKeys,
AppliedDesignTokens,
AppliedStyleModules,
AppliedStyleValues,
Config,
deserializeMap,
DesignTokenValues,
PluginNodeData,
PluginUINodeData,
} from "@adaptive-web/adaptive-ui-designer-core";
import { FIGMA_SHARED_DATA_NAMESPACE } from "./constants.js";
function getPluginData<T extends FigmaRestAPI.Node, K extends keyof PluginNodeData>(node: T, key: K): string | null {
const data = node.sharedPluginData;
if (data && (data as any)[FIGMA_SHARED_DATA_NAMESPACE]) {
return (data as any)[FIGMA_SHARED_DATA_NAMESPACE][key];
}
return null;
}
function hasChildren<T extends FigmaRestAPI.Node>(node: T): node is FigmaRestAPI.HasChildrenTrait & T {
return "children" in node;
}
/**
* Convert a Figma REST API node to a {@link PluginUINodeData}
* @param node - The Figma REST API node.
* @returns The corresponding PluginUINodeData.
*/
export function parseNode(node: FigmaRestAPI.Node): PluginUINodeData {
const children = hasChildren(node) ? node.children : [];
const additionalData: AdditionalData = new AdditionalData(); // Where do I get data for this?
if (node.type === "COMPONENT" || node.type === "COMPONENT_SET") {
additionalData.set(AdditionalDataKeys.codeGenName, node.name);
}
const configData = getPluginData(node, "config");
const appliedTokensPluginData = getPluginData(node, "appliedDesignTokens");
const appliedStylesPluginData = getPluginData(node, "appliedStyleModules");
const config: Config = configData
? JSON.parse(configData)
: new Config();
const appliedDesignTokens: AppliedDesignTokens = appliedTokensPluginData
? deserializeMap(appliedTokensPluginData)
: new AppliedDesignTokens();
// Parse appliedStyleModules and ensure it's an array
let appliedStyleModules: AppliedStyleModules = new AppliedStyleModules();
if (appliedStylesPluginData) {
try {
const parsed = JSON.parse(appliedStylesPluginData);
// Ensure we have an array - if parsed data is not an array, ignore it
if (Array.isArray(parsed)) {
appliedStyleModules = new AppliedStyleModules(...parsed);
}
} catch (e) {
console.warn('Failed to parse appliedStyleModules:', e);
}
}
return {
id: node.id,
name: node.name,
type: node.type,
supports: [],
children: children.map(parseNode),
config,
additionalData,
appliedDesignTokens,
appliedStyleModules,
effectiveAppliedStyleValues: new AppliedStyleValues(),
designTokens: new DesignTokenValues(), // Intentionally empty
inheritedDesignTokens: new DesignTokenValues(), // Intentionally empty
};
}