|
@@ -25,6 +25,12 @@ export default class LinkCommand extends Command {
|
|
|
* @member {Object|undefined} #value
|
|
* @member {Object|undefined} #value
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
|
|
+ constructor( editor ) {
|
|
|
|
|
+ super( editor );
|
|
|
|
|
+
|
|
|
|
|
+ this.customAttributes = new Map();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* @inheritDoc
|
|
* @inheritDoc
|
|
|
*/
|
|
*/
|
|
@@ -52,10 +58,21 @@ export default class LinkCommand extends Command {
|
|
|
* @fires execute
|
|
* @fires execute
|
|
|
* @param {String} href Link destination.
|
|
* @param {String} href Link destination.
|
|
|
*/
|
|
*/
|
|
|
- execute( href ) {
|
|
|
|
|
|
|
+ execute( href, customAttrs = {} ) {
|
|
|
const model = this.editor.model;
|
|
const model = this.editor.model;
|
|
|
const selection = model.document.selection;
|
|
const selection = model.document.selection;
|
|
|
|
|
|
|
|
|
|
+ // Stores information about custom attributes to turn on/off.
|
|
|
|
|
+ const truthyCustomAttributes = [];
|
|
|
|
|
+ const falsyCustomAttributes = [];
|
|
|
|
|
+ Object.entries( customAttrs ).forEach( entriesPair => {
|
|
|
|
|
+ if ( entriesPair[ 1 ] ) {
|
|
|
|
|
+ truthyCustomAttributes.push( entriesPair[ 0 ] );
|
|
|
|
|
+ } else {
|
|
|
|
|
+ falsyCustomAttributes.push( entriesPair[ 0 ] );
|
|
|
|
|
+ }
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
model.change( writer => {
|
|
model.change( writer => {
|
|
|
// If selection is collapsed then update selected link or insert new one at the place of caret.
|
|
// If selection is collapsed then update selected link or insert new one at the place of caret.
|
|
|
if ( selection.isCollapsed ) {
|
|
if ( selection.isCollapsed ) {
|
|
@@ -64,9 +81,15 @@ export default class LinkCommand extends Command {
|
|
|
// When selection is inside text with `linkHref` attribute.
|
|
// When selection is inside text with `linkHref` attribute.
|
|
|
if ( selection.hasAttribute( 'linkHref' ) ) {
|
|
if ( selection.hasAttribute( 'linkHref' ) ) {
|
|
|
// Then update `linkHref` value.
|
|
// Then update `linkHref` value.
|
|
|
- const linkRange = findLinkRange( selection.getFirstPosition(), selection.getAttribute( 'linkHref' ), model );
|
|
|
|
|
|
|
+ const linkRange = findLinkRange( position, selection.getAttribute( 'linkHref' ), model );
|
|
|
|
|
|
|
|
writer.setAttribute( 'linkHref', href, linkRange );
|
|
writer.setAttribute( 'linkHref', href, linkRange );
|
|
|
|
|
+ truthyCustomAttributes.forEach( item => {
|
|
|
|
|
+ writer.setAttribute( item, true, linkRange );
|
|
|
|
|
+ } );
|
|
|
|
|
+ falsyCustomAttributes.forEach( item => {
|
|
|
|
|
+ writer.removeAttribute( item, linkRange );
|
|
|
|
|
+ } );
|
|
|
|
|
|
|
|
// Create new range wrapping changed link.
|
|
// Create new range wrapping changed link.
|
|
|
writer.setSelection( linkRange );
|
|
writer.setSelection( linkRange );
|
|
@@ -79,6 +102,10 @@ export default class LinkCommand extends Command {
|
|
|
|
|
|
|
|
attributes.set( 'linkHref', href );
|
|
attributes.set( 'linkHref', href );
|
|
|
|
|
|
|
|
|
|
+ truthyCustomAttributes.forEach( item => {
|
|
|
|
|
+ attributes.set( item, true );
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
const node = writer.createText( href, attributes );
|
|
const node = writer.createText( href, attributes );
|
|
|
|
|
|
|
|
model.insertContent( node, position );
|
|
model.insertContent( node, position );
|
|
@@ -93,6 +120,12 @@ export default class LinkCommand extends Command {
|
|
|
|
|
|
|
|
for ( const range of ranges ) {
|
|
for ( const range of ranges ) {
|
|
|
writer.setAttribute( 'linkHref', href, range );
|
|
writer.setAttribute( 'linkHref', href, range );
|
|
|
|
|
+ truthyCustomAttributes.forEach( item => {
|
|
|
|
|
+ writer.setAttribute( item, true, range );
|
|
|
|
|
+ } );
|
|
|
|
|
+ falsyCustomAttributes.forEach( item => {
|
|
|
|
|
+ writer.removeAttribute( item, range );
|
|
|
|
|
+ } );
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
} );
|
|
} );
|