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

t/93a: LinkCommand and UnlinkCommand should update their state upon editor load #119

Merged
merged 3 commits into from
May 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 4 additions & 2 deletions src/unlinkcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
} );
}

/**
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
4 changes: 2 additions & 2 deletions tests/unlinkcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
} );
Expand Down