浏览代码

Added browser prompt on beforeunload event in a case of pending actions.

Oskar Wróbel 7 年之前
父节点
当前提交
feda49e63c
共有 1 个文件被更改,包括 32 次插入0 次删除
  1. 32 0
      packages/ckeditor5-core/src/pendingactions.js

+ 32 - 0
packages/ckeditor5-core/src/pendingactions.js

@@ -7,8 +7,11 @@
  * @module core/pendingactions
  */
 
+/* global window */
+
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
+import DOMEmitterMixin from '@ckeditor/ckeditor5-utils/src/dom/emittermixin';
 import Collection from '@ckeditor/ckeditor5-utils/src/collection';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
@@ -26,6 +29,21 @@ export default class PendingActions extends Plugin {
 	/**
 	 * @inheritDoc
 	 */
+	constructor( editor ) {
+		super( editor );
+
+		/**
+		 * DOM Emitter.
+		 *
+		 * @private
+		 * @type {module:utils/dom/emittermixin~EmitterMixin}
+		 */
+		this._domEmitter = Object.create( DOMEmitterMixin );
+	}
+
+	/**
+	 * @inheritDoc
+	 */
 	init() {
 		/**
 		 * Defines whether there is any registered pending action or not.
@@ -43,6 +61,15 @@ export default class PendingActions extends Plugin {
 		 * @type {module:utils/collection~Collection}
 		 */
 		this._actions = new Collection( { idProperty: '_id' } );
+
+		// 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.isPending ) {
+				domEvt.returnValue = this._actions.get( 0 ).message;
+			}
+		} );
 	}
 
 	/**
@@ -82,4 +109,9 @@ export default class PendingActions extends Plugin {
 	[ Symbol.iterator ]() {
 		return this._actions[ Symbol.iterator ]();
 	}
+
+	destroy() {
+		super.destroy();
+		this._domEmitter.stopListening();
+	}
 }