瀏覽代碼

Add enableCommand method to RestrictedEditingModeEditing.

Maciej Gołaszewski 6 年之前
父節點
當前提交
cef7f78107

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

@@ -16,6 +16,9 @@ import {
 	upcastHighlightToMarker
 } from './restrictededitingmode/converters';
 import { getMarkerAtPosition, isSelectionInMarker } from './restrictededitingmode/utils';
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
+
+const COMMAND_FORCE_DISABLE_ID = 'RestrictedEditingMode';
 
 /**
  * The Restricted Editing Mode editing feature.
@@ -93,6 +96,30 @@ export default class RestrictedEditingModeEditing extends Plugin {
 	}
 
 	/**
+	 * Enables command with given `commandName` in restricted editing mode.
+	 *
+	 * @param {String} commandName Name of the command to enable.
+	 */
+	enableCommand( commandName ) {
+		const command = this.editor.commands.get( commandName );
+
+		if ( !command ) {
+			/**
+			 * Trying to enable command that does not exist.
+			 *
+			 * @error restricted-editing-command-not-found
+			 */
+			throw new CKEditorError( 'restricted-editing-command-not-found: Trying to enable command that does not exist.', this );
+		}
+
+		if ( command ) {
+			command.clearForceDisabled( COMMAND_FORCE_DISABLE_ID );
+		}
+
+		this._alwaysEnabled.add( commandName );
+	}
+
+	/**
 	 * Setups restricted mode editing conversion:
 	 *
 	 * * ucpast & downcast converters
@@ -264,7 +291,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 );
 		}
 	}
 
@@ -279,7 +306,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 );
 		}
 	}
 

+ 42 - 1
packages/ckeditor5-restricted-editing/tests/restrictededitingmodeediting.js

@@ -9,7 +9,7 @@ import { getData as getModelData, setData as setModelData } from '@ckeditor/cked
 import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
 import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
 import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
-import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
+import { assertEqualMarkup, expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
 import StrikethroughEditing from '@ckeditor/ckeditor5-basic-styles/src/strikethrough/strikethroughediting';
@@ -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,46 @@ 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 throw if non-existing command is being enabled', () => {
+			expectToThrowCKEditorError(
+				() => {
+					plugin.enableCommand( 'foo' );
+				},
+				/^restricted-editing-command-not-found/,
+				editor
+			);
+		} );
+
+		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 ] } );