From 7f596db09bef3fa67a20fe402b131424a46484b5 Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov <38059171+askvortsov1@users.noreply.github.com> Date: Mon, 10 May 2021 17:54:58 -0400 Subject: [PATCH] Some minor JS cleanup (#2846) - Change index files to ts - Remove deprecated EditUserModal from forum compat - Remove b14-specific error - Remove transChoice method (ICU should be used instead) - Translator to TypeScript - Small JS => TS cleanup * Trivial js => ts change Co-authored-by: David Wheatley --- js/src/admin/{index.js => index.ts} | 0 .../common/{Translator.js => Translator.ts} | 62 ++++++++++--------- js/src/common/states/ModalManagerState.js | 16 +---- js/src/common/utils/{Stream.js => Stream.ts} | 0 js/src/forum/compat.js | 8 --- js/src/forum/{index.js => index.ts} | 4 -- 6 files changed, 36 insertions(+), 54 deletions(-) rename js/src/admin/{index.js => index.ts} (100%) rename js/src/common/{Translator.js => Translator.ts} (56%) rename js/src/common/utils/{Stream.js => Stream.ts} (100%) rename js/src/forum/{index.js => index.ts} (75%) diff --git a/js/src/admin/index.js b/js/src/admin/index.ts similarity index 100% rename from js/src/admin/index.js rename to js/src/admin/index.ts diff --git a/js/src/common/Translator.js b/js/src/common/Translator.ts similarity index 56% rename from js/src/common/Translator.js rename to js/src/common/Translator.ts index f6b7c90dc..51b9c95a6 100644 --- a/js/src/common/Translator.js +++ b/js/src/common/Translator.ts @@ -3,35 +3,46 @@ import { pluralTypeHandler, selectTypeHandler } from '@ultraq/icu-message-format import username from './helpers/username'; import extract from './utils/extract'; -export default class Translator { - constructor() { - /** - * A map of translation keys to their translated values. - * - * @type {Object} - * @public - */ - this.translations = {}; +type Translations = Record; +type TranslatorParameters = Record; - this.formatter = new RichMessageFormatter(null, this.formatterTypeHandlers(), mithrilRichHandler); +export default class Translator { + /** + * A map of translation keys to their translated values. + */ + translations: Translations = {}; + + /** + * The underlying ICU MessageFormatter util. + */ + protected formatter = new RichMessageFormatter(null, this.formatterTypeHandlers(), mithrilRichHandler); + + setLocale(locale: string) { + this.formatter.locale = locale; } - formatterTypeHandlers() { + addTranslations(translations: Translations) { + Object.assign(this.translations, translations); + } + + /** + * An extensible entrypoint for extenders to register type handlers for translations. + */ + protected formatterTypeHandlers() { return { plural: pluralTypeHandler, select: selectTypeHandler, }; } - setLocale(locale) { - this.formatter.locale = locale; - } - - addTranslations(translations) { - Object.assign(this.translations, translations); - } - - preprocessParameters(parameters) { + /** + * A temporary system to preprocess parameters. + * Should not be used by extensions. + * TODO: An extender will be added in v1.x. + * + * @internal + */ + protected preprocessParameters(parameters: TranslatorParameters) { // If we've been given a user model as one of the input parameters, then // we'll extract the username and use that for the translation. In the // future there should be a hook here to inspect the user and change the @@ -45,21 +56,14 @@ export default class Translator { return parameters; } - trans(id, parameters) { + trans(id: string, parameters: TranslatorParameters = {}) { const translation = this.translations[id]; if (translation) { - parameters = this.preprocessParameters(parameters || {}); + parameters = this.preprocessParameters(parameters); return this.formatter.rich(translation, parameters); } return id; } - - /** - * @deprecated, remove before stable - */ - transChoice(id, number, parameters) { - return this.trans(id, parameters); - } } diff --git a/js/src/common/states/ModalManagerState.js b/js/src/common/states/ModalManagerState.js index 93b0c3b2b..a11196279 100644 --- a/js/src/common/states/ModalManagerState.js +++ b/js/src/common/states/ModalManagerState.js @@ -11,22 +11,12 @@ export default class ModalManagerState { * @public */ show(componentClass, attrs) { - // Breaking Change Compliance Warning, Remove in Beta 15. if (!(componentClass.prototype instanceof Modal)) { // This is duplicated so that if the error is caught, an error message still shows up in the debug console. - console.error('The ModalManager can only show Modals'); - throw new Error('The ModalManager can only show Modals'); + const invalidModalWarning = 'The ModalManager can only show Modals.'; + console.error(invalidModalWarning); + throw new Error(invalidModalWarning); } - if (componentClass.init) { - // This is duplicated so that if the error is caught, an error message still shows up in the debug console. - console.error( - 'The componentClass parameter must be a modal class, not a modal instance. Whichever extension triggered this modal should be updated to comply with beta 14.' - ); - throw new Error( - 'The componentClass parameter must be a modal class, not a modal instance. Whichever extension triggered this modal should be updated to comply with beta 14.' - ); - } - // End Change Compliance Warning, Remove in Beta 15 clearTimeout(this.closeTimeout); diff --git a/js/src/common/utils/Stream.js b/js/src/common/utils/Stream.ts similarity index 100% rename from js/src/common/utils/Stream.js rename to js/src/common/utils/Stream.ts diff --git a/js/src/forum/compat.js b/js/src/forum/compat.js index a2bb3ffe8..fb54383ab 100644 --- a/js/src/forum/compat.js +++ b/js/src/forum/compat.js @@ -69,10 +69,6 @@ import Search from './components/Search'; import DiscussionListItem from './components/DiscussionListItem'; import LoadingPost from './components/LoadingPost'; import PostsUserPage from './components/PostsUserPage'; -/** - * @deprecated - */ -import EditUserModal from '../common/components/EditUserModal'; import DiscussionPageResolver from './resolvers/DiscussionPageResolver'; import BasicEditorDriver from '../common/utils/BasicEditorDriver'; import routes from './routes'; @@ -131,10 +127,6 @@ export default Object.assign(compat, { 'components/EventPost': EventPost, 'components/DiscussionHero': DiscussionHero, 'components/PostMeta': PostMeta, - /** - * @deprecated Used for backwards compatibility now that the EditUserModal has moved to common. Remove in beta 17. - */ - 'components/EditUserModal': EditUserModal, 'components/SearchSource': SearchSource, 'components/DiscussionRenamedPost': DiscussionRenamedPost, 'components/DiscussionComposer': DiscussionComposer, diff --git a/js/src/forum/index.js b/js/src/forum/index.ts similarity index 75% rename from js/src/forum/index.js rename to js/src/forum/index.ts index c949c7ed8..0537213af 100644 --- a/js/src/forum/index.js +++ b/js/src/forum/index.ts @@ -6,10 +6,6 @@ import app from './app'; export { app }; -// Export public API -// export { default as Extend } from './Extend'; -// export { IndexPage, DicsussionList } from './components'; - // Export compat API import compatObj from './compat'; import proxifyCompat from '../common/utils/proxifyCompat';