1
0
mirror of https://github.com/flarum/core.git synced 2025-07-10 19:36:27 +02:00

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 <hi@davwheat.dev>
This commit is contained in:
Alexander Skvortsov
2021-05-10 17:54:58 -04:00
committed by GitHub
parent dd8323ee36
commit 7f596db09b
6 changed files with 36 additions and 54 deletions

View File

@ -3,35 +3,46 @@ import { pluralTypeHandler, selectTypeHandler } from '@ultraq/icu-message-format
import username from './helpers/username'; import username from './helpers/username';
import extract from './utils/extract'; import extract from './utils/extract';
type Translations = Record<string, string>;
type TranslatorParameters = Record<string, unknown>;
export default class Translator { export default class Translator {
constructor() {
/** /**
* A map of translation keys to their translated values. * A map of translation keys to their translated values.
*
* @type {Object}
* @public
*/ */
this.translations = {}; translations: Translations = {};
this.formatter = new RichMessageFormatter(null, this.formatterTypeHandlers(), mithrilRichHandler); /**
* 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 { return {
plural: pluralTypeHandler, plural: pluralTypeHandler,
select: selectTypeHandler, select: selectTypeHandler,
}; };
} }
setLocale(locale) { /**
this.formatter.locale = locale; * A temporary system to preprocess parameters.
} * Should not be used by extensions.
* TODO: An extender will be added in v1.x.
addTranslations(translations) { *
Object.assign(this.translations, translations); * @internal
} */
protected preprocessParameters(parameters: TranslatorParameters) {
preprocessParameters(parameters) {
// If we've been given a user model as one of the input parameters, then // 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 // 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 // future there should be a hook here to inspect the user and change the
@ -45,21 +56,14 @@ export default class Translator {
return parameters; return parameters;
} }
trans(id, parameters) { trans(id: string, parameters: TranslatorParameters = {}) {
const translation = this.translations[id]; const translation = this.translations[id];
if (translation) { if (translation) {
parameters = this.preprocessParameters(parameters || {}); parameters = this.preprocessParameters(parameters);
return this.formatter.rich(translation, parameters); return this.formatter.rich(translation, parameters);
} }
return id; return id;
} }
/**
* @deprecated, remove before stable
*/
transChoice(id, number, parameters) {
return this.trans(id, parameters);
}
} }

View File

@ -11,22 +11,12 @@ export default class ModalManagerState {
* @public * @public
*/ */
show(componentClass, attrs) { show(componentClass, attrs) {
// Breaking Change Compliance Warning, Remove in Beta 15.
if (!(componentClass.prototype instanceof Modal)) { 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. // 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'); const invalidModalWarning = 'The ModalManager can only show Modals.';
throw new Error('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); clearTimeout(this.closeTimeout);

View File

@ -69,10 +69,6 @@ import Search from './components/Search';
import DiscussionListItem from './components/DiscussionListItem'; import DiscussionListItem from './components/DiscussionListItem';
import LoadingPost from './components/LoadingPost'; import LoadingPost from './components/LoadingPost';
import PostsUserPage from './components/PostsUserPage'; import PostsUserPage from './components/PostsUserPage';
/**
* @deprecated
*/
import EditUserModal from '../common/components/EditUserModal';
import DiscussionPageResolver from './resolvers/DiscussionPageResolver'; import DiscussionPageResolver from './resolvers/DiscussionPageResolver';
import BasicEditorDriver from '../common/utils/BasicEditorDriver'; import BasicEditorDriver from '../common/utils/BasicEditorDriver';
import routes from './routes'; import routes from './routes';
@ -131,10 +127,6 @@ export default Object.assign(compat, {
'components/EventPost': EventPost, 'components/EventPost': EventPost,
'components/DiscussionHero': DiscussionHero, 'components/DiscussionHero': DiscussionHero,
'components/PostMeta': PostMeta, '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/SearchSource': SearchSource,
'components/DiscussionRenamedPost': DiscussionRenamedPost, 'components/DiscussionRenamedPost': DiscussionRenamedPost,
'components/DiscussionComposer': DiscussionComposer, 'components/DiscussionComposer': DiscussionComposer,

View File

@ -6,10 +6,6 @@ import app from './app';
export { app }; export { app };
// Export public API
// export { default as Extend } from './Extend';
// export { IndexPage, DicsussionList } from './components';
// Export compat API // Export compat API
import compatObj from './compat'; import compatObj from './compat';
import proxifyCompat from '../common/utils/proxifyCompat'; import proxifyCompat from '../common/utils/proxifyCompat';