8
0
فهرست منبع

Bound Command#isEnabled with Editor#readOnly.

Oskar Wróbel 8 سال پیش
والد
کامیت
02f4e960b6
2فایلهای تغییر یافته به همراه39 افزوده شده و 0 حذف شده
  1. 19 0
      packages/ckeditor5-core/src/command.js
  2. 20 0
      packages/ckeditor5-core/tests/command.js

+ 19 - 0
packages/ckeditor5-core/src/command.js

@@ -20,6 +20,8 @@ import mix from '@ckeditor/ckeditor5-utils/src/mix';
  * Instances of registered commands can be retrieved from {@link module:core/editor/editor~Editor#commands}.
  * The easiest way to execute a command is through {@link module:core/editor/editor~Editor#execute}.
  *
+ * By default commands are disabled when editor is in {@link module:core/editor/editor~Editor#readOnly read-only} mode.
+ *
  * @mixes module:utils/observablemixin~ObservableMixin
  */
 export default class Command {
@@ -73,6 +75,23 @@ export default class Command {
 				evt.stop();
 			}
 		}, { priority: 'high' } );
+
+		// By default commands are disabled when editor switches to read-only mode.
+		this.listenTo( editor, 'change:readOnly', ( evt, name, value ) => {
+			if ( value ) {
+				this.on( 'change:isEnabled', forceDisable, { priority: 'high' } );
+				this.isEnabled = false;
+			} else {
+				this.off( 'change:isEnabled', forceDisable );
+				this.refresh();
+			}
+		} );
+
+		// Forces command#isEnabled value to be false.
+		function forceDisable( evt ) {
+			this.isEnabled = false;
+			evt.stop();
+		}
 	}
 
 	/**

+ 20 - 0
packages/ckeditor5-core/tests/command.js

@@ -65,6 +65,26 @@ describe( 'Command', () => {
 
 			expect( spy.calledOnce ).to.be.true;
 		} );
+
+		it( 'is always falsy when the editor is in read-only mode', () => {
+			editor.readOnly = false;
+			command.isEnabled = true;
+
+			editor.readOnly = true;
+
+			// Is false.
+			expect( command.isEnabled ).to.false;
+
+			command.refresh();
+
+			// Still false.
+			expect( command.isEnabled ).to.false;
+
+			editor.readOnly = false;
+
+			// And is back to true.
+			expect( command.isEnabled ).to.true;
+		} );
 	} );
 
 	describe( 'execute()', () => {