-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.ts
232 lines (212 loc) · 6.33 KB
/
code.ts
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// This plugin will open a tab that indicates that it will monitor the current
// selection on the page. It cannot change the document itself.
// This file holds the main code for plugins. Code in this file has access to
// the *figma document* via the figma global object.
// You can access browser APIs in the <script> tag inside "ui.html" which has a
// full browser environment (See https://www.figma.com/plugin-docs/how-plugins-run).
// This shows the HTML page in "ui.html".
figma.showUI(__html__, { themeColors: true });
type CoreNodeInfo = { id: string; pageId: string; name: string };
type ComponentCount = {
node: ComponentNode | ComponentSetNode;
count: number;
dependsOn: (ComponentNode | ComponentSetNode)[];
};
const isInstanceNode = (node: BaseNode): node is InstanceNode =>
node.type === 'INSTANCE';
/** Helper to get the Page a node is contained in */
const getPage = (node: BaseNode): PageNode | undefined => {
if (node.parent?.type === 'PAGE') {
return node.parent;
}
return node.parent ? getPage(node.parent) : undefined;
};
/** global var to hold ignored Sections set in UI */
let ignoredSectionsOrFrames: string[] = [];
/** Helper to recursively check if node is in one of the submitted Sections/Frames */
const isContainedInSectionOrFrame = (
node: BaseNode,
sections: string[]
): boolean => {
if (
(node.parent?.type === 'SECTION' || node.parent?.type === 'FRAME') &&
sections.includes(node.parent.name)
) {
return true;
}
return node.parent
? isContainedInSectionOrFrame(node.parent, sections)
: false;
};
const updateFinds = (
finds: ComponentCount[],
node: ComponentNode | ComponentSetNode
): ComponentCount[] => {
const index = finds.findIndex((c) => c.node.id === node.id);
if (index >= 0) {
finds[index].count++;
return finds;
}
return [
...finds,
{
node,
dependsOn: [],
count: 1,
},
];
};
/** Resolves to the `ComponentNode` or `ComponentSetNode` for Components with Variants */
const getMainComponent = ({ mainComponent }: InstanceNode) => {
if (mainComponent?.parent?.type === 'COMPONENT_SET') {
return mainComponent.parent;
}
return mainComponent;
};
const findComponentsRecursive = (
node: BaseNode,
parentComponentIds: string[],
finds: ComponentCount[],
ignoredSectionsOrFrames: string[]
): ComponentCount[] => {
if (!node || isContainedInSectionOrFrame(node, ignoredSectionsOrFrames)) {
return finds;
}
if (isInstanceNode(node)) {
const mainComponent = getMainComponent(node);
if (
mainComponent === null ||
isContainedInSectionOrFrame(mainComponent, ignoredSectionsOrFrames)
) {
return finds;
}
// mark dependencies
parentComponentIds.forEach((p) => {
finds.forEach((f) => {
if (f.node.id === p && !f.dependsOn.includes(mainComponent)) {
f.dependsOn.push(mainComponent);
}
});
});
let updatedFinds = [...finds];
if (node.children) {
node.children.forEach((childNode) => {
updatedFinds = findComponentsRecursive(
childNode,
parentComponentIds.includes(mainComponent.id)
? parentComponentIds
: [...parentComponentIds, mainComponent.id],
updatedFinds,
ignoredSectionsOrFrames
);
});
}
return updateFinds(updatedFinds, mainComponent);
}
if (!('children' in node) || !node.children) {
return finds;
}
let updatedFinds = [...finds];
node.children.forEach((childNode) => {
updatedFinds = findComponentsRecursive(
childNode,
parentComponentIds,
updatedFinds,
ignoredSectionsOrFrames
);
});
return updatedFinds;
};
/** Start the scan for Components in the current selection */
const scanSelection = (ignoredSectionsOrFrames: string[]) => {
const baseNode: BaseNode = figma.currentPage.selection[0]
? figma.currentPage.selection[0]
: figma.currentPage;
if (!('findAll' in baseNode)) {
return;
}
const componentsWithDependencies = findComponentsRecursive(
baseNode,
[],
[],
ignoredSectionsOrFrames
).map((r) => {
return {
...r,
node: {
id: r.node.id,
name: r.node.name,
pageId: getPage(r.node)?.id,
},
dependsOn: r.dependsOn
// .filter((d) => !isContainedInSectionOrFrame(d, ignoredSectionsOrFrames))
.map((d) => ({
id: d.id,
name: d.name,
pageId: getPage(d)?.id,
})),
};
});
figma.ui.postMessage({
type: 'result',
componentsWithDependencies: componentsWithDependencies.sort((a, b) => {
const aIsStandalone = a.dependsOn.length === 0;
const bIsStandalone = b.dependsOn.length === 0;
if (aIsStandalone && !bIsStandalone) {
return -1;
}
if (bIsStandalone && !aIsStandalone) {
return 1;
}
return 0;
}),
});
};
// This monitors the selection changes and posts the selection to the UI
figma.on('selectionchange', () => scanSelection(ignoredSectionsOrFrames));
/** Main handler for `focus-instance` */
const handleFocusInstance = (pageId: string, instanceId: string) => {
const page = figma.getNodeById(pageId) as PageNode;
const node = figma.getNodeById(instanceId) as SceneNode;
if (!page || !node) {
const error = `Could not find ${
!page ? 'Page' : 'Node'
} with ID: ${instanceId}`;
console.error(error);
figma.ui.postMessage({ type: 'error', error });
return;
}
page.selection = [node];
figma.currentPage = page;
figma.viewport.scrollAndZoomIntoView([node]);
};
figma.ui.onmessage = async (msg, props) => {
switch (msg.type) {
case 'focus-instance': {
handleFocusInstance(msg.pageId, msg.instanceId);
return;
}
case 'scan': {
ignoredSectionsOrFrames = msg.ignoredSectionsOrFrames;
await figma.clientStorage.setAsync(
'ignored-sections-or-frames',
ignoredSectionsOrFrames
);
scanSelection(ignoredSectionsOrFrames);
return;
}
case 'init': {
ignoredSectionsOrFrames = await figma.clientStorage.getAsync(
'ignored-sections-or-frames'
);
figma.ui.postMessage({
type: 'settings-retrieved',
ignoredSectionsOrFrames: ignoredSectionsOrFrames,
});
scanSelection(ignoredSectionsOrFrames);
return;
}
default:
console.error('unsupported message', msg);
}
};