Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/ve.ui.MWExtensionWindow.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MWExtensionWindow class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * Mixin for windows for editing generic MediaWiki extensions.
10 *
11 * @class
12 * @abstract
13 *
14 * @constructor
15 * @param {Object} [config] Configuration options
16 */
17 ve.ui.MWExtensionWindow = function VeUiMWExtensionWindow() {
18 this.whitespace = null;
19 this.input = null;
20 this.originalMwData = null;
21
22 this.onChangeHandler = ve.debounce( this.onChange.bind( this ) );
23 };
24
25 /* Inheritance */
26
27 OO.initClass( ve.ui.MWExtensionWindow );
28
29 /* Static properties */
30
31 /**
32 * Extension is allowed to have empty contents
33 *
34 * @static
35 * @property {boolean}
36 * @inheritable
37 */
38 ve.ui.MWExtensionWindow.static.allowedEmpty = false;
39
40 /**
41 * Tell Parsoid to self-close tags when the body is empty
42 *
43 * i.e. `<foo></foo>` -> `<foo/>`
44 *
45 * @static
46 * @property {boolean}
47 * @inheritable
48 */
49 ve.ui.MWExtensionWindow.static.selfCloseEmptyBody = false;
50
51 /**
52 * Inspector's directionality, 'ltr' or 'rtl'
53 *
54 * Leave as null to use the directionality of the current fragment.
55 *
56 * @static
57 * @property {string|null}
58 * @inheritable
59 */
60 ve.ui.MWExtensionWindow.static.dir = null;
61
62 /* Methods */
63
64 /**
65 * @inheritdoc OO.ui.Window
66 */
67 ve.ui.MWExtensionWindow.prototype.initialize = function () {
68 this.input = new ve.ui.WhitespacePreservingTextInputWidget( {
69 limit: 1,
70 classes: [ 've-ui-mwExtensionWindow-input' ]
71 } );
72 };
73
74 /**
75 * Get the placeholder text for the content input area.
76 *
77 * @return {string} Placeholder text
78 */
79 ve.ui.MWExtensionWindow.prototype.getInputPlaceholder = function () {
80 return '';
81 };
82
83 /**
84 * @inheritdoc OO.ui.Window
85 */
86 ve.ui.MWExtensionWindow.prototype.getSetupProcess = function ( data, process ) {
87 data = data || {};
88 return process.next( () => {
89 // Initialization
90 this.whitespace = [ '', '' ];
91
92 if ( this.selectedNode ) {
93 const mwData = this.selectedNode.getAttribute( 'mw' );
94 // mwData.body can be null in <selfclosing/> extensions
95 this.input.setValueAndWhitespace( ( mwData.body && mwData.body.extsrc ) || '' );
96 this.originalMwData = mwData;
97 } else {
98 if ( !this.constructor.static.modelClasses[ 0 ].static.isContent ) {
99 // New nodes should use linebreaks for blocks
100 this.input.setWhitespace( [ '\n', '\n' ] );
101 }
102 this.input.setValue( '' );
103 }
104
105 this.input.$input.attr( 'placeholder', this.getInputPlaceholder() );
106
107 const dir = this.constructor.static.dir || data.dir;
108 this.input.setDir( dir );
109 this.input.setReadOnly( this.isReadOnly() );
110
111 this.actions.setAbilities( { done: false } );
112 this.input.connect( this, { change: 'onChangeHandler' } );
113 } );
114 };
115
116 /**
117 * @inheritdoc OO.ui.Window
118 */
119 ve.ui.MWExtensionWindow.prototype.getReadyProcess = function ( data, process ) {
120 return process;
121 };
122
123 /**
124 * @inheritdoc OO.ui.Window
125 */
126 ve.ui.MWExtensionWindow.prototype.getTeardownProcess = function ( data, process ) {
127 return process.next( () => {
128 // Don't hold on to the original data, it's only refreshed on setup for existing nodes
129 this.originalMwData = null;
130 this.input.disconnect( this, { change: 'onChangeHandler' } );
131 } );
132 };
133
134 /**
135 * @inheritdoc OO.ui.Dialog
136 */
137 ve.ui.MWExtensionWindow.prototype.getActionProcess = function ( action, process ) {
138 return process.first( () => {
139 if ( action === 'done' ) {
140 if ( this.constructor.static.allowedEmpty || this.input.getValue() !== '' ) {
141 this.insertOrUpdateNode();
142 } else if ( this.selectedNode && !this.constructor.static.allowedEmpty ) {
143 // Content has been emptied on a node which isn't allowed to
144 // be empty, so delete it.
145 this.removeNode();
146 }
147 }
148 } );
149 };
150
151 /**
152 * Handle change event.
153 */
154 ve.ui.MWExtensionWindow.prototype.onChange = function () {
155 this.updateActions();
156 };
157
158 /**
159 * Update the 'done' action according to whether there are changes
160 */
161 ve.ui.MWExtensionWindow.prototype.updateActions = function () {
162 this.actions.setAbilities( { done: this.isSaveable() } );
163 };
164
165 /**
166 * Check if mwData would be modified if window contents were applied.
167 * This is used to determine if it's meaningful for the user to save the
168 * contents into the document; this is likely true of newly-created elements.
169 *
170 * @return {boolean} mwData would be modified
171 */
172 ve.ui.MWExtensionWindow.prototype.isSaveable = function () {
173 let modified;
174 if ( this.originalMwData ) {
175 const mwDataCopy = ve.copy( this.originalMwData );
176 this.updateMwData( mwDataCopy );
177 modified = !ve.compare( this.originalMwData, mwDataCopy );
178 } else {
179 modified = true;
180 }
181 return modified;
182 };
183
184 /**
185 * @deprecated Moved to ve.ui.MWExtensionWindow.prototype.isSaveable
186 * @return {boolean} mwData would be modified
187 */
188 ve.ui.MWExtensionWindow.prototype.isModified = ve.ui.MWExtensionWindow.prototype.isSaveable;
189
190 /**
191 * Check if mwData has meaningful edits. This is used to determine if it's
192 * meaningful to warn the user before closing the dialog without saving. Unlike
193 * `isModified()` above, we consider a newly-created but unmodified element to
194 * be non-meaningful because the user can simply re-open the dialog to restore
195 * their state.
196 *
197 * @return {boolean} mwData would contain new user input
198 */
199 ve.ui.MWExtensionWindow.prototype.hasMeaningfulEdits = function () {
200 let mwDataBaseline;
201 if ( this.originalMwData ) {
202 mwDataBaseline = this.originalMwData;
203 } else {
204 mwDataBaseline = this.getNewElement().attributes.mw;
205 }
206 const mwDataCopy = ve.copy( mwDataBaseline );
207 this.updateMwData( mwDataCopy );
208
209 // We have some difficulty here. `updateMwData()` in this class calls on
210 // `this.input.getValueAndWhitespace()`. The 'and whitespace' means that
211 // we cannot directly compare a new element's mwData with a newly-opened
212 // dialog's mwData because it may have additional newlines.
213 // We don't want to touch `this.input` or `prototype.updateMwData` because
214 // they're overridden in subclasses. Therefore, we consider whitespace-only
215 // changes to a new element to be non-meaningful too.
216 const changed = OO.getProp( mwDataCopy, 'body', 'extsrc' );
217 if ( changed !== undefined ) {
218 OO.setProp( mwDataCopy, 'body', 'extsrc', changed.trim() );
219 }
220
221 // Also trim the baseline. In "edit" mode we likely have added whitespace,
222 // and in "insert" mode we don't want to break if the default value starts
223 // or ends with whitespace.
224 const baselineChanged = OO.getProp( mwDataBaseline, 'body', 'extsrc' );
225 if ( baselineChanged !== undefined ) {
226 OO.setProp( mwDataBaseline, 'body', 'extsrc', baselineChanged.trim() );
227 }
228
229 return !ve.compare( mwDataBaseline, mwDataCopy );
230 };
231
232 /**
233 * Create an new data element for the model class associated with this inspector
234 *
235 * @return {Object} Element data
236 */
237 ve.ui.MWExtensionWindow.prototype.getNewElement = function () {
238 // Extension inspectors which create elements should either match
239 // a single modelClass or override this method.
240 const modelClass = this.constructor.static.modelClasses[ 0 ];
241 return {
242 type: modelClass.static.name,
243 attributes: {
244 mw: {
245 name: modelClass.static.extensionName,
246 attrs: {},
247 body: {
248 extsrc: ''
249 }
250 }
251 }
252 };
253 };
254
255 /**
256 * Insert or update the node in the document model from the new values
257 */
258 ve.ui.MWExtensionWindow.prototype.insertOrUpdateNode = function () {
259 const surfaceModel = this.getFragment().getSurface();
260 if ( this.selectedNode ) {
261 const mwData = ve.copy( this.selectedNode.getAttribute( 'mw' ) );
262 this.updateMwData( mwData );
263 surfaceModel.change(
264 ve.dm.TransactionBuilder.static.newFromAttributeChanges(
265 surfaceModel.getDocument(),
266 this.selectedNode.getOuterRange().start,
267 { mw: mwData }
268 )
269 );
270 } else {
271 const element = this.getNewElement();
272 this.updateMwData( element.attributes.mw );
273 // Collapse returns a new fragment, so update this.fragment
274 this.fragment = this.getFragment().collapseToEnd();
275 this.getFragment().insertContent( [
276 element,
277 { type: '/' + element.type }
278 ] );
279 }
280 };
281
282 /**
283 * Remove the node form the document model
284 */
285 ve.ui.MWExtensionWindow.prototype.removeNode = function () {
286 this.getFragment().removeContent();
287 };
288
289 /**
290 * Update mwData object with the new values from the inspector or dialog
291 *
292 * @param {Object} mwData MediaWiki data object
293 */
294 ve.ui.MWExtensionWindow.prototype.updateMwData = function ( mwData ) {
295 const tagName = mwData.name;
296 let value = this.input.getValueAndWhitespace();
297
298 // XML-like tags in wikitext are not actually XML and don't expect their contents to be escaped.
299 // This means that it is not possible for a tag '<foo>…</foo>' to contain the string '</foo>'.
300 // Prevent that by escaping the first angle bracket '<' to '&lt;'. (T59429)
301 value = value.replace( new RegExp( '<(/' + tagName + '\\s*>)', 'gi' ), '&lt;$1' );
302
303 if ( value.trim() === '' && this.constructor.static.selfCloseEmptyBody ) {
304 delete mwData.body;
305 } else {
306 mwData.body = mwData.body || {};
307 mwData.body.extsrc = value;
308 }
309 };