浏览代码

Added isReadOnly state to the EditingController.

Oskar Wróbel 8 年之前
父节点
当前提交
d01834bfd0

+ 17 - 0
packages/ckeditor5-engine/src/controller/editingcontroller.js

@@ -64,6 +64,20 @@ export default class EditingController {
 		this.mapper = new Mapper();
 
 		/**
+		 * Defines whether controller is in read-only mode.
+		 *
+		 * When controller is read-ony {module:engine/view/document~Document view document} and all view roots
+		 * are read-only as well.
+		 *
+		 * @observable
+		 * @member {Boolean} #isReadOnly
+		 */
+		this.set( 'isReadOnly', false );
+
+		// When controller is read-only the view document is read-only as well.
+		this.view.bind( 'isReadOnly' ).to( this );
+
+		/**
 		 * Model to view conversion dispatcher, which converts changes from the model to
 		 * {@link #view editing view}.
 		 *
@@ -143,6 +157,9 @@ export default class EditingController {
 
 		this.mapper.bindElements( modelRoot, viewRoot );
 
+		// When controller is read-only the all view roots are read-only as well.
+		viewRoot.bind( 'isReadOnly' ).to( this );
+
 		return viewRoot;
 	}
 

+ 35 - 5
packages/ckeditor5-engine/tests/controller/editingcontroller.js

@@ -29,21 +29,28 @@ import { getData as getViewData } from '../../src/dev-utils/view';
 
 describe( 'EditingController', () => {
 	describe( 'constructor()', () => {
-		it( 'should create controller with properties', () => {
-			const model = new ModelDocument();
-			const editing = new EditingController( model );
+		let model, editing;
+
+		beforeEach( () => {
+			model = new ModelDocument();
+			editing = new EditingController( model );
+		} );
 
+		afterEach( () => {
+			editing.destroy();
+		} );
+
+		it( 'should create controller with properties', () => {
 			expect( editing ).to.have.property( 'model' ).that.equals( model );
 			expect( editing ).to.have.property( 'view' ).that.is.instanceof( ViewDocument );
 			expect( editing ).to.have.property( 'mapper' ).that.is.instanceof( Mapper );
 			expect( editing ).to.have.property( 'modelToView' ).that.is.instanceof( ModelConversionDispatcher );
+			expect( editing ).to.have.property( 'isReadOnly' ).that.is.false;
 
 			editing.destroy();
 		} );
 
 		it( 'should be observable', () => {
-			const model = new ModelDocument();
-			const editing = new EditingController( model );
 			const spy = sinon.spy();
 
 			editing.on( 'change:foo', spy );
@@ -51,6 +58,16 @@ describe( 'EditingController', () => {
 
 			sinon.assert.calledOnce( spy );
 		} );
+
+		it( 'should bind view#isReadOnly to controller#isReadOnly', () => {
+			editing.isReadOnly = false;
+
+			expect( editing.view.isReadOnly ).to.false;
+
+			editing.isReadOnly = true;
+
+			expect( editing.view.isReadOnly ).to.true;
+		} );
 	} );
 
 	describe( 'createRoot()', () => {
@@ -117,6 +134,19 @@ describe( 'EditingController', () => {
 			expect( editing.mapper.toModelElement( viewRoot ) ).to.equal( modelRoot );
 			expect( editing.mapper.toViewElement( modelRoot ) ).to.equal( viewRoot );
 		} );
+
+		it( 'should bind root#isReadOnly to controller#isReadOnly', () => {
+			const domRoot = createElement( document, 'div', null, createElement( document, 'p' ) );
+			const viewRoot = editing.createRoot( domRoot );
+
+			editing.isReadOnly = false;
+
+			expect( viewRoot.isReadOnly ).to.false;
+
+			editing.isReadOnly = true;
+
+			expect( viewRoot.isReadOnly ).to.true;
+		} );
 	} );
 
 	describe( 'conversion', () => {