8
0
Просмотр исходного кода

Fix: LinkCommand should update its state upon editor load.

Aleksander Nowodzinski 8 лет назад
Родитель
Сommit
539b2d089c

+ 11 - 2
packages/ckeditor5-link/src/linkcommand.js

@@ -35,11 +35,20 @@ export default class LinkCommand extends Command {
 		 */
 		this.set( 'value', undefined );
 
-		this.listenTo( this.editor.document.selection, 'change:attribute', () => {
-			this.value = this.editor.document.selection.getAttribute( 'linkHref' );
+		// Checks whether the command should be enabled or disabled.
+		this.listenTo( editor.document, 'changesDone', () => {
+			this.refreshState();
+			this.refreshValue();
 		} );
 	}
 
+	/**
+	 * Updates command's {@link #value} based on the current selection.
+	 */
+	refreshValue() {
+		this.value = this.editor.document.selection.getAttribute( 'linkHref' );
+	}
+
 	/**
 	 * Checks if {@link module:engine/model/document~Document#schema} allows to create attribute in {@link
 	 * module:engine/model/document~Document#selection}

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

@@ -23,7 +23,7 @@ export default class UnlinkCommand extends Command {
 	constructor( editor ) {
 		super( editor );
 
-		// Checks when command should be enabled or disabled.
+		// Checks whether the command should be enabled or disabled.
 		this.listenTo( editor.document, 'changesDone', () => {
 			this.refreshState();
 		} );

+ 18 - 0
packages/ckeditor5-link/tests/linkcommand.js

@@ -31,7 +31,25 @@ describe( 'LinkCommand', () => {
 		command.destroy();
 	} );
 
+	describe( 'constructor()', () => {
+		// https://github.com/ckeditor/ckeditor5-link/issues/93
+		it( 'should listen on document#changesDone and refresh the command\'s state', () => {
+			const refreshStateSpy = sinon.spy( command, 'refreshState' );
+
+			document.fire( 'changesDone' );
+
+			expect( refreshStateSpy.calledOnce ).to.true;
+		} );
+	} );
+
 	describe( 'value', () => {
+		it( 'should be updated on document#changesDone', () => {
+			const spy = sinon.spy( command, 'refreshValue' );
+
+			document.fire( 'changesDone' );
+			sinon.assert.calledOnce( spy );
+		} );
+
 		describe( 'collapsed selection', () => {
 			it( 'should be equal attribute value when selection is placed inside element with `linkHref` attribute', () => {
 				setData( document, '<$text linkHref="url">foo[]bar</$text>' );