@ckeditor/ckeditor5-utils 41.0.0 → 41.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckeditor/ckeditor5-utils",
3
- "version": "41.0.0",
3
+ "version": "41.2.0",
4
4
  "description": "Miscellaneous utilities used by CKEditor 5.",
5
5
  "keywords": [
6
6
  "ckeditor",
package/src/index.d.ts CHANGED
@@ -45,7 +45,7 @@ export { default as remove } from './dom/remove.js';
45
45
  export * from './dom/scroll.js';
46
46
  export * from './keyboard.js';
47
47
  export * from './language.js';
48
- export { default as Locale, type LocaleTranslate } from './locale.js';
48
+ export { default as Locale, type LocaleTranslate, type Translations } from './locale.js';
49
49
  export { default as Collection, type CollectionAddEvent, type CollectionChangeEvent, type CollectionRemoveEvent } from './collection.js';
50
50
  export { default as first } from './first.js';
51
51
  export { default as FocusTracker } from './focustracker.js';
package/src/keyboard.js CHANGED
@@ -15,6 +15,13 @@ const modifiersToGlyphsNonMac = {
15
15
  alt: 'Alt+',
16
16
  shift: 'Shift+'
17
17
  };
18
+ const keyCodesToGlyphs = {
19
+ 37: '←',
20
+ 38: '↑',
21
+ 39: '→',
22
+ 40: '↓',
23
+ 9: '⇥'
24
+ };
18
25
  /**
19
26
  * An object with `keyName => keyCode` pairs for a set of known keys.
20
27
  *
@@ -29,7 +36,16 @@ const modifiersToGlyphsNonMac = {
29
36
  * * `ctrl`, `cmd`, `shift`, `alt`.
30
37
  */
31
38
  export const keyCodes = generateKnownKeyCodes();
32
- const keyCodeNames = Object.fromEntries(Object.entries(keyCodes).map(([name, code]) => [code, name.charAt(0).toUpperCase() + name.slice(1)]));
39
+ const keyCodeNames = Object.fromEntries(Object.entries(keyCodes).map(([name, code]) => {
40
+ let prettyKeyName;
41
+ if (code in keyCodesToGlyphs) {
42
+ prettyKeyName = keyCodesToGlyphs[code];
43
+ }
44
+ else {
45
+ prettyKeyName = name.charAt(0).toUpperCase() + name.slice(1);
46
+ }
47
+ return [code, prettyKeyName];
48
+ }));
33
49
  /**
34
50
  * Converts a key name or {@link module:utils/keyboard~KeystrokeInfo keystroke info} into a key code.
35
51
  *
@@ -211,9 +227,19 @@ function generateKnownKeyCodes() {
211
227
  keyCodes['f' + (code - 111)] = code;
212
228
  }
213
229
  // other characters
214
- for (const char of '`-=[];\',./\\') {
215
- keyCodes[char] = char.charCodeAt(0);
216
- }
230
+ Object.assign(keyCodes, {
231
+ '\'': 222,
232
+ ',': 108,
233
+ '-': 109,
234
+ '.': 110,
235
+ '/': 111,
236
+ ';': 186,
237
+ '=': 187,
238
+ '[': 219,
239
+ '\\': 220,
240
+ ']': 221,
241
+ '`': 223
242
+ });
217
243
  return keyCodes;
218
244
  }
219
245
  function splitKeystrokeText(keystroke) {
package/src/locale.d.ts CHANGED
@@ -2,6 +2,10 @@
2
2
  * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
+ /**
6
+ * @module utils/locale
7
+ */
8
+ import { type ArrayOrItem } from './toarray.js';
5
9
  import { type Message } from './translation-service.js';
6
10
  import { type LanguageDirection } from './language.js';
7
11
  /**
@@ -83,6 +87,10 @@ export default class Locale {
83
87
  * ```
84
88
  */
85
89
  readonly t: LocaleTranslate;
90
+ /**
91
+ * Object that contains translations.
92
+ */
93
+ translations?: Translations;
86
94
  /**
87
95
  * Creates a new instance of the locale class. Learn more about
88
96
  * {@glink features/ui-language configuring the language of the editor}.
@@ -93,10 +101,12 @@ export default class Locale {
93
101
  * @param options.contentLanguage The editor content language code in the
94
102
  * [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format. If not specified, the same as `options.language`.
95
103
  * See {@link #contentLanguage}.
104
+ * @param translations Translations passed as a editor config parameter.
96
105
  */
97
- constructor({ uiLanguage, contentLanguage }?: {
106
+ constructor({ uiLanguage, contentLanguage, translations }?: {
98
107
  readonly uiLanguage?: string;
99
108
  readonly contentLanguage?: string;
109
+ readonly translations?: ArrayOrItem<Translations>;
100
110
  });
101
111
  /**
102
112
  * The editor UI language code in the [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
@@ -118,3 +128,14 @@ export default class Locale {
118
128
  * For messages supporting plural forms the first value will determine the plural form.
119
129
  */
120
130
  export type LocaleTranslate = (message: string | Message, values?: number | string | ReadonlyArray<number | string>) => string;
131
+ /**
132
+ * Translations object definition.
133
+ */
134
+ export type Translations = {
135
+ [language: string]: {
136
+ dictionary: {
137
+ [messageId: string]: string | ReadonlyArray<string>;
138
+ };
139
+ getPluralForm?: (n: number) => number;
140
+ };
141
+ };
package/src/locale.js CHANGED
@@ -7,7 +7,7 @@
7
7
  */
8
8
  /* globals console */
9
9
  import toArray from './toarray.js';
10
- import { _translate } from './translation-service.js';
10
+ import { _translate, _unifyTranslations } from './translation-service.js';
11
11
  import { getLanguageDirection } from './language.js';
12
12
  /**
13
13
  * Represents the localization services.
@@ -23,12 +23,14 @@ export default class Locale {
23
23
  * @param options.contentLanguage The editor content language code in the
24
24
  * [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format. If not specified, the same as `options.language`.
25
25
  * See {@link #contentLanguage}.
26
+ * @param translations Translations passed as a editor config parameter.
26
27
  */
27
- constructor({ uiLanguage = 'en', contentLanguage } = {}) {
28
+ constructor({ uiLanguage = 'en', contentLanguage, translations } = {}) {
28
29
  this.uiLanguage = uiLanguage;
29
30
  this.contentLanguage = contentLanguage || this.uiLanguage;
30
31
  this.uiLanguageDirection = getLanguageDirection(this.uiLanguage);
31
32
  this.contentLanguageDirection = getLanguageDirection(this.contentLanguage);
33
+ this.translations = _unifyTranslations(translations);
32
34
  this.t = (message, values) => this._t(message, values);
33
35
  }
34
36
  /**
@@ -62,7 +64,7 @@ export default class Locale {
62
64
  }
63
65
  const hasPluralForm = !!message.plural;
64
66
  const quantity = hasPluralForm ? values[0] : 1;
65
- const translatedString = _translate(this.uiLanguage, message, quantity);
67
+ const translatedString = _translate(this.uiLanguage, message, quantity, this.translations);
66
68
  return interpolateString(translatedString, values);
67
69
  }
68
70
  }
@@ -2,15 +2,13 @@
2
2
  * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
+ /**
6
+ * @module utils/translation-service
7
+ */
8
+ import type { Translations } from './locale.js';
9
+ import { type ArrayOrItem } from './toarray.js';
5
10
  declare global {
6
- var CKEDITOR_TRANSLATIONS: {
7
- [language: string]: {
8
- dictionary: {
9
- [messageId: string]: string | ReadonlyArray<string>;
10
- };
11
- getPluralForm?: (n: number) => number;
12
- };
13
- };
11
+ var CKEDITOR_TRANSLATIONS: Translations;
14
12
  }
15
13
  /**
16
14
  * Adds translations to existing ones or overrides the existing translations. These translations will later
@@ -135,15 +133,23 @@ export declare function add(language: string, translations: {
135
133
  * @param language Target language.
136
134
  * @param message A message that will be translated.
137
135
  * @param quantity The number of elements for which a plural form should be picked from the target language dictionary.
136
+ * @param translations Translations passed in editor config, if not provided use the global `window.CKEDITOR_TRANSLATIONS`.
138
137
  * @returns Translated sentence.
139
138
  */
140
- export declare function _translate(language: string, message: Message, quantity?: number): string;
139
+ export declare function _translate(language: string, message: Message, quantity?: number, translations?: Translations): string;
141
140
  /**
142
141
  * Clears dictionaries for test purposes.
143
142
  *
144
143
  * @internal
145
144
  */
146
145
  export declare function _clear(): void;
146
+ /**
147
+ * If array then merge objects which are inside otherwise return given object.
148
+ *
149
+ * @internal
150
+ * @param translations Translations passed in editor config.
151
+ */
152
+ export declare function _unifyTranslations(translations?: ArrayOrItem<Translations>): Translations | undefined;
147
153
  /**
148
154
  * The internationalization message interface. A message that implements this interface can be passed to the `t()` function
149
155
  * to be translated to the target UI language.
@@ -2,12 +2,9 @@
2
2
  * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
- /* eslint-disable no-var */
6
- /**
7
- * @module utils/translation-service
8
- */
9
5
  import CKEditorError from './ckeditorerror.js';
10
6
  import global from './dom/global.js';
7
+ import { merge } from 'lodash-es';
11
8
  /* istanbul ignore else -- @preserve */
12
9
  if (!global.window.CKEDITOR_TRANSLATIONS) {
13
10
  global.window.CKEDITOR_TRANSLATIONS = {};
@@ -141,9 +138,10 @@ export function add(language, translations, getPluralForm) {
141
138
  * @param language Target language.
142
139
  * @param message A message that will be translated.
143
140
  * @param quantity The number of elements for which a plural form should be picked from the target language dictionary.
141
+ * @param translations Translations passed in editor config, if not provided use the global `window.CKEDITOR_TRANSLATIONS`.
144
142
  * @returns Translated sentence.
145
143
  */
146
- export function _translate(language, message, quantity = 1) {
144
+ export function _translate(language, message, quantity = 1, translations) {
147
145
  if (typeof quantity !== 'number') {
148
146
  /**
149
147
  * An incorrect value was passed to the translation function. This was probably caused
@@ -154,22 +152,23 @@ export function _translate(language, message, quantity = 1) {
154
152
  */
155
153
  throw new CKEditorError('translation-service-quantity-not-a-number', null, { quantity });
156
154
  }
157
- const numberOfLanguages = getNumberOfLanguages();
155
+ const normalizedTranslations = translations || global.window.CKEDITOR_TRANSLATIONS;
156
+ const numberOfLanguages = getNumberOfLanguages(normalizedTranslations);
158
157
  if (numberOfLanguages === 1) {
159
158
  // Override the language to the only supported one.
160
159
  // This can't be done in the `Locale` class, because the translations comes after the `Locale` class initialization.
161
- language = Object.keys(global.window.CKEDITOR_TRANSLATIONS)[0];
160
+ language = Object.keys(normalizedTranslations)[0];
162
161
  }
163
162
  const messageId = message.id || message.string;
164
- if (numberOfLanguages === 0 || !hasTranslation(language, messageId)) {
163
+ if (numberOfLanguages === 0 || !hasTranslation(language, messageId, normalizedTranslations)) {
165
164
  if (quantity !== 1) {
166
165
  // Return the default plural form that was passed in the `message.plural` parameter.
167
166
  return message.plural;
168
167
  }
169
168
  return message.string;
170
169
  }
171
- const dictionary = global.window.CKEDITOR_TRANSLATIONS[language].dictionary;
172
- const getPluralForm = global.window.CKEDITOR_TRANSLATIONS[language].getPluralForm || (n => n === 1 ? 0 : 1);
170
+ const dictionary = normalizedTranslations[language].dictionary;
171
+ const getPluralForm = normalizedTranslations[language].getPluralForm || (n => n === 1 ? 0 : 1);
173
172
  const translation = dictionary[messageId];
174
173
  if (typeof translation === 'string') {
175
174
  return translation;
@@ -184,15 +183,27 @@ export function _translate(language, message, quantity = 1) {
184
183
  * @internal
185
184
  */
186
185
  export function _clear() {
187
- global.window.CKEDITOR_TRANSLATIONS = {};
186
+ if (global.window.CKEDITOR_TRANSLATIONS) {
187
+ global.window.CKEDITOR_TRANSLATIONS = {};
188
+ }
189
+ }
190
+ /**
191
+ * If array then merge objects which are inside otherwise return given object.
192
+ *
193
+ * @internal
194
+ * @param translations Translations passed in editor config.
195
+ */
196
+ export function _unifyTranslations(translations) {
197
+ return Array.isArray(translations) ?
198
+ translations.reduce((acc, translation) => merge(acc, translation)) :
199
+ translations;
188
200
  }
189
201
  /**
190
202
  * Checks whether the dictionary exists and translation in that dictionary exists.
191
203
  */
192
- function hasTranslation(language, messageId) {
193
- return (!!global.window.CKEDITOR_TRANSLATIONS[language] &&
194
- !!global.window.CKEDITOR_TRANSLATIONS[language].dictionary[messageId]);
204
+ function hasTranslation(language, messageId, translations) {
205
+ return !!translations[language] && !!translations[language].dictionary[messageId];
195
206
  }
196
- function getNumberOfLanguages() {
197
- return Object.keys(global.window.CKEDITOR_TRANSLATIONS).length;
207
+ function getNumberOfLanguages(translations) {
208
+ return Object.keys(translations).length;
198
209
  }
package/src/version.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
- declare const version = "41.0.0";
5
+ declare const version = "41.2.0";
6
6
  export default version;
7
7
  export declare const releaseDate: Date;
8
8
  declare global {
package/src/version.js CHANGED
@@ -6,10 +6,10 @@
6
6
  * @module utils/version
7
7
  */
8
8
  import CKEditorError from './ckeditorerror.js';
9
- const version = '41.0.0';
9
+ const version = '41.2.0';
10
10
  export default version;
11
11
  // The second argument is not a month. It is `monthIndex` and starts from `0`.
12
- export const releaseDate = new Date(2024, 0, 17);
12
+ export const releaseDate = new Date(2024, 2, 6);
13
13
  /* istanbul ignore next -- @preserve */
14
14
  if (globalThis.CKEDITOR_VERSION) {
15
15
  /**