-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.ts
339 lines (278 loc) · 8.44 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import type {ActionObject} from './lib/action.js';
import Action from './lib/action.js';
import Version from './lib/version.js';
import Plugin from './lib/plugin.js';
// @ts-expect-error: TS2307
import ui from './ui.html';
type SettingsObject = {
useRfc?: boolean;
updateName?: boolean;
useCommitMessage?: boolean;
saveToFigmaVersionHistory?: boolean;
};
const versionRegex = /@(\d+\.\d+\.\d+(-rfc\.\d+)?)$/im;
/**
* Derives possible Actions based on current version
*
* Returns an array ob possible actions, based on the given version and
* the current settings.
*/
function deriveActions(node: BaseNode, settings: SettingsObject, version?: Version): ActionObject[] {
const versionFromName = getVersionFromName(node);
const actions: ActionObject[] = [{
version: version ? version.toString() : null,
label: 'keep',
}];
if (version) {
const options
= version
.deriveOptions(settings.useRfc)
.map(versionObject => {
const newVersion = new Version(versionObject);
const label = version.elevatedLevel(newVersion) || 'keep';
const action = new Action(node, newVersion, label);
return action.toObject();
});
actions.push(...options);
} else {
const initialVersion = new Version(undefined, settings.useRfc);
const action = new Action(node, initialVersion, 'initial');
actions.push(action.toObject());
}
const hasOneVersionUndefined = (!versionFromName !== !version);
const hasDifferentVersion = (Boolean(versionFromName) && Boolean(version) && !version.equals(versionFromName));
if (settings.updateName && (hasOneVersionUndefined || hasDifferentVersion)) {
actions.push({
nodeId: node.id,
version: versionFromName ? versionFromName.toString() : undefined,
label: 'fromName',
}, {
nodeId: node.id,
label: 'toName',
version: version ? version.toString() : undefined,
nameVersion: versionFromName ? versionFromName.toString() : undefined,
});
}
const history = Plugin.getHistory(node) || [];
if (history.length > 1) {
actions.push({
nodeId: node.id,
version: history[1].version.toString(),
commitMessage: history[1].commitMessage,
label: 'revert',
});
}
actions.sort((a, b) => Action.getIndex(a.label) - Action.getIndex(b.label));
return actions;
}
function getVersionFromName(node: BaseNode): undefined | Version {
const name = node.name;
const match = versionRegex.exec(name);
if (match) {
return new Version(match[1]);
}
return undefined;
}
function updateVersionInName(node: BaseNode, version?: Version | string): void {
const name = node.name;
const hasVersionInName = getVersionFromName(node) !== undefined;
if (version === undefined || version === '') {
node.name = name.replace(versionRegex, '');
} else {
const newVersionString = `@${version.toString()}`;
node.name = hasVersionInName
? name.replace(versionRegex, newVersionString)
: `${name}${newVersionString}`;
}
}
function updateSettings(message: any): void {
const oldSettings = (Plugin.getConfig('settings') || {}) as SettingsObject;
const newSettings = {...oldSettings, ...(message.settings as SettingsObject)};
Plugin.setConfig('settings', newSettings);
}
function updateCommitMessage(message: any): void {
const node = figma.getNodeById(message.nodeId as string);
const commitMessage = message.commitMessage as string;
const history = Plugin.getHistory(node) || [];
if (history.length > 0 && !history[0].version) {
history[0].commitMessage = commitMessage;
} else {
history.unshift({
commitMessage,
});
}
Plugin.setHistory(node, history);
}
function resizeUi(_width = 300, height = 600) {
const maxHeight = Math.floor(figma.viewport.bounds.height * figma.viewport.zoom.valueOf() * 0.681);
const newHeight = height > maxHeight ? maxHeight : height;
figma.ui.resize(300, newHeight);
}
function updateVersion(message: any): void {
const action = message.action as ActionObject;
const node = figma.getNodeById(action.nodeId);
const {updateName, useCommitMessage, saveToFigmaVersionHistory} = (Plugin.getConfig('settings') || {}) as SettingsObject;
const version = action.version ? new Version(action.version) : '';
Plugin.setVersion(node, version);
if (updateName) {
updateVersionInName(node, version);
}
if (version !== '') {
const history = Plugin.getHistory(node) || [];
if (action.label === 'revert') {
if (history.length > 0 && history[0].version === undefined) {
history.shift();
}
if (history.length > 0) {
history[0].version = undefined;
}
} else {
const commitMessage = useCommitMessage ? message.commitMessage as string : undefined;
if (history.length > 0 && !history[0].version) {
history[0].version = new Version(version);
history[0].commitMessage = commitMessage;
} else {
history.unshift({
version: new Version(version),
commitMessage,
});
}
}
Plugin.setHistory(node, history);
}
if (saveToFigmaVersionHistory
&& version !== ''
&& ['major', 'minor', 'patch', 'rfc', 'release', 'initial'].includes(action.label)) {
const commitMessage = message.commitMessage as string || undefined;
const name = node.name;
const titleParts = name.split('@');
let title = name;
if (Version.pattern.test(titleParts.slice(-1)[0])) {
title = titleParts.slice(0, -1).join('@');
}
title = `${title}@${version.toString()}`;
void figma.saveVersionHistoryAsync(title, commitMessage);
}
}
const selectionChange = (): void => {
updateUi(true);
};
figma.on('currentpagechange', selectionChange);
figma.on('selectionchange', selectionChange);
figma.on('close', () => {
figma.off('selectionchange', selectionChange);
});
// eslint-disable-next-line unicorn/prefer-add-event-listener
figma.ui.onmessage = message => {
switch (message.type) {
case 'settings': {
const settings: SettingsObject = {
useRfc: false,
useCommitMessage: false,
updateName: false,
saveToFigmaVersionHistory: false,
...(Plugin.getConfig('settings') as SettingsObject),
};
figma.ui.postMessage({
type: 'settings',
settings,
});
break;
}
case 'updateSettings': {
updateSettings(message);
updateUi();
break;
}
case 'updateVersion': {
updateVersion(message);
updateUi();
break;
}
case 'updateCommitMessage': {
updateCommitMessage(message);
break;
}
case 'resize': {
resizeUi(message.width, message.height);
break;
}
case 'select': {
const node = figma.getNodeById(message.nodeId);
if (node !== null) {
const page = figma.currentPage;
page.selection = [node as SectionNode];
updateUi();
}
break;
}
default: {
break;
}
}
};
function updateUi(hasSelectionChanged = false) {
const page = figma.currentPage;
const selection = page.selection;
let message = null;
const uiOptions: ShowUIOptions = {};
if (selection.length > 0) {
if (selection.length === 1) {
const settings = (Plugin.getConfig('settings') || {}) as SettingsObject;
const node = selection[0] as BaseNode;
const version = Plugin.getVersion(node);
const history = Plugin.getHistory(node);
const actions = deriveActions(node, settings, version);
uiOptions.title = node.name;
message = {
type: 'actions',
data: actions,
history,
};
} else {
const selectedNodes = selection.map(node => {
const versionValue = Plugin.getNode(node, 'version') as string | undefined;
const version = versionValue ? (new Version(versionValue)).toString() : null;
return {
id: node.id,
name: node.name,
version,
};
});
uiOptions.title = `${selection.length} selected Nodes`;
message = {
type: 'list',
data: selectedNodes,
};
}
} else {
figma.skipInvisibleInstanceChildren = true;
const versionedNodes
= figma.currentPage
.findAll(node => versionRegex.test(node.name)
|| Plugin.getVersion(node) !== undefined)
.map(node => {
const versionValue = Plugin.getNode(node, 'version') as string | undefined;
const version = versionValue ? (new Version(versionValue)).toString() : null;
return {
id: node.id,
name: node.name,
version,
};
});
versionedNodes.sort((a, b) => a.name.localeCompare(b.name));
figma.skipInvisibleInstanceChildren = false;
uiOptions.title = `${versionedNodes.length} versioned Nodes`;
message = {
type: 'list',
data: versionedNodes,
};
}
figma.showUI(ui, uiOptions);
figma.ui.postMessage(message);
}
if (figma.editorType === 'figma') {
updateUi();
} else {
figma.closePlugin('Semantic Versioning is currently only running in Figma Design.');
}