2021-07-30 12:44:06 +00:00
|
|
|
/* global $ $create $remove messageBoxProxy showSpinner toggleDataset */// dom.js
|
|
|
|
/* global API msg */// msg.js
|
|
|
|
/* global URLS */// toolbox.js
|
2021-06-29 09:36:59 +00:00
|
|
|
/* global editor */
|
2021-07-30 12:44:06 +00:00
|
|
|
/* global t */// localization.js
|
2021-06-29 09:36:59 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-07-30 12:44:06 +00:00
|
|
|
(() => {
|
|
|
|
//#region Main
|
2021-06-29 09:36:59 +00:00
|
|
|
|
2021-07-30 12:44:06 +00:00
|
|
|
const ERROR_TITLE = 'UserStyles.world ' + t('genericError');
|
|
|
|
const PROGRESS = '#usw-progress';
|
|
|
|
let spinnerTimer = 0;
|
|
|
|
let prevCode = '';
|
|
|
|
|
|
|
|
msg.onExtension(request => {
|
|
|
|
if (request.method === 'uswData' &&
|
|
|
|
request.style.id === editor.style.id) {
|
|
|
|
Object.assign(editor.style, request.style);
|
|
|
|
updateUI();
|
|
|
|
}
|
|
|
|
});
|
2021-06-29 09:36:59 +00:00
|
|
|
|
2022-03-24 14:08:04 +00:00
|
|
|
window.on('domReady', () => {
|
2021-07-30 12:44:06 +00:00
|
|
|
updateUI();
|
|
|
|
$('#usw-publish-style').onclick = disableWhileActive(publishStyle);
|
|
|
|
$('#usw-disconnect').onclick = disableWhileActive(disconnect);
|
2022-03-24 14:08:04 +00:00
|
|
|
}, {once: true});
|
2021-07-30 12:44:06 +00:00
|
|
|
|
|
|
|
async function publishStyle() {
|
|
|
|
const {id} = editor.style;
|
|
|
|
if (await API.data.has('usw' + id) &&
|
|
|
|
!await messageBoxProxy.confirm(t('publishRetry'), 'danger', ERROR_TITLE)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const code = editor.getValue();
|
|
|
|
const isDiff = code !== prevCode;
|
|
|
|
const res = isDiff ? await API.usw.publish(id, code) : t('importReportUnchanged');
|
|
|
|
const title = `${new Date().toLocaleString()}\n${res}`;
|
|
|
|
const failed = /^Error:/.test(res);
|
|
|
|
$(PROGRESS).append(...failed && [
|
|
|
|
$create('div.error', {title}, res),
|
|
|
|
$create('div', t('publishReconnect')),
|
|
|
|
] || [
|
|
|
|
$create(`span.${isDiff ? 'success' : 'unchanged'}`, {title}),
|
|
|
|
]);
|
|
|
|
if (!failed) prevCode = code;
|
|
|
|
}
|
2021-06-29 09:36:59 +00:00
|
|
|
|
2021-07-30 12:44:06 +00:00
|
|
|
async function disconnect() {
|
|
|
|
await API.usw.revoke(editor.style.id);
|
|
|
|
prevCode = null; // to allow the next publishStyle to upload style
|
|
|
|
}
|
2021-06-29 09:36:59 +00:00
|
|
|
|
2021-07-30 12:44:06 +00:00
|
|
|
function updateUI(style = editor.style) {
|
|
|
|
const usw = style._usw || {};
|
|
|
|
const section = $('#publish');
|
|
|
|
toggleDataset(section, 'connected', usw.token);
|
|
|
|
for (const type of ['name', 'description']) {
|
|
|
|
const el = $(`dd[data-usw="${type}"]`, section);
|
|
|
|
el.textContent = el.title = usw[type] || '';
|
|
|
|
}
|
|
|
|
const elUrl = $('#usw-url');
|
|
|
|
elUrl.href = `${URLS.usw}${usw.id ? `style/${usw.id}` : ''}`;
|
|
|
|
elUrl.textContent = t('publishUsw').replace(/<(.+)>/, `$1${usw.id ? `#${usw.id}` : ''}`);
|
|
|
|
}
|
2021-06-29 09:36:59 +00:00
|
|
|
|
2021-07-30 12:44:06 +00:00
|
|
|
//#endregion
|
|
|
|
//#region Utility
|
2021-06-29 09:36:59 +00:00
|
|
|
|
2021-07-30 12:44:06 +00:00
|
|
|
function disableWhileActive(fn) {
|
|
|
|
/** @this {Element} */
|
|
|
|
return async function () {
|
|
|
|
this.disabled = true;
|
|
|
|
timerOn();
|
|
|
|
await fn().catch(console.error);
|
|
|
|
timerOff();
|
|
|
|
this.disabled = false;
|
|
|
|
};
|
|
|
|
}
|
2021-06-29 09:36:59 +00:00
|
|
|
|
2021-07-30 12:44:06 +00:00
|
|
|
function timerOn() {
|
|
|
|
if (!spinnerTimer) {
|
|
|
|
$(PROGRESS).textContent = '';
|
|
|
|
spinnerTimer = setTimeout(showSpinner, 250, PROGRESS);
|
|
|
|
}
|
|
|
|
}
|
2021-06-29 09:36:59 +00:00
|
|
|
|
2021-07-30 12:44:06 +00:00
|
|
|
function timerOff() {
|
|
|
|
$remove(`${PROGRESS} .lds-spinner`);
|
|
|
|
clearTimeout(spinnerTimer);
|
|
|
|
spinnerTimer = 0;
|
2021-06-29 09:36:59 +00:00
|
|
|
}
|
2021-07-30 12:44:06 +00:00
|
|
|
|
|
|
|
//#endregion
|
|
|
|
})();
|