@ckeditor/ckeditor5-emoji 44.2.0-alpha.8 → 44.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-emoji",
3
- "version": "44.2.0-alpha.8",
3
+ "version": "44.2.0",
4
4
  "description": "Emoji feature for CKEditor 5.",
5
5
  "keywords": [
6
6
  "ckeditor",
@@ -13,12 +13,12 @@
13
13
  "type": "module",
14
14
  "main": "src/index.js",
15
15
  "dependencies": {
16
- "@ckeditor/ckeditor5-core": "44.2.0-alpha.8",
17
- "@ckeditor/ckeditor5-mention": "44.2.0-alpha.8",
18
- "@ckeditor/ckeditor5-typing": "44.2.0-alpha.8",
19
- "@ckeditor/ckeditor5-ui": "44.2.0-alpha.8",
20
- "@ckeditor/ckeditor5-utils": "44.2.0-alpha.8",
21
- "ckeditor5": "44.2.0-alpha.8",
16
+ "@ckeditor/ckeditor5-core": "44.2.0",
17
+ "@ckeditor/ckeditor5-mention": "44.2.0",
18
+ "@ckeditor/ckeditor5-typing": "44.2.0",
19
+ "@ckeditor/ckeditor5-ui": "44.2.0",
20
+ "@ckeditor/ckeditor5-utils": "44.2.0",
21
+ "ckeditor5": "44.2.0",
22
22
  "fuse.js": "7.0.0",
23
23
  "lodash-es": "4.17.21"
24
24
  },
@@ -58,6 +58,22 @@ export interface EmojiConfig {
58
58
  * @default 'default'
59
59
  */
60
60
  skinTone?: SkinToneId;
61
+ /**
62
+ * The URL from which the emoji definitions should be loaded.
63
+ *
64
+ * ```ts
65
+ * ClassicEditor
66
+ * .create( editorElement, {
67
+ * plugins: [ Emoji, ... ],
68
+ * emoji: {
69
+ * definitionsUrl: ''
70
+ * }
71
+ * } )
72
+ * .then( ... )
73
+ * .catch( ... );
74
+ * ```
75
+ */
76
+ definitionsUrl?: string;
61
77
  /**
62
78
  * The emoji database version.
63
79
  *
@@ -73,8 +89,10 @@ export interface EmojiConfig {
73
89
  * .catch( ... );
74
90
  * ```
75
91
  *
76
- * @default 16
92
+ * If the {@link module:emoji/emojiconfig~EmojiConfig#definitionsUrl `emoji.definitionsUrl`}
93
+ * option is provided, `version` is ignored as the defined URL takes precedence over the `version`.
77
94
  */
78
- version?: 15 | 16;
95
+ version?: EmojiVersion;
79
96
  }
80
97
  export type SkinToneId = 'default' | 'light' | 'medium-light' | 'medium' | 'medium-dark' | 'dark';
98
+ export type EmojiVersion = 15 | 16;
@@ -176,6 +176,10 @@ export default class EmojiMention extends Plugin {
176
176
  if (searchQuery.startsWith(' ')) {
177
177
  return [];
178
178
  }
179
+ // Do not show anything when a query starts with a marker character.
180
+ if (searchQuery.startsWith(EMOJI_MENTION_MARKER)) {
181
+ return [];
182
+ }
179
183
  // If the repository plugin is not available, return an empty feed to avoid confusion. See: #17842.
180
184
  if (!this._isEmojiRepositoryAvailable) {
181
185
  return [];
@@ -8,26 +8,26 @@ import type { SkinToneId } from './emojiconfig.js';
8
8
  /**
9
9
  * The emoji repository plugin.
10
10
  *
11
- * Loads the emoji database from URL during plugin initialization and provides utility methods to search it.
11
+ * Loads the emoji repository from URL during plugin initialization and provides utility methods to search it.
12
12
  */
13
13
  export default class EmojiRepository extends Plugin {
14
14
  /**
15
- * A callback to resolve the {@link #_databasePromise} to control the return value of this promise.
15
+ * A callback to resolve the {@link #_repositoryPromise} to control the return value of this promise.
16
16
  */
17
- private _databasePromiseResolveCallback;
17
+ private _repositoryPromiseResolveCallback;
18
18
  /**
19
19
  * An instance of the [Fuse.js](https://www.fusejs.io/) library.
20
20
  */
21
21
  private _fuseSearch;
22
22
  /**
23
- * Emoji database.
23
+ * The resolved URL from which the emoji repository is downloaded.
24
24
  */
25
- private _database;
25
+ private readonly _url;
26
26
  /**
27
- * A promise resolved after downloading the emoji database.
28
- * The promise resolves with `true` when the database is successfully downloaded or `false` otherwise.
27
+ * A promise resolved after downloading the emoji collection.
28
+ * The promise resolves with `true` when the repository is successfully downloaded or `false` otherwise.
29
29
  */
30
- private readonly _databasePromise;
30
+ private readonly _repositoryPromise;
31
31
  /**
32
32
  * @inheritDoc
33
33
  */
@@ -50,7 +50,7 @@ export default class EmojiRepository extends Plugin {
50
50
  init(): Promise<void>;
51
51
  /**
52
52
  * Returns an array of emoji entries that match the search query.
53
- * If the emoji database is not loaded, the [Fuse.js](https://www.fusejs.io/) instance is not created,
53
+ * If the emoji repository is not loaded, the [Fuse.js](https://www.fusejs.io/) instance is not created,
54
54
  * hence this method returns an empty array.
55
55
  *
56
56
  * @param searchQuery A search query to match emoji.
@@ -59,7 +59,7 @@ export default class EmojiRepository extends Plugin {
59
59
  getEmojiByQuery(searchQuery: string): Array<EmojiEntry>;
60
60
  /**
61
61
  * Groups all emojis by categories.
62
- * If the emoji database is not loaded, it returns an empty array.
62
+ * If the emoji repository is not loaded, it returns an empty array.
63
63
  *
64
64
  * @returns An array of emoji entries grouped by categories.
65
65
  */
@@ -69,9 +69,40 @@ export default class EmojiRepository extends Plugin {
69
69
  */
70
70
  getSkinTones(): Array<SkinTone>;
71
71
  /**
72
- * Indicates whether the emoji database has been successfully downloaded and the plugin is operational.
72
+ * Indicates whether the emoji repository has been successfully downloaded and the plugin is operational.
73
73
  */
74
74
  isReady(): Promise<boolean>;
75
+ /**
76
+ * Returns the URL from which the emoji repository is downloaded. If the URL is not provided
77
+ * in the configuration, the default URL is used with the version from the configuration.
78
+ *
79
+ * If both the URL and version are provided, a warning is logged.
80
+ */
81
+ private _getUrl;
82
+ /**
83
+ * Warn users on self-hosted installations that this plugin uses a CDN to fetch the emoji repository.
84
+ */
85
+ private _warnAboutCdnUse;
86
+ /**
87
+ * Returns the emoji repository in a configured version if it is a non-empty array. Returns `null` otherwise.
88
+ */
89
+ private _getItems;
90
+ /**
91
+ * Loads the emoji repository. If the repository is already loaded, it returns the cached result.
92
+ * Otherwise, it fetches the repository from the URL and adds it to the cache.
93
+ */
94
+ private _loadAndCacheEmoji;
95
+ /**
96
+ * Normalizes the raw data fetched from CDN. By normalization, we meant:
97
+ *
98
+ * * Filter out unsupported emoji (these that will not render correctly),
99
+ * * Prepare skin tone variants if an emoji defines them.
100
+ */
101
+ private _normalizeEmoji;
102
+ /**
103
+ * Versioned emoji repository.
104
+ */
105
+ private static _results;
75
106
  }
76
107
  /**
77
108
  * Represents a single group of the emoji category, e.g., "Smileys & Expressions".
@@ -8,17 +8,18 @@
8
8
  import Fuse from 'fuse.js';
9
9
  import { groupBy } from 'lodash-es';
10
10
  import { Plugin } from 'ckeditor5/src/core.js';
11
- import { logWarning } from 'ckeditor5/src/utils.js';
11
+ import { logWarning, version as editorVersion } from 'ckeditor5/src/utils.js';
12
12
  import EmojiUtils from './emojiutils.js';
13
- // An endpoint from which the emoji database will be downloaded during plugin initialization.
13
+ // An endpoint from which the emoji data will be downloaded during plugin initialization.
14
14
  // The `{version}` placeholder is replaced with the value from editor config.
15
- const EMOJI_DATABASE_URL = 'https://cdn.ckeditor.com/ckeditor5/data/emoji/{version}/en.json';
15
+ const DEFAULT_EMOJI_DATABASE_URL = 'https://cdn.ckeditor.com/ckeditor5/data/emoji/{version}/en.json';
16
+ const DEFAULT_EMOJI_VERSION = 16;
16
17
  /**
17
18
  * The emoji repository plugin.
18
19
  *
19
- * Loads the emoji database from URL during plugin initialization and provides utility methods to search it.
20
+ * Loads the emoji repository from URL during plugin initialization and provides utility methods to search it.
20
21
  */
21
- export default class EmojiRepository extends Plugin {
22
+ class EmojiRepository extends Plugin {
22
23
  /**
23
24
  * @inheritDoc
24
25
  */
@@ -42,13 +43,14 @@ export default class EmojiRepository extends Plugin {
42
43
  */
43
44
  constructor(editor) {
44
45
  super(editor);
45
- this.editor.config.define('emoji', {
46
- version: 16,
47
- skinTone: 'default'
46
+ editor.config.define('emoji', {
47
+ version: undefined,
48
+ skinTone: 'default',
49
+ definitionsUrl: undefined
48
50
  });
49
- this._database = [];
50
- this._databasePromise = new Promise(resolve => {
51
- this._databasePromiseResolveCallback = resolve;
51
+ this._url = this._getUrl();
52
+ this._repositoryPromise = new Promise(resolve => {
53
+ this._repositoryPromiseResolveCallback = resolve;
52
54
  });
53
55
  this._fuseSearch = null;
54
56
  }
@@ -56,26 +58,16 @@ export default class EmojiRepository extends Plugin {
56
58
  * @inheritDoc
57
59
  */
58
60
  async init() {
59
- const emojiUtils = this.editor.plugins.get('EmojiUtils');
60
- const emojiVersion = this.editor.config.get('emoji.version');
61
- const emojiDatabaseUrl = EMOJI_DATABASE_URL.replace('{version}', `${emojiVersion}`);
62
- const emojiDatabase = await loadEmojiDatabase(emojiDatabaseUrl);
63
- const emojiSupportedVersionByOs = emojiUtils.getEmojiSupportedVersionByOs();
64
- // Skip the initialization if the emoji database download has failed.
65
- // An empty database prevents the initialization of other dependent plugins, such as `EmojiMention` and `EmojiPicker`.
66
- if (!emojiDatabase.length) {
67
- return this._databasePromiseResolveCallback(false);
61
+ this._warnAboutCdnUse();
62
+ await this._loadAndCacheEmoji();
63
+ const items = this._getItems();
64
+ // Skip plugin initialization if the emoji repository is not available.
65
+ // The initialization of other dependent plugins, such as `EmojiMention` and `EmojiPicker`, is prevented as well.
66
+ if (!items) {
67
+ return this._repositoryPromiseResolveCallback(false);
68
68
  }
69
- const container = emojiUtils.createEmojiWidthTestingContainer();
70
- document.body.appendChild(container);
71
- // Store the emoji database after normalizing the raw data.
72
- this._database = emojiDatabase
73
- .filter(item => emojiUtils.isEmojiCategoryAllowed(item))
74
- .filter(item => emojiUtils.isEmojiSupported(item, emojiSupportedVersionByOs, container))
75
- .map(item => emojiUtils.normalizeEmojiSkinTone(item));
76
- container.remove();
77
69
  // Create instance of the Fuse.js library with configured weighted search keys and disabled fuzzy search.
78
- this._fuseSearch = new Fuse(this._database, {
70
+ this._fuseSearch = new Fuse(items, {
79
71
  keys: [
80
72
  { name: 'emoticon', weight: 5 },
81
73
  { name: 'annotation', weight: 3 },
@@ -85,11 +77,11 @@ export default class EmojiRepository extends Plugin {
85
77
  threshold: 0,
86
78
  ignoreLocation: true
87
79
  });
88
- return this._databasePromiseResolveCallback(true);
80
+ return this._repositoryPromiseResolveCallback(true);
89
81
  }
90
82
  /**
91
83
  * Returns an array of emoji entries that match the search query.
92
- * If the emoji database is not loaded, the [Fuse.js](https://www.fusejs.io/) instance is not created,
84
+ * If the emoji repository is not loaded, the [Fuse.js](https://www.fusejs.io/) instance is not created,
93
85
  * hence this method returns an empty array.
94
86
  *
95
87
  * @param searchQuery A search query to match emoji.
@@ -123,12 +115,13 @@ export default class EmojiRepository extends Plugin {
123
115
  }
124
116
  /**
125
117
  * Groups all emojis by categories.
126
- * If the emoji database is not loaded, it returns an empty array.
118
+ * If the emoji repository is not loaded, it returns an empty array.
127
119
  *
128
120
  * @returns An array of emoji entries grouped by categories.
129
121
  */
130
122
  getEmojiCategories() {
131
- if (!this._database.length) {
123
+ const repository = this._getItems();
124
+ if (!repository) {
132
125
  return [];
133
126
  }
134
127
  const { t } = this.editor.locale;
@@ -143,7 +136,7 @@ export default class EmojiRepository extends Plugin {
143
136
  { title: t('Symbols'), icon: '🟢', groupId: 8 },
144
137
  { title: t('Flags'), icon: '🏁', groupId: 9 }
145
138
  ];
146
- const groups = groupBy(this._database, 'group');
139
+ const groups = groupBy(repository, 'group');
147
140
  return categories.map(category => {
148
141
  return {
149
142
  ...category,
@@ -166,37 +159,132 @@ export default class EmojiRepository extends Plugin {
166
159
  ];
167
160
  }
168
161
  /**
169
- * Indicates whether the emoji database has been successfully downloaded and the plugin is operational.
162
+ * Indicates whether the emoji repository has been successfully downloaded and the plugin is operational.
170
163
  */
171
164
  isReady() {
172
- return this._databasePromise;
165
+ return this._repositoryPromise;
173
166
  }
174
- }
175
- /**
176
- * Makes the HTTP request to download the emoji database.
177
- */
178
- async function loadEmojiDatabase(emojiDatabaseUrl) {
179
- const result = await fetch(emojiDatabaseUrl)
180
- .then(response => {
181
- if (!response.ok) {
182
- return [];
167
+ /**
168
+ * Returns the URL from which the emoji repository is downloaded. If the URL is not provided
169
+ * in the configuration, the default URL is used with the version from the configuration.
170
+ *
171
+ * If both the URL and version are provided, a warning is logged.
172
+ */
173
+ _getUrl() {
174
+ const { definitionsUrl, version } = this.editor.config.get('emoji');
175
+ if (!definitionsUrl || definitionsUrl === 'cdn') {
176
+ // URL was not provided or is set to 'cdn', so we use the default CDN URL.
177
+ const urlVersion = version || DEFAULT_EMOJI_VERSION;
178
+ const url = new URL(DEFAULT_EMOJI_DATABASE_URL.replace('{version}', urlVersion.toString()));
179
+ url.searchParams.set('editorVersion', editorVersion);
180
+ return url;
181
+ }
182
+ if (version) {
183
+ /**
184
+ * Both {@link module:emoji/emojiconfig~EmojiConfig#definitionsUrl `emoji.definitionsUrl`} and
185
+ * {@link module:emoji/emojiconfig~EmojiConfig#version `emoji.version`} configuration options
186
+ * are set. Only the `emoji.definitionsUrl` option will be used.
187
+ *
188
+ * The `emoji.version` option will be ignored and should be removed from the configuration.
189
+ *
190
+ * @error emoji-repository-redundant-version
191
+ */
192
+ logWarning('emoji-repository-redundant-version');
193
+ }
194
+ return new URL(definitionsUrl);
195
+ }
196
+ /**
197
+ * Warn users on self-hosted installations that this plugin uses a CDN to fetch the emoji repository.
198
+ */
199
+ _warnAboutCdnUse() {
200
+ const editor = this.editor;
201
+ const config = editor.config.get('emoji');
202
+ const licenseKey = editor.config.get('licenseKey');
203
+ const distributionChannel = window[Symbol.for('cke distribution')];
204
+ if (licenseKey === 'GPL') {
205
+ // Don't warn GPL users.
206
+ return;
207
+ }
208
+ if (distributionChannel === 'cloud') {
209
+ // Don't warn cloud users, because they already use our CDN.
210
+ return;
211
+ }
212
+ if (config && config.definitionsUrl) {
213
+ // Don't warn users who have configured their own definitions URL.
214
+ return;
183
215
  }
184
- return response.json();
185
- })
186
- .catch(() => {
187
- return [];
188
- });
189
- if (!result.length) {
190
216
  /**
191
- * Unable to load the emoji database from CDN.
217
+ * By default, the Emoji plugin fetches the emoji repository from CKEditor 5 CDN. To avoid this,
218
+ * you can use the {@link module:emoji/emojiconfig~EmojiConfig#definitionsUrl `emoji.definitionsUrl`}
219
+ * configuration option to provide a URL to your own emoji repository.
192
220
  *
193
- * If the CDN works properly and there is no disruption of communication, please check your
194
- * {@glink getting-started/setup/csp Content Security Policy (CSP)} setting and make sure
195
- * the CDN connection is allowed by the editor.
221
+ * If you only want to suppress this warning, set this configuration option to `cdn`.
196
222
  *
197
- * @error emoji-database-load-failed
223
+ * @error emoji-repository-cdn-use
198
224
  */
199
- logWarning('emoji-database-load-failed');
225
+ logWarning('emoji-repository-cdn-use');
226
+ }
227
+ /**
228
+ * Returns the emoji repository in a configured version if it is a non-empty array. Returns `null` otherwise.
229
+ */
230
+ _getItems() {
231
+ const repository = EmojiRepository._results[this._url.href];
232
+ return repository && repository.length ? repository : null;
233
+ }
234
+ /**
235
+ * Loads the emoji repository. If the repository is already loaded, it returns the cached result.
236
+ * Otherwise, it fetches the repository from the URL and adds it to the cache.
237
+ */
238
+ async _loadAndCacheEmoji() {
239
+ if (EmojiRepository._results[this._url.href]) {
240
+ // The repository has already been downloaded.
241
+ return;
242
+ }
243
+ const result = await fetch(this._url, { cache: 'force-cache' })
244
+ .then(response => {
245
+ if (!response.ok) {
246
+ return [];
247
+ }
248
+ return response.json();
249
+ })
250
+ .catch(() => {
251
+ return [];
252
+ });
253
+ if (!result.length) {
254
+ /**
255
+ * Unable to load the emoji repository from the URL.
256
+ *
257
+ * If the URL works properly and there is no disruption of communication, please check your
258
+ * {@glink getting-started/setup/csp Content Security Policy (CSP)} setting and make sure
259
+ * the URL connection is allowed by the editor.
260
+ *
261
+ * @error emoji-repository-load-failed
262
+ */
263
+ logWarning('emoji-repository-load-failed');
264
+ }
265
+ EmojiRepository._results[this._url.href] = this._normalizeEmoji(result);
266
+ }
267
+ /**
268
+ * Normalizes the raw data fetched from CDN. By normalization, we meant:
269
+ *
270
+ * * Filter out unsupported emoji (these that will not render correctly),
271
+ * * Prepare skin tone variants if an emoji defines them.
272
+ */
273
+ _normalizeEmoji(data) {
274
+ const emojiUtils = this.editor.plugins.get('EmojiUtils');
275
+ const emojiSupportedVersionByOs = emojiUtils.getEmojiSupportedVersionByOs();
276
+ const container = emojiUtils.createEmojiWidthTestingContainer();
277
+ document.body.appendChild(container);
278
+ const results = data
279
+ .filter(item => emojiUtils.isEmojiCategoryAllowed(item))
280
+ .filter(item => emojiUtils.isEmojiSupported(item, emojiSupportedVersionByOs, container))
281
+ .map(item => emojiUtils.normalizeEmojiSkinTone(item));
282
+ container.remove();
283
+ return results;
200
284
  }
201
- return result;
202
285
  }
286
+ /**
287
+ * Versioned emoji repository.
288
+ */
289
+ EmojiRepository._results = {};
290
+ export default EmojiRepository;
@@ -59,7 +59,7 @@ export default function isEmojiSupported(unicode) {
59
59
  ;
60
60
  function getCanvas() {
61
61
  try {
62
- return document.createElement('canvas').getContext('2d');
62
+ return document.createElement('canvas').getContext('2d', { willReadFrequently: true });
63
63
  }
64
64
  catch {
65
65
  /* istanbul ignore next -- @preserve */