浏览代码

Merge pull request #119 from ckeditor/t/93a

Fix: `LinkCommand` and `UnlinkCommand` should update their state upon editor load. Closes #93.
Piotrek Koszuliński 8 年之前
父节点
当前提交
c7c0612c2b

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

@@ -35,12 +35,21 @@ 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}
 	 *

+ 4 - 2
packages/ckeditor5-link/src/unlinkcommand.js

@@ -23,8 +23,10 @@ export default class UnlinkCommand extends Command {
 	constructor( editor ) {
 		super( editor );
 
-		// Checks when command should be enabled or disabled.
-		this.listenTo( editor.document.selection, 'change:attribute', () => this.refreshState() );
+		// 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>' );

+ 2 - 2
packages/ckeditor5-link/tests/unlinkcommand.js

@@ -34,10 +34,10 @@ describe( 'UnlinkCommand', () => {
 	} );
 
 	describe( 'constructor()', () => {
-		it( 'should listen on selection attribute change and refresh state', () => {
+		it( 'should listen on document#changesDone and refresh the state', () => {
 			const refreshStateSpy = testUtils.sinon.spy( command, 'refreshState' );
 
-			document.selection.fire( 'change:attribute' );
+			document.fire( 'changesDone' );
 
 			expect( refreshStateSpy.calledOnce ).to.true;
 		} );