Selaa lähdekoodia

Changed the way of checking of linkCommand is enabled.

Oskar Wrobel 9 vuotta sitten
vanhempi
commit
1295751c39

+ 1 - 1
packages/ckeditor5-link/src/link.js

@@ -107,7 +107,7 @@ export default class Link extends Feature {
 		} );
 
 		// Bind button model to the command.
-		unlinkButtonModel.bind( 'isEnabled' ).to( unlinkCommand, 'hasValue' );
+		unlinkButtonModel.bind( 'isEnabled' ).to( unlinkCommand, 'isEnabled' );
 
 		// Execute unlink command and hide panel, if open.
 		this.listenTo( unlinkButtonModel, 'execute', () => {

+ 12 - 12
packages/ckeditor5-link/src/unlinkcommand.js

@@ -20,18 +20,8 @@ export default class UnlinkCommand extends Command {
 	constructor( editor ) {
 		super( editor );
 
-		/**
-		 * Flag indicating whether command is active. For collapsed selection it means that typed characters will have
-		 * the command's attribute set. For range selection it means that all nodes inside have the attribute applied.
-		 *
-		 * @observable
-		 * @member {Boolean} link.UnlinkCommand#hasValue
-		 */
-		this.set( 'hasValue', undefined );
-
-		this.listenTo( this.editor.document.selection, 'change:attribute', () => {
-			this.hasValue = this.editor.document.selection.hasAttribute( 'linkHref' );
-		} );
+		// Checks when command should be enabled or disabled.
+		this.listenTo( this.editor.document.selection, 'change:attribute', () => this.refreshState() );
 	}
 
 	/**
@@ -60,4 +50,14 @@ export default class UnlinkCommand extends Command {
 			}
 		} );
 	}
+
+	/**
+	 * Checks if selection has `linkHref` attribute.
+	 *
+	 * @protected
+	 * @returns {Boolean}
+	 */
+	_checkEnabled() {
+		return this.editor.document.selection.hasAttribute( 'linkHref' );
+	}
 }

+ 27 - 0
packages/ckeditor5-link/tests/unlinkcommand.js

@@ -6,6 +6,9 @@
 import ModelTestEditor from '/tests/core/_utils/modeltesteditor.js';
 import UnlinkCommand from '/ckeditor5/link/unlinkcommand.js';
 import { setData, getData } from '/tests/engine/_utils/model.js';
+import testUtils from '/tests/core/_utils/utils.js';
+
+testUtils.createSinonSandbox();
 
 describe( 'UnlinkCommand', () => {
 	let editor, document, command;
@@ -30,6 +33,16 @@ describe( 'UnlinkCommand', () => {
 		command.destroy();
 	} );
 
+	describe( 'constructor', () => {
+		it( 'should listen on selection attribute change and refresh state', () => {
+			const refreshStateSpy = testUtils.sinon.spy( command, 'refreshState' );
+
+			document.selection.fire( 'change:attribute' );
+
+			expect( refreshStateSpy.calledOnce ).to.true;
+		} );
+	} );
+
 	describe( '_doExecute', () => {
 		describe( 'non-collapsed selection', () => {
 			it( 'should remove `linkHref` attribute from selected text', () => {
@@ -196,4 +209,18 @@ describe( 'UnlinkCommand', () => {
 			} );
 		} );
 	} );
+
+	describe( '_checkEnabled', () => {
+		it( 'should return false when selection has `linkHref` attribute', () => {
+			document.selection.setAttribute( 'linkHref', 'value' );
+
+			expect( command._checkEnabled() ).to.true;
+		} );
+
+		it( 'should return false when selection doesn\'t have `linkHref` attribute', () => {
+			document.selection.removeAttribute( 'linkHref' );
+
+			expect( command._checkEnabled() ).to.false;
+		} );
+	} );
 } );