-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
autosave.js
375 lines (328 loc) · 10.4 KB
/
autosave.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module autosave/autosave
*/
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import PendingActions from '@ckeditor/ckeditor5-core/src/pendingactions';
import DomEmitterMixin from '@ckeditor/ckeditor5-utils/src/dom/emittermixin';
import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
import mix from '@ckeditor/ckeditor5-utils/src/mix';
import { debounce } from 'lodash-es';
/* globals window */
/**
* The {@link module:autosave/autosave~Autosave} plugin allows you to automatically save the data (e.g. send it to the server)
* when needed (when the user changed the content).
*
* It listens to the {@link module:engine/model/document~Document#event:change:data `editor.model.document#change:data`}
* and `window#beforeunload` events and calls the
* {@link module:autosave/autosave~AutosaveAdapter#save `config.autosave.save()`} function.
*
* ClassicEditor
* .create( document.querySelector( '#editor' ), {
* plugins: [ ArticlePluginSet, Autosave ],
* toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote', 'undo', 'redo' ],
* image: {
* toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ],
* },
* autosave: {
* save( editor ) {
* // The saveData() function must return a promise
* // which should be resolved when the data is successfully saved.
* return saveData( editor.getData() );
* }
* }
* } );
*
* Read more about this feature in the {@glink builds/guides/integration/saving-data#autosave-feature Autosave feature}
* section of the {@glink builds/guides/integration/saving-data Saving and getting data}.
*
* @extends module:core/plugin~Plugin
*/
export default class Autosave extends Plugin {
/**
* @inheritDoc
*/
static get pluginName() {
return 'Autosave';
}
/**
* @inheritDoc
*/
static get requires() {
return [ PendingActions ];
}
/**
* @inheritDoc
*/
constructor( editor ) {
super( editor );
const config = editor.config.get( 'autosave' ) || {};
// A minimum amount of time that needs to pass after the last action.
// After that time the provided save callbacks are being called.
const waitingTime = config.waitingTime || 1000;
/**
* The adapter is an object with a `save()` method. That method will be called whenever
* the data changes. It might be called some time after the change,
* since the event is throttled for performance reasons.
*
* @member {module:autosave/autosave~AutosaveAdapter} #adapter
*/
/**
* The state of this plugin.
*
* The plugin can be in the following states:
*
* * synchronized – When all changes are saved.
* * waiting – When the plugin is waiting for other changes before calling `adapter#save()` and `config.autosave.save()`.
* * saving – When the provided save method is called and the plugin waits for the response.
* * error &ndash When the provided save method will throw an error. This state immediately changes to the `saving` state and
* the save method will be called again in the short period of time.
*
* @member {'synchronized'|'waiting'|'saving'} #state
*/
this.set( 'state', 'synchronized' );
/**
* Debounced save method. The `save()` method is called the specified `waitingTime` after `debouncedSave()` is called,
* unless a new action happens in the meantime.
*
* @private
* @type {Function}
*/
this._debouncedSave = debounce( this._save.bind( this ), waitingTime );
/**
* The last document version.
*
* @private
* @type {Number}
*/
this._lastDocumentVersion = editor.model.document.version;
/**
* DOM emitter.
*
* @private
* @type {DomEmitterMixin}
*/
this._domEmitter = Object.create( DomEmitterMixin );
/**
* The configuration of this plugins.
*
* @private
* @type {Object}
*/
this._config = config;
/**
* An action that will be added to pending action manager for actions happening in that plugin.
*
* @private
* @member {Object} #_action
*/
/**
* Editor's pending actions manager.
*
* @private
* @member {@module:core/pendingactions~PendingActions} #_pendingActions
*/
}
/**
* @inheritDoc
*/
init() {
const editor = this.editor;
const doc = editor.model.document;
const t = editor.t;
this._pendingActions = editor.plugins.get( PendingActions );
this.listenTo( doc, 'change:data', () => {
if ( !this._saveCallbacks.length ) {
return;
}
if ( this.state == 'synchronized' ) {
this._action = this._pendingActions.add( t( 'Saving changes' ) );
this.state = 'waiting';
this._debouncedSave();
}
else if ( this.state == 'waiting' ) {
this._debouncedSave();
}
// If the plugin is in `saving` state, it will change its state later basing on the `document.version`.
// If the `document.version` will be higher than stored `#_lastDocumentVersion`, then it means, that some `change:data`
// event has fired in the meantime.
} );
// Flush on the editor's destroy listener with the highest priority to ensure that
// `editor.getData()` will be called before plugins are destroyed.
this.listenTo( editor, 'destroy', () => this._flush(), { priority: 'highest' } );
// It's not possible to easy test it because karma uses `beforeunload` event
// to warn before full page reload and this event cannot be dispatched manually.
/* istanbul ignore next */
this._domEmitter.listenTo( window, 'beforeunload', ( evtInfo, domEvt ) => {
if ( this._pendingActions.hasAny ) {
domEvt.returnValue = this._pendingActions.first.message;
}
} );
}
/**
* @inheritDoc
*/
destroy() {
// There's no need for canceling or flushing the throttled save, as
// it's done on the editor's destroy event with the highest priority.
this._domEmitter.stopListening();
super.destroy();
}
/**
* Invokes the remaining `_save()` method call.
*
* @protected
*/
_flush() {
this._debouncedSave.flush();
}
/**
* If the adapter is set and a new document version exists,
* the `_save()` method creates a pending action and calls the `adapter.save()` method.
* It waits for the result and then removes the created pending action.
*
* @private
*/
_save() {
const version = this.editor.model.document.version;
// Change may not produce an operation, so the document's version
// can be the same after that change.
if (
version < this._lastDocumentVersion ||
this.editor.state === 'initializing'
) {
this._debouncedSave.cancel();
return;
}
this._lastDocumentVersion = version;
this.state = 'saving';
// Wait one promise cycle to be sure that save callbacks are not called
// inside a conversion or when the editor's state changes.
Promise.resolve()
.then( () => Promise.all(
this._saveCallbacks.map( cb => cb( this.editor ) )
) )
// In case of an error re-try the save later and throw the original error.
// Being in the `saving` state ensures that the debounced save action
// won't be delayed further by the `change:data` event listener.
.catch( err => {
this.state = 'error';
// Change immediately to the `saving` state so the `change:state` event will be fired.
this.state = 'saving';
this._debouncedSave();
throw err;
} )
.then( () => {
if ( this.editor.model.document.version > this._lastDocumentVersion ) {
this.state = 'waiting';
this._debouncedSave();
} else {
this.state = 'synchronized';
this._pendingActions.remove( this._action );
this._action = null;
}
} );
}
/**
* Saves callbacks.
*
* @private
* @type {Array.<Function>}
*/
get _saveCallbacks() {
const saveCallbacks = [];
if ( this.adapter && this.adapter.save ) {
saveCallbacks.push( this.adapter.save );
}
if ( this._config.save ) {
saveCallbacks.push( this._config.save );
}
return saveCallbacks;
}
}
mix( Autosave, ObservableMixin );
/**
* An interface that requires the `save()` method.
*
* Used by {@link module:autosave/autosave~Autosave#adapter}.
*
* @interface module:autosave/autosave~AutosaveAdapter
*/
/**
* The method that will be called when the data changes. It should return a promise (e.g. in case of saving content to the database),
* so the autosave plugin will wait for that action before removing it from pending actions.
*
* @method #save
* @param {module:core/editor/editor~Editor} editor The editor instance.
* @returns {Promise.<*>}
*/
/**
* The configuration of the {@link module:autosave/autosave~Autosave autosave feature}.
*
* Read more in {@link module:autosave/autosave~AutosaveConfig}.
*
* @member {module:autosave/autosave~AutosaveConfig} module:core/editor/editorconfig~EditorConfig#autosave
*/
/**
* The configuration of the {@link module:autosave/autosave~Autosave autosave feature}.
*
* ClassicEditor
* .create( editorElement, {
* autosave: {
* save( editor ) {
* // The saveData() function must return a promise
* // which should be resolved when the data is successfully saved.
* return saveData( editor.getData() );
* }
* }
* } );
* .then( ... )
* .catch( ... );
*
* See {@link module:core/editor/editorconfig~EditorConfig all editor configuration options}.
*
* See also the demo of the {@glink builds/guides/integration/saving-data#autosave-feature autosave feature}.
*
* @interface AutosaveConfig
*/
/**
* The callback to be executed when the data needs to be saved.
*
* This function must return a promise which should be resolved when the data is successfully saved.
*
* ClassicEditor
* .create( editorElement, {
* autosave: {
* save( editor ) {
* return saveData( editor.getData() );
* }
* }
* } );
* .then( ... )
* .catch( ... );
*
* @method module:autosave/autosave~AutosaveConfig#save
* @param {module:core/editor/editor~Editor} editor The editor instance.
* @returns {Promise.<*>}
*/
/**
* The minimum amount of time that needs to pass after the last action to call the provided callback.
* By default it is 1000 ms.
*
* ClassicEditor
* .create( editorElement, {
* autosave: {
* save( editor ) {
* return saveData( editor.getData() );
* },
* waitingTime: 2000
* }
* } );
* .then( ... )
* .catch( ... );
*
* @member {Number} module:autosave/autosave~AutosaveConfig#waitingTime
*/