-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathcode.ts
66 lines (59 loc) · 1.52 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
figma.showUI(__html__);
let timer = undefined;
figma.ui.onmessage = async message => {
if (message.query !== undefined) {
if (timer) {
clearTimeout(timer);
}
if (message.query) {
searchFor(message.query);
}
} else if (message.show) {
const node = await figma.getNodeByIdAsync(message.show);
if (node.type === 'DOCUMENT' || node.type === 'PAGE') {
// DOCUMENTs and PAGEs can't be put into the selection.
return;
}
figma.currentPage.selection = [node];
figma.viewport.scrollAndZoomIntoView([node]);
} else if (message.quit) {
figma.closePlugin();
}
}
// This is a generator that recursively produces all the nodes in subtree
// starting at the given node
function* walkTree(node) {
yield node;
const children = node.children;
if (children) {
for (const child of children) {
yield* walkTree(child)
}
}
}
function searchFor(query) {
query = query.toLowerCase()
const walker = walkTree(figma.currentPage)
function processOnce() {
const results = [];
let count = 0;
let done = true;
let res
while (!(res = walker.next()).done) {
const node = res.value
if (node.type === 'TEXT') {
const characters = node.characters.toLowerCase()
if (characters.includes(query)) {
results.push(node.id);
}
}
if (++count === 1000) {
done = false
timer = setTimeout(processOnce, 20)
break
}
}
figma.ui.postMessage({ query, results, done })
}
processOnce()
}