Browse Source

Make command toggle on data change also.

Maciej Gołaszewski 6 năm trước cách đây
mục cha
commit
973a7af2d6

+ 1 - 0
packages/ckeditor5-restricted-editing/src/restrictededitingediting.js

@@ -66,6 +66,7 @@ export default class RestrictedEditingEditing extends Plugin {
 		const selection = editor.model.document.selection;
 
 		this.listenTo( selection, 'change', this._checkCommands.bind( this ) );
+		this.listenTo( editor.model.document, 'change:data', this._checkCommands.bind( this ) );
 	}
 
 	_checkCommands() {

+ 41 - 1
packages/ckeditor5-restricted-editing/tests/restrictededitingediting.js

@@ -13,6 +13,7 @@ import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils
 import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
 import RestrictedEditingEditing from './../src/restrictededitingediting';
+import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
 
 describe( 'RestrictedEditingEditing', () => {
 	let editor;
@@ -201,7 +202,7 @@ describe( 'RestrictedEditingEditing', () => {
 		} );
 	} );
 
-	describe( 'commands integration', () => {
+	describe( 'commands behavior', () => {
 		let model, firstParagraph;
 
 		beforeEach( async () => {
@@ -676,6 +677,45 @@ describe( 'RestrictedEditingEditing', () => {
 		} );
 	} );
 
+	describe( 'commands integration', () => {
+		let model, firstParagraph;
+
+		beforeEach( async () => {
+			editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, UndoEditing, RestrictedEditingEditing ] } );
+			model = editor.model;
+
+			setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
+			firstParagraph = model.document.getRoot().getChild( 0 );
+
+			model.change( writer => {
+				writer.addMarker( 'restricted-editing-exception:1', {
+					range: writer.createRange(
+						writer.createPositionAt( firstParagraph, 4 ),
+						writer.createPositionAt( firstParagraph, 7 ) ),
+					usingOperation: true,
+					affectsData: true
+				} );
+			} );
+		} );
+
+		afterEach( async () => {
+			await editor.destroy();
+		} );
+
+		describe( 'delete + undo', () => {
+			it( 'should be enabled after data change (no selection change event on undo)', () => {
+				model.change( writer => {
+					writer.setSelection( firstParagraph, 7 );
+				} );
+
+				editor.execute( 'delete' );
+				editor.execute( 'undo' );
+
+				expect( editor.commands.get( 'delete' ).isEnabled ).to.be.true;
+			} );
+		} );
+	} );
+
 	class FakeCommand extends Command {
 		refresh() {
 			this.isEnabled = true;