ckeditor5-livewire 1.1.1 → 1.1.2
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/dist/hooks/editor/plugins/livewire-sync.d.ts.map +1 -1
- package/dist/index.cjs +2 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +66 -62
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/hooks/editor/editor.test.ts +66 -0
- package/src/hooks/editor/plugins/livewire-sync.ts +43 -22
|
@@ -794,6 +794,72 @@ describe('editor component', () => {
|
|
|
794
794
|
|
|
795
795
|
expect(editor.getData()).toBe('<p>Initial content</p>');
|
|
796
796
|
});
|
|
797
|
+
|
|
798
|
+
it('should defer content update if editor is focused during `afterCommitSynced` and apply it on blur', async () => {
|
|
799
|
+
const { id } = livewireStub.$internal.appendComponentToDOM<EditorSnapshot>({
|
|
800
|
+
name: 'ckeditor5',
|
|
801
|
+
el: createEditorHtmlElement({
|
|
802
|
+
wireModel: 'content',
|
|
803
|
+
}),
|
|
804
|
+
canonical: {
|
|
805
|
+
...createEditorSnapshot(),
|
|
806
|
+
content: {
|
|
807
|
+
main: '<p>Initial content</p>',
|
|
808
|
+
},
|
|
809
|
+
},
|
|
810
|
+
});
|
|
811
|
+
|
|
812
|
+
const editor = await waitForTestEditor();
|
|
813
|
+
const { ui: { focusTracker } } = editor;
|
|
814
|
+
|
|
815
|
+
focusTracker.isFocused = true;
|
|
816
|
+
|
|
817
|
+
await livewireStub.$internal.dispatchComponentCommit<EditorSnapshot>(id, {
|
|
818
|
+
content: {
|
|
819
|
+
main: '<p>Updated content from Livewire</p>',
|
|
820
|
+
},
|
|
821
|
+
});
|
|
822
|
+
|
|
823
|
+
expect(editor.getData()).toBe('<p>Initial content</p>');
|
|
824
|
+
|
|
825
|
+
focusTracker.isFocused = false;
|
|
826
|
+
|
|
827
|
+
expect(editor.getData()).toBe('<p>Updated content from Livewire</p>');
|
|
828
|
+
});
|
|
829
|
+
|
|
830
|
+
it('should abort content update if editor is focused during `afterCommitSynced` and content changes before blur', async () => {
|
|
831
|
+
const { id } = livewireStub.$internal.appendComponentToDOM<EditorSnapshot>({
|
|
832
|
+
name: 'ckeditor5',
|
|
833
|
+
el: createEditorHtmlElement({
|
|
834
|
+
wireModel: 'content',
|
|
835
|
+
}),
|
|
836
|
+
canonical: {
|
|
837
|
+
...createEditorSnapshot(),
|
|
838
|
+
content: {
|
|
839
|
+
main: '<p>Initial content</p>',
|
|
840
|
+
},
|
|
841
|
+
},
|
|
842
|
+
});
|
|
843
|
+
|
|
844
|
+
const editor = await waitForTestEditor();
|
|
845
|
+
const { ui: { focusTracker } } = editor;
|
|
846
|
+
|
|
847
|
+
focusTracker.isFocused = true;
|
|
848
|
+
|
|
849
|
+
await livewireStub.$internal.dispatchComponentCommit<EditorSnapshot>(id, {
|
|
850
|
+
content: {
|
|
851
|
+
main: '<p>Updated content from Livewire</p>',
|
|
852
|
+
},
|
|
853
|
+
});
|
|
854
|
+
|
|
855
|
+
expect(editor.getData()).toBe('<p>Initial content</p>');
|
|
856
|
+
|
|
857
|
+
editor.setData('<p>Content changed by user</p>');
|
|
858
|
+
|
|
859
|
+
focusTracker.isFocused = false;
|
|
860
|
+
|
|
861
|
+
expect(editor.getData()).toBe('<p>Content changed by user</p>');
|
|
862
|
+
});
|
|
797
863
|
});
|
|
798
864
|
});
|
|
799
865
|
|
|
@@ -31,23 +31,61 @@ export async function createLivewireSyncPlugin(
|
|
|
31
31
|
this.setupTypingContentPush();
|
|
32
32
|
this.setupFocusableEventPush();
|
|
33
33
|
this.setupContentServerSync();
|
|
34
|
-
this.setupLivewireEventListeners();
|
|
35
34
|
}
|
|
36
35
|
|
|
37
36
|
/**
|
|
38
37
|
* Setups the content sync from Livewire to the editor.
|
|
39
38
|
*/
|
|
40
39
|
private setupContentServerSync() {
|
|
41
|
-
|
|
40
|
+
const { editor } = this;
|
|
41
|
+
const { model, ui: { focusTracker } } = editor;
|
|
42
|
+
|
|
43
|
+
let pendingContent: Record<string, string> | null = null;
|
|
44
|
+
|
|
45
|
+
editor.on('afterCommitSynced', () => {
|
|
46
|
+
const { content } = component.canonical;
|
|
47
|
+
const values = this.getEditorRootsValues();
|
|
48
|
+
|
|
42
49
|
if (!isWireModelConnected(component.element)) {
|
|
43
50
|
return;
|
|
44
51
|
}
|
|
45
52
|
|
|
46
|
-
|
|
47
|
-
|
|
53
|
+
// If editor is focused, save the content to apply later when it blurs.
|
|
54
|
+
if (focusTracker.isFocused) {
|
|
55
|
+
if (!shallowEqual(content, values)) {
|
|
56
|
+
pendingContent = content;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
48
61
|
|
|
49
62
|
if (!shallowEqual(content, values)) {
|
|
50
|
-
|
|
63
|
+
editor.setData(content);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
Livewire.on('set-editor-content', ({ editorId, content }: SetContentPayload) => {
|
|
68
|
+
if (editorId !== component.canonical.editorId) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const currentValues = this.getEditorRootsValues();
|
|
73
|
+
|
|
74
|
+
if (!shallowEqual(currentValues, content)) {
|
|
75
|
+
editor.setData(content);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// Track user changes while focused.
|
|
80
|
+
model.document.on('change:data', () => {
|
|
81
|
+
pendingContent = null;
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// Apply pending content on blur if user didn't make changes.
|
|
85
|
+
focusTracker.on('change:isFocused', () => {
|
|
86
|
+
if (!focusTracker.isFocused && pendingContent !== null) {
|
|
87
|
+
editor.setData(pendingContent);
|
|
88
|
+
pendingContent = null;
|
|
51
89
|
}
|
|
52
90
|
});
|
|
53
91
|
}
|
|
@@ -103,23 +141,6 @@ export async function createLivewireSyncPlugin(
|
|
|
103
141
|
private getEditorRootsValues(): Record<string, string> {
|
|
104
142
|
return getEditorRootsValues(this.editor);
|
|
105
143
|
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Setups Livewire event listeners for external content updates.
|
|
109
|
-
*/
|
|
110
|
-
private setupLivewireEventListeners() {
|
|
111
|
-
Livewire.on('set-editor-content', ({ editorId, content }: SetContentPayload) => {
|
|
112
|
-
if (editorId !== component.canonical.editorId) {
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const currentValues = this.getEditorRootsValues();
|
|
117
|
-
|
|
118
|
-
if (!shallowEqual(currentValues, content)) {
|
|
119
|
-
this.editor.setData(content);
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
144
|
};
|
|
124
145
|
}
|
|
125
146
|
|