From 80aad4ef9a5cad5c284d3b23e1a884869444b20d Mon Sep 17 00:00:00 2001 From: Aleksander Nowodzinski Date: Mon, 15 May 2017 12:47:54 +0200 Subject: [PATCH 1/2] Fix: UnlinkCommand should update its state upon editor load. Closes #93. --- src/unlinkcommand.js | 4 +++- tests/unlinkcommand.js | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/unlinkcommand.js b/src/unlinkcommand.js index ce96407..7fe5457 100644 --- a/src/unlinkcommand.js +++ b/src/unlinkcommand.js @@ -24,7 +24,9 @@ export default class UnlinkCommand extends Command { super( editor ); // Checks when command should be enabled or disabled. - this.listenTo( editor.document.selection, 'change:attribute', () => this.refreshState() ); + this.listenTo( editor.document, 'changesDone', () => { + this.refreshState(); + } ); } /** diff --git a/tests/unlinkcommand.js b/tests/unlinkcommand.js index 3104b70..303207f 100644 --- a/tests/unlinkcommand.js +++ b/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; } ); From d31609be69f901646cfc539fa7dd529c3317276f Mon Sep 17 00:00:00 2001 From: Aleksander Nowodzinski Date: Thu, 18 May 2017 16:09:04 +0200 Subject: [PATCH 2/2] Fix: LinkCommand should update its state upon editor load. --- src/linkcommand.js | 13 +++++++++++-- src/unlinkcommand.js | 2 +- tests/linkcommand.js | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/linkcommand.js b/src/linkcommand.js index fe5049a..fe9c479 100644 --- a/src/linkcommand.js +++ b/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} diff --git a/src/unlinkcommand.js b/src/unlinkcommand.js index 8f9ce71..22611cd 100644 --- a/src/unlinkcommand.js +++ b/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(); } ); diff --git a/tests/linkcommand.js b/tests/linkcommand.js index 673b9f0..ec7e698 100644 --- a/tests/linkcommand.js +++ b/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' );