Browse Source

Prevented from changing view document during the render phase.

Oskar Wróbel 6 years ago
parent
commit
e04e794404

+ 13 - 5
packages/ckeditor5-ui/src/editableui/editableuiview.js

@@ -121,11 +121,19 @@ export default class EditableUIView extends View {
 	_updateIsFocusedClasses() {
 		const editingView = this._editingView;
 
-		editingView.change( writer => {
-			const viewRoot = editingView.document.getRoot( this.name );
+		if ( editingView.isRenderingInProgress ) {
+			editingView.once( 'change:isRenderingInProgress', () => update( this ) );
+		} else {
+			update( this );
+		}
 
-			writer.addClass( this.isFocused ? 'ck-focused' : 'ck-blurred', viewRoot );
-			writer.removeClass( this.isFocused ? 'ck-blurred' : 'ck-focused', viewRoot );
-		} );
+		function update( view ) {
+			editingView.change( writer => {
+				const viewRoot = editingView.document.getRoot( view.name );
+
+				writer.addClass( view.isFocused ? 'ck-focused' : 'ck-blurred', viewRoot );
+				writer.removeClass( view.isFocused ? 'ck-blurred' : 'ck-focused', viewRoot );
+			} );
+		}
 	}
 }

+ 19 - 0
packages/ckeditor5-ui/tests/editableui/editableuiview.js

@@ -78,6 +78,25 @@ describe( 'EditableUIView', () => {
 				expect( editingViewRoot.hasClass( 'ck-focused' ) ).to.be.false;
 				expect( editingViewRoot.hasClass( 'ck-blurred' ) ).to.be.true;
 			} );
+
+			// https://github.com/ckeditor/ckeditor5/issues/1530.
+			it( 'should work when update is handled during the rendering phase', () => {
+				view.isFocused = true;
+				editingView.isRenderingInProgress = true;
+
+				expect( editingViewRoot.hasClass( 'ck-focused' ) ).to.be.true;
+				expect( editingViewRoot.hasClass( 'ck-blurred' ) ).to.be.false;
+
+				view.isFocused = false;
+
+				expect( editingViewRoot.hasClass( 'ck-focused' ) ).to.be.true;
+				expect( editingViewRoot.hasClass( 'ck-blurred' ) ).to.be.false;
+
+				editingView.isRenderingInProgress = false;
+
+				expect( editingViewRoot.hasClass( 'ck-focused' ) ).to.be.false;
+				expect( editingViewRoot.hasClass( 'ck-blurred' ) ).to.be.true;
+			} );
 		} );
 	} );