-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
unlinkcommand.js
81 lines (73 loc) · 2.59 KB
/
unlinkcommand.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module link/unlinkcommand
*/
import Command from '@ckeditor/ckeditor5-core/src/command';
import findAttributeRange from '@ckeditor/ckeditor5-typing/src/utils/findattributerange';
import first from '@ckeditor/ckeditor5-utils/src/first';
import { isImageAllowed } from './utils';
/**
* The unlink command. It is used by the {@link module:link/link~Link link plugin}.
*
* @extends module:core/command~Command
*/
export default class UnlinkCommand extends Command {
/**
* @inheritDoc
*/
refresh() {
const model = this.editor.model;
const doc = model.document;
const selectedElement = first( doc.selection.getSelectedBlocks() );
// A check for the `LinkImage` plugin. If the selection contains an image element, get values from the element.
// Currently the selection reads attributes from text nodes only. See #7429 and #7465.
if ( isImageAllowed( selectedElement, model.schema ) ) {
this.isEnabled = model.schema.checkAttribute( selectedElement, 'linkHref' );
} else {
this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, 'linkHref' );
}
}
/**
* Executes the command.
*
* When the selection is collapsed, it removes the `linkHref` attribute from each node with the same `linkHref` attribute value.
* When the selection is non-collapsed, it removes the `linkHref` attribute from each node in selected ranges.
*
* # Decorators
*
* If {@link module:link/link~LinkConfig#decorators `config.link.decorators`} is specified,
* all configured decorators are removed together with the `linkHref` attribute.
*
* @fires execute
*/
execute() {
const editor = this.editor;
const model = this.editor.model;
const selection = model.document.selection;
const linkCommand = editor.commands.get( 'link' );
model.change( writer => {
// Get ranges to unlink.
const rangesToUnlink = selection.isCollapsed ?
[ findAttributeRange(
selection.getFirstPosition(),
'linkHref',
selection.getAttribute( 'linkHref' ),
model
) ] :
selection.getRanges();
// Remove `linkHref` attribute from specified ranges.
for ( const range of rangesToUnlink ) {
writer.removeAttribute( 'linkHref', range );
// If there are registered custom attributes, then remove them during unlink.
if ( linkCommand ) {
for ( const manualDecorator of linkCommand.manualDecorators ) {
writer.removeAttribute( manualDecorator.id, range );
}
}
}
} );
}
}