Jelajahi Sumber

Other: Remove RemoveHighlightCommand.

Maciej Gołaszewski 8 tahun lalu
induk
melakukan
b78ef6e183

+ 19 - 19
packages/ckeditor5-highlight/src/highlightcommand.js

@@ -16,13 +16,12 @@ import Command from '@ckeditor/ckeditor5-core/src/command';
  * @extends module:core/command~Command
  * @extends module:core/command~Command
  */
  */
 export default class HighlightCommand extends Command {
 export default class HighlightCommand extends Command {
-	constructor( editor, className ) {
-		super( editor );
-
-		/**
-		 * Name of marker class that is used by associated highlighter.
-		 */
-		this.className = className;
+	/**
+	 * @inheritDoc
+	 */
+	refresh() {
+		const model = this.editor.model;
+		const doc = model.document;
 
 
 		/**
 		/**
 		 * A flag indicating whether the command is active, which means that the selection has highlight attribute set.
 		 * A flag indicating whether the command is active, which means that the selection has highlight attribute set.
@@ -31,16 +30,7 @@ export default class HighlightCommand extends Command {
 		 * @readonly
 		 * @readonly
 		 * @member {undefined|String} module:highlight/highlightcommand~HighlightCommand#value
 		 * @member {undefined|String} module:highlight/highlightcommand~HighlightCommand#value
 		 */
 		 */
-	}
-
-	/**
-	 * @inheritDoc
-	 */
-	refresh() {
-		const model = this.editor.model;
-		const doc = model.document;
-
-		this.value = doc.selection.getAttribute( 'highlight' ) === this.className;
+		this.value = doc.selection.getAttribute( 'highlight' );
 		this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, 'highlight' );
 		this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, 'highlight' );
 	}
 	}
 
 
@@ -48,8 +38,12 @@ export default class HighlightCommand extends Command {
 	 * Executes the command.
 	 * Executes the command.
 	 *
 	 *
 	 * @protected
 	 * @protected
+	 * @param {Object} [options] Options for the executed command.
+	 * @param {String} [options.value] a value to apply.
+	 *
+	 * @fires execute
 	 */
 	 */
-	execute() {
+	execute( options = {} ) {
 		const model = this.editor.model;
 		const model = this.editor.model;
 		const document = model.document;
 		const document = model.document;
 		const selection = document.selection;
 		const selection = document.selection;
@@ -59,11 +53,17 @@ export default class HighlightCommand extends Command {
 			return;
 			return;
 		}
 		}
 
 
+		const value = options.value;
+
 		model.change( writer => {
 		model.change( writer => {
 			const ranges = model.schema.getValidRanges( selection.getRanges(), 'highlight' );
 			const ranges = model.schema.getValidRanges( selection.getRanges(), 'highlight' );
 
 
 			for ( const range of ranges ) {
 			for ( const range of ranges ) {
-				writer.setAttribute( 'highlight', this.className, range );
+				if ( value ) {
+					writer.setAttribute( 'highlight', value, range );
+				} else {
+					writer.removeAttribute( 'highlight', range );
+				}
 			}
 			}
 		} );
 		} );
 	}
 	}

+ 2 - 2
packages/ckeditor5-highlight/src/highlightediting.js

@@ -15,7 +15,6 @@ import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/build
 import AttributeElement from '@ckeditor/ckeditor5-engine/src/view/attributeelement';
 import AttributeElement from '@ckeditor/ckeditor5-engine/src/view/attributeelement';
 
 
 import HighlightCommand from './highlightcommand';
 import HighlightCommand from './highlightcommand';
-import RemoveHighlightCommand from './removehighlightcommand';
 
 
 /**
 /**
  * The highlight editing feature. It introduces `highlight` command which allow to highlight selected text with defined 'marker' or 'pen'.
  * The highlight editing feature. It introduces `highlight` command which allow to highlight selected text with defined 'marker' or 'pen'.
@@ -71,9 +70,10 @@ export default class HighlightEditing extends Plugin {
 
 
 		editor.config
 		editor.config
 			.get( 'highlight' )
 			.get( 'highlight' )
+			// TODO: change as in Font
 			.map( highlighter => editor.commands.add( highlighter.name, new HighlightCommand( editor, highlighter.class ) ) );
 			.map( highlighter => editor.commands.add( highlighter.name, new HighlightCommand( editor, highlighter.class ) ) );
 
 
-		editor.commands.add( 'removeHighlight', new RemoveHighlightCommand( editor ) );
+		editor.commands.add( 'removeHighlight', new HighlightCommand( editor ) );
 	}
 	}
 }
 }
 
 

+ 0 - 53
packages/ckeditor5-highlight/src/removehighlightcommand.js

@@ -1,53 +0,0 @@
-/**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-/**
- * @module highlight/removehighlightcommand
- */
-
-import Command from '@ckeditor/ckeditor5-core/src/command';
-
-/**
- * The remove highlight command. It is used by the {@link module:highlight/highlightediting~HighlightEditing highlight feature}
- * to remove text highlighting.
- *
- * @extends module:core/command~Command
- */
-export default class RemoveHighlightCommand extends Command {
-	/**
-	 * @inheritDoc
-	 */
-	refresh() {
-		const model = this.editor.model;
-		const doc = model.document;
-
-		this.value = false;
-		this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, 'highlight' );
-	}
-
-	/**
-	 * Executes the command.
-	 *
-	 * @protected
-	 */
-	execute() {
-		const model = this.editor.model;
-		const document = model.document;
-		const selection = document.selection;
-
-		// Do nothing on collapsed selection.
-		if ( selection.isCollapsed ) {
-			return;
-		}
-
-		model.change( writer => {
-			const ranges = model.schema.getValidRanges( selection.getRanges(), 'highlight' );
-
-			for ( const range of ranges ) {
-				writer.removeAttribute( range, 'highlight' );
-			}
-		} );
-	}
-}

+ 5 - 5
packages/ckeditor5-highlight/tests/highlightcommand.js

@@ -9,7 +9,7 @@ import Command from '@ckeditor/ckeditor5-core/src/command';
 import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
 import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
 import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 
 
-describe.skip( 'HighlightCommand', () => {
+describe( 'HighlightCommand', () => {
 	let editor, model, command;
 	let editor, model, command;
 
 
 	beforeEach( () => {
 	beforeEach( () => {
@@ -37,7 +37,7 @@ describe.skip( 'HighlightCommand', () => {
 
 
 	describe( 'value', () => {
 	describe( 'value', () => {
 		it( 'is set to highlight attribute value when selection is in text with highlight attribute', () => {
 		it( 'is set to highlight attribute value when selection is in text with highlight attribute', () => {
-			setData( model, '<paragraph><$text highlight="marker">fo[]o</$text></paragraph>' );
+			setData( model, '<paragraph><$text highlight="marker">fo[o]</$text></paragraph>' );
 
 
 			expect( command ).to.have.property( 'value', 'marker' );
 			expect( command ).to.have.property( 'value', 'marker' );
 		} );
 		} );
@@ -63,7 +63,7 @@ describe.skip( 'HighlightCommand', () => {
 
 
 			expect( command.value ).to.be.undefined;
 			expect( command.value ).to.be.undefined;
 
 
-			command.execute( { class: 'marker' } );
+			command.execute( { value: 'marker' } );
 
 
 			expect( command.value ).to.equal( 'marker' );
 			expect( command.value ).to.equal( 'marker' );
 
 
@@ -78,7 +78,7 @@ describe.skip( 'HighlightCommand', () => {
 				'<paragraph>barbar]bar</paragraph>'
 				'<paragraph>barbar]bar</paragraph>'
 			);
 			);
 
 
-			command.execute( { class: 'marker' } );
+			command.execute( { value: 'marker' } );
 
 
 			expect( command.value ).to.equal( 'marker' );
 			expect( command.value ).to.equal( 'marker' );
 
 
@@ -94,7 +94,7 @@ describe.skip( 'HighlightCommand', () => {
 
 
 			expect( command.value ).to.equal( 'marker' );
 			expect( command.value ).to.equal( 'marker' );
 
 
-			command.execute( { class: 'foo' } );
+			command.execute( { value: 'foo' } );
 
 
 			expect( getData( model ) ).to.equal(
 			expect( getData( model ) ).to.equal(
 				'<paragraph>abc[<$text highlight="foo">foo</$text>]<$text highlight="marker">bar</$text>xyz</paragraph>'
 				'<paragraph>abc[<$text highlight="foo">foo</$text>]<$text highlight="marker">bar</$text>xyz</paragraph>'