-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathmain.js
130 lines (112 loc) · 3.54 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
const commands = require("commands");
const { alert, error } = require("/lib/dialogs");
// Generate and show the dialog and get user's input
async function showDialog() {
let buttons = [
{ label: 'Cancel', variant: 'primary' },
{ label: 'Find and replace', variant: 'cta', type: 'submit' }
];
const dialog = document.createElement('dialog');
dialog.innerHTML = `
<form method="dialog">
<h1>Find and replace</h1>
<label>
<span>Find what</span>
<input type="text" id="find" name="find" />
</label>
<label>
<span>Replace with</span>
<input type="text" id="replace" name="replace" />
</label>
<footer>
${buttons.map(({label, type, variant} = {}, idx) => `<button id="btn${idx}" type="${type}" uxp-variant="${variant}">${label}</button>`).join('')}
</footer>
</form>
`;
let okBtnIdx = -1;
let cancelBtnIdx = -1;
let clickedBtnIdx = -1;
const form = dialog.querySelector('form');
form.onsubmit = () => dialog.close('ok');
buttons.forEach(({type, variant} = {}, idx) => {
const button = dialog.querySelector(`#btn${idx}`);
if (type === 'submit' || variant === 'cta') {
okBtnIdx = idx;
}
if (type === 'reset') {
cancelBtnIdx = idx;
}
button.onclick = e => {
e.preventDefault();
clickedBtnIdx = idx;
dialog.close( idx === cancelBtnIdx ? 'reasonCanceled' : 'ok');
}
});
try {
document.appendChild(dialog);
const response = await dialog.showModal();
if (response === 'reasonCanceled') {
return {which: cancelBtnIdx, value: ''};
} else {
if (clickedBtnIdx === -1) {
clickedBtnIdx = okBtnIdx;
}
return {
which: clickedBtnIdx,
values: {
findValue: dialog.querySelector("#find").value || "",
replaceValue: dialog.querySelector("#replace").value || "",
}
};
}
} catch (err) {
return {which: cancelBtnIdx, value: ''};
} finally {
dialog.remove();
}
}
// Check selection and user's input, and if all okay replace
async function findAndReplace (selection) {
const totalObjCount = selection.items.length;
if (totalObjCount === 0) {
await error(
"Selection error",
"Please select at least one text layer to proceed."
);
return ;
}
if ((totalObjCount === 1) && (selection.items[0].constructor.name !== "Text")) {
await error(
"Selection error",
"Please select at least one text layer to proceed."
);
return ;
}
const response = await showDialog();
if (response.which === 0) {
return ;
}
const findUser = response.values.findValue;
const replaceUser = response.values.replaceValue;
if (findUser !== '') {
for (let node of selection.items) {
if (node.constructor.name === "Text") {
let str = node.text;
str = str.split(findUser).join(replaceUser);
node.text = str;
}
}
}
else {
await error(
"Input error",
"Please enter a value to be found."
);
return ;
}
}
module.exports = {
commands: {
findAndReplace
}
}