浏览代码

Added: remove entire highlight on collapsed range when inside highlighted text of the same value.

Maciej Gołaszewski 8 年之前
父节点
当前提交
a2fe601029

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

@@ -62,8 +62,9 @@ export default class HighlightCommand extends Command {
 			if ( selection.isCollapsed ) {
 				const position = selection.getFirstPosition();
 
-				// When selection is inside text with `linkHref` attribute.
+				// When selection is inside text with `highlight` attribute.
 				if ( selection.hasAttribute( 'highlight' ) ) {
+					// Find the full highlighted range.
 					const isSameHighlight = value => {
 						return value.item.hasAttribute( 'highlight' ) && value.item.getAttribute( 'highlight' ) === this.value;
 					};
@@ -73,11 +74,17 @@ export default class HighlightCommand extends Command {
 
 					const highlightRange = new Range( highlightStart, highlightEnd );
 
-					// Then update `highlight` value.
-					writer.setAttribute( 'highlight', highlighter, highlightRange );
+					// Then depending on current value...
+					if ( this.value === highlighter ) {
+						// ...remove attribute.
+						writer.removeAttribute( 'highlight', highlightRange );
+					} else {
+						// ...update `highlight` value.
+						writer.setAttribute( 'highlight', highlighter, highlightRange );
 
-					// Create new range wrapping changed link.
-					selection.setRanges( [ highlightRange ] );
+						// And create new range wrapping changed highlighter.
+						selection.setRanges( [ highlightRange ] );
+					}
 				} else {
 					// TODO
 				}

+ 12 - 0
packages/ckeditor5-highlight/tests/highlightcommand.js

@@ -126,5 +126,17 @@ describe( 'HighlightCommand', () => {
 
 			expect( command.value ).to.equal( 'greenMarker' );
 		} );
+
+		it( 'should remove entire highlight on collapsed range when inside highlighted text of the same value', () => {
+			setData( model, '<paragraph>abc<$text highlight="marker">foo[]bar</$text>xyz</paragraph>' );
+
+			expect( command.value ).to.equal( 'marker' );
+
+			command.execute( { value: 'marker' } );
+
+			expect( getData( model ) ).to.equal( '<paragraph>abcfoo[]barxyz</paragraph>' );
+
+			expect( command.value ).to.be.undefined;
+		} );
 	} );
 } );