-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathmain.js
504 lines (480 loc) · 16.1 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// Import required modules
const scenegraph = require('scenegraph'),
{LinearGradient, BooleanGroup, RepeatGrid} = scenegraph,
assets = require('assets'),
application = require('application')
// Set variables
let colorAssets = getUsefullColorAssets(),
isInLightMode = autoselectMode(),
panel,
lightModeButton,
darkModeButton,
reloadColorsButton,
generateAssetsButton,
helpButton,
colorsSection,
style,
editionHasBeenDone = false,
helpDialog
/**
* Variables Initializer
*/
// colorAssets initializer - Browse assets to get usefull ones
function getUsefullColorAssets() {
// Reset colorAssets
let usefullColorAssets = []
const allColorAssets = assets.colors.get()
allColorAssets.forEach(lightColor => {
// Check first if the color is light and if a dark variant exists
if ( lightColor.name && lightColor.name.includes('/Light') ) {
let darkColor = aDarkVariantExistOf(lightColor.name, allColorAssets)
if (darkColor !== false) {
usefullColorAssets.push([lightColor,darkColor])
}
}
})
return usefullColorAssets
}
// isInLightMode initializer = Return the current mode by broswing all items (Light or Dark)
function autoselectMode() {
let i = 0
let darkColors = 0
scenegraph.root.children.forEach(rootItem => {
i++
darkColors += searchDarkColors(rootItem,0)
})
return (i >= darkColors)
}
// Recursive function to count dark colors used in the file
function searchDarkColors(item, darkColorsCounter) {
for (const colors of colorAssets) {
if (colors[1].color) { // On fait le test uniquement si c'est une couleur simple
if (item.fill != null && item.fill.value == colors[1].color.value) {
darkColorsCounter++
}
if (item.stroke != null && item.stroke.value == colors[1].color.value) {
darkColorsCounter++
}
}
}
if (item.children != null) {
item.children.forEach(item => {
darkColorsCounter = searchDarkColors(item,darkColorsCounter)
})
}
return darkColorsCounter
}
/**
* Helpers functions to check if a variant of the color exists
*/
function aDarkVariantExistOf(lightColorName, colors) {
const darkVariantName = lightColorName.replace('/Light','/Dark')
return colorExistInWithName(colors, darkVariantName)
}
function aLightVariantExistOf(darkColorName, colors) {
const lightVariantName = darkColorName.replace('/Dark','/Light')
return colorExistInWithName(colors, lightVariantName)
}
function colorExistInWithName(colors, colorName) {
for (const color of colors) {
if (color.name == colorName) {
return color
}
}
return false
}
/**
* Helpers functions to generate dark variant of a light color
*/
function generateDarkVariantOf(lightColor) {
const darkColor = lightColor
if (darkColor.gradientType) {
darkColor.colorStops.forEach(colorStop => {
colorStop.color.r = decreaseColorValue(colorStop.color.r)
colorStop.color.g = decreaseColorValue(colorStop.color.g)
colorStop.color.b = decreaseColorValue(colorStop.color.b)
})
} else {
darkColor.color.r = decreaseColorValue(darkColor.color.r)
darkColor.color.g = decreaseColorValue(darkColor.color.g)
darkColor.color.b = decreaseColorValue(darkColor.color.b)
}
darkColor.name = lightColor.name.replace('/Light','/Dark')
assets.colors.add(darkColor)
}
function decreaseColorValue(colorVal) {
colorVal -= 50
if (colorVal > 205 || colorVal < 0) {
colorVal = 0
}
return colorVal
}
/**
* Helpers functions to generate light variant of a dark color
*/
function generateLightVariantOf(darkColor) {
const lightColor = darkColor
if (darkColor.gradientType) {
lightColor.colorStops.forEach(colorStop => {
colorStop.color.r = increaseColorValue(colorStop.color.r)
colorStop.color.g = increaseColorValue(colorStop.color.g)
colorStop.color.b = increaseColorValue(colorStop.color.b)
})
} else {
lightColor.color.r = increaseColorValue(lightColor.color.r)
lightColor.color.g = increaseColorValue(lightColor.color.g)
lightColor.color.b = increaseColorValue(lightColor.color.b)
}
lightColor.name = darkColor.name.replace('/Dark','/Light')
assets.colors.add(lightColor)
}
function increaseColorValue(colorVal) {
colorVal += 50
if (colorVal < 50 || colorVal > 255) {
colorVal = 255
}
return colorVal
}
/**
* Helper functions to add a suffix to a color name
*/
function renameColorWithSuffix(color,suffix) {
let renamedColor = color
renamedColor.name += suffix
assets.colors.delete(color)
assets.colors.add(renamedColor)
}
function togglePalette(toLight) {
editionHasBeenDone = false
application.editDocument(selection => {
scenegraph.root.children.forEach(artboard => {
paintItem(artboard, toLight)
})
})
isInLightMode = toLight
alertUserIfNotAnyEditionHasBeenDone()
}
function alertUserIfNotAnyEditionHasBeenDone() {
if (!editionHasBeenDone) {
if (!helpDialog) {
helpDialog = document.createElement('dialog');
helpDialog.innerHTML = `
<style>
dialog form {
width: 500px;
}
dialog form h2 {
display: none;
}
</style>
<form method="dialog">
<h1 class="h1">
<span>Dark Mode Palette Toggler Help</span>
</h1>
<hr />
${getHelpHtml()}
<footer>
<button type="submit" uxp-variant="cta">Ok</button>
</footer>
</form>
`;
panel.appendChild(helpDialog)
}
return helpDialog.showModal();
}
}
function paintItem(item, toLight) {
let oldColorIndex = toLight ? 1 : 0
if (item.mask) { return }
if (item instanceof BooleanGroup) { return }
if (item instanceof RepeatGrid) { return }
for (const colors of colorAssets) {
if (colors[oldColorIndex].color) {
// Normal color
if (item.fill && item.fill.value == colors[oldColorIndex].color.value) {
item.fill = colors[(oldColorIndex + 1) % 2].color
editionHasBeenDone = true
}
if (item.stroke && item.stroke.value == colors[oldColorIndex].color.value) {
item.stroke = colors[(oldColorIndex + 1) % 2].color
editionHasBeenDone = true
}
} else if (colors[oldColorIndex].gradientType) {
// Gradient
if (item.fill && item.fill instanceof LinearGradient) {
let gradient = item.fill.clone()
gradient.colorStops = colors[(oldColorIndex + 1) % 2].colorStops
item.fill = gradient
editionHasBeenDone = true
}
}
}
if (item.children != null) {
item.children.forEach(item => {
paintItem(item, toLight)
})
}
}
/**
* Generate assets
*/
function generateAssets() {
application.editDocument(selection => {
const allColorAssets = assets.colors.get()
allColorAssets.forEach(color => {
if (color.name) {
if (color.name.includes('/Light')) {
if (aDarkVariantExistOf(color.name, allColorAssets) === false) {
generateDarkVariantOf(color)
}
} else if (color.name.includes('/Dark')) {
if (aLightVariantExistOf(color.name, allColorAssets) === false) {
generateLightVariantOf(color)
}
} else {
if (isInLightMode) {
renameColorWithSuffix(color,'/Light')
generateDarkVariantOf(color)
} else {
renameColorWithSuffix(color,'/Dark')
generateLightVariantOf(color)
}
}
}
})
})
reloadColorsSection()
}
/**
* Toggle dark palette
*/
function toggleDarkPalette(e) {
if (!e.target.classList.contains('not-active')) {
return
}
togglePalette(false)
panel.querySelector('#light').classList.add('not-active')
panel.querySelector('#dark').classList.remove('not-active')
}
/**
* Toggle light palette
*/
function toggleLightPalette(e) {
if (!e.target.classList.contains('not-active')) {
return
}
togglePalette(true)
panel.querySelector('#light').classList.remove('not-active')
panel.querySelector('#dark').classList.add('not-active')
}
/**
* Reload assets and actualize colors section
*/
function reloadColorsSection() {
colorAssets = getUsefullColorAssets()
style.innerHTML = getCSS()
colorsSection.innerHTML = getColorLinesHtml()
}
/**
* Generate CSS
*/
function getColorsCSS() {
let css = ``
colorAssets.forEach(colors => {
colors.forEach(color => {
if (color.gradientType) {
if (color.gradientType == 'linear') {
css += `.color[data-name=${color.name.replace(/ /g, '')}] {
background: linear-gradient(to right`
} else if (color.gradientType == 'radial') {
css += `.color[data-name=${color.name.replace(/ /g, '')}] {
background: radial-gradient(circle at center`
}
color.colorStops.forEach(colorStop => {
css += `, ${colorStop.color.toHex()} ${colorStop.stop * 100}%`
})
css += `);
}`
} else {
css += `.color[data-name=${color.name.replace(/ /g, '')}] {
background: ${color.color.toHex()};
}`
}
})
})
return css
}
function getCSS() {
return `${getColorsCSS()}
.titles {
display: flex;
justify-content: space-around;
}
.color-line {
display: flex;
align-items: center;
justify-content: space-around;
margin-bottom: 20px;
}
.color {
width: 30vw;
height: 30vw;
max-width: 100px;
max-height: 100px;
background: black;
border-radius: 10px;
border: 1px solid rgb(220,220,220);
}
h2,
.secondary-actions,
header.color-line {
text-align: center;
margin-bottom: 5px;
}
button.not-active {
opacity: 0.3;
}
button.not-active:hover {
opacity: 0.7;
}
.help-detail .user-guide,
.help-detail .bug-alert,
.help-detail .plugin-limits {
display: none;
}
.help-detail.open .user-guide,
.help-detail.open .bug-alert,
.help-detail.open .plugin-limits {
display: block;
}
.user-guide,
.bug-alert,
.plugin-limits {
padding: 5px;
margin-bottom: 5px;
}
.user-guide {
background: rgba(0, 255, 0, 0.1);
}
.bug-alert {
background: rgba(0, 0, 255, 0.1);
}
.plugin-limits {
background: rgba(255, 255, 0, 0.2);
}
.help-detail p {
text-align: left;
line-height: 1.7;
margin-bottom: 10px;
}`
}
/**
* Generate HTML
*/
function getColorLinesHtml() {
let lines = ``
colorAssets.forEach(colors => {
lines += `
<h2>${colors[0].name.replace('/Light','')}</h2>
<div class="color-line">
<div class="color" data-name="${colors[0].name.replace(/ /g, '')}"></div>
<span><-></span>
<div class="color" data-name="${colors[1].name.replace(/ /g, '')}"></div>
</div>`
})
return lines
}
function getHeaderHtml() {
return `<header class="color-line">
<button id="light" type="submit" ${isInLightMode ? '' : 'class="not-active"'} uxp-variant="cta">Light</button>
<button id="dark" type="submit" ${isInLightMode ? 'class="not-active"' : ''} uxp-variant="cta">Dark</button>
</header>`
}
function getSecondaryActionsHtml() {
return `<div class="secondary-actions">
<button id="generateVariants" uxp-variant="primary">Generate assets variants</button>
</div>
<div class="secondary-actions">
<button id="reloadColors" uxp-variant="primary">Reload Color assets</button>
</div>`
}
function getHelpHtml() {
return `
<div class="user-guide">
<h3>User guide</h3>
<p>1. Use the "Generate assets variants" button to transform your named color assets in compatible ones <br/> OR <br/> For each color, create a name/Light and name/Dark asset and then use the "Reload colors" button</p>
<p>2. Switch between light-mode and dark-mode via the DarkModeToggler</p>
<p>3. Edit colors via the Adobe XD assets panel</p>
</div>
<div class="bug-alert" >
<h3>If colors doesn't toggle well...</h3>
<p>
If everything is well set up but the plugin doesn't toggle colors properly : Cut all your artboards in the same time and paste it again. Then, try again to switch between light and darkmode, the manipulation must have fix the problem.
</p>
</div>
<div class="plugin-limits">
<p>
Because of software restrictions, this plugin is not able to edit : circular gradients, repeatGrids and objects into a shape mask. It can change in the futur if these restrictions are removed.
</p>
</div>
<div class="titles">
<button uxp-variant="action" id="help-action">Open In-App User Guide</button>
</div>
`
}
function initializePanel() {
function getHtml() {
return `
<style>${getCSS()}</style>
<form method="dialog" id="main">
<div class="help-detail">
${getHelpHtml()}
</div>
${getHeaderHtml()}
<div id="colorsSection">
${getColorLinesHtml()}
</div>
</form>
${getSecondaryActionsHtml()}`
}
function setElementVariables() {
colorsSection = panel.querySelector('#colorsSection')
lightModeButton = panel.querySelector('#light')
darkModeButton = panel.querySelector('#dark')
reloadColorsButton = panel.querySelector('#reloadColors')
generateAssetsButton = panel.querySelector('#generateVariants')
helpButton = panel.querySelector('#help-action')
style = panel.querySelector('style')
}
function setEventListeners() {
lightModeButton.addEventListener('click', toggleLightPalette)
darkModeButton.addEventListener('click', toggleDarkPalette)
reloadColorsButton.addEventListener('click', reloadColorsSection)
generateAssetsButton.addEventListener('click', generateAssets)
helpButton.addEventListener('click',() => {
const helpDetail = panel.querySelector('.help-detail')
helpDetail.classList.toggle('open')
if (helpDetail.classList.contains('open')) {
helpButton.innerText = `Hide User Guide`
} else {
helpButton.innerText = `Open In-App User Guide`
}
})
}
panel = document.createElement("div")
panel.innerHTML = getHtml()
setElementVariables()
setEventListeners()
return panel
}
/**
* Needed function to show left plugin panel
*/
function show(event) {
if (!panel) event.node.appendChild(initializePanel());
}
module.exports = {
panels: {
darkModeToggler: { show }
}
};