From 1110f04ed3830edf40d396b3f482c235a8215af3 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Thu, 21 Mar 2024 22:42:47 +0400 Subject: [PATCH] Up i18n events --- docs/api/i18n.md | 21 +++++++++++++++++---- src/i18n/index.ts | 27 ++++++++++++--------------- src/i18n/types.ts | 31 +++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 19 deletions(-) create mode 100644 src/i18n/types.ts diff --git a/docs/api/i18n.md b/docs/api/i18n.md index dc1a3bb4c..95a360d86 100644 --- a/docs/api/i18n.md +++ b/docs/api/i18n.md @@ -23,11 +23,24 @@ Once the editor is instantiated you can use its API. Before using these methods const i18n = editor.I18n; ``` -### Events +## Available Events +* `i18n:add` New set of messages is added. -* `i18n:add` - New set of messages is added -* `i18n:update` - The set of messages is updated -* `i18n:locale` - Locale changed +```javascript +editor.on('i18n:add', (messages) => { ... }); +``` + +* `i18n:update` The set of messages is updated. + +```javascript +editor.on('i18n:update', (messages) => { ... }); +``` + +* `i18n:locale` Locale changed. + +```javascript +editor.on('i18n:locale', ({ value, valuePrev }) => { ... }); +``` ## setLocale diff --git a/src/i18n/index.ts b/src/i18n/index.ts index 5d3966d58..f673002c7 100644 --- a/src/i18n/index.ts +++ b/src/i18n/index.ts @@ -19,10 +19,7 @@ * const i18n = editor.I18n; * ``` * - * ### Events - * * `i18n:add` - New set of messages is added - * * `i18n:update` - The set of messages is updated - * * `i18n:locale` - Locale changed + * {REPLACE_EVENTS} * * @module I18n */ @@ -31,10 +28,11 @@ import { Module } from '../abstract'; import EditorModel from '../editor/model/Editor'; import { hasWin, deepMerge } from '../utils/mixins'; import defaults, { I18nConfig } from './config'; - -type Messages = Required['messages']; +import I18nEvents, { Messages } from './types'; export default class I18nModule extends Module { + events = I18nEvents; + /** * Initialize module * @param {Object} config Configurations @@ -65,9 +63,8 @@ export default class I18nModule extends Module { en: { msg2: 'Msg 2 up', msg3: 'Msg 3', } } */ setMessages(msg: Messages) { - const { em, config } = this; + const { em, config, events } = this; config.messages = msg; - em && em.trigger('i18n:update', msg); + em.trigger(events.update, msg); return this; } @@ -130,9 +127,9 @@ export default class I18nModule extends Module { en: { msg1: 'Msg 1', msg2: 'Msg 2 up', msg3: 'Msg 3', } } */ addMessages(msg: Messages) { - const { em } = this; - const { messages } = this.config; - em && em.trigger('i18n:add', msg); + const { em, events, config } = this; + const { messages } = config; + em.trigger(events.add, msg); this.setMessages(deepMerge(messages!, msg)); return this; diff --git a/src/i18n/types.ts b/src/i18n/types.ts new file mode 100644 index 000000000..90f4527f5 --- /dev/null +++ b/src/i18n/types.ts @@ -0,0 +1,31 @@ +import { I18nConfig } from './config'; + +export type Messages = Required['messages']; + +/**{START_EVENTS}*/ +export enum I18nEvents { + /** + * @event `i18n:add` New set of messages is added. + * @example + * editor.on('i18n:add', (messages) => { ... }); + */ + add = 'i18n:add', + + /** + * @event `i18n:update` The set of messages is updated. + * @example + * editor.on('i18n:update', (messages) => { ... }); + */ + update = 'i18n:update', + + /** + * @event `i18n:locale` Locale changed. + * @example + * editor.on('i18n:locale', ({ value, valuePrev }) => { ... }); + */ + locale = 'i18n:locale', +} +/**{END_EVENTS}*/ + +// need this to avoid the TS documentation generator to break +export default I18nEvents;