1 | /**
|
2 | * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3 | * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4 | */
|
5 | /**
|
6 | * @module link/linkcommand
|
7 | */
|
8 | import { Command } from 'ckeditor5/src/core';
|
9 | import { Collection } from 'ckeditor5/src/utils';
|
10 | import AutomaticDecorators from './utils/automaticdecorators';
|
11 | import type ManualDecorator from './utils/manualdecorator';
|
12 | /**
|
13 | * The link command. It is used by the {@link module:link/link~Link link feature}.
|
14 | */
|
15 | export default class LinkCommand extends Command {
|
16 | /**
|
17 | * The value of the `'linkHref'` attribute if the start of the selection is located in a node with this attribute.
|
18 | *
|
19 | * @observable
|
20 | * @readonly
|
21 | */
|
22 | value: string | undefined;
|
23 | /**
|
24 | * A collection of {@link module:link/utils/manualdecorator~ManualDecorator manual decorators}
|
25 | * corresponding to the {@link module:link/linkconfig~LinkConfig#decorators decorator configuration}.
|
26 | *
|
27 | * You can consider it a model with states of manual decorators added to the currently selected link.
|
28 | */
|
29 | readonly manualDecorators: Collection<ManualDecorator>;
|
30 | /**
|
31 | * An instance of the helper that ties together all {@link module:link/linkconfig~LinkDecoratorAutomaticDefinition}
|
32 | * that are used by the {@glink features/link link} and the {@glink features/images/images-linking linking images} features.
|
33 | */
|
34 | readonly automaticDecorators: AutomaticDecorators;
|
35 | /**
|
36 | * Synchronizes the state of {@link #manualDecorators} with the currently present elements in the model.
|
37 | */
|
38 | restoreManualDecoratorStates(): void;
|
39 | /**
|
40 | * @inheritDoc
|
41 | */
|
42 | refresh(): void;
|
43 | /**
|
44 | * Executes the command.
|
45 | *
|
46 | * When the selection is non-collapsed, the `linkHref` attribute will be applied to nodes inside the selection, but only to
|
47 | * those nodes where the `linkHref` attribute is allowed (disallowed nodes will be omitted).
|
48 | *
|
49 | * When the selection is collapsed and is not inside the text with the `linkHref` attribute, a
|
50 | * new {@link module:engine/model/text~Text text node} with the `linkHref` attribute will be inserted in place of the caret, but
|
51 | * only if such element is allowed in this place. The `_data` of the inserted text will equal the `href` parameter.
|
52 | * The selection will be updated to wrap the just inserted text node.
|
53 | *
|
54 | * When the selection is collapsed and inside the text with the `linkHref` attribute, the attribute value will be updated.
|
55 | *
|
56 | * # Decorators and model attribute management
|
57 | *
|
58 | * There is an optional argument to this command that applies or removes model
|
59 | * {@glink framework/architecture/editing-engine#text-attributes text attributes} brought by
|
60 | * {@link module:link/utils/manualdecorator~ManualDecorator manual link decorators}.
|
61 | *
|
62 | * Text attribute names in the model correspond to the entries in the {@link module:link/linkconfig~LinkConfig#decorators
|
63 | * configuration}.
|
64 | * For every decorator configured, a model text attribute exists with the "link" prefix. For example, a `'linkMyDecorator'` attribute
|
65 | * corresponds to `'myDecorator'` in the configuration.
|
66 | *
|
67 | * To learn more about link decorators, check out the {@link module:link/linkconfig~LinkConfig#decorators `config.link.decorators`}
|
68 | * documentation.
|
69 | *
|
70 | * Here is how to manage decorator attributes with the link command:
|
71 | *
|
72 | * ```ts
|
73 | * const linkCommand = editor.commands.get( 'link' );
|
74 | *
|
75 | * // Adding a new decorator attribute.
|
76 | * linkCommand.execute( 'http://example.com', {
|
77 | * linkIsExternal: true
|
78 | * } );
|
79 | *
|
80 | * // Removing a decorator attribute from the selection.
|
81 | * linkCommand.execute( 'http://example.com', {
|
82 | * linkIsExternal: false
|
83 | * } );
|
84 | *
|
85 | * // Adding multiple decorator attributes at the same time.
|
86 | * linkCommand.execute( 'http://example.com', {
|
87 | * linkIsExternal: true,
|
88 | * linkIsDownloadable: true,
|
89 | * } );
|
90 | *
|
91 | * // Removing and adding decorator attributes at the same time.
|
92 | * linkCommand.execute( 'http://example.com', {
|
93 | * linkIsExternal: false,
|
94 | * linkFoo: true,
|
95 | * linkIsDownloadable: false,
|
96 | * } );
|
97 | * ```
|
98 | *
|
99 | * **Note**: If the decorator attribute name is not specified, its state remains untouched.
|
100 | *
|
101 | * **Note**: {@link module:link/unlinkcommand~UnlinkCommand#execute `UnlinkCommand#execute()`} removes all
|
102 | * decorator attributes.
|
103 | *
|
104 | * @fires execute
|
105 | * @param href Link destination.
|
106 | * @param manualDecoratorIds The information about manual decorator attributes to be applied or removed upon execution.
|
107 | */
|
108 | execute(href: string, manualDecoratorIds?: Record<string, boolean>): void;
|
109 | /**
|
110 | * Provides information whether a decorator with a given name is present in the currently processed selection.
|
111 | *
|
112 | * @param decoratorName The name of the manual decorator used in the model
|
113 | * @returns The information whether a given decorator is currently present in the selection.
|
114 | */
|
115 | private _getDecoratorStateFromModel;
|
116 | /**
|
117 | * Checks whether specified `range` is inside an element that accepts the `linkHref` attribute.
|
118 | *
|
119 | * @param range A range to check.
|
120 | * @param allowedRanges An array of ranges created on elements where the attribute is accepted.
|
121 | */
|
122 | private _isRangeToUpdate;
|
123 | /**
|
124 | * Updates selected link with a new value as its content and as its href attribute.
|
125 | *
|
126 | * @param model Model is need to insert content.
|
127 | * @param writer Writer is need to create text element in model.
|
128 | * @param range A range where should be inserted content.
|
129 | * @param href A link value which should be in the href attribute and in the content.
|
130 | */
|
131 | private _updateLinkContent;
|
132 | }
|