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

Commit

Permalink
Other: Aligned the implementation to the new Command API (see https:/…
Browse files Browse the repository at this point in the history
…/github.com/ckeditor/ckeditor5-core/issues/88).

BREAKING CHANGES: The command API has been changed.
  • Loading branch information
szymonkups committed Jun 13, 2017
2 parents 9dc8973 + 189f660 commit 919b497
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 170 deletions.
55 changes: 15 additions & 40 deletions src/linkcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @module link/linkcommand
*/

import Command from '@ckeditor/ckeditor5-core/src/command/command';
import Command from '@ckeditor/ckeditor5-core/src/command';
import Text from '@ckeditor/ckeditor5-engine/src/model/text';
import Range from '@ckeditor/ckeditor5-engine/src/model/range';
import getSchemaValidRanges from '@ckeditor/ckeditor5-core/src/command/helpers/getschemavalidranges';
Expand All @@ -17,49 +17,25 @@ import findLinkRange from './findlinkrange';
/**
* The link command. It is used by the {@link module:link/link~Link link feature}.
*
* @extends module:core/command/command~Command
* @extends module:core/command~Command
*/
export default class LinkCommand extends Command {
/**
* @see module:core/command/command~Command
* @param {module:core/editor/editor~Editor} editor
*/
constructor( editor ) {
super( editor );

/**
* Currently selected `linkHref` attribute value.
*
* @observable
* @member {Boolean} module:core/command/toggleattributecommand~ToggleAttributeCommand#value
*/
this.set( 'value', undefined );

// 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.
* The value of `'linkHref'` attribute if the start of a selection is located in a node with this attribute.
*
* @observable
* @readonly
* @member {Object|undefined} #value
*/
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}
*
* @protected
* @returns {Boolean}
* @inheritDoc
*/
_checkEnabled() {
const document = this.editor.document;
refresh() {
const doc = this.editor.document;

return isAttributeAllowedInSelection( 'linkHref', document.selection, document.schema );
this.value = doc.selection.getAttribute( 'linkHref' );
this.isEnabled = isAttributeAllowedInSelection( 'linkHref', doc.selection, doc.schema );
}

/**
Expand All @@ -75,10 +51,10 @@ export default class LinkCommand extends Command {
*
* When selection is collapsed and inside text with `linkHref` attribute, the attribute value will be updated.
*
* @protected
* @fires execute
* @param {String} href Link destination.
*/
_doExecute( href ) {
execute( href ) {
const document = this.editor.document;
const selection = document.selection;

Expand All @@ -89,7 +65,6 @@ export default class LinkCommand extends Command {
// If selection is collapsed then update selected link or insert new one at the place of caret.
if ( selection.isCollapsed ) {
const position = selection.getFirstPosition();
const parent = position.parent;

// When selection is inside text with `linkHref` attribute.
if ( selection.hasAttribute( 'linkHref' ) ) {
Expand All @@ -102,7 +77,7 @@ export default class LinkCommand extends Command {
selection.setRanges( [ linkRange ] );
}
// If not then insert text node with `linkHref` attribute in place of caret.
else if ( document.schema.check( { name: '$text', attributes: 'linkHref', inside: parent.name } ) ) {
else {
const node = new Text( href, { linkHref: href } );

batch.insert( position, node );
Expand Down
4 changes: 2 additions & 2 deletions src/linkengine.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default class LinkEngine extends Plugin {
} ) );

// Create linking commands.
editor.commands.set( 'link', new LinkCommand( editor ) );
editor.commands.set( 'unlink', new UnlinkCommand( editor ) );
editor.commands.add( 'link', new LinkCommand( editor ) );
editor.commands.add( 'unlink', new UnlinkCommand( editor ) );
}
}
30 changes: 7 additions & 23 deletions src/unlinkcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,20 @@
* @module link/unlinkcommand
*/

import Command from '@ckeditor/ckeditor5-core/src/command/command';
import Command from '@ckeditor/ckeditor5-core/src/command';
import findLinkRange from './findlinkrange';

/**
* The unlink command. It is used by the {@link module:link/link~Link link plugin}.
*
* @extends module:core/command/command~Command
* @extends module:core/command~Command
*/
export default class UnlinkCommand extends Command {
/**
* @see module:core/command/command~Command
* @param {module:core/editor/editor~Editor} editor
* @inheritDoc
*/
constructor( editor ) {
super( editor );

// Checks whether the command should be enabled or disabled.
this.listenTo( editor.document, 'changesDone', () => {
this.refreshState();
} );
refresh() {
this.isEnabled = this.editor.document.selection.hasAttribute( 'linkHref' );
}

/**
Expand All @@ -35,9 +29,9 @@ export default class UnlinkCommand extends Command {
* When the selection is collapsed, removes `linkHref` attribute from each node with the same `linkHref` attribute value.
* When the selection is non-collapsed, removes `linkHref` from each node in selected ranges.
*
* @protected
* @fires execute
*/
_doExecute() {
execute() {
const document = this.editor.document;
const selection = document.selection;

Expand All @@ -55,14 +49,4 @@ export default class UnlinkCommand extends Command {
}
} );
}

/**
* Checks if selection has `linkHref` attribute.
*
* @protected
* @returns {Boolean}
*/
_checkEnabled() {
return this.editor.document.selection.hasAttribute( 'linkHref' );
}
}
Loading

0 comments on commit 919b497

Please sign in to comment.