Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/ve.ui.Sequence.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface Sequence class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Key sequence.
9 *
10 * @class
11 *
12 * @constructor
13 * @param {string} name Symbolic name
14 * @param {string} commandName Command name this sequence executes
15 * @param {string|Array|RegExp} data Data to match. String, linear data array, or regular expression.
16 * When using a RegularExpression always match the end of the sequence with a '$' so that
17 * only sequences next to the user's cursor match.
18 * @param {number} [strip=0] Number of data elements to strip after execution
19 * (from the right)
20 * @param {Object} [config] [description]
21 * @param {boolean} [config.setSelection=false] Whether to set the selection to the
22 * range matching the sequence before executing the command.
23 * @param {boolean} [config.delayed=false] Whether to wait for the user to stop typing matching content
24 * before executing the command. When the sequence matches typed text, it will not be executed
25 * immediately, but only after more non-matching text is added afterwards or the selection is
26 * changed. This is useful for variable-length sequences (defined with RegExps).
27 * @param {boolean} [config.checkOnPaste=false] Whether the sequence should also be matched after paste.
28 * @param {boolean} [config.checkOnDelete=false] Whether the sequence should also be matched after delete.
29 */
30 ve.ui.Sequence = function VeUiSequence( name, commandName, data, strip, config ) {
31 this.name = name;
32 this.commandName = commandName;
33 this.data = data;
34 this.strip = strip || 0;
35 if ( typeof config === 'object' ) {
36 // TODO: Add `config = config || {};` when variadic fallback is dropped.
37 this.setSelection = !!config.setSelection;
38 this.delayed = !!config.delayed;
39 this.checkOnPaste = !!config.checkOnPaste;
40 this.checkOnDelete = !!config.checkOnDelete;
41 } else {
42 // Backwards compatibility with variadic arguments
43 this.setSelection = !!arguments[ 4 ];
44 this.delayed = !!arguments[ 5 ];
45 this.checkOnPaste = !!arguments[ 6 ];
46 this.checkOnDelete = !!arguments[ 7 ];
47 }
48 };
49
50 /* Inheritance */
51
52 OO.initClass( ve.ui.Sequence );
53
54 /* Methods */
55
56 /**
57 * Check if the sequence matches a given offset in the data
58 *
59 * @param {ve.dm.ElementLinearData} data String or linear data
60 * @param {number} offset
61 * @param {string} plaintext Plain text of data
62 * @return {ve.Range|null} Range corresponding to the match, or else null
63 */
64 ve.ui.Sequence.prototype.match = function ( data, offset, plaintext ) {
65 let i, j = offset - 1;
66
67 if ( this.data instanceof RegExp ) {
68 i = plaintext.search( this.data );
69 return ( i < 0 ) ? null :
70 new ve.Range( offset - plaintext.length + i, offset );
71 }
72 for ( i = this.data.length - 1; i >= 0; i--, j-- ) {
73 if ( typeof this.data[ i ] === 'string' ) {
74 if ( this.data[ i ] !== data.getCharacterData( j ) ) {
75 return null;
76 }
77 } else if ( !ve.compare( this.data[ i ], data.getData( j ), true ) ) {
78 return null;
79 }
80 }
81 return new ve.Range( offset - this.data.length, offset );
82 };
83
84 /**
85 * Execute the command associated with the sequence
86 *
87 * @param {ve.ui.Surface} surface
88 * @param {ve.Range} range Range to set
89 * @return {boolean} The command executed
90 */
91 ve.ui.Sequence.prototype.execute = function ( surface, range ) {
92 const surfaceModel = surface.getModel();
93
94 if ( surface.getCommands().indexOf( this.getCommandName() ) === -1 ) {
95 return false;
96 }
97
98 const command = surface.commandRegistry.lookup( this.getCommandName() );
99
100 if ( !command ) {
101 return false;
102 }
103
104 let stripFragment;
105 if ( this.strip ) {
106 const stripRange = surfaceModel.getSelection().getRange();
107 stripFragment = surfaceModel.getLinearFragment(
108 // noAutoSelect = true, excludeInsertions = true
109 new ve.Range( stripRange.end, stripRange.end - this.strip ), true, true
110 );
111 }
112
113 surfaceModel.breakpoint();
114
115 // Use SurfaceFragment rather than Selection to automatically adjust the selection for any changes
116 // (additions, removals) caused by executing the command
117 const originalSelectionFragment = surfaceModel.getFragment();
118 if ( this.setSelection ) {
119 surfaceModel.setLinearSelection( range );
120 }
121
122 let args;
123 // For sequences that trigger dialogs, pass an extra flag so the window knows
124 // to un-strip the sequence if it is closed without action. See ve.ui.WindowAction.
125 if ( command.getAction() === 'window' && command.getMethod() === 'open' ) {
126 args = ve.copy( command.args );
127 args[ 1 ] = args[ 1 ] || {};
128 args[ 1 ].strippedSequence = !!this.strip;
129 }
130
131 if ( stripFragment ) {
132 // Strip the typed text. This will be undone if the action triggered was
133 // window/open and the window is dismissed
134 stripFragment.removeContent();
135 }
136
137 // `args` can be passed undefined, and the defaults will be used
138 const executed = command.execute( surface, args, 'sequence' );
139
140 // Restore user's selection if:
141 // * This sequence was not executed after all
142 // * This sequence is delayed, so it only executes after the user changed the selection
143 if ( !executed || this.delayed ) {
144 originalSelectionFragment.select();
145 }
146
147 if ( stripFragment && !executed ) {
148 surfaceModel.undo();
149 // Prevent redoing (which would remove the typed text)
150 surfaceModel.truncateUndoStack();
151 surfaceModel.emit( 'history' );
152 }
153
154 return executed;
155 };
156
157 /**
158 * Get the symbolic name of the sequence
159 *
160 * @return {string} Symbolic name
161 */
162 ve.ui.Sequence.prototype.getName = function () {
163 return this.name;
164 };
165
166 /**
167 * Get the command name which the sequence will execute
168 *
169 * @return {string} Command name
170 */
171 ve.ui.Sequence.prototype.getCommandName = function () {
172 return this.commandName;
173 };
174
175 /**
176 * Get a representation of the sequence useful for display
177 *
178 * What this means depends a bit on how the sequence was defined:
179 * - It strips out undisplayable things like the paragraph-start marker.
180 * - Regexps are just returned as a toString of the regexp.
181 *
182 * @param {boolean} explode Whether to return the message split up into some
183 * reasonable sequence of inputs required to trigger the sequence (regexps
184 * in sequences will be considered a single "input" as a toString of
185 * the regexp, because they're hard to display no matter what…)
186 * @return {string} Message for display
187 */
188 ve.ui.Sequence.prototype.getMessage = function ( explode ) {
189 let data;
190 if ( typeof this.data === 'string' ) {
191 data = this.data.split( '' );
192 } else if ( this.data instanceof RegExp ) {
193 data = [ this.data.toString() ];
194 } else {
195 data = this.data.filter( ( key ) => !ve.isPlainObject( key ) );
196 }
197 return explode ? data : data.join( '' );
198 };