8
0
Quellcode durchsuchen

Added support for multi-root editors.

Maciej Bukowski vor 6 Jahren
Ursprung
Commit
7235b7d474

+ 21 - 4
packages/ckeditor5-watchdog/src/watchdog.js

@@ -137,10 +137,10 @@ export default class Watchdog {
 		 */
 
 		/**
-		 * The latest saved editor data.
+		 * The latest saved editor data represented as a root name -> root data object.
 		 *
 		 * @private
-		 * @member {String} #_data
+		 * @member {Object.<String,String>} #_data
 		 */
 
 		/**
@@ -257,7 +257,8 @@ export default class Watchdog {
 				this.listenTo( editor.model.document, 'change:data', this._throttledSave );
 
 				this._lastDocumentVersion = editor.model.document.version;
-				this._data = editor.data.get();
+
+				this._data = this._getData();
 				this.state = 'ready';
 			} );
 	}
@@ -311,7 +312,7 @@ export default class Watchdog {
 		}
 
 		try {
-			this._data = this._editor.data.get();
+			this._data = this._getData();
 			this._lastDocumentVersion = version;
 		} catch ( err ) {
 			console.error(
@@ -323,6 +324,22 @@ export default class Watchdog {
 	}
 
 	/**
+	 * Returns the editor data.
+	 *
+	 * @private
+	 * @returns {Object<String,String>}
+	 */
+	_getData() {
+		const data = {};
+
+		for ( const rootName of this._editor.model.document.getRootNames() ) {
+			data[ rootName ] = this._editor.data.get( { rootName } );
+		}
+
+		return data;
+	}
+
+	/**
 	 * Checks if the error comes from the editor that is handled by the watchdog (by checking the error context) and
 	 * restarts the editor. It reacts to {@link module:utils/ckeditorerror~CKEditorError `CKEditorError` errors} only.
 	 *

+ 57 - 0
packages/ckeditor5-watchdog/tests/watchdog.js

@@ -6,10 +6,12 @@
 /* globals setTimeout, window, console, document */
 
 import Watchdog from '../src/watchdog';
+import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
 import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
+import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
 
 describe( 'Watchdog', () => {
 	let element;
@@ -989,6 +991,61 @@ describe( 'Watchdog', () => {
 			} );
 		} );
 	} );
+
+	it( 'should support multi-root editors', () => {
+		class MultiRootEditor extends Editor {
+			constructor( sourceElements, config ) {
+				super( config );
+
+				this.data.processor = new HtmlDataProcessor();
+
+				// Create root and UIView element for each editable container.
+				for ( const rootName of Object.keys( sourceElements ) ) {
+					this.model.document.createRoot( '$root', rootName );
+				}
+			}
+
+			static async create( sourceElements, config ) {
+				const editor = new this( sourceElements, config );
+
+				await editor.initPlugins();
+
+				await editor.data.init( config.initialData );
+
+				editor.fire( 'ready' );
+
+				return editor;
+			}
+		}
+
+		const watchdog = Watchdog.for( MultiRootEditor );
+
+		// sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
+		const originalErrorHandler = window.onerror;
+		window.onerror = undefined;
+
+		return watchdog
+			.create( {
+				header: element
+			}, {
+				initialData: {
+					header: '<p>Foo</p>'
+				},
+				plugins: [ Paragraph ]
+			} )
+			.then( () => {
+				expect( watchdog.editor.data.get( { rootName: 'header' } ) ).to.equal( '<p>Foo</p>' );
+
+				setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
+
+				return new Promise( res => {
+					window.onerror = originalErrorHandler;
+					expect( watchdog.editor.data.get( { rootName: 'header' } ) ).to.equal( '<p>Foo</p>' );
+
+					res();
+				} );
+			} );
+	} );
 } );
 
 function throwCKEditorError( name, context ) {