UNPKG

4.29 kBTypeScriptView Raw
1import { Command } from '@ckeditor/ckeditor5-core';
2import { Collection } from '@ckeditor/ckeditor5-utils';
3import AutomaticDecorators from './utils/automaticdecorators';
4import ManualDecorator from './utils/manualdecorator';
5
6/**
7 * The link command. It is used by the {@link module:link/link~Link link feature}.
8 */
9export default class LinkCommand extends Command {
10 /**
11 * An instance of the helper that ties together all {@link module:link/link~LinkDecoratorAutomaticDefinition}
12 * that are used by the {@glink features/link link} and the {@glink features/images/images-linking linking images} features.
13 */
14 readonly automaticDecorators: AutomaticDecorators;
15
16 /**
17 * A collection of {@link module:link/utils~ManualDecorator manual decorators}
18 * corresponding to the {@link module:link/link~LinkConfig#decorators decorator configuration}.
19 *
20 * You can consider it a model with states of manual decorators added to the currently selected link.
21 */
22 readonly manualDecorators: Collection<ManualDecorator>;
23
24 get value(): string | undefined;
25 protected set value(href: string | undefined);
26
27 /**
28 * Synchronizes the state of {@link #manualDecorators} with the currently present elements in the model.
29 */
30 restoreManualDecoratorStates(): void;
31 refresh(): void;
32
33 /**
34 * Executes the command.
35 *
36 * When the selection is non-collapsed, the `linkHref` attribute will be applied to nodes inside the selection, but only to
37 * those nodes where the `linkHref` attribute is allowed (disallowed nodes will be omitted).
38 *
39 * When the selection is collapsed and is not inside the text with the `linkHref` attribute, a
40 * new {@link module:engine/model/text~Text text node} with the `linkHref` attribute will be inserted in place of the caret, but
41 * only if such element is allowed in this place. The `_data` of the inserted text will equal the `href` parameter.
42 * The selection will be updated to wrap the just inserted text node.
43 *
44 * When the selection is collapsed and inside the text with the `linkHref` attribute, the attribute value will be updated.
45 *
46 * # Decorators and model attribute management
47 *
48 * There is an optional argument to this command that applies or removes model
49 * {@glink framework/guides/architecture/editing-engine#text-attributes text attributes} brought by
50 * {@link module:link/utils~ManualDecorator manual link decorators}.
51 *
52 * Text attribute names in the model correspond to the entries in the {@link module:link/link~LinkConfig#decorators configuration}.
53 * For every decorator configured, a model text attribute exists with the "link" prefix. For example, a `'linkMyDecorator'` attribute
54 * corresponds to `'myDecorator'` in the configuration.
55 *
56 * To learn more about link decorators, check out the {@link module:link/link~LinkConfig#decorators `config.link.decorators`}
57 * documentation.
58 *
59 * Here is how to manage decorator attributes with the link command:
60 *
61 * const linkCommand = editor.commands.get( 'link' );
62 *
63 * // Adding a new decorator attribute.
64 * linkCommand.execute( 'http://example.com', {
65 * linkIsExternal: true
66 * } );
67 *
68 * // Removing a decorator attribute from the selection.
69 * linkCommand.execute( 'http://example.com', {
70 * linkIsExternal: false
71 * } );
72 *
73 * // Adding multiple decorator attributes at the same time.
74 * linkCommand.execute( 'http://example.com', {
75 * linkIsExternal: true,
76 * linkIsDownloadable: true,
77 * } );
78 *
79 * // Removing and adding decorator attributes at the same time.
80 * linkCommand.execute( 'http://example.com', {
81 * linkIsExternal: false,
82 * linkFoo: true,
83 * linkIsDownloadable: false,
84 * } );
85 *
86 * **Note**: If the decorator attribute name is not specified, its state remains untouched.
87 *
88 * **Note**: {@link module:link/unlinkcommand~UnlinkCommand#execute `UnlinkCommand#execute()`} removes all
89 * decorator attributes.
90 */
91 execute(href: string, manualDecoratorIds?: Record<string, unknown>): void;
92}
93
94declare module '@ckeditor/ckeditor5-core/src/commandcollection' {
95 interface Commands {
96 LinkCommand: LinkCommand;
97 }
98}