This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
command.js
122 lines (109 loc) · 3.59 KB
/
command.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/**
* @module core/command
*/
import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
import mix from '@ckeditor/ckeditor5-utils/src/mix';
/**
* The base class for CKEditor commands.
*
* Commands are the main way to manipulate editor contents and state. They are mostly used by UI elements (or by other
* commands) to make changes in the model. Commands are available in every part of code that has access to
* the {@link module:core/editor/editor~Editor editor} instance.
*
* Instances of registered commands can be retrieved from {@link module:core/editor/editor~Editor#commands}.
* The easiest way to execute a command is through {@link module:core/editor/editor~Editor#execute}.
*
* @mixes module:utils/observablemixin~ObservableMixin
*/
export default class Command {
/**
* Creates a new `Command` instance.
*
* @param {module:core/editor/editor~Editor} editor Editor on which this command will be used.
*/
constructor( editor ) {
/**
* The editor on which this command will be used.
*
* @readonly
* @member {module:core/editor/editor~Editor}
*/
this.editor = editor;
/**
* The value of a command. Concrete command class should define what it represents.
*
* For example, the `bold` command's value is whether the selection starts in a bolded text.
* And the value of the `link` command may be an object with links details.
*
* It's possible for a command to have no value (e.g. for stateless actions such as `uploadImage`).
*
* @observable
* @readonly
* @member #value
*/
this.set( 'value', undefined );
/**
* Flag indicating whether a command is enabled or disabled.
* A disabled command should do nothing when executed.
*
* @observable
* @readonly
* @member {Boolean} #isEnabled
*/
this.set( 'isEnabled', false );
this.decorate( 'execute' );
// By default every command is refreshed when changes are applied to the model.
this.listenTo( this.editor.document, 'changesDone', () => {
this.refresh();
} );
this.on( 'execute', evt => {
if ( !this.isEnabled ) {
evt.stop();
}
}, { priority: 'high' } );
}
/**
* Refreshes the command. The command should update its {@link #isEnabled} and {@link #value} property
* in this method.
*
* This method is automatically called when
* {@link module:engine/model/document~Document#event:changesDone any changes are applied to the model}.
*/
refresh() {
this.isEnabled = true;
}
/**
* Executes the command.
*
* A command may accept parameters. They will be passed from {@link module:core/editor/editor~Editor#execute}
* to the command.
*
* The `execute()` method will automatically abort when the command is disabled ({@link #isEnabled} is `false`).
* This behavior is implemented by a high priority listener to the {@link #event:execute} event.
*
* @fires execute
*/
execute() {}
/**
* Destroys the command.
*/
destroy() {
this.stopListening();
}
/**
* Event fired by the {@link #execute} method. The command action is a listener to this event so it's
* possible to change/cancel the behavior of the command by listening to this event.
*
* See {@link module:utils/observablemixin~ObservableMixin.decorate} for more information and samples.
*
* **Note:** This event is fired even if command is disabled. However, it is automatically blocked
* by a high priority listener in order to prevent command execution.
*
* @event execute
*/
}
mix( Command, ObservableMixin );