Преглед на файлове

Other: Initial implementation of `HighlightCommand#execute` method.

Maciej Gołaszewski преди 8 години
родител
ревизия
25db0e8fb4
променени са 2 файла, в които са добавени 61 реда и са изтрити 13 реда
  1. 24 5
      packages/ckeditor5-highlight/src/highlightcommand.js
  2. 37 8
      packages/ckeditor5-highlight/tests/highlightcommand.js

+ 24 - 5
packages/ckeditor5-highlight/src/highlightcommand.js

@@ -57,15 +57,34 @@ export default class HighlightCommand extends Command {
 	 *
 	 * @protected
 	 * @param {Object} [options] Options for the executed command.
+	 * @param {String} options.class Name of marker class name.
 	 * @param {module:engine/model/batch~Batch} [options.batch] A batch to collect all the change steps.
 	 * A new batch will be created if this option is not set.
 	 */
-	execute() {
-		const editor = this.editor;
-		const document = editor.document;
+	execute( options = {} ) {
+		const doc = this.editor.document;
+		const selection = doc.selection;
+		const value = options.class;
+
+		doc.enqueueChanges( () => {
+			if ( selection.isCollapsed ) {
+				if ( value ) {
+					selection.setAttribute( 'highlight', value );
+				} else {
+					selection.removeAttribute( 'highlight' );
+				}
+			} else {
+				const ranges = doc.schema.getValidRanges( selection.getRanges(), 'highlight' );
+				const batch = options.batch || doc.batch();
 
-		document.enqueueChanges( () => {
-			// TODO
+				for ( const range of ranges ) {
+					if ( value ) {
+						batch.setAttribute( range, 'highlight', value );
+					} else {
+						batch.removeAttribute( range, 'highlight' );
+					}
+				}
+			}
 		} );
 	}
 }

+ 37 - 8
packages/ckeditor5-highlight/tests/highlightcommand.js

@@ -5,10 +5,11 @@
 
 import HighlightCommand from './../src/highlightcommand';
 
-import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
+import { getData, setData } from '../../ckeditor5-engine/src/dev-utils/model';
 
 describe( 'HighlightCommand', () => {
 	let editor, doc, command;
@@ -59,15 +60,43 @@ describe( 'HighlightCommand', () => {
 		} );
 	} );
 
-	describe.skip( 'execute()', () => {
-		describe( 'applying highlight', () => {
-			it( 'adds highlight to selected text element', () => {
-				setModelData( doc, '<paragraph>f[o]o</paragraph>' );
+	describe( 'execute()', () => {
+		it( 'should add highlight attribute on selected nodes nodes when passed as parameter', () => {
+			setData( doc, '<paragraph>a[bc<$text highlight="marker">fo]obar</$text>xyz</paragraph>' );
 
-				editor.execute( 'highlight', { class: 'marker' } );
+			expect( command.value ).to.be.undefined;
 
-				expect( getModelData( doc ) ).to.equal( '<paragraph>f<$text highlight="marker">[o]</$text>o</paragraph>' );
-			} );
+			command.execute( { class: 'marker' } );
+
+			expect( command.value ).to.equal( 'marker' );
+
+			expect( getData( doc ) ).to.equal( '<paragraph>a[<$text highlight="marker">bcfo]obar</$text>xyz</paragraph>' );
+		} );
+
+		it( 'should set highlight attribute on selected nodes when passed as parameter', () => {
+			setData( doc, '<paragraph>abc[<$text highlight="marker">foo]bar</$text>xyz</paragraph>' );
+
+			expect( command.value ).to.equal( 'marker' );
+
+			command.execute( { class: 'foo' } );
+
+			expect( getData( doc ) ).to.equal(
+				'<paragraph>abc[<$text highlight="foo">foo</$text>]<$text highlight="marker">bar</$text>xyz</paragraph>'
+			);
+
+			expect( command.value ).to.equal( 'foo' );
+		} );
+
+		it( 'should remove highlight attribute on selected nodes nodes when undefined passed as parameter', () => {
+			setData( doc, '<paragraph>abc[<$text highlight="marker">foo]bar</$text>xyz</paragraph>' );
+
+			expect( command.value ).to.equal( 'marker' );
+
+			command.execute();
+
+			expect( getData( doc ) ).to.equal( '<paragraph>abc[foo]<$text highlight="marker">bar</$text>xyz</paragraph>' );
+
+			expect( command.value ).to.be.undefined;
 		} );
 	} );
 } );