ckeditor5-phoenix 1.26.0 → 1.27.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.
Files changed (26) hide show
  1. package/dist/hooks/editor/editor.d.ts.map +1 -1
  2. package/dist/hooks/editor/types/editor-relaxed-constructor.type.d.ts +6 -0
  3. package/dist/hooks/editor/types/editor-relaxed-constructor.type.d.ts.map +1 -0
  4. package/dist/hooks/editor/types/index.d.ts +2 -0
  5. package/dist/hooks/editor/types/index.d.ts.map +1 -0
  6. package/dist/hooks/editor/utils/assign-initial-data-to-editor-config.d.ts +10 -0
  7. package/dist/hooks/editor/utils/assign-initial-data-to-editor-config.d.ts.map +1 -0
  8. package/dist/hooks/editor/utils/assign-source-elements-to-editor-config.d.ts +12 -0
  9. package/dist/hooks/editor/utils/assign-source-elements-to-editor-config.d.ts.map +1 -0
  10. package/dist/hooks/editor/utils/create-editor-in-context.d.ts +1 -3
  11. package/dist/hooks/editor/utils/create-editor-in-context.d.ts.map +1 -1
  12. package/dist/hooks/editor/utils/index.d.ts +2 -0
  13. package/dist/hooks/editor/utils/index.d.ts.map +1 -1
  14. package/dist/index.cjs +2 -2
  15. package/dist/index.cjs.map +1 -1
  16. package/dist/index.mjs +346 -296
  17. package/dist/index.mjs.map +1 -1
  18. package/package.json +1 -1
  19. package/src/hooks/editable.ts +1 -1
  20. package/src/hooks/editor/editor.ts +9 -6
  21. package/src/hooks/editor/types/editor-relaxed-constructor.type.ts +6 -0
  22. package/src/hooks/editor/types/index.ts +1 -0
  23. package/src/hooks/editor/utils/assign-initial-data-to-editor-config.ts +47 -0
  24. package/src/hooks/editor/utils/assign-source-elements-to-editor-config.ts +60 -0
  25. package/src/hooks/editor/utils/create-editor-in-context.ts +2 -5
  26. package/src/hooks/editor/utils/index.ts +2 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ckeditor5-phoenix",
3
- "version": "1.26.0",
3
+ "version": "1.27.0",
4
4
  "description": "CKEditor 5 integration for Phoenix Framework",
5
5
  "author": "Mateusz Bagiński <cziken58@gmail.com>",
6
6
  "license": "MIT",
@@ -56,7 +56,7 @@ class EditableHookImpl extends ClassHook {
56
56
 
57
57
  editor.addRoot(rootName, {
58
58
  isUndoable: false,
59
- data: initialValue,
59
+ initialData: initialValue,
60
60
  });
61
61
 
62
62
  const editable = ui.view.createEditable(rootName, contentElement);
@@ -11,6 +11,8 @@ import {
11
11
  createSyncEditorWithPhoenixPlugin,
12
12
  } from './plugins';
13
13
  import {
14
+ assignInitialDataToEditorConfig,
15
+ assignSourceElementsToEditorConfig,
14
16
  cleanupOrphanEditorElements,
15
17
  createEditorInContext,
16
18
  isSingleRootEditor,
@@ -262,15 +264,17 @@ class EditorHookImpl extends ClassHook {
262
264
  sourceElements = sourceElements['main'];
263
265
  }
264
266
 
265
- // Construct parsed config. First resolve DOM element references in the provided configuration.
266
- let resolvedConfig = resolveEditorConfigElementReferences(config);
267
+ let resolvedConfig = { ...config };
267
268
 
268
- // Then resolve translation references in the provided configuration, using the mixed translations.
269
+ // Do some postprocessing on received configuration.
270
+ resolvedConfig = resolveEditorConfigElementReferences(resolvedConfig);
269
271
  resolvedConfig = resolveEditorConfigTranslations([...mixedTranslations].reverse(), language.ui, resolvedConfig);
272
+ resolvedConfig = assignSourceElementsToEditorConfig(Constructor, sourceElements, resolvedConfig);
273
+ resolvedConfig = assignInitialDataToEditorConfig(initialData, resolvedConfig);
270
274
 
275
+ // Post-processed configuration of the editor.
271
276
  const parsedConfig = {
272
277
  ...resolvedConfig,
273
- initialData,
274
278
  licenseKey: license.key,
275
279
  plugins: loadedPlugins,
276
280
  language,
@@ -281,12 +285,11 @@ class EditorHookImpl extends ClassHook {
281
285
 
282
286
  const editor = await (async () => {
283
287
  if (!context || !(sourceElements instanceof HTMLElement)) {
284
- return (Constructor.create as any)(sourceElements, parsedConfig);
288
+ return Constructor.create(parsedConfig);
285
289
  }
286
290
 
287
291
  const result = await createEditorInContext({
288
292
  context,
289
- element: sourceElements,
290
293
  creator: Constructor,
291
294
  config: parsedConfig,
292
295
  });
@@ -0,0 +1,6 @@
1
+ import type { Editor } from 'ckeditor5';
2
+
3
+ export type EditorRelaxedConstructor<TEditor extends Editor = Editor> = {
4
+ create: (...args: any) => Promise<TEditor>;
5
+ editorName?: string;
6
+ };
@@ -0,0 +1 @@
1
+ export * from './editor-relaxed-constructor.type';
@@ -0,0 +1,47 @@
1
+ import type { EditorConfig } from 'ckeditor5';
2
+
3
+ /**
4
+ * Assigns initial data to specified editor config.
5
+ *
6
+ * @param dataOrMap Initial data to be assigned to config.
7
+ * @param config Config of the editor.
8
+ * @returns The updated configuration object.
9
+ */
10
+ export function assignInitialDataToEditorConfig<C extends EditorConfig>(
11
+ dataOrMap: string | Record<string, string>,
12
+ config: C,
13
+ ): C {
14
+ const dataMap = toDataMap(dataOrMap);
15
+ const allRootsKeys = new Set([
16
+ ...Object.keys(dataMap),
17
+ ...Object.keys(config.roots ?? {}),
18
+ ]);
19
+
20
+ const rootsConfig = Array.from(allRootsKeys).reduce((acc, rootKey) => ({
21
+ ...acc,
22
+ [rootKey]: {
23
+ ...config.roots?.[rootKey],
24
+ ...rootKey === 'main' ? config.root : {},
25
+
26
+ /* v8 ignore next 5 */
27
+ ...rootKey in dataMap
28
+ ? {
29
+ initialData: dataMap[rootKey],
30
+ }
31
+ : {},
32
+ },
33
+ }), Object.create(config.roots || {}));
34
+
35
+ const mappedConfig: C = {
36
+ ...config,
37
+ roots: rootsConfig,
38
+ };
39
+
40
+ delete mappedConfig.root;
41
+
42
+ return mappedConfig;
43
+ }
44
+
45
+ function toDataMap(element: string | Record<string, string>): Record<string, string> {
46
+ return typeof element === 'string' ? { main: element } : { ...element };
47
+ }
@@ -0,0 +1,60 @@
1
+ import type { EditorConfig } from 'ckeditor5';
2
+
3
+ import type { EditorRelaxedConstructor } from '../types/editor-relaxed-constructor.type';
4
+
5
+ /**
6
+ * Assigns a DOM element to the editor configuration in a way that is compatible with the specific editor type.
7
+ *
8
+ * @param Editor Constructor of the editor used to determine the location of element config entry.
9
+ * @param elementOrMap Element to be assigned to config.
10
+ * @param config Config of the editor.
11
+ * @returns The updated configuration object.
12
+ */
13
+ export function assignSourceElementsToEditorConfig<C extends EditorConfig>(
14
+ Editor: EditorRelaxedConstructor,
15
+ elementOrMap: HTMLElement | Record<string, HTMLElement>,
16
+ config: C,
17
+ ): C {
18
+ const elementsMap = toElementsMap(elementOrMap);
19
+
20
+ if (!Editor.editorName || Editor.editorName === 'ClassicEditor') {
21
+ return {
22
+ ...config,
23
+ attachTo: elementsMap['main'],
24
+ };
25
+ }
26
+
27
+ const allRootsKeys = new Set([
28
+ ...Object.keys(elementsMap),
29
+ ...Object.keys(config.roots ?? {}),
30
+ ]);
31
+
32
+ const rootsConfig = Array.from(allRootsKeys).reduce((acc, rootKey) => ({
33
+ ...acc,
34
+ [rootKey]: {
35
+ /* v8 ignore next */
36
+ ...config.roots?.[rootKey],
37
+ ...rootKey === 'main' ? config.root : {},
38
+
39
+ /* v8 ignore next 5 */
40
+ ...rootKey in elementsMap
41
+ ? {
42
+ element: elementsMap[rootKey],
43
+ }
44
+ : {},
45
+ },
46
+ }), Object.create(config.roots || {}));
47
+
48
+ const mappedConfig: C = {
49
+ ...config,
50
+ roots: rootsConfig,
51
+ };
52
+
53
+ delete mappedConfig.root;
54
+
55
+ return mappedConfig;
56
+ }
57
+
58
+ function toElementsMap(element: HTMLElement | Record<string, HTMLElement>): Record<string, HTMLElement> {
59
+ return element instanceof HTMLElement ? { main: element } : { ...element };
60
+ }
@@ -12,19 +12,17 @@ const CONTEXT_EDITOR_WATCHDOG_SYMBOL = Symbol.for('context-editor-watchdog');
12
12
  * Creates a CKEditor 5 editor instance within a given context watchdog.
13
13
  *
14
14
  * @param params Parameters for editor creation.
15
- * @param params.element The DOM element or data for the editor.
16
15
  * @param params.context The context watchdog instance.
17
16
  * @param params.creator The editor creator utility.
18
17
  * @param params.config The editor configuration object.
19
18
  * @returns The created editor instance.
20
19
  */
21
- export async function createEditorInContext({ element, context, creator, config }: Attrs) {
20
+ export async function createEditorInContext({ context, creator, config }: Attrs) {
22
21
  const editorContextId = uid();
23
22
 
24
23
  await context.add({
25
- creator: (_element, _config) => creator.create(_element, _config),
24
+ creator: creator.create.bind(creator),
26
25
  id: editorContextId,
27
- sourceElementOrData: element,
28
26
  type: 'editor',
29
27
  config,
30
28
  });
@@ -74,7 +72,6 @@ export function unwrapEditorContext(editor: Editor): EditorContextDescriptor | n
74
72
  type Attrs = {
75
73
  context: ContextWatchdog<Context>;
76
74
  creator: EditorCreator;
77
- element: HTMLElement;
78
75
  config: EditorConfig;
79
76
  };
80
77
 
@@ -1,3 +1,5 @@
1
+ export * from './assign-initial-data-to-editor-config';
2
+ export * from './assign-source-elements-to-editor-config';
1
3
  export * from './cleanup-orphan-editor-elements';
2
4
  export * from './create-editor-in-context';
3
5
  export * from './is-multiroot-editor-instance';