1 | /**
|
2 | * @license Copyright (c) 2003-2024, 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 basic-styles/attributecommand
|
7 | */
|
8 | import { Command, type Editor } from 'ckeditor5/src/core.js';
|
9 | /**
|
10 | * An extension of the base {@link module:core/command~Command} class, which provides utilities for a command
|
11 | * that toggles a single attribute on a text or an element.
|
12 | *
|
13 | * `AttributeCommand` uses {@link module:engine/model/document~Document#selection}
|
14 | * to decide which nodes (if any) should be changed, and applies or removes the attribute from them.
|
15 | *
|
16 | * The command checks the {@link module:engine/model/model~Model#schema} to decide if it can be enabled
|
17 | * for the current selection and to which nodes the attribute can be applied.
|
18 | */
|
19 | export default class AttributeCommand extends Command {
|
20 | /**
|
21 | * Flag indicating whether the command is active. The command is active when the
|
22 | * {@link module:engine/model/selection~Selection#hasAttribute selection has the attribute} which means that:
|
23 | *
|
24 | * * If the selection is not empty – That the attribute is set on the first node in the selection that allows this attribute.
|
25 | * * If the selection is empty – That the selection has the attribute itself (which means that newly typed
|
26 | * text will have this attribute, too).
|
27 | *
|
28 | * @observable
|
29 | * @readonly
|
30 | */
|
31 | value: boolean;
|
32 | /**
|
33 | * The attribute that will be set by the command.
|
34 | */
|
35 | readonly attributeKey: string;
|
36 | /**
|
37 | * @param attributeKey Attribute that will be set by the command.
|
38 | */
|
39 | constructor(editor: Editor, attributeKey: string);
|
40 | /**
|
41 | * Updates the command's { #value} and { #isEnabled} based on the current selection.
|
42 | */
|
43 | refresh(): void;
|
44 | /**
|
45 | * Executes the command – applies the attribute to the selection or removes it from the selection.
|
46 | *
|
47 | * If the command is active (`value == true`), it will remove attributes. Otherwise, it will set attributes.
|
48 | *
|
49 | * The execution result differs, depending on the {@link module:engine/model/document~Document#selection}:
|
50 | *
|
51 | * * If the selection is on a range, the command applies the attribute to all nodes in that range
|
52 | * (if they are allowed to have this attribute by the {@link module:engine/model/schema~Schema schema}).
|
53 | * * If the selection is collapsed in a non-empty node, the command applies the attribute to the
|
54 | * {@link module:engine/model/document~Document#selection} itself (note that typed characters copy attributes from the selection).
|
55 | * * If the selection is collapsed in an empty node, the command applies the attribute to the parent node of the selection (note
|
56 | * that the selection inherits all attributes from a node if it is in an empty node).
|
57 | *
|
58 | * @fires execute
|
59 | * @param options Command options.
|
60 | * @param options.forceValue If set, it will force the command behavior. If `true`,
|
61 | * the command will apply the attribute, otherwise the command will remove the attribute.
|
62 | * If not set, the command will look for its current value to decide what it should do.
|
63 | */
|
64 | execute(options?: {
|
65 | forceValue?: boolean;
|
66 | }): void;
|
67 | /**
|
68 | * Checks the attribute value of the first node in the selection that allows the attribute.
|
69 | * For the collapsed selection returns the selection attribute.
|
70 | *
|
71 | * @returns The attribute value.
|
72 | */
|
73 | private _getValueFromFirstAllowedNode;
|
74 | }
|