Debian Codegraph @

extensions/Math/modules/ve-math/ve.ui.MWLatexDialog.js

raw symbols SCIP
1 /*!
2 * VisualEditor user interface MWLatexDialog class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license MIT
6 */
7
8 /**
9 * Abstract dialog for inserting and editing different formulas
10 * provided by the Math extension.
11 *
12 * @abstract
13 * @class
14 * @extends ve.ui.MWExtensionPreviewDialog
15 *
16 * @constructor
17 * @param {Object} [config] Configuration options
18 */
19 ve.ui.MWLatexDialog = function VeUiMWLatexDialog( config ) {
20 // Parent constructor
21 ve.ui.MWLatexDialog.super.call( this, config );
22 };
23
24 /* Inheritance */
25
26 OO.inheritClass( ve.ui.MWLatexDialog, ve.ui.MWExtensionPreviewDialog );
27
28 /* Static properties */
29
30 ve.ui.MWLatexDialog.static.size = 'larger';
31
32 ve.ui.MWLatexDialog.static.dir = 'ltr';
33
34 ve.ui.MWLatexDialog.static.symbolsModule = null;
35
36 /* Methods */
37
38 /**
39 * @inheritdoc
40 */
41 ve.ui.MWLatexDialog.prototype.initialize = function () {
42 // Parent method
43 ve.ui.MWLatexDialog.super.prototype.initialize.call( this );
44
45 // Layout for the formula inserter (formula tab panel) and options form (options tab panel)
46 this.indexLayout = new OO.ui.IndexLayout();
47
48 const formulaTabPanel = new OO.ui.TabPanelLayout( 'formula', {
49 label: ve.msg( 'math-visualeditor-mwlatexdialog-card-formula' ),
50 padded: true,
51 classes: [ 'latex-dialog-formula-panel' ]
52 } );
53 const optionsTabPanel = new OO.ui.TabPanelLayout( 'options', {
54 label: ve.msg( 'math-visualeditor-mwlatexdialog-card-options' ),
55 padded: true,
56 classes: [ 'latex-dialog-options-panel' ]
57 } );
58
59 this.indexLayout.addTabPanels( [
60 formulaTabPanel,
61 optionsTabPanel
62 ] );
63
64 // Layout for symbol picker (menu) and input and preview (content)
65 this.menuLayout = new OO.ui.MenuLayout( {
66 menuPosition: 'bottom',
67 classes: [ 've-ui-mwLatexDialog-menuLayout' ]
68 } );
69
70 this.previewElement.$element.addClass(
71 've-ui-mwLatexDialog-preview'
72 );
73
74 this.input = new ve.ui.MWAceEditorWidget( {
75 rows: 1, // This will be recalculated later in onWindowManagerResize
76 autocomplete: 'live',
77 autocompleteWordList: this.constructor.static.autocompleteWordList
78 } ).setLanguage( 'latex' );
79
80 this.input.togglePrintMargin( false );
81
82 this.displaySelect = new OO.ui.ButtonSelectWidget( {
83 items: [
84 new OO.ui.ButtonOptionWidget( {
85 data: 'default',
86 icon: 'mathematicsDisplayDefault',
87 label: ve.msg( 'math-visualeditor-mwlatexinspector-display-default' )
88 } ),
89 new OO.ui.ButtonOptionWidget( {
90 data: 'inline',
91 icon: 'mathematicsDisplayInline',
92 label: ve.msg( 'math-visualeditor-mwlatexinspector-display-inline' )
93 } ),
94 new OO.ui.ButtonOptionWidget( {
95 data: 'block',
96 icon: 'mathematicsDisplayBlock',
97 label: ve.msg( 'math-visualeditor-mwlatexinspector-display-block' )
98 } )
99 ]
100 } );
101
102 this.idInput = new OO.ui.TextInputWidget();
103 this.qidInput = new mw.widgets.MathWbEntitySelector();
104
105 const inputField = new OO.ui.FieldLayout( this.input, {
106 align: 'top',
107 classes: [ 'latex-dialog-formula-field' ],
108 label: ve.msg( 'math-visualeditor-mwlatexdialog-card-formula' )
109 } );
110 const displayField = new OO.ui.FieldLayout( this.displaySelect, {
111 align: 'top',
112 classes: [ 'latex-dialog-display-field' ],
113 label: ve.msg( 'math-visualeditor-mwlatexinspector-display' )
114 } );
115 const idField = new OO.ui.FieldLayout( this.idInput, {
116 align: 'top',
117 classes: [ 'latex-dialog-id-field' ],
118 label: ve.msg( 'math-visualeditor-mwlatexinspector-id' )
119 } );
120 const qidField = new OO.ui.FieldLayout( this.qidInput, {
121 align: 'top',
122 classes: [ 'latex-dialog-qid-field' ],
123 label: ve.msg( 'math-visualeditor-mwlatexinspector-qid' )
124 } );
125
126 const formulaPanel = new OO.ui.PanelLayout( {
127 scrollable: true,
128 padded: true
129 } );
130
131 // Layout for the symbol picker
132 this.bookletLayout = new ve.ui.SymbolListBookletLayout( {
133 classes: [ 've-ui-mwLatexDialog-symbols' ]
134 } );
135 this.pages = [];
136 this.symbolsPromise = mw.loader.using( this.constructor.static.symbolsModule ).done( ( require ) => {
137 // eslint-disable-next-line security/detect-non-literal-require
138 const symbols = require( this.constructor.static.symbolsModule );
139 const symbolData = {};
140 for ( const category in symbols ) {
141 const symbolList = symbols[ category ].filter( ( symbol ) => {
142 if ( symbol.notWorking || symbol.duplicate ) {
143 return false;
144 }
145 const tex = symbol.tex || symbol.insert;
146 const classes = [ 've-ui-mwLatexDialog-symbol' ];
147 classes.push(
148 've-ui-mwLatexSymbol-' + tex.replace( /[^\w]/g, ( c ) => '_' + c.charCodeAt( 0 ) + '_' )
149 );
150 if ( symbol.width ) {
151 // The following classes are used here:
152 // * ve-ui-mwLatexDialog-symbol-wide
153 // * ve-ui-mwLatexDialog-symbol-wider
154 // * ve-ui-mwLatexDialog-symbol-widest
155 classes.push( 've-ui-mwLatexDialog-symbol-' + symbol.width );
156 }
157 if ( symbol.contain ) {
158 classes.push( 've-ui-mwLatexDialog-symbol-contain' );
159 }
160 if ( symbol.largeLayout ) {
161 classes.push( 've-ui-mwLatexDialog-symbol-largeLayout' );
162 }
163
164 // T366737 - make sure the symbols appear in night mode
165 classes.push( 'skin-invert' );
166
167 symbol.label = '';
168 symbol.classes = classes;
169
170 return true;
171 } );
172 symbolData[ category ] = {
173 // eslint-disable-next-line mediawiki/msg-doc
174 label: ve.msg( category ),
175 symbols: symbolList
176 };
177 }
178 this.bookletLayout.setSymbolData( symbolData );
179 this.bookletLayout.connect( this, {
180 choose: 'onSymbolChoose'
181 } );
182
183 // Append everything
184 formulaPanel.$element.append(
185 this.previewElement.$element,
186 inputField.$element
187 );
188 this.menuLayout.setMenuPanel( this.bookletLayout );
189 this.menuLayout.setContentPanel( formulaPanel );
190
191 formulaTabPanel.$element.append(
192 this.menuLayout.$element
193 );
194 optionsTabPanel.$element.append(
195 displayField.$element,
196 idField.$element,
197 qidField.$element
198 );
199
200 this.$body
201 .addClass( 've-ui-mwLatexDialog-content' )
202 .append( this.indexLayout.$element );
203 } );
204
205 };
206
207 /**
208 * @inheritdoc
209 */
210 ve.ui.MWLatexDialog.prototype.getSetupProcess = function ( data ) {
211 return ve.ui.MWLatexDialog.super.prototype.getSetupProcess.call( this, data )
212 .next( () => {
213 const attributes = this.selectedNode && this.selectedNode.getAttribute( 'mw' ).attrs,
214 display = attributes && attributes.display || 'default',
215 id = attributes && attributes.id || '',
216 qid = attributes && attributes.qid || '',
217 isReadOnly = this.isReadOnly();
218
219 // Populate form
220 // TODO: This widget is not readable when disabled
221 this.displaySelect.selectItemByData( display ).setDisabled( isReadOnly );
222 this.idInput.setValue( id ).setReadOnly( isReadOnly );
223 this.qidInput.setValue( qid ).setReadOnly( isReadOnly );
224
225 // Add event handlers
226 this.input.on( 'change', this.onChangeHandler );
227 this.displaySelect.on( 'choose', this.onChangeHandler );
228 this.idInput.on( 'change', this.onChangeHandler );
229 this.qidInput.on( 'change', this.onChangeHandler );
230 } );
231 };
232
233 /**
234 * @inheritdoc
235 */
236 ve.ui.MWLatexDialog.prototype.getReadyProcess = function ( data ) {
237 mw.hook( 've.ui.MwLatexDialogReadyProcess' ).fire();
238 return ve.ui.MWLatexDialog.super.prototype.getReadyProcess.call( this, data )
239 .next( () => this.symbolsPromise )
240 .next( () => {
241 // Resize the input once the dialog has been appended
242 this.input.adjustSize( true ).focus().moveCursorToEnd();
243 this.getManager().connect( this, { resize: 'onWindowManagerResize' } );
244 this.onWindowManagerResize();
245 } );
246 };
247
248 /**
249 * @inheritdoc
250 */
251 ve.ui.MWLatexDialog.prototype.getTeardownProcess = function ( data ) {
252 return ve.ui.MWLatexDialog.super.prototype.getTeardownProcess.call( this, data )
253 .first( () => {
254 this.input.off( 'change', this.onChangeHandler );
255 this.displaySelect.off( 'choose', this.onChangeHandler );
256 this.idInput.off( 'change', this.onChangeHandler );
257 this.qidInput.off( 'change', this.onChangeHandler );
258 this.getManager().disconnect( this );
259 this.indexLayout.setTabPanel( 'formula' );
260 this.indexLayout.resetScroll();
261 this.menuLayout.resetScroll();
262 this.bookletLayout.resetScroll();
263 } );
264 };
265
266 /**
267 * @inheritdoc
268 */
269 ve.ui.MWLatexDialog.prototype.updateMwData = function ( mwData ) {
270 // Parent method
271 ve.ui.MWLatexDialog.super.prototype.updateMwData.call( this, mwData );
272
273 // Get data from dialog
274 const display = this.displaySelect.findSelectedItem().getData();
275 const id = this.idInput.getValue();
276 const qid = this.qidInput.getValue();
277
278 // Update attributes
279 mwData.attrs.display = display !== 'default' ? display : undefined;
280 mwData.attrs.id = id || undefined;
281 mwData.attrs.qid = qid || undefined;
282 };
283
284 /**
285 * @inheritdoc
286 */
287 ve.ui.MWLatexDialog.prototype.getBodyHeight = function () {
288 return 600;
289 };
290
291 /**
292 * Handle the window resize event
293 */
294 ve.ui.MWLatexDialog.prototype.onWindowManagerResize = function () {
295 this.input.loadingPromise.always( () => {
296 // Toggle short mode as necessary
297 // NB a change of mode triggers a transition...
298 this.menuLayout.$element.toggleClass(
299 've-ui-mwLatexDialog-menuLayout-short', this.menuLayout.$element.height() < 450
300 );
301
302 // ...So wait for the possible menuLayout transition to finish
303 setTimeout( () => {
304 // Give the input the right number of rows to fit the space
305 const availableSpace = this.menuLayout.$content.height() - this.input.$element.position().top;
306 // TODO: Compute this line height from the skin
307 const singleLineHeight = 21;
308 const border = 1;
309 const padding = 3;
310 const borderAndPadding = 2 * ( border + padding );
311 const maxInputHeight = availableSpace - borderAndPadding;
312 const minRows = Math.floor( maxInputHeight / singleLineHeight );
313 this.input.loadingPromise.done( () => {
314 this.input.setMinRows( minRows );
315 } ).fail( () => {
316 this.input.$input.attr( 'rows', minRows );
317 } );
318 }, OO.ui.theme.getDialogTransitionDuration() );
319 } );
320 };
321
322 /**
323 * Handle a symbol being chosen from the list
324 *
325 * @param {Object} symbol
326 */
327 ve.ui.MWLatexDialog.prototype.onSymbolChoose = function ( symbol ) {
328 if ( this.isReadOnly() ) {
329 return;
330 }
331
332 const encapsulate = symbol.encapsulate;
333
334 if ( encapsulate ) {
335 const range = this.input.getRange();
336 if ( range.from === range.to ) {
337 this.input.insertContent( encapsulate.placeholder );
338 this.input.selectRange( range.from, range.from + encapsulate.placeholder.length );
339 }
340 this.input.encapsulateContent( encapsulate.pre, encapsulate.post );
341 } else {
342 const insert = symbol.insert;
343 this.input.insertContent( insert );
344 }
345 };