-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.ts
127 lines (115 loc) · 4.57 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
// Function to rename a node and its children
function renameNode(node: SceneNode, isInsideComponentOrInstance: boolean = false, isRootNode: boolean = true): number {
let renamedCount = 0;
// Special handling for components and instances
if (node.type === 'COMPONENT' || node.type === 'INSTANCE') {
if ('children' in node) {
for (const child of node.children) {
renamedCount += renameNode(child, true, false);
}
}
return renamedCount;
}
// List of names to preserve
const preserveNames = ['Headline', 'Head', 'Top', 'Bottom', 'Label', 'Paragraph'];
if (!isInsideComponentOrInstance && preserveNames.indexOf(node.name) === -1) {
let newName = '';
// Rename based on node type and properties
if (isRootNode && node.type === 'FRAME' && 'width' in node && node.width === 1680) {
newName = 'Screen';
} else if (isMask(node)) {
newName = 'Mask';
if (node.parent && node.parent.type === 'GROUP') {
node.parent.name = 'Mask Group';
renamedCount++;
}
} else if (node.type === 'TEXT') {
// For text layers, use the actual text content as the name
newName = (node as TextNode).characters.slice(0, 50); // Limit to 50 characters
if ((node as TextNode).characters.length > 50) {
newName += '...'; // Add ellipsis if text is longer than 50 characters
}
} else if (node.type === 'RECTANGLE' || node.type === 'ELLIPSE' || node.type === 'POLYGON' || node.type === 'STAR' || node.type === 'VECTOR') {
if ('fills' in node && 'strokes' in node) {
if (hasOnlyStroke(node)) {
newName = 'Line';
} else if (Array.isArray(node.fills) && node.fills.length > 0) {
if (node.fills.some((fill: Paint) => fill.type === 'IMAGE')) {
newName = 'Image';
} else if (node.fills.some((fill: Paint) => {
return fill.type === 'GRADIENT_LINEAR' || fill.type === 'GRADIENT_RADIAL' ||
fill.type === 'GRADIENT_ANGULAR' || fill.type === 'GRADIENT_DIAMOND';
})) {
newName = 'Gradient';
} else {
newName = 'Shape';
}
} else {
newName = 'Shape';
}
} else {
newName = 'Shape';
}
} else if (node.type === 'LINE') {
newName = 'Line';
} else if (node.type === 'FRAME') {
// Rename frames based on their layout mode or height
if ('height' in node && node.height === 1) {
newName = 'Divider';
} else if ('layoutMode' in node && node.layoutMode !== 'NONE') {
newName = 'Wrapper';
if (node.parent && (node.parent.type === 'FRAME' || node.parent.type === 'COMPONENT' || node.parent.type === 'INSTANCE') && 'layoutMode' in node.parent && node.parent.layoutMode !== 'NONE') {
newName = node.layoutMode === 'VERTICAL' ? 'Inner-column' : 'Inner-row';
}
} else {
newName = 'Contain';
}
} else if (node.type === 'GROUP') {
if (node.children.some(child => isMask(child))) {
newName = 'Mask Group';
} else {
newName = 'Group';
}
}
if (newName && node.name !== newName) {
(node as BaseNode).name = newName;
renamedCount++;
}
}
// Recursively rename children
if ('children' in node) {
for (const child of node.children) {
renamedCount += renameNode(child, isInsideComponentOrInstance, false);
}
}
return renamedCount;
}
// Function to check if a node is a mask
function isMask(node: SceneNode): boolean {
return (
(node.type === 'BOOLEAN_OPERATION' && 'isMask' in node && node.isMask) ||
((node.type === 'RECTANGLE' || node.type === 'ELLIPSE' || node.type === 'POLYGON' || node.type === 'STAR' || node.type === 'VECTOR') && 'isMask' in node && node.isMask)
);
}
// Function to check if a node has only a stroke
function hasOnlyStroke(node: SceneNode & { fills?: readonly Paint[] | typeof figma.mixed, strokes?: readonly Paint[] | typeof figma.mixed }): boolean {
return (
Array.isArray(node.fills) && node.fills.length === 0 &&
Array.isArray(node.strokes) && node.strokes.length > 0
);
}
// Main plugin logic
const selection = figma.currentPage.selection;
if (selection.length > 0) {
let totalRenamed = 0;
for (const node of selection) {
totalRenamed += renameNode(node);
}
// Notify about the number of renamed layers
figma.notify(`✨ Done, ${totalRenamed} Layer${totalRenamed !== 1 ? 's' : ''} Renamed ✨`);
} else {
// Notify if no selection was made
figma.notify(`🙈 Oops! Please select at least one frame to get started 🎨`);
}
// Close the plugin
figma.closePlugin();