From 0bdbc2899e0860d86f305000ab0e8e571a467395 Mon Sep 17 00:00:00 2001 From: Luna D Date: Sat, 8 Jun 2024 11:53:44 -0400 Subject: [PATCH] Convert miscellaneous scripts to TypeScript --- assets/js/{app.js => app.ts} | 0 assets/js/{captcha.js => captcha.ts} | 0 assets/js/{pmwarning.js => pmwarning.ts} | 14 ++++++-------- assets/js/{poll.js => poll.ts} | 4 +--- assets/js/{settings.js => settings.ts} | 9 +++++---- assets/js/{when-ready.js => when-ready.ts} | 0 6 files changed, 12 insertions(+), 15 deletions(-) rename assets/js/{app.js => app.ts} (100%) rename assets/js/{captcha.js => captcha.ts} (100%) rename assets/js/{pmwarning.js => pmwarning.ts} (57%) rename assets/js/{poll.js => poll.ts} (81%) rename assets/js/{settings.js => settings.ts} (51%) rename assets/js/{when-ready.js => when-ready.ts} (100%) diff --git a/assets/js/app.js b/assets/js/app.ts similarity index 100% rename from assets/js/app.js rename to assets/js/app.ts diff --git a/assets/js/captcha.js b/assets/js/captcha.ts similarity index 100% rename from assets/js/captcha.js rename to assets/js/captcha.ts diff --git a/assets/js/pmwarning.js b/assets/js/pmwarning.ts similarity index 57% rename from assets/js/pmwarning.js rename to assets/js/pmwarning.ts index 104bd09c..23772dff 100644 --- a/assets/js/pmwarning.js +++ b/assets/js/pmwarning.ts @@ -4,11 +4,11 @@ * Warn users that their PM will be reviewed. */ -import { $ } from './utils/dom'; +import { $, hideEl, showEl } from './utils/dom'; -function warnAboutPMs() { - const textarea = $('.js-toolbar-input'); - const warning = $('.js-hidden-warning'); +export function warnAboutPMs() { + const textarea = $('.js-toolbar-input'); + const warning = $('.js-hidden-warning'); const imageEmbedRegex = /!+\[/g; if (!warning || !textarea) return; @@ -17,12 +17,10 @@ function warnAboutPMs() { const value = textarea.value; if (value.match(imageEmbedRegex)) { - warning.classList.remove('hidden'); + showEl(warning); } else if (!warning.classList.contains('hidden')) { - warning.classList.add('hidden'); + hideEl(warning); } }); } - -export { warnAboutPMs }; diff --git a/assets/js/poll.js b/assets/js/poll.ts similarity index 81% rename from assets/js/poll.js rename to assets/js/poll.ts index 68debf5d..38c74907 100644 --- a/assets/js/poll.js +++ b/assets/js/poll.ts @@ -1,6 +1,6 @@ import { inputDuplicatorCreator } from './input-duplicator'; -function pollOptionCreator() { +export function pollOptionCreator() { inputDuplicatorCreator({ addButtonSelector: '.js-poll-add-option', fieldSelector: '.js-poll-option', @@ -8,5 +8,3 @@ function pollOptionCreator() { removeButtonSelector: '.js-option-remove', }); } - -export { pollOptionCreator }; diff --git a/assets/js/settings.js b/assets/js/settings.ts similarity index 51% rename from assets/js/settings.js rename to assets/js/settings.ts index 360228ee..1faf28d0 100644 --- a/assets/js/settings.js +++ b/assets/js/settings.ts @@ -2,6 +2,7 @@ * Settings. */ +import { assertNotNull, assertNotUndefined } from './utils/assert'; import { $, $$ } from './utils/dom'; import store from './utils/store'; @@ -9,9 +10,9 @@ export function setupSettings() { if (!$('#js-setting-table')) return; - const localCheckboxes = $$('[data-tab="local"] input[type="checkbox"]'); - const themeSelect = $('#user_theme'); - const styleSheet = $('head link[rel="stylesheet"]'); + const localCheckboxes = $$('[data-tab="local"] input[type="checkbox"]'); + const themeSelect = assertNotNull($('#user_theme')); + const styleSheet = assertNotNull($('head link[rel="stylesheet"]')); // Local settings localCheckboxes.forEach(checkbox => { @@ -22,7 +23,7 @@ export function setupSettings() { // Theme preview themeSelect && themeSelect.addEventListener('change', () => { - styleSheet.href = themeSelect.options[themeSelect.selectedIndex].dataset.themePath; + styleSheet.href = assertNotUndefined(themeSelect.options[themeSelect.selectedIndex].dataset.themePath); }); } diff --git a/assets/js/when-ready.js b/assets/js/when-ready.ts similarity index 100% rename from assets/js/when-ready.js rename to assets/js/when-ready.ts