Skip to content

Commit

Permalink
fix: changes findAll to only run on plugindata nodes, except for insp…
Browse files Browse the repository at this point in the history
…ect (#2163)

introduces performance optimization for the findAll function utilizing
Figma's new addition to their plugin API, with the exception for the
`Inspect` where we still want to show all (eg styles or variables)

---------

Co-authored-by: SorsOps <[email protected]>
  • Loading branch information
six7 and SorsOps authored Aug 14, 2023
1 parent 6573dca commit b5e7b88
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-oranges-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tokens-studio/figma-plugin": patch
---

Improves performance of most operations by utilizing a new findAll function to faster traverse the tree of nodes.
7 changes: 4 additions & 3 deletions src/plugin/NodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ export class NodeManager {
public async findNodesWithData(opts: {
updateMode?: UpdateMode;
nodes?: readonly BaseNode[];
nodesWithoutPluginData?: boolean;
}) {
postToUI({
type: MessageFromPluginTypes.START_JOB,
Expand All @@ -269,11 +270,11 @@ export class NodeManager {
if (nodes) {
relevantNodes = Array.from(nodes);
} else if (updateMode === UpdateMode.PAGE) {
relevantNodes = findAll([figma.currentPage], false);
relevantNodes = findAll([figma.currentPage], false, opts.nodesWithoutPluginData);
} else if (updateMode === UpdateMode.SELECTION) {
relevantNodes = findAll(figma.currentPage.selection, true);
relevantNodes = findAll(figma.currentPage.selection, true, opts.nodesWithoutPluginData);
} else {
relevantNodes = findAll([figma.root], false);
relevantNodes = findAll([figma.root], false, opts.nodesWithoutPluginData);
}

const unregisteredNodes = relevantNodes
Expand Down
4 changes: 3 additions & 1 deletion src/plugin/sendSelectionChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { UpdateMode } from '@/constants/UpdateMode';
export async function sendSelectionChange(): Promise<SelectionContent | null> {
// Big O (sendPluginValues)
const nodes = store.inspectDeep && store.shouldSendSelectionValues
? (await defaultNodeManager.findNodesWithData({ updateMode: UpdateMode.SELECTION })).map((node) => node.node)
? (
await defaultNodeManager.findNodesWithData({ updateMode: UpdateMode.SELECTION, nodesWithoutPluginData: true })
).map((node) => node.node)
: Array.from(figma.currentPage.selection);
const currentSelectionLength = figma.currentPage.selection.length;

Expand Down
18 changes: 14 additions & 4 deletions src/utils/findAll.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { ValidNodeTypes } from '@/constants/ValidNodeTypes';

export function findAll(nodes: readonly BaseNode[], includeSelf = false): BaseNode[] {
export function findAll(nodes: readonly BaseNode[], includeSelf = false, nodesWithoutPluginData = false): BaseNode[] {
let allNodes = includeSelf ? [...nodes] : [];
const pluginDataOptions = nodesWithoutPluginData
? {}
: {
sharedPluginData: {
namespace: 'tokens',
},
};
nodes.forEach((node) => {
if ('children' in node) {
allNodes = allNodes.concat(node.findAllWithCriteria({
types: ValidNodeTypes,
}));
allNodes = allNodes.concat(
node.findAllWithCriteria({
types: ValidNodeTypes,
...pluginDataOptions,
}),
);
}
});
return allNodes;
Expand Down

0 comments on commit b5e7b88

Please sign in to comment.