usw-integration.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* global $ $create $remove messageBoxProxy showSpinner toggleDataset */// dom.js
  2. /* global API msg */// msg.js
  3. /* global URLS */// toolbox.js
  4. /* global baseInit */
  5. /* global editor */
  6. /* global t */// localization.js
  7. 'use strict';
  8. (() => {
  9. //#region Main
  10. const ERROR_TITLE = 'UserStyles.world ' + t('genericError');
  11. const PROGRESS = '#usw-progress';
  12. let spinnerTimer = 0;
  13. let prevCode = '';
  14. msg.onExtension(request => {
  15. if (request.method === 'uswData' &&
  16. request.style.id === editor.style.id) {
  17. Object.assign(editor.style, request.style);
  18. updateUI();
  19. }
  20. });
  21. baseInit.ready.then(() => {
  22. updateUI();
  23. $('#usw-publish-style').onclick = disableWhileActive(publishStyle);
  24. $('#usw-disconnect').onclick = disableWhileActive(disconnect);
  25. });
  26. async function publishStyle() {
  27. const {id} = editor.style;
  28. if (await API.data.has('usw' + id) &&
  29. !await messageBoxProxy.confirm(t('publishRetry'), 'danger', ERROR_TITLE)) {
  30. return;
  31. }
  32. const code = editor.getValue();
  33. const isDiff = code !== prevCode;
  34. const res = isDiff ? await API.usw.publish(id, code) : t('importReportUnchanged');
  35. const title = `${new Date().toLocaleString()}\n${res}`;
  36. const failed = /^Error:/.test(res);
  37. $(PROGRESS).append(...failed && [
  38. $create('div.error', {title}, res),
  39. $create('div', t('publishReconnect')),
  40. ] || [
  41. $create(`span.${isDiff ? 'success' : 'unchanged'}`, {title}),
  42. ]);
  43. if (!failed) prevCode = code;
  44. }
  45. async function disconnect() {
  46. await API.usw.revoke(editor.style.id);
  47. prevCode = null; // to allow the next publishStyle to upload style
  48. }
  49. function updateUI(style = editor.style) {
  50. const usw = style._usw || {};
  51. const section = $('#publish');
  52. toggleDataset(section, 'connected', usw.token);
  53. for (const type of ['name', 'description']) {
  54. const el = $(`dd[data-usw="${type}"]`, section);
  55. el.textContent = el.title = usw[type] || '';
  56. }
  57. const elUrl = $('#usw-url');
  58. elUrl.href = `${URLS.usw}${usw.id ? `style/${usw.id}` : ''}`;
  59. elUrl.textContent = t('publishUsw').replace(/<(.+)>/, `$1${usw.id ? `#${usw.id}` : ''}`);
  60. }
  61. //#endregion
  62. //#region Utility
  63. function disableWhileActive(fn) {
  64. /** @this {Element} */
  65. return async function () {
  66. this.disabled = true;
  67. timerOn();
  68. await fn().catch(console.error);
  69. timerOff();
  70. this.disabled = false;
  71. };
  72. }
  73. function timerOn() {
  74. if (!spinnerTimer) {
  75. $(PROGRESS).textContent = '';
  76. spinnerTimer = setTimeout(showSpinner, 250, PROGRESS);
  77. }
  78. }
  79. function timerOff() {
  80. $remove(`${PROGRESS} .lds-spinner`);
  81. clearTimeout(spinnerTimer);
  82. spinnerTimer = 0;
  83. }
  84. //#endregion
  85. })();