8
0
فهرست منبع

Enable undo and redo commands everywhere.

Maciej Gołaszewski 6 سال پیش
والد
کامیت
bc01a0c3ee

+ 28 - 4
packages/ckeditor5-restricted-editing/src/restrictedediting.js

@@ -21,6 +21,15 @@ export default class RestrictedEditing extends Plugin {
 		return 'RestrictedEditing';
 	}
 
+	/**
+	 * @inheritDoc
+	 */
+	constructor( editor ) {
+		super( editor );
+
+		this._alwaysEnabled = new Set( [ 'undo', 'redo' ] );
+	}
+
 	/**
 	 * @inheritDoc
 	 */
@@ -64,17 +73,32 @@ export default class RestrictedEditing extends Plugin {
 				return;
 			}
 
-			for ( const command of editor.commands.commands() ) {
-				command.clearForceDisabled( 'RestrictedMode' );
-			}
+			this._enableCommands( editor );
 		} );
 	}
 
+	_enableCommands( editor ) {
+		const commands = this._getCommandsToToggle( editor );
+
+		for ( const command of commands ) {
+			command.clearForceDisabled( 'RestrictedMode' );
+		}
+	}
+
 	_disableCommands( editor ) {
-		for ( const command of editor.commands.commands() ) {
+		const names = Array.from( editor.commands.names() ).filter( name => !this._alwaysEnabled.has( name ) );
+		const commands = names.map( name => editor.commands.get( name ) );
+
+		for ( const command of commands ) {
 			command.forceDisabled( 'RestrictedMode' );
 		}
 	}
+
+	_getCommandsToToggle( editor ) {
+		return Array.from( editor.commands.names() )
+			.filter( name => !this._alwaysEnabled.has( name ) )
+			.map( name => editor.commands.get( name ) );
+	}
 }
 
 function upcastHighlightToMarker( config ) {

+ 81 - 3
packages/ckeditor5-restricted-editing/tests/restrictedediting.js

@@ -15,6 +15,7 @@ import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtest
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import Typing from '@ckeditor/ckeditor5-typing/src/typing';
 import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
+import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
 
 describe( 'RestrictedEditing', () => {
 	let editor, element;
@@ -135,12 +136,12 @@ describe( 'RestrictedEditing', () => {
 		let model;
 
 		beforeEach( async () => {
-			editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditing ] } );
+			editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, UndoEditing, RestrictedEditing ] } );
 			model = editor.model;
 		} );
 
-		afterEach( () => {
-			return editor.destroy();
+		afterEach( async () => {
+			await editor.destroy();
 		} );
 
 		it( 'should keep markers in the view when editable region is edited', () => {
@@ -207,4 +208,81 @@ describe( 'RestrictedEditing', () => {
 			assertEqualMarkup( getModelData( model ), '<paragraph>foo bX[]ar baz</paragraph>' );
 		} );
 	} );
+
+	describe( 'commands integration', () => {
+		let model;
+
+		beforeEach( async () => {
+			editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, UndoEditing, RestrictedEditing ] } );
+			model = editor.model;
+		} );
+
+		afterEach( async () => {
+			await editor.destroy();
+		} );
+
+		describe( 'undo command', () => {
+			it( 'should be enabled outside exception marker', () => {
+				setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
+
+				expect( editor.commands.get( 'undo' ).isEnabled ).to.be.true;
+			} );
+
+			it( 'should be enabled inside exception marker', () => {
+				setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
+				const 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
+					} );
+
+					writer.setSelection( firstParagraph, 5 );
+				} );
+
+				expect( editor.commands.get( 'undo' ).isEnabled ).to.be.true;
+			} );
+		} );
+
+		describe( 'redo command', () => {
+			it( 'should be enabled outside exception marker', () => {
+				setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
+
+				model.change( writer => {
+					model.insertContent( writer.createText( 'R', model.document.selection.getAttributes() ) );
+				} );
+				editor.execute( 'undo' );
+
+				expect( editor.commands.get( 'redo' ).isEnabled ).to.be.true;
+			} );
+
+			it( 'should be enabled inside exception marker', () => {
+				setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
+				const firstParagraph = model.document.getRoot().getChild( 0 );
+
+				model.change( writer => {
+					model.insertContent( writer.createText( 'R', model.document.selection.getAttributes() ) );
+				} );
+
+				model.change( writer => {
+					writer.addMarker( 'restricted-editing-exception:1', {
+						range: writer.createRange(
+							writer.createPositionAt( firstParagraph, 4 ),
+							writer.createPositionAt( firstParagraph, 7 ) ),
+						usingOperation: true,
+						affectsData: true
+					} );
+
+					writer.setSelection( firstParagraph, 5 );
+				} );
+				editor.execute( 'undo' );
+
+				expect( editor.commands.get( 'redo' ).isEnabled ).to.be.true;
+			} );
+		} );
+	} );
 } );