Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Fix: LinkCommand should update its state upon editor load.
Browse files Browse the repository at this point in the history
  • Loading branch information
oleq committed May 18, 2017
1 parent 3a1a623 commit d31609b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/linkcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion src/unlinkcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
} );
Expand Down
18 changes: 18 additions & 0 deletions tests/linkcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>' );
Expand Down

0 comments on commit d31609b

Please sign in to comment.