Explorar el Código

Other: Implement HighlightCommand value and isEnabled properties calculation on refresh.

Maciej Gołaszewski hace 8 años
padre
commit
f92815cd24

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

@@ -46,8 +46,10 @@ export default class HighlightCommand extends Command {
 	 * @inheritDoc
 	 */
 	refresh() {
-		this.isEnabled = this._checkEnabled();
-		this.value = this._getValue();
+		const doc = this.editor.document;
+
+		this.value = doc.selection.getAttribute( 'highlight' );
+		this.isEnabled = doc.schema.checkAttributeInSelection( doc.selection, 'highlight' );
 	}
 
 	/**
@@ -66,26 +68,4 @@ export default class HighlightCommand extends Command {
 			// TODO
 		} );
 	}
-
-	/**
-	 * Checks whether the command can be enabled in the current context.
-	 *
-	 * @private
-	 * @param {module:engine/model/element~Element} firstBlock A first block in selection to be checked.
-	 * @returns {Boolean} Whether the command should be enabled.
-	 */
-	_checkEnabled() {
-		return true;
-	}
-
-	/**
-	 * Checks the command's {@link #value}.
-	 *
-	 * @private
-	 * @param {module:engine/model/element~Element} firstBlock A first block in selection to be checked.
-	 * @returns {Boolean} The current value.
-	 */
-	_getValue() {
-		return true;
-	}
 }

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

@@ -24,7 +24,7 @@ describe( 'HighlightCommand', () => {
 
 				doc.schema.registerItem( 'paragraph', '$block' );
 
-				doc.schema.allow( { name: '$block', inside: '$root', attributes: 'highlight' } );
+				doc.schema.allow( { name: '$inline', attributes: 'highlight', inside: '$block' } );
 			} );
 	} );
 
@@ -38,16 +38,22 @@ describe( 'HighlightCommand', () => {
 	} );
 
 	describe( 'value', () => {
-		it( 'is true when selection is in block with commend type highlight', () => {
-			setModelData( doc, '<paragraph highlight="center"><$text highlight="true">fo[]o</$text></paragraph>' );
+		it( 'is set to highlight attribute value when selection is in text with highlight attribute', () => {
+			setModelData( doc, '<paragraph><$text highlight="marker">fo[]o</$text></paragraph>' );
 
-			expect( command ).to.have.property( 'value', true );
+			expect( command ).to.have.property( 'value', 'marker' );
+		} );
+
+		it( 'is undefined when selection is not in text with highlight attribute', () => {
+			setModelData( doc, '<paragraph>fo[]o</paragraph>' );
+
+			expect( command ).to.have.property( 'value', undefined );
 		} );
 	} );
 
 	describe( 'isEnabled', () => {
-		it( 'is true when selection is in a block which can have added highlight', () => {
-			setModelData( doc, '<paragraph highlight="center"><$text highlight="true">fo[]o</$text></paragraph>' );
+		it( 'is true when selection is on text which can have highlight added', () => {
+			setModelData( doc, '<paragraph>fo[]o</paragraph>' );
 
 			expect( command ).to.have.property( 'isEnabled', true );
 		} );
@@ -58,9 +64,9 @@ describe( 'HighlightCommand', () => {
 			it( 'adds highlight to selected text element', () => {
 				setModelData( doc, '<paragraph>f[o]o</paragraph>' );
 
-				editor.execute( 'highlight' );
+				editor.execute( 'highlight', { class: 'marker' } );
 
-				expect( getModelData( doc ) ).to.equal( '<paragraph>f<$text highlight="true">[o]</$text>o</paragraph>' );
+				expect( getModelData( doc ) ).to.equal( '<paragraph>f<$text highlight="marker">[o]</$text>o</paragraph>' );
 			} );
 		} );
 	} );