1 | import { Observable } from '@ckeditor/ckeditor5-utils/src/observablemixin';
|
2 | import Editor from './editor/editor';
|
3 |
|
4 | // tslint:disable-next-line:no-empty-interface
|
5 | export default interface Command extends Observable {}
|
6 |
|
7 | /**
|
8 | * The base class for CKEditor commands.
|
9 | *
|
10 | * Commands are the main way to manipulate editor contents and state. They are mostly used by UI elements (or by other
|
11 | * commands) to make changes in the model. Commands are available in every part of code that has access to
|
12 | * the {@link module:core/editor/editor~Editor editor} instance.
|
13 | *
|
14 | * Instances of registered commands can be retrieved from {@link module:core/editor/editor~Editor#commands `editor.commands`}.
|
15 | * The easiest way to execute a command is through {@link module:core/editor/editor~Editor#execute `editor.execute()`}.
|
16 | *
|
17 | * By default, commands are disabled when the editor is in {@link module:core/editor/editor~Editor#isReadOnly read-only} mode
|
18 | * but commands with the {@link module:core/command~Command#affectsData `affectsData`} flag set to `false` will not be disabled.
|
19 | */
|
20 | export default class Command implements Observable {
|
21 | /**
|
22 | * Creates a new `Command` instance.
|
23 | */
|
24 | constructor(editor: Editor);
|
25 |
|
26 | /**
|
27 | * The editor on which this command will be used.
|
28 | */
|
29 | readonly editor: Editor;
|
30 |
|
31 | /**
|
32 | * The value of the command. A concrete command class should define what it represents for it.
|
33 | *
|
34 | * For example, the `'bold'` command's value indicates whether the selection starts in a bolded text.
|
35 | * And the value of the `'link'` command may be an object with links details.
|
36 | *
|
37 | * It is possible for a command to have no value (e.g. for stateless actions such as `'uploadImage'`).
|
38 | *
|
39 | * A concrete command class should control this value by overriding the {`refresh()`} method.
#refresh |
40 | */
|
41 | get value(): unknown;
|
42 | protected set value(val: unknown);
|
43 |
|
44 | /**
|
45 | * Flag indicating whether a command is enabled or disabled.
|
46 | * A disabled command will do nothing when executed.
|
47 | *
|
48 | * A concrete command class should control this value by overriding the {@link #refresh `refresh()`} method.
|
49 | *
|
50 | * It is possible to disable a command from "outside". For instance, in your integration you may want to disable
|
51 | * a certain set of commands for the time being. To do that, you can use the fact that `isEnabled` is observable
|
52 | * and it fires the `set:isEnabled` event every time anyone tries to modify its value:
|
53 | *
|
54 | * function disableCommand( cmd ) {
|
55 | * cmd.on( 'set:isEnabled', forceDisable, { priority: 'highest' } );
|
56 | *
|
57 | * cmd.isEnabled = false;
|
58 | *
|
59 | * // Make it possible to enable the command again.
|
60 | * return () => {
|
61 | * cmd.off( 'set:isEnabled', forceDisable );
|
62 | * cmd.refresh();
|
63 | * };
|
64 | *
|
65 | * function forceDisable( evt ) {
|
66 | * evt.return = false;
|
67 | * evt.stop();
|
68 | * }
|
69 | * }
|
70 | *
|
71 | * // Usage:
|
72 | *
|
73 | * // Disabling the command.
|
74 | * const enableBold = disableCommand( editor.commands.get( 'bold' ) );
|
75 | *
|
76 | * // Enabling the command again.
|
77 | * enableBold();
|
78 | */
|
79 | get isEnabled(): boolean;
|
80 | protected set isEnabled(value: boolean);
|
81 |
|
82 | /**
|
83 | * A flag indicating whether a command execution changes the editor data or not.
|
84 | *
|
85 | * Commands with `affectsData` set to `false` will not be automatically disabled in
|
86 | * the {@link module:core/editor/editor~Editor#isReadOnly read-only mode} and
|
87 | * {@glink features/read-only#related-features other editor modes} with restricted user write permissions.
|
88 | *
|
89 | * **Note:** You do not have to set it for your every command. It is `true` by default.
|
90 | */
|
91 | readonly affectsData: boolean;
|
92 |
|
93 | /**
|
94 | * Refreshes the command. The command should update its {@link #isEnabled} and {@link #value} properties
|
95 | * in this method.
|
96 | *
|
97 | * This method is automatically called when
|
98 | * {@link module:engine/model/document~Document#event:change any changes are applied to the document}.
|
99 | */
|
100 | refresh(): void;
|
101 |
|
102 | /**
|
103 | * Disables the command.
|
104 | *
|
105 | * Command may be disabled by multiple features or algorithms (at once). When disabling a command, unique id should be passed
|
106 | * (e.g. feature name). The same identifier should be used when {@link #clearForceDisabled enabling back} the command.
|
107 | * The command becomes enabled only after all features {@link #clearForceDisabled enabled it back}.
|
108 | *
|
109 | * Disabling and enabling a command:
|
110 | *
|
111 | * command.isEnabled; // -> true
|
112 | * command.forceDisabled( 'MyFeature' );
|
113 | * command.isEnabled; // -> false
|
114 | * command.clearForceDisabled( 'MyFeature' );
|
115 | * command.isEnabled; // -> true
|
116 | *
|
117 | * Command disabled by multiple features:
|
118 | *
|
119 | * command.forceDisabled( 'MyFeature' );
|
120 | * command.forceDisabled( 'OtherFeature' );
|
121 | * command.clearForceDisabled( 'MyFeature' );
|
122 | * command.isEnabled; // -> false
|
123 | * command.clearForceDisabled( 'OtherFeature' );
|
124 | * command.isEnabled; // -> true
|
125 | *
|
126 | * Multiple disabling with the same identifier is redundant:
|
127 | *
|
128 | * command.forceDisabled( 'MyFeature' );
|
129 | * command.forceDisabled( 'MyFeature' );
|
130 | * command.clearForceDisabled( 'MyFeature' );
|
131 | * command.isEnabled; // -> true
|
132 | *
|
133 | * **Note:** some commands or algorithms may have more complex logic when it comes to enabling or disabling certain commands,
|
134 | * so the command might be still disabled after {@link #clearForceDisabled} was used.
|
135 | */
|
136 | forceDisabled(id: string): void;
|
137 |
|
138 | /**
|
139 | * Clears forced disable previously set through {@link #forceDisabled}. See {@link #forceDisabled}.
|
140 | */
|
141 | clearForceDisabled(id: string): void;
|
142 |
|
143 | /**
|
144 | * Executes the command.
|
145 | *
|
146 | * A command may accept parameters. They will be passed from {@link module:core/editor/editor~Editor#execute `editor.execute()`}
|
147 | * to the command.
|
148 | *
|
149 | * The `execute()` method will automatically abort when the command is disabled ({@link #isEnabled} is `false`).
|
150 | * This behavior is implemented by a high priority listener to the {@link #event:execute} event.
|
151 | *
|
152 | * In order to see how to disable a command from "outside" see the {@link #isEnabled} documentation.
|
153 | *
|
154 | * This method may return a value, which would be forwarded all the way down to the
|
155 | * {@link module:core/editor/editor~Editor#execute `editor.execute()`}.
|
156 | */
|
157 | execute(...options: unknown[]): void;
|
158 |
|
159 | /**
|
160 | * Destroys the command.
|
161 | */
|
162 | destroy(): void;
|
163 | }
|