-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
208 lines (182 loc) · 6.04 KB
/
utils.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
import { ExColor } from "./ExColor.js";
/**
* C#-style string format. Does not support named arguments.
*
* @param {string} str
* @param {any[]} args
*
* @link https://stackoverflow.com/a/8463429
*/
export function stringFormat(str, ...args) {
return str.replace(/\{\{|\}\}|\{(\d+)\}/g, function (curlyBrack, index) {
return ((curlyBrack == "{{") ? "{" : ((curlyBrack == "}}") ? "}" : args[index]));
});
}
/**
* Loads an image from the specified url.
*
* @param {string} url
* @returns {Promise<HTMLImageElement>}
*/
export function loadImage(url) {
return new Promise((resolve, reject) => {
const img = new Image();
img.addEventListener('load', () => resolve(img), { once: true });
img.addEventListener('error', reject, { once: true });
img.src = url;
});
}
/**
* Ensures that the given image is fully loaded.
*
* @param {HTMLImageElement} img
* @returns {Promise<HTMLImageElement>}
*/
export function waitForImage(img) {
return new Promise((resolve, reject) =>
{
img.addEventListener('load', () => resolve(img), { once: true });
img.addEventListener('error', reject, { once: true });
if (img.complete)
resolve(img);
});
}
/**
* Loads a script from a URI.
*
* @param {string} scriptUri
*
* @returns {Promise<void>}
*/
export function loadScript(scriptUri) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.type = "text/javascript";
script.async = true;
script.addEventListener('load', () => {
script.parentNode.removeChild(script);
resolve();
}, { once: true });
script.addEventListener('error', (err) => {
script.parentNode.removeChild(script);
reject(err);
}, { once: true });
script.src = scriptUri;
document.body.appendChild(script);
});
}
/**
* @param {string} groupName
* @returns {HTMLInputElement}
*/
export function getCheckedRadioButtonInGroup(groupName) {
// @ts-ignore
return document.querySelector(`input[type="radio"][name="${groupName}"]:checked`).value;
}
/**
*
* @param {string} groupName
* @param {string} value
* @returns {HTMLInputElement}
*/
export function getRadioButtonInGroup(groupName, value) {
return document.querySelector(`input[type="radio"][name="${groupName}"][value="${value}"]`);
}
/**
* @param {string} groupName
* @returns {NodeListOf<HTMLInputElement>}
*/
export function getAllRadioButtonsInGroup(groupName) {
return document.querySelectorAll(`input[type="radio"][name="${groupName}"]`)
}
/**
*
* @param {HTMLInputElement} picker
* @param {HTMLInputElement} textbox
* @param {(textboxValue: string) => string?} textboxValueProcessor
* @param {(colorValue: string) => void} onSuccess
*/
export function linkInputColorTextPicker(picker, textbox, textboxValueProcessor, onSuccess) {
// Using .addEventListener was not seeming to work correctly, so I swapped to JQuery.
// It was probably an error on my part.
$(picker).on('change', () => {
const pickerValue = picker.value;
const textValue = convertColorPickerValueToTextValue(pickerValue);
textbox.value = textValue;
textbox.setAttribute("lastValidValue", textValue);
onSuccess(pickerValue);
});
$(textbox).on('focusout', () => {
let textboxValue = textboxValueProcessor(textbox.value);
if (textboxValue === null) {
const lastValidValue = textbox.getAttribute("lastValidValue") ?? picker.value;
textbox.value = lastValidValue;
textbox.setAttribute("lastValidValue", lastValidValue);
return;
}
let pickerValue = "#000000";
if (textboxValue.length !== 0)
pickerValue = '#' + ExColor.hexShortToLong(textboxValue);
textbox.value = textboxValue;
textbox.setAttribute("lastValidValue", textboxValue);
picker.value = pickerValue;
onSuccess(textboxValue);
});
}
/**
* Sets the values of the text and color inputs of a combo picker.
*
* @param {HTMLElement} elemComboWrapper
* @param {string?} hexValue
*/
export function setInputColorPickerComboValue(elemComboWrapper, hexValue) {
const textInput = /** @type {HTMLInputElement} */ (elemComboWrapper.querySelector(`input[type="text"]`));
const colorInput =/** @type {HTMLInputElement} */ (elemComboWrapper.querySelector(`input[type="color"]`));
textInput.value = hexValue ?? "";
colorInput.value = hexValue ?? "#000000";
}
/**
* Assumes a value directly taken from a color picker value property.
*
* @param {string} pickerHexValue
*/
function convertColorPickerValueToTextValue(pickerHexValue) {
if (pickerHexValue === undefined)
throw new TypeError("Picker hex string value is undefined.");
if (ExColor.hexCanBeShort(pickerHexValue))
pickerHexValue = '#' + ExColor.hexLongToShort(pickerHexValue);
return pickerHexValue;
}
/**
* Assumes a value directly taken from a textbox value property.
*
* Returns `null` on invalid hex values.
* Returns `#000000` on empty string.
*
* @param {string?} textHexValue
*/
function convertColorTextValueToPickerValue(textHexValue) {
if (textHexValue === undefined || textHexValue === null)
return null;
textHexValue = textHexValue.trim();
if (textHexValue.length === 0)
return "#000000";
if (!ExColor.isValidHexString(textHexValue))
return null;
textHexValue = '#' + ExColor.hexShortToLong(textHexValue);
return textHexValue;
}
export function wrapUnquotedText(mesText) {
const textNodes = [];
for (const node of mesText.childNodes) {
if (node.nodeType === Node.TEXT_NODE && node.textContent.trim() !== '') {
textNodes.push(node);
}
}
textNodes.forEach(textNode => {
const span = document.createElement('span');
span.className = 'unquoted-text';
textNode.parentNode.insertBefore(span, textNode);
span.appendChild(textNode);
});
}