forked from dreygur/XDPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
185 lines (152 loc) · 3.75 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
let panel;
var newText = [];
var selectionCount = 0;
var input = "";
function create() {
const html = `
<style>
form {
width:100%;
}
label {
width: 100%;
}
label input {
width: 100%;
}
label span {
width: 100%;
}
#warning {
width: 100%;
background-color: #0DA7DC;
padding: 12px;
border-radius: 4px;
color: #FFFFFF;
}
.show {
display: block;
}
.hide {
display: none;
}
</style>
<form id="main">
<div>
<label>
<span>Text Pattern</span>
<input type="text" id="inputValue" value="" placeholder="Example: Item 1" />
</label>
</div>
<footer><button id="apply" type="submit" uxp-variant="cta">Apply</button></footer>
</form>
<div id="warning">Select at least one Text object.</div>
`;
panel = document.createElement("div");
panel.innerHTML = html;
panel.querySelector("form").addEventListener("submit", updateText);
return panel;
}
function parseRange(value) {
const reg = new RegExp(/(\d*)([^\d:]+)(\d*)$/);
const rangeMatch = value.match(reg);
const [a, b] = rangeMatch || [];
if (a && b) {
return rangeMatch;
}
return;
};
function parseNumber(value) {
const reg2 = new RegExp(/(\d*)$/);
const numberMatch = value.match(reg2);
if (numberMatch && numberMatch[0] !== "") {
return numberMatch[0];
}
return;
};
function createNewText() {
console.log("### CreateNewText");
newText = [];
// handle case where user has entered a range
const range = parseRange(input);
if (range) {
const [originalRangeStr, from, delim, to] = range;
const prefix = ""
const diff = to - from;
// Loop through each element and create label
var elementNumber = 0;
for (var i = 0; i < selectionCount; i++) {
var newFrom = parseInt(from) + (diff + 1) * i;
var newTo = parseInt(to) + (diff + 1) * i;
newText[i] = input.replace(
originalRangeStr,
`${newFrom}${delim}${newTo}`
);
}
return;
}
// handle case where user input just ends in a single number
const number = parseNumber(input);
if (number) {
// Loop through each element and create label
var elementNumber = 0;
for (var i = 0; i < selectionCount; i++) {
var newNumber = parseInt(number) + i;
newText[i] = input.replace(
number,
newNumber
);
}
return;
}
// handle case where user input does not have a suffix
// Loop through each element and create label
for (var i = 0; i < selectionCount; i++) {
var newNumber = parseInt(number) + i;
newText[i] = input;
}
return;
}
function getInput() {
input = document.querySelector("#inputValue").value;
}
function updateText() {
console.log("### Update Text");
const { Text } = require("scenegraph");
getInput();
createNewText();
const { editDocument } = require("application");
editDocument({ editLabel: "Update Text" }, function(selection) {
var itemIndex = 0;
selection.items.map(i => {
i.text = newText[itemIndex];
itemIndex++;
});
});
}
function show(event) {
if (!panel) event.node.appendChild(create());
}
function update(selection) {
console.log("### Update");
const { Text } = require("scenegraph");
const form = document.querySelector("form");
const warning = document.querySelector("#warning");
if (!selection || !(selection.items[0] instanceof Text)) {
form.className = "hide";
warning.className = "show";
} else {
form.className = "show";
warning.className = "hide";
}
selectionCount = selection.items.length;
console.log("selectionCount: " + selectionCount);
}
module.exports = {
panels: {
sequentialText: {
show,
update
}
}
};