Explorar el Código

Merge pull request #15 from ckeditor/t/6041

Feature: Add `enableCommand()` method to `RestrictedEditingModeEditing`. Closes ckeditor/ckeditor5#6041. Closes ckeditor/ckeditor5#6011.
Maciej hace 6 años
padre
commit
a03ecd0d45

+ 3 - 0
packages/ckeditor5-restricted-editing/src/restrictededitingmode.js

@@ -82,6 +82,9 @@ export default class RestrictedEditingMode extends Plugin {
  *			allowedCommands: [ 'bold', 'italic', 'link', 'unlink' ]
  *		};
  *
+ * To make a command always enabled (also outside non-restricted areas) use
+ * {@link module:restricted-editing/restrictededitingmodeediting~RestrictedEditingModeEditing#enableCommand} method.
+ *
  * @member {Array.<String>} module:restricted-editing/restrictededitingmode~RestrictedEditingModeConfig#allowedCommands
  */
 

+ 21 - 2
packages/ckeditor5-restricted-editing/src/restrictededitingmodeediting.js

@@ -17,6 +17,8 @@ import {
 } from './restrictededitingmode/converters';
 import { getMarkerAtPosition, isSelectionInMarker } from './restrictededitingmode/utils';
 
+const COMMAND_FORCE_DISABLE_ID = 'RestrictedEditingMode';
+
 /**
  * The Restricted Editing Mode editing feature.
  *
@@ -93,6 +95,23 @@ export default class RestrictedEditingModeEditing extends Plugin {
 		} );
 	}
 
+	/**
+	 * Makes the given command always enabled in the restricted editing mode (regardless
+	 * of selection location).
+	 *
+	 * To enable some commands in non-restricted areas of the content use
+	 * {@link module:restricted-editing/restrictededitingmode~RestrictedEditingModeConfig#allowedCommands} configuration option.
+	 *
+	 * @param {String} commandName Name of the command to enable.
+	 */
+	enableCommand( commandName ) {
+		const command = this.editor.commands.get( commandName );
+
+		command.clearForceDisabled( COMMAND_FORCE_DISABLE_ID );
+
+		this._alwaysEnabled.add( commandName );
+	}
+
 	/**
 	 * Setups restricted mode editing conversion:
 	 *
@@ -265,7 +284,7 @@ export default class RestrictedEditingModeEditing extends Plugin {
 			.map( name => editor.commands.get( name ) );
 
 		for ( const command of commands ) {
-			command.clearForceDisabled( 'RestrictedEditingMode' );
+			command.clearForceDisabled( COMMAND_FORCE_DISABLE_ID );
 		}
 	}
 
@@ -280,7 +299,7 @@ export default class RestrictedEditingModeEditing extends Plugin {
 			.map( name => editor.commands.get( name ) );
 
 		for ( const command of commands ) {
-			command.forceDisabled( 'RestrictedEditingMode' );
+			command.forceDisabled( COMMAND_FORCE_DISABLE_ID );
 		}
 	}
 

+ 31 - 0
packages/ckeditor5-restricted-editing/tests/restrictededitingmodeediting.js

@@ -22,6 +22,7 @@ import RestrictedEditingModeNavigationCommand from '../src/restrictededitingmode
 import ItalicEditing from '@ckeditor/ckeditor5-basic-styles/src/italic/italicediting';
 import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting';
 import TableEditing from '@ckeditor/ckeditor5-table/src/tableediting';
+import Command from '@ckeditor/ckeditor5-core/src/command';
 
 describe( 'RestrictedEditingModeEditing', () => {
 	let editor, model;
@@ -63,6 +64,36 @@ describe( 'RestrictedEditingModeEditing', () => {
 		} );
 	} );
 
+	describe( 'enableCommand()', () => {
+		let plugin, command;
+
+		class FakeCommand extends Command {
+			refresh() {
+				this.isEnabled = true;
+			}
+		}
+
+		beforeEach( async () => {
+			editor = await VirtualTestEditor.create( { plugins: [ Paragraph, RestrictedEditingModeEditing ] } );
+			model = editor.model;
+
+			plugin = editor.plugins.get( RestrictedEditingModeEditing );
+			command = new FakeCommand( editor );
+			editor.commands.add( 'fakeCommand', command );
+
+			setModelData( editor.model, '<paragraph>[]foo bar baz qux</paragraph>' );
+			addExceptionMarker( 4, 7, model.document.getRoot().getChild( 0 ) );
+		} );
+
+		it( 'should enable the command globally', () => {
+			expect( command.isEnabled ).to.be.false;
+
+			plugin.enableCommand( 'fakeCommand' );
+
+			expect( command.isEnabled ).to.be.true;
+		} );
+	} );
+
 	describe( 'conversion', () => {
 		beforeEach( async () => {
 			editor = await VirtualTestEditor.create( { plugins: [ Paragraph, TableEditing, RestrictedEditingModeEditing ] } );