ckeditor5-livewire 1.12.1 → 1.13.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 (32) hide show
  1. package/dist/hooks/editable.d.ts +4 -0
  2. package/dist/hooks/editable.d.ts.map +1 -1
  3. package/dist/hooks/editor/editor.d.ts +4 -0
  4. package/dist/hooks/editor/editor.d.ts.map +1 -1
  5. package/dist/hooks/editor/utils/assign-editor-roots-to-config.d.ts +17 -0
  6. package/dist/hooks/editor/utils/assign-editor-roots-to-config.d.ts.map +1 -0
  7. package/dist/hooks/editor/utils/index.d.ts +2 -3
  8. package/dist/hooks/editor/utils/index.d.ts.map +1 -1
  9. package/dist/hooks/editor/utils/query-all-editor-editables.d.ts +24 -0
  10. package/dist/hooks/editor/utils/query-all-editor-editables.d.ts.map +1 -0
  11. package/dist/index.cjs +2 -2
  12. package/dist/index.cjs.map +1 -1
  13. package/dist/index.mjs +175 -214
  14. package/dist/index.mjs.map +1 -1
  15. package/package.json +1 -1
  16. package/src/hooks/editable.test.ts +34 -0
  17. package/src/hooks/editable.ts +7 -1
  18. package/src/hooks/editor/editor.test.ts +165 -1
  19. package/src/hooks/editor/editor.ts +45 -64
  20. package/src/hooks/editor/utils/assign-editor-roots-to-config.ts +61 -0
  21. package/src/hooks/editor/utils/get-editor-roots-values.ts +1 -1
  22. package/src/hooks/editor/utils/index.ts +2 -3
  23. package/src/hooks/editor/utils/query-all-editor-editables.ts +87 -0
  24. package/dist/hooks/editor/utils/assign-initial-data-to-editor-config.d.ts +0 -10
  25. package/dist/hooks/editor/utils/assign-initial-data-to-editor-config.d.ts.map +0 -1
  26. package/dist/hooks/editor/utils/assign-source-elements-to-editor-config.d.ts +0 -12
  27. package/dist/hooks/editor/utils/assign-source-elements-to-editor-config.d.ts.map +0 -1
  28. package/dist/hooks/editor/utils/query-editor-editables.d.ts +0 -32
  29. package/dist/hooks/editor/utils/query-editor-editables.d.ts.map +0 -1
  30. package/src/hooks/editor/utils/assign-initial-data-to-editor-config.ts +0 -48
  31. package/src/hooks/editor/utils/assign-source-elements-to-editor-config.ts +0 -61
  32. package/src/hooks/editor/utils/query-editor-editables.ts +0 -93
@@ -1,32 +0,0 @@
1
- import { EditorId } from '../typings';
2
- /**
3
- * Gets the initial root elements for the editor based on its type.
4
- *
5
- * @param editorId The editor's ID.
6
- * @returns The root element(s) for the editor.
7
- */
8
- export declare function queryEditablesElements(editorId: EditorId): Record<string, HTMLElement>;
9
- /**
10
- * Queries all editable elements within a specific editor instance.
11
- *
12
- * @param editorId The ID of the editor to query.
13
- * @returns An object mapping editable names to their corresponding elements and initial values.
14
- */
15
- export declare function queryAllEditorEditables(editorId: EditorId): Record<string, EditableItem>;
16
- /**
17
- * Gets the initial data for the roots of the editor. If the editor is a single editing-like editor,
18
- * it retrieves the initial value from the element's attribute. Otherwise, it returns an object mapping
19
- * editable names to their initial values.
20
- *
21
- * @param editorId The editor's ID.
22
- * @returns The initial values for the editor's roots.
23
- */
24
- export declare function queryEditablesSnapshotContent(editorId: EditorId): Record<string, string>;
25
- /**
26
- * Type representing an editable item within an editor.
27
- */
28
- export type EditableItem = {
29
- element: HTMLElement;
30
- content: string | null;
31
- };
32
- //# sourceMappingURL=query-editor-editables.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"query-editor-editables.d.ts","sourceRoot":"","sources":["../../../../../src/hooks/editor/utils/query-editor-editables.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAI3C;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,QAAQ,+BAIxD;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CA+CxF;AAED;;;;;;;GAOG;AACH,wBAAgB,6BAA6B,CAAC,QAAQ,EAAE,QAAQ,GAIW,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAChG;AAED;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB,OAAO,EAAE,WAAW,CAAC;IACrB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;CACxB,CAAC"}
@@ -1,48 +0,0 @@
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 start -- @preserve */
27
- ...rootKey in dataMap
28
- ? {
29
- initialData: dataMap[rootKey],
30
- }
31
- : {},
32
- /* v8 ignore stop -- @preserve */
33
- },
34
- }), Object.create(config.roots || {}));
35
-
36
- const mappedConfig: C = {
37
- ...config,
38
- roots: rootsConfig,
39
- };
40
-
41
- delete mappedConfig.root;
42
-
43
- return mappedConfig;
44
- }
45
-
46
- function toDataMap(element: string | Record<string, string>): Record<string, string> {
47
- return typeof element === 'string' ? { main: element } : { ...element };
48
- }
@@ -1,61 +0,0 @@
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 start -- @preserve */
40
- ...rootKey in elementsMap
41
- ? {
42
- element: elementsMap[rootKey],
43
- }
44
- : {},
45
- /* v8 ignore stop -- @preserve */
46
- },
47
- }), Object.create(config.roots || {}));
48
-
49
- const mappedConfig: C = {
50
- ...config,
51
- roots: rootsConfig,
52
- };
53
-
54
- delete mappedConfig.root;
55
-
56
- return mappedConfig;
57
- }
58
-
59
- function toElementsMap(element: HTMLElement | Record<string, HTMLElement>): Record<string, HTMLElement> {
60
- return element instanceof HTMLElement ? { main: element } : { ...element };
61
- }
@@ -1,93 +0,0 @@
1
- import type { EditorId } from '../typings';
2
-
3
- import { filterObjectValues, mapObjectValues } from '../../../shared';
4
-
5
- /**
6
- * Gets the initial root elements for the editor based on its type.
7
- *
8
- * @param editorId The editor's ID.
9
- * @returns The root element(s) for the editor.
10
- */
11
- export function queryEditablesElements(editorId: EditorId) {
12
- const editables = queryAllEditorEditables(editorId);
13
-
14
- return mapObjectValues(editables, ({ element }) => element);
15
- }
16
-
17
- /**
18
- * Queries all editable elements within a specific editor instance.
19
- *
20
- * @param editorId The ID of the editor to query.
21
- * @returns An object mapping editable names to their corresponding elements and initial values.
22
- */
23
- export function queryAllEditorEditables(editorId: EditorId): Record<string, EditableItem> {
24
- const acc = (
25
- window.Livewire
26
- .all()
27
- .filter(({ name, canonical }) => name === 'ckeditor5-editable' && canonical['editorId'] === editorId)
28
- .reduce<Record<string, EditableItem>>((acc, { canonical, el }) => ({
29
- ...acc,
30
- [canonical['rootName'] as string]: {
31
- element: el.querySelector('[data-cke-editable-content]')!,
32
- content: canonical['content'],
33
- },
34
- }), Object.create({}))
35
- );
36
-
37
- const rootHook = (
38
- window.Livewire
39
- .all()
40
- .find(({ name, canonical }) => name === 'ckeditor5' && canonical['editorId'] === editorId)
41
- );
42
-
43
- const initialRootEditableValue = rootHook?.canonical.content as Record<string, string> | null;
44
- const contentElement = document.querySelector<HTMLElement>(`#${editorId}_editor `);
45
- const currentMain = acc['main'];
46
-
47
- // If found `main` editable, but it has no content, try to fill it from the editor container.
48
- if (currentMain && initialRootEditableValue?.['main']) {
49
- return {
50
- ...acc,
51
- main: {
52
- ...currentMain,
53
- content: currentMain.content || initialRootEditableValue['main'],
54
- },
55
- };
56
- }
57
-
58
- // If no `main` editable found, try to create it from the editor container.
59
- if (contentElement) {
60
- return {
61
- ...acc,
62
- main: {
63
- element: contentElement,
64
- content: initialRootEditableValue?.['main'] || null,
65
- },
66
- };
67
- }
68
-
69
- return acc;
70
- }
71
-
72
- /**
73
- * Gets the initial data for the roots of the editor. If the editor is a single editing-like editor,
74
- * it retrieves the initial value from the element's attribute. Otherwise, it returns an object mapping
75
- * editable names to their initial values.
76
- *
77
- * @param editorId The editor's ID.
78
- * @returns The initial values for the editor's roots.
79
- */
80
- export function queryEditablesSnapshotContent(editorId: EditorId) {
81
- const editables = queryAllEditorEditables(editorId);
82
- const values = mapObjectValues(editables, ({ content }) => content);
83
-
84
- return filterObjectValues(values, value => typeof value === 'string') as Record<string, string>;
85
- }
86
-
87
- /**
88
- * Type representing an editable item within an editor.
89
- */
90
- export type EditableItem = {
91
- element: HTMLElement;
92
- content: string | null;
93
- };