-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathmain.js
427 lines (379 loc) · 12 KB
/
main.js
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/**
* @file 選択:共通,選択:オブジェクト
* @version 2.0.0
* @author sttk3.com
* @copyright (c) 2020 sttk3.com
*/
const { alert, error } = require('./lib/dialogs.js') ;
const { selection, root } = require('scenegraph') ;
/**
* selection.itemsが空かどうかを返す
* @return {Boolean}
*/
function isEmpty() {
let res = false ;
if(selection.items.length <= 0) {
error('Error : ', 'Please select the object, and then execute this plugin.') ;
res = true ;
}
return res ;
}
/**
* 配列の要素数が指定した数,またはそれ以下かどうかを返す
* @param {Array} arr 対象の配列
* @param {Integer} value 個数
* @return {Boolean}
*/
function lessThanOrEquals(arr, value) {
let res = false ;
if(arr.length <= value) {
alert('SelectMenu : ', 'There were no items to select.') ;
res = true ;
}
return res ;
}
/**
* 配下のnodeを取得する
* @param {SceneNodeList} objs 探索開始位置
* @param {Boolean} accessibleOnly trueの場合,EditContextが編集可能な範囲にあるアイテムのみ取得する
* @param {Set} [excludeSet] childrenの探索を除外する種類のセット
* @return {Array}
*/
function entireContents(objs, accessibleOnly = true, excludeSet = new Set([])) {
let res = [] ;
let children, itemClass ;
objs.forEach((aItem) => {
itemClass = aItem.constructor.name ;
if(accessibleOnly) {
if(selection.isInEditContext(aItem)) {res.push(aItem) ;}
} else {
res.push(aItem) ;
}
children = aItem.children ;
if(children && !excludeSet.has(itemClass)) {
res = res.concat(entireContents(children, excludeSet)) ;
}
}) ;
return res ;
}
/**
* 配列にグループ本体とそのグループの配下が同時にある場合,グループ本体だけを残す
* @param {Array} targetItems 対象の配列
* @return {Array}
*/
function rejectDescendants(targetItems) {
const ancestorSet = new Set() ;
let isInAncestors ;
const res = targetItems.filter((aNode) => {
isInAncestors = ancestorSet.has(aNode.parent) ;
if(/Group/.test(aNode.constructor.name)) {
ancestorSet.add(aNode) ;
}
return (!isInAncestors) ;
}) ;
return res ;
}
/**
* 同じ名前のアイテムを選択する
* @return
*/
function selectSameNameMenuItem() {
if(isEmpty()) {return ;}
const itemName = selection.items[0].name ;
let targetItems = entireContents(root.children, true).filter((aNode) => {
return (aNode.name === itemName) ;
}) ;
if(lessThanOrEquals(targetItems, 1)) {return ;}
targetItems = rejectDescendants(targetItems) ;
if(lessThanOrEquals(targetItems, 1)) {return ;}
selection.items = targetItems ;
}
/**
* 同じ名前と種類のアイテムを選択する
* @return
*/
function selectSameNameAndClassMenuItem() {
if(isEmpty()) {return ;}
const itemName = selection.items[0].name ;
const itemClass = selection.items[0].constructor.name ;
let targetItems = entireContents(root.children, true).filter((aNode) => {
return (aNode.name === itemName && aNode.constructor.name === itemClass) ;
}) ;
if(lessThanOrEquals(targetItems, 1)) {return ;}
targetItems = rejectDescendants(targetItems) ;
if(lessThanOrEquals(targetItems, 1)) {return ;}
selection.items = targetItems ;
}
/**
* 同じ種類のアイテムを選択する
* @return
*/
function selectSameClassMenuItem() {
if(isEmpty()) {return ;}
const itemClass = selection.items[0].constructor.name ;
let targetItems = entireContents(root.children, true).filter((aNode) => {
return (aNode.constructor.name === itemClass) ;
}) ;
if(lessThanOrEquals(targetItems, 1)) {return ;}
targetItems = rejectDescendants(targetItems) ;
if(lessThanOrEquals(targetItems, 1)) {return ;}
selection.items = targetItems ;
}
/**
* 同じコンポーネントのインスタンスを選択する
* @return
*/
function selectSameComponentsMenuItem() {
if(isEmpty()) {return ;}
const itemClass = selection.items[0].constructor.name ;
if(itemClass !== 'SymbolInstance') {return ;}
const itemSymbol = selection.items[0].symbolId ;
let targetItems = entireContents(root.children, true).filter((aNode) => {
return (aNode.constructor.name === itemClass && aNode.symbolId === itemSymbol) ;
}) ;
if(lessThanOrEquals(targetItems, 1)) {return ;}
targetItems = rejectDescendants(targetItems) ;
if(lessThanOrEquals(targetItems, 1)) {return ;}
selection.items = targetItems
}
/**
* 同じローカル座標のアイテムを選択する
* @return
*/
function selectSameLocalPositionMenuItem() {
if(isEmpty()) {return ;}
const masterPos = selection.items[0].topLeftInParent ;
let itemPos ;
let targetItems = entireContents(root.children, true).filter((aNode) => {
itemPos = aNode.topLeftInParent ;
return (masterPos.x === itemPos.x && masterPos.y === itemPos.y) ;
}) ;
if(lessThanOrEquals(targetItems, 1)) {return ;}
targetItems = rejectDescendants(targetItems) ;
if(lessThanOrEquals(targetItems, 1)) {return ;}
selection.items = targetItems ;
}
/**
* 表のように規則的に並ぶアイテムを座標でフィルタし選択する。RepeatGridの中身には対応していない
* @param {Function} filterCallback フィルタ関数
* @return
*/
function selectGridData(filterCallback) {
if(isEmpty()) {return ;}
const hasArtwork = selection.hasArtwork ;
const oldSel = selection.items[0] ;
const masterPos = oldSel.globalBounds ;
let tempItems ;
if(hasArtwork) {
// オブジェクトを選択している場合
tempItems = entireContents(root.children, true) ;
} else {
// アートボードを選択している場合
tempItems = root.children.filter((aNode) => {
return (aNode.constructor.name === 'Artboard') ;
}) ;
}
// 座標で絞る
let targetItems = tempItems.filter((aItem) => {
return filterCallback(aItem, masterPos) ;
}) ;
if(lessThanOrEquals(targetItems, 1)) {return ;}
// グループ本体とそのグループの配下が同時にある場合,グループ本体だけを残す
if(hasArtwork) {
targetItems = rejectDescendants(targetItems) ;
if(lessThanOrEquals(targetItems, 1)) {return ;}
}
// Singariと連携しやすくするため,元の選択アイテムを末尾に送る(最後のアイテムをキーオブジェクトとみなすから)
for(let i = 0, len = targetItems.length ; i < len ; i++) {
if(targetItems[i] === oldSel) {
targetItems.splice(i, 1) ;
break ;
}
}
targetItems.push(oldSel) ;
selection.items = targetItems ;
}
/**
* 同じ列(同じLeft座標)のアイテムを選択する
* @return
*/
function selectSameColumnMenuItem() {
let itemPos ;
selectGridData((aNode, masterPos) => {
itemPos = aNode.globalBounds ;
return (itemPos.x === masterPos.x) ;
}) ;
}
/**
* 選択アイテムと同じ列の先頭までを選択する
* @return
*/
function selectAboveOfColumnMenuItem() {
let itemPos ;
selectGridData((aNode, masterPos) => {
itemPos = aNode.globalBounds ;
return ((itemPos.x === masterPos.x) && (itemPos.y <= masterPos.y)) ;
}) ;
}
/**
* 選択アイテムと同じ列の末尾までを選択する
* @return
*/
function selectBelowOfColumnMenuItem() {
let itemPos ;
selectGridData((aNode, masterPos) => {
itemPos = aNode.globalBounds ;
return ((itemPos.x === masterPos.x) && (itemPos.y >= masterPos.y)) ;
}) ;
}
/**
* 同じ行(同じTop座標)のアイテムを選択する
* @return
*/
function selectSameRowMenuItem() {
let itemPos ;
selectGridData((aNode, masterPos) => {
itemPos = aNode.globalBounds ;
return (itemPos.y === masterPos.y) ;
}) ;
}
/**
* 選択アイテムと同じ行の先頭までを選択する
* @return
*/
function selectLeftOfRowMenuItem() {
let itemPos ;
selectGridData((aNode, masterPos) => {
itemPos = aNode.globalBounds ;
return ((itemPos.y === masterPos.y) && (itemPos.x <= masterPos.x)) ;
}) ;
}
/**
* 選択アイテムと同じ行の末尾までを選択する
* @return
*/
function selectRightOfRowMenuItem() {
let itemPos ;
selectGridData((aNode, masterPos) => {
itemPos = aNode.globalBounds ;
return ((itemPos.y === masterPos.y) && (itemPos.x >= masterPos.x)) ;
}) ;
}
/**
* すべてのアートボードを選択する
* @return
*/
function selectAllArtboardMenuItem() {
const targetItems = root.children.filter((aNode) => {
return (aNode.constructor.name === 'Artboard') ;
}) ;
if(lessThanOrEquals(targetItems, 0)) {
return ;
} else {
if(selection.isInEditContext(targetItems[0])) {
selection.items = targetItems ;
} else {
error('Error : ', 'Please execute this plugin with nothing selected or an artboard selected.') ;
}
}
}
/**
* すべてのテキストを選択する
* @return
*/
function selectAllTextMenuItem() {
const itemClass = 'Text' ;
const targetItems = entireContents(root.children, true).filter((aNode) => {
return (aNode.constructor.name === itemClass) ;
}) ;
if(lessThanOrEquals(targetItems, 0)) {return ;}
selection.items = targetItems ;
}
/**
* エリアテキストを選択する
* @return
*/
function selectAreaTextsMenuItem() {
const itemClass = 'Text' ;
const targetItems = entireContents(root.children, true).filter((aNode) => {
return (aNode.constructor.name === itemClass && aNode.areaBox !== null) ;
}) ;
if(lessThanOrEquals(targetItems, 0)) {return ;}
selection.items = targetItems ;
}
/**
* ポイントテキストを選択する
* @return
*/
function selectPointTextsMenuItem() {
const itemClass = 'Text' ;
const targetItems = entireContents(root.children, true).filter((aNode) => {
return (aNode.constructor.name === itemClass && aNode.areaBox === null) ;
}) ;
if(lessThanOrEquals(targetItems, 0)) {return ;}
selection.items = targetItems ;
}
/**
* ロックされているアイテムを選択する
* @return
*/
function selectLockedItemsMenuItem() {
let targetItems = entireContents(root.children, true).filter((aNode) => {
return (aNode.locked) ;
}) ;
if(lessThanOrEquals(targetItems, 0)) {return ;}
targetItems = rejectDescendants(targetItems) ;
if(lessThanOrEquals(targetItems, 1)) {return ;}
selection.items = targetItems ;
}
/**
* 非表示のアイテムを選択する
* @return
*/
function selectHiddenItemsMenuItem() {
let targetItems = entireContents(root.children, true).filter((aNode) => {
return (!aNode.visible) ;
}) ;
if(lessThanOrEquals(targetItems, 0)) {return ;}
targetItems = rejectDescendants(targetItems) ;
if(lessThanOrEquals(targetItems, 1)) {return ;}
selection.items = targetItems ;
}
/**
* 書き出し用にマークされたアイテムを選択する
* @return
*/
function selectMarkedItemsMenuItem() {
let targetItems = entireContents(root.children, true).filter((aNode) => {
return (aNode.markedForExport) ;
}) ;
if(lessThanOrEquals(targetItems, 0)) {return ;}
targetItems = rejectDescendants(targetItems) ;
if(lessThanOrEquals(targetItems, 1)) {return ;}
selection.items = targetItems ;
}
function ignoreMenuItem(selection) {
}
module.exports = {
commands: {
ignore: ignoreMenuItem,
selectSameName: selectSameNameMenuItem,
selectSameNameAndClass: selectSameNameAndClassMenuItem,
selectSameClass: selectSameClassMenuItem,
selectSameComponents: selectSameComponentsMenuItem,
selectSameLocalPosition: selectSameLocalPositionMenuItem,
selectSameRow: selectSameRowMenuItem,
selectLeftOfRow: selectLeftOfRowMenuItem,
selectRightOfRow: selectRightOfRowMenuItem,
selectSameColumn: selectSameColumnMenuItem,
selectAboveOfColumn: selectAboveOfColumnMenuItem,
selectBelowOfColumn: selectBelowOfColumnMenuItem,
selectAllArtboard: selectAllArtboardMenuItem,
selectAllText: selectAllTextMenuItem,
selectAreaTexts: selectAreaTextsMenuItem,
selectPointTexts: selectPointTextsMenuItem,
selectLockedItems: selectLockedItemsMenuItem,
selectHiddenItems: selectHiddenItemsMenuItem,
selectMarkedItems: selectMarkedItemsMenuItem
}
} ;