ckeditor5-livewire 1.3.0 → 1.3.1

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.
@@ -87,8 +87,14 @@ export class EditableComponentHook extends ClassHook<Snapshot> {
87
87
  this.$wire.set('content', html);
88
88
  };
89
89
 
90
- editor.model.document.on('change:data', debounce(saveDebounceMs, sync));
90
+ const debouncedSync = debounce(saveDebounceMs, sync);
91
+
92
+ editor.model.document.on('change:data', debouncedSync);
91
93
  sync();
94
+
95
+ this.onBeforeDestroy(() => {
96
+ editor.model.document.off('change:data', debouncedSync);
97
+ });
92
98
  }
93
99
 
94
100
  /**
@@ -97,20 +103,29 @@ export class EditableComponentHook extends ClassHook<Snapshot> {
97
103
  */
98
104
  private setupPendingReceivedContentHandlers(editor: MultiRootEditor): void {
99
105
  const { ui, model } = editor;
106
+ const { focusTracker } = ui;
100
107
  const { rootName } = this.canonical;
101
108
 
102
- model.document.on('change:data', () => {
109
+ const onDataChange = () => {
103
110
  this.pendingContent = null;
104
- });
111
+ };
105
112
 
106
- ui.focusTracker.on('change:isFocused', () => {
107
- if (!ui.focusTracker.isFocused && this.pendingContent !== null) {
113
+ const onFocusChange = () => {
114
+ if (!focusTracker.isFocused && this.pendingContent !== null) {
108
115
  editor.setData({
109
116
  [rootName]: this.pendingContent,
110
117
  });
111
118
 
112
119
  this.pendingContent = null;
113
120
  }
121
+ };
122
+
123
+ model.document.on('change:data', onDataChange);
124
+ focusTracker.on('change:isFocused', onFocusChange);
125
+
126
+ this.onBeforeDestroy(() => {
127
+ model.document.off('change:data', onDataChange);
128
+ focusTracker.off('change:isFocused', onFocusChange);
114
129
  });
115
130
  }
116
131
 
package/src/hooks/hook.ts CHANGED
@@ -10,6 +10,11 @@ export abstract class ClassHook<T extends object = Record<string, unknown>> {
10
10
  */
11
11
  state: ClassHookState = 'mounting';
12
12
 
13
+ /**
14
+ * Callbacks to run before the hook is destroyed.
15
+ */
16
+ private _beforeDestroyCallbacks: Array<() => void> = [];
17
+
13
18
  constructor(
14
19
  /**
15
20
  * The Livewire component instance associated with this hook.
@@ -17,6 +22,14 @@ export abstract class ClassHook<T extends object = Record<string, unknown>> {
17
22
  protected livewireComponent: LivewireComponent,
18
23
  ) {}
19
24
 
25
+ /**
26
+ * Registers a callback to be called before the hook is destroyed.
27
+ * Callbacks are called in LIFO order (last registered, first called).
28
+ */
29
+ onBeforeDestroy(callback: () => void): void {
30
+ this._beforeDestroyCallbacks.push(callback);
31
+ }
32
+
20
33
  /**
21
34
  * The canonical snapshot of the Livewire component.
22
35
  */
@@ -59,6 +72,18 @@ export abstract class ClassHook<T extends object = Record<string, unknown>> {
59
72
  * Called when the component is updated by Livewire.
60
73
  */
61
74
  afterCommitSynced?(): CanBePromise<void>;
75
+
76
+ /**
77
+ * Runs all registered before-destroy callbacks and clears the list.
78
+ * Called internally by makeHook before destroyed().
79
+ */
80
+ _runBeforeDestroyCallbacks(): void {
81
+ for (const cb of this._beforeDestroyCallbacks.reverse()) {
82
+ cb();
83
+ }
84
+
85
+ this._beforeDestroyCallbacks = [];
86
+ }
62
87
  }
63
88
 
64
89
  /**
@@ -89,6 +114,7 @@ export function registerLivewireComponentHook(name: string, Hook: { new(componen
89
114
  cleanup(async () => {
90
115
  instance.state = 'destroying';
91
116
 
117
+ instance._runBeforeDestroyCallbacks();
92
118
  await instance.destroyed();
93
119
 
94
120
  instance.state = 'destroyed';