-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
294 lines (258 loc) · 7.81 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
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
const SELECTION = {
all: 0,
selection: 1
}
const TIMECODE_SOURCE = {
file: 0,
created: 1,
lastChanged: 2
};
const PROCESS_METHODS = {
rebase: 'rebase',
fixXMP: 'fix',
revertTimeCode: 'revert',
fromCreated: 'updateFromCreated',
fromLastChange: 'updateFromLastChanged',
fromTimecode: 'updateFromTimecode',
fromTimecodes: 'updateFromTimecodes'
}
const CEP_PANEL = {
correction: 0,
repair: 1,
};
class Logger {
log; //log parent Node
logArea; //logging textArea
explainer;
error;
verboseLogging = false;
#initialized = false;
static LOG_LEVELS = {
critical: "CRIT",
status: "STAT",
info: "INFO",
error: "ERR "
}
/**
* Empty constructor
*/
Logger() {
}
/**
* Constructor
* @param {JQuery-Object} logElement
* @param {DOM-Object} logAreaElement
* @param {jQuery-Object} explainerElement
* @param {jQuery-Object} errorElement
*/
Logger(logElement, logAreaElement, explainerElement, errorElement) {
this.init(logElement, logAreaElement, explainerElement, errorElement);
}
/**
* Initialize object
* @param {JQuery-Object} logElement
* @param {DOM-Object} logAreaElement
* @param {jQuery-Object} explainerElement
* @param {jQuery-Object} errorElement
*/
init(logElement, logAreaElement, explainerElement, errorElement) {
this.log = logElement;
this.logArea = logAreaElement;
this.explainer = explainerElement;
this.error = errorElement;
this.#initialized = true;
}
/**
* Adds a new log message to the log text area.
* @param {string} text
* @param {string} level
*/
addLog(text, level) {
if (!this.#initialized) {
return;
}
if (level === Logger.LOG_LEVELS.error || this.verboseLogging) {
if (this.log.hasClass('hidden')) {
this.log.removeClass('hidden');
}
this.logArea.value = this.#timeStamp() + level + " " + text + "\n" + this.logArea.value;
}
if (level === Logger.LOG_LEVELS.status) {
this.explainer.addClass('hidden');
this.error.removeClass('hidden');
this.error.text(text);
}
return level;
}
/**
* Clears the log area.
*/
clearLog() {
if (!this.#initialized) {
return;
}
this.logArea.value = '';
this.error.addClass('hidden');
this.explainer.removeClass('hidden');
}
/**
* Hides log area.
*/
hideLog() {
this.verboseLogging = false;
this.log.addClass('hidden');
}
/**
* Toggles log area visiblity.
*/
toggleLog() {
this.log.toggleClass('hidden');
}
/**
* Creates a new timestamp, which is put in fron of the log messages.
* @returns {string}
*/
#timeStamp() {
let date = new Date();
return "[" + this.#leadingZero(date.getDate()) + "." + this.#leadingZero(date.getMonth() + 1) + "." +
date.getFullYear() + " - " + this.#leadingZero(date.getHours()) + ":" + this.#leadingZero(date.getMinutes()) +
":" + this.#leadingZero(date.getSeconds()) + "] ";
}
/**
* Adds a leading zero to single digit numbers.
* @param {number} number
* @returns {string}
*/
#leadingZero(number) {
return number < 10 ? "0" + number : number;
}
}
/**
* Stores settings in local storage.
* @param {Object} newSettings
* @returns {boolean} true on success
*/
function storeSettings(newSettings) {
let settingsTxt = "";
try {
settingsTxt = JSON.stringify(newSettings);
} catch (e) {
logger.addLog("Failed to create settings string.", Logger.LOG_LEVELS.critical);
return false;
}
settings = newSettings;
localStorage.setItem(settingsKey, settingsTxt);
return true;
}
/**
* Loads gui settings from local storage.
* @returns {Object}
*/
function loadSettings() {
let settings = localStorage.getItem(settingsKey);
if (settings === null) {
logger.addLog("No settings have been stored.", Logger.LOG_LEVELS.info);
settings = defaultSettings;
} else {
try {
settings = JSON.parse(settings);
} catch {
logger.addLog("Failed to create settings object.", Logger.LOG_LEVELS.critical);
settings = defaultSettings;
}
}
return settings;
}
/**
* Retrieves the appSkinInfo.
* @param {*} event
*/
function onAppThemeColorChanged() {
// Should get a latest HostEnvironment object from application.
let skinInfo = JSON.parse(window.__adobe_cep__.getHostEnvironment()).appSkinInfo;
// Gets the style information such as color info from the skinInfo,
// and redraw all UI controls of your extension according to the style info.
updateThemeWithAppSkinInfo(skinInfo);
}
/**
* Set host in html as class - e.g. ppro or kbrg
*/
function setHostinDOM() {
let host = JSON.parse(window.__adobe_cep__.getHostEnvironment()).appId.toLowerCase();
document.children[0].classList.add(host);
return host;
}
/**
* Adds a new css style to the style#dynStyle element. This enables dynamic theme updates according to the
* settings in Premiere.
* @param {*} appSkinInfo
*/
function updateThemeWithAppSkinInfo(appSkinInfo) {
//Update the background color of the panel
let panelBackgroundColor = toHex(appSkinInfo.panelBackgroundColor.color);
let cssStyle = `:root {
--dark-color: ${panelBackgroundColor};
--table-color-bright: ${lightenDarkenColor(panelBackgroundColor, 10)};
--table-highlight-color: ${lightenDarkenColor(panelBackgroundColor, 50)};
--table-highlight-color-bright: ${lightenDarkenColor(panelBackgroundColor, 60)};
--bright-color: ${lightenDarkenColor(panelBackgroundColor, 150)};
--highlight-color: ${toHex(appSkinInfo.systemHighlightColor)};
--font-size: ${appSkinInfo.baseFontSize}px;
}`;
$("#dynStyle")[0].textContent = cssStyle;
}
/**
* Convert the Color object to string in hexadecimal format;
* @param {{red: string|number, green: string|number, blue: string|number}} color
* @param {number} delta
* @return {string} color as hex value
*/
function toHex(color, delta) {
/**
* Creates a hex value for a given number offset by the delta.
* @param {number} value
* @param {number} delta
* @return {string} hex value
*/
function computeValue(value, delta) {
var computedValue = !isNaN(delta) ? value + delta : value;
if (computedValue < 0) {
computedValue = 0;
} else if (computedValue > 255) {
computedValue = 255;
}
computedValue = Math.round(computedValue).toString(16);
return computedValue.length == 1 ? "0" + computedValue : computedValue;
}
var hex = "";
if (color) {
hex = computeValue(color.red, delta) + computeValue(color.green, delta) + computeValue(color.blue, delta);
}
return "#" + hex;
}
/**
* Lightens and darkens colors in hex format.
* @author Chris Coyier
* @source https://css-tricks.com/snippets/javascript/lighten-darken-color/
* @param {string} col
* @param {number} amt
* @return {string} color as hex string
*/
function lightenDarkenColor(col, amt) {
var usePound = false;
if (col[0] == "#") {
col = col.slice(1);
usePound = true;
}
var num = parseInt(col, 16);
var r = (num >> 16) + amt;
if (r > 255) r = 255;
else if (r < 0) r = 0;
var b = ((num >> 8) & 0x00FF) + amt;
if (b > 255) b = 255;
else if (b < 0) b = 0;
var g = (num & 0x0000FF) + amt;
if (g > 255) g = 255;
else if (g < 0) g = 0;
return (usePound ? "#" : "") + (g | (b << 8) | (r << 16)).toString(16);
}