-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathmain.js
256 lines (239 loc) · 7.88 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
/*
* Sample plugin scaffolding for Adobe XD.
*
* Visit http://adobexdplatform.com/ for API docs and more sample code.
*/
const { selection } = require("scenegraph");
var commands = require("commands");
let application = require("application");
let panel;
let actionData;
let numReplaced
let clipboard;
let marked;
let status={
copy:'',
mark:'',
global:''
};
function show(event) {
if (!panel) {
panel = document.createElement("div");
event.node.appendChild(panel);
}
}
function reset(){
actionData=[];
numReplaced=0
clipboard=null;
marked=null;
status={
copy:'',
mark:'',
global:''
}
update();
}
function globalFontReplace(){
if(clipboard==undefined ){
status.global = `<img src="images/small-x.png" class="small-check" /> `+"You must copy a font first";
return
}
if(marked==undefined ){
status.global = `<img src="images/small-x.png" class="small-check" /> `+"You must mark a font for replacement first";
return
}
application.editDocument( function(selection, root){
if(selection.items.length>0 ){
status.global = `<img src="images/small-x.png" class="small-check" /> `+"Deselect all and try again";
return
}
actionData = [];
numReplaced = 0;
walkDownTree(root, onGlobalFontReplaceNode, selection);
let unaffectedArtboards = [];
actionData.forEach(actionDataNode => {
const unaffectedNodeAncestry = getNodeAncestry(actionDataNode);
const unaffectedNodeArtboard = unaffectedNodeAncestry[unaffectedNodeAncestry.length - 2]
if (unaffectedArtboards.indexOf(unaffectedNodeArtboard) === -1) {
unaffectedArtboards.push(unaffectedNodeArtboard);
}
});
selection.items = unaffectedArtboards;
status.global = `<div class="hr row break">
<div class="big-check-wrapper"><img src="images/big-check.png" /></div>
<p class="large">${numReplaced} replacements made.</p>
<div class="pos-r"><img src="images/small-alert.png" class="small-check alert-icon" />
<div class="status-not-replaced">
<p class="small"> There are ${actionData.length} instances that could not be replaced. Check the ${unaffectedArtboards.length} selected artboards.
</p>
</div>
</div>
<button id="reset" uxp-variant="secondary">Reset</button>
</div>`;
});
}
function onGlobalFontReplaceNode(node, selection, safe=true) {
if (node.constructor.name == "Text") {
if (node.fontFamily == marked.fontFamily && node.fontStyle == marked.fontStyle) {
if (safe) {
node.fontFamily = clipboard.fontFamily;
node.fontStyle = clipboard.fontStyle;
numReplaced++;
} else {
actionData.push(node);
}
}
}
}
function getNodeAncestry(node){
let ancestors = [];
let currentNode = node;
while (currentNode.parent!=null) {
ancestors.push(currentNode.parent);
currentNode = currentNode.parent;
}
return ancestors;
}
function walkDownTree(node, command, value = null) {
command(node, value);
if (
(!node.mask) &&
(node.constructor.name != "RepeatGrid") &&
(node.constructor.name != "BooleanGroup") &&
(node.constructor.name != "LinkedGraphic")
) {
node.children.forEach(childNode => {
walkDownTree(childNode, command, value);
});
}else{
node.children.forEach(childNode => {
unsafeWalkDownTree(childNode, command, value);
});
}
}
function unsafeWalkDownTree(node, command, value = null) {
command(node, value, false); //Pass false to the command, so we can store the nodes that are out of edit context
node.children.forEach(childNode => {
unsafeWalkDownTree(childNode, command, value);
});
}
function copyFont() {
storeFont("clipboard");
}
function markForReplacement() {
storeFont("mark");
}
function storeFont(store){
application.editDocument(function(selection,root) {
let validSelection = false;
if(selection.items.length>0){
if (selection.items[0].constructor.name == "Text"){
validSelection = true;
if (store=="clipboard"){
clipboard = {
fontFamily: selection.items[0].fontFamily,
fontStyle: selection.items[0].fontStyle
}
}
else if (store=="mark"){
marked = {
fontFamily: selection.items[0].fontFamily,
fontStyle: selection.items[0].fontStyle
}
}
}
}
if(validSelection) {
if (store=="clipboard"){
status.copy =`<img src="images/small-check.png" class="small-check" /> `+ clipboard.fontFamily + ' ' + clipboard.fontStyle;
}
else if (store=="mark"){
status.mark = `<img src="images/small-check.png" class="small-check" /> `+marked.fontFamily + ' ' + marked.fontStyle;
}
selection.items=[];
} else {
if (store=="clipboard"){
status.copy = `<img src="images/small-x.png" class="small-check" /> `+"Please select a text node";
}
else if (store=="mark"){
status.mark = `<img src="images/small-x.png" class="small-check" /> `+"Please select a text node";
}
}
});
}
function hide(){
}
function update(){
const isStepOne = clipboard==undefined && marked==undefined;
const isStepTwo = clipboard!=undefined && marked==undefined;
const isStepThree = clipboard!=undefined && marked!=undefined;
let html=`
<style>
button {
min-width:185px;
}
.row{
width:100%;
}
.break {
flex-wrap: wrap;
}
.show {
display: block;
}
.hide {
display: none;
}
.icon{
width:16px;
height:16px;
}
.big-check-wrapper{
padding-left:75px;
}
.block{
display:block;
}
.status {
padding-left:10px;
padding-bottom:5px;
}
.hr{
border-top:1px solid #E0E0E0;
padding-top:10px;
margin-top:5px;
}
.pos-r{
position:relative;
}
.alert-icon{
position:absolute;
top:8px;
left:9px;
}
</style>
<div class="row break">
<button id="copyFont" uxp-variant="${( isStepOne ? "primary" : "secondary" )}"> Copy Font</button>
<div class="row break status" id="copyStatus">${status.copy}</div>
<button id="markForReplacement" uxp-variant="${( isStepTwo ? "primary" : "secondary" )}">Mark For Replacement</button>
<div class="row break status" id="markStatus">${status.mark}</div>
<button id="globalFontReplace" uxp-variant="${( isStepThree ? "cta" : "secondary" )}" ${( isStepThree ? "" : "disabled" )}>Global Font Replace</button>
<div class="row break status global-status" id="status">${status.global}</div>
</div>
`;
panel.innerHTML=html;
panel.querySelector("#copyFont").addEventListener("click", copyFont);
panel.querySelector("#markForReplacement").addEventListener("click", markForReplacement);
panel.querySelector("#globalFontReplace").addEventListener("click", globalFontReplace);
panel.querySelector("#reset").addEventListener("click", reset);
}
module.exports = {
panels: {
globalFontReplace: {
show,
hide,
update
}
}
};