Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor DebugBar class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Debug bar
9 *
10 * @class
11 * @extends OO.ui.Element
12 *
13 * @constructor
14 * @param {ve.ui.Surface} surface Surface to debug
15 * @param {Object} [config] Configuration options
16 */
17 ve.ui.DebugBar = function VeUiDebugBar( surface, config ) {
18 // Parent constructor
19 ve.ui.DebugBar.super.call( this, config );
20
21 this.surface = surface;
22
23 this.$commands = $( '<div>' ).addClass( 've-ui-debugBar-commands' );
24 this.$linmodData = $( '<td>' ).addClass( 've-ui-debugBar-dump-linmod-data' );
25 this.$viewTree = $( '<td>' ).addClass( 've-ui-debugBar-view-tree' );
26 this.$modelTree = $( '<td>' ).addClass( 've-ui-debugBar-model-tree' );
27
28 const closeButton = new OO.ui.ButtonWidget( {
29 icon: 'close',
30 label: ve.msg( 'visualeditor-debugbar-close' )
31 } );
32
33 // Widgets
34 this.selectionLabel = new OO.ui.LabelWidget( { classes: [ 've-ui-debugBar-selectionLabel' ] } );
35
36 this.logRangeButton = new OO.ui.ButtonWidget( { label: ve.msg( 'visualeditor-debugbar-logrange' ), disabled: true } );
37 this.showModelToggle = new OO.ui.ToggleButtonWidget( { label: ve.msg( 'visualeditor-debugbar-showmodel' ) } );
38 this.updateModelToggle = new OO.ui.ToggleButtonWidget( { label: ve.msg( 'visualeditor-debugbar-updatemodel' ) } );
39 this.transactionsToggle = new OO.ui.ToggleButtonWidget( { label: ve.msg( 'visualeditor-debugbar-showtransactions' ) } );
40 this.testSquasherToggle = new OO.ui.ToggleButtonWidget( { label: ve.msg( 'visualeditor-debugbar-testsquasher' ) } );
41 this.inputDebuggingToggle = new OO.ui.ToggleButtonWidget( { label: ve.msg( 'visualeditor-debugbar-inputdebug' ) } ).setValue( ve.inputDebug );
42 this.filibusterToggle = new OO.ui.ToggleButtonWidget( { label: ve.msg( 'visualeditor-debugbar-startfilibuster' ) } );
43
44 this.$dump =
45 $( '<div>' ).addClass( 've-ui-debugBar-dump' ).append(
46 this.updateModelToggle.$element,
47 $( '<table>' ).append(
48 $( '<thead>' ).append(
49 $( '<th>' ).text( 'Linear model data' ),
50 $( '<th>' ).text( 'View tree' ),
51 $( '<th>' ).text( 'Model tree' )
52 ),
53 $( '<tbody>' ).append(
54 $( '<tr>' ).append(
55 this.$linmodData, this.$viewTree, this.$modelTree
56 )
57 )
58 )
59 ).addClass( 'oo-ui-element-hidden' );
60
61 this.$transactions = $( '<div>' ).addClass( 've-ui-debugBar-transactions' );
62
63 this.$filibuster = $( '<div>' ).addClass( [ 've-ui-debugBar-filibuster', 'oo-ui-element-hidden' ] );
64
65 // Events
66 this.logRangeButton.on( 'click', this.onLogRangeButtonClick.bind( this ) );
67 this.showModelToggle.on( 'change', this.onShowModelToggleChange.bind( this ) );
68 this.updateModelToggle.on( 'change', this.onUpdateModelToggleChange.bind( this ) );
69 this.inputDebuggingToggle.on( 'change', this.onInputDebuggingToggleChange.bind( this ) );
70 this.filibusterToggle.on( 'click', this.onFilibusterToggleClick.bind( this ) );
71 this.transactionsToggle.on( 'change', this.onTransactionsToggleChange.bind( this ) );
72 this.testSquasherToggle.on( 'change', this.onTestSquasherToggleChange.bind( this ) );
73 closeButton.on( 'click', this.$element.remove.bind( this.$element ) );
74
75 this.onHistoryDebounced = ve.debounce( this.onHistory.bind( this ) );
76
77 this.getSurface().getModel().connect( this, {
78 select: 'onSurfaceSelect',
79 history: 'onHistoryDebounced'
80 } );
81 this.onSurfaceSelect( this.getSurface().getModel().getSelection() );
82
83 this.$element.addClass( 've-ui-debugBar' );
84 this.$element.append(
85 this.$commands.append(
86 this.selectionLabel.$element,
87 this.logRangeButton.$element,
88 $( this.constructor.static.dividerTemplate ),
89 this.showModelToggle.$element,
90 this.inputDebuggingToggle.$element,
91 this.filibusterToggle.$element,
92 this.transactionsToggle.$element,
93 this.testSquasherToggle.$element,
94 $( this.constructor.static.dividerTemplate ),
95 closeButton.$element
96 ),
97 this.$dump,
98 this.$transactions,
99 this.$filibuster
100 );
101
102 this.target = null;
103 };
104
105 /* Inheritance */
106
107 OO.inheritClass( ve.ui.DebugBar, OO.ui.Element );
108
109 /**
110 * Divider HTML template
111 *
112 * @property {string}
113 */
114 ve.ui.DebugBar.static.dividerTemplate = '<span class="ve-ui-debugBar-commands-divider">&nbsp;</span>';
115
116 /**
117 * Get surface the debug bar is attached to
118 *
119 * @return {ve.ui.Surface|null}
120 */
121 ve.ui.DebugBar.prototype.getSurface = function () {
122 return this.surface;
123 };
124
125 /**
126 * Handle select events on the attached surface
127 *
128 * @param {ve.dm.Selection} selection
129 */
130 ve.ui.DebugBar.prototype.onSurfaceSelect = function () {
131 // Do not trust the emitted selection: nested emits can invalidate it. See T145938.
132 const selection = this.surface.model.getSelection();
133 this.selectionLabel.setLabel( selection.getDescription() );
134 this.logRangeButton.setDisabled( !(
135 ( selection instanceof ve.dm.LinearSelection && !selection.isCollapsed() ) ||
136 selection instanceof ve.dm.TableSelection
137 ) );
138 };
139
140 /**
141 * Handle history events on the attached surface
142 */
143 ve.ui.DebugBar.prototype.onHistory = function () {
144 if ( this.transactionsToggle.getValue() ) {
145 this.updateTransactions();
146 }
147 };
148
149 /**
150 * Handle click events on the log range button
151 *
152 * @param {jQuery.Event} e
153 */
154 ve.ui.DebugBar.prototype.onLogRangeButtonClick = function () {
155 const selection = this.getSurface().getModel().getSelection(),
156 documentModel = this.getSurface().getModel().getDocument();
157 if ( selection instanceof ve.dm.LinearSelection || selection instanceof ve.dm.TableSelection ) {
158 const ranges = selection.getRanges( documentModel );
159 for ( let i = 0; i < ranges.length; i++ ) {
160 ve.dir( this.getSurface().view.documentView.model.data.slice( ranges[ i ].start, ranges[ i ].end ) );
161 }
162 }
163 };
164
165 /**
166 * Handle change events on the show model toggle
167 *
168 * @param {boolean} value
169 */
170 ve.ui.DebugBar.prototype.onShowModelToggleChange = function ( value ) {
171 if ( value ) {
172 this.updateDump();
173 } else {
174 this.updateModelToggle.setValue( false );
175 }
176 this.$dump.toggleClass( 'oo-ui-element-hidden', !value );
177 };
178
179 /**
180 * Update the model dump
181 */
182 ve.ui.DebugBar.prototype.updateDump = function () {
183 const surface = this.getSurface(),
184 documentModel = surface.getModel().getDocument(),
185 documentView = surface.getView().getDocument();
186
187 // Linear model dump
188 const $linmodData = this.generateListFromLinearData( documentModel.data );
189 this.$linmodData.empty().append( $linmodData );
190 const $modelTree = this.generateListFromNode( documentModel.getDocumentNode() );
191 this.$modelTree.empty().append( $modelTree );
192 const $viewTree = this.generateListFromNode( documentView.getDocumentNode() );
193 this.$viewTree.empty().append( $viewTree );
194 };
195
196 /**
197 * Get an ordered list representation of some linear data
198 *
199 * @param {ve.dm.ElementLinearData} linearData Linear data
200 * @return {jQuery} Ordered list
201 */
202 ve.ui.DebugBar.prototype.generateListFromLinearData = function ( linearData ) {
203 const $ol = $( '<ol>' ).attr( 'start', '0' ),
204 data = linearData.data;
205
206 let $chunk, prevType, prevAnnotations, $annotations;
207 for ( let i = 0; i < data.length; i++ ) {
208 const $label = $( '<span>' );
209 const element = data[ i ];
210 let annotations = null;
211 let text;
212 if ( element.type ) {
213 $label.addClass( 've-ui-debugBar-dump-element' );
214 text = element.type;
215 annotations = element.annotations;
216 } else if ( Array.isArray( element ) ) {
217 $label.addClass( 've-ui-debugBar-dump-achar' );
218 text = element[ 0 ];
219 annotations = element[ 1 ];
220 } else {
221 $label.addClass( 've-ui-debugBar-dump-char' );
222 text = element;
223 }
224
225 $label.text( /\S/.test( text ) ? text : '\u00a0' );
226
227 if ( $chunk && !prevType && !element.type && OO.compare( prevAnnotations, annotations ) ) {
228 // This is a run of text with identical annotations. Continue current chunk.
229 $chunk.append( $label );
230 } else {
231 // End current chunk, if any.
232 if ( $chunk ) {
233 if ( $annotations ) {
234 $chunk.append( $annotations );
235 }
236 $ol.append( $chunk );
237 $chunk = null;
238 $annotations = null;
239 }
240
241 // Begin a new chunk
242 $chunk = $( '<li>' ).attr( 'value', i );
243 $chunk.append( $label );
244 if ( annotations ) {
245 $annotations = $( '<span>' ).addClass( 've-ui-debugBar-dump-note' ).text(
246 '[' + this.getSurface().getModel().getDocument().getStore().values( annotations ).map( ( ann ) => JSON.stringify( ann.getComparableObject() ) ).join( ', ' ) + ']'
247 );
248 }
249 }
250
251 prevType = element.type;
252 prevAnnotations = annotations;
253 }
254
255 // End current chunk, if any.
256 if ( $chunk ) {
257 if ( $annotations ) {
258 $chunk.append( $annotations );
259 }
260 $ol.append( $chunk );
261 }
262
263 return $ol;
264 };
265
266 /**
267 * Generate an ordered list describing a node
268 *
269 * @param {ve.Node} node
270 * @return {jQuery} Ordered list
271 */
272 ve.ui.DebugBar.prototype.generateListFromNode = function ( node ) {
273 const $ol = $( '<ol>' ).attr( 'start', '0' );
274
275 for ( let i = 0; i < node.children.length; i++ ) {
276 const $li = $( '<li>' );
277 const $label = $( '<span>' ).addClass( 've-ui-debugBar-dump-element' );
278 const $note = $( '<span>' ).addClass( 've-ui-debugBar-dump-note' );
279 if ( node.children[ i ].length !== undefined ) {
280 $li.append(
281 $label.text( node.children[ i ].type ),
282 $note.text( '(' + node.children[ i ].length + ')' )
283 );
284 } else {
285 $li.append( $label.text( node.children[ i ].type ) );
286 }
287
288 if ( node.children[ i ].children ) {
289 const $sublist = this.generateListFromNode( node.children[ i ] );
290 $li.append( $sublist );
291 }
292
293 $ol.append( $li );
294 }
295 return $ol;
296 };
297
298 /**
299 * Handle change events on the update model toggle button
300 *
301 * @param {boolean} value
302 */
303 ve.ui.DebugBar.prototype.onUpdateModelToggleChange = function ( value ) {
304 if ( value ) {
305 this.updateDump();
306 this.getSurface().model.connect( this, { documentUpdate: 'updateDump' } );
307 } else {
308 this.getSurface().model.disconnect( this, { documentUpdate: 'updateDump' } );
309 }
310 };
311
312 /**
313 * Handle click events on the input debugging toggle button
314 *
315 * @param {boolean} value
316 */
317 ve.ui.DebugBar.prototype.onInputDebuggingToggleChange = function ( value ) {
318 const surfaceModel = this.getSurface().getModel(),
319 selection = surfaceModel.getSelection();
320
321 ve.inputDebug = value;
322
323 // Clear the cursor before rebuilding, it will be restored later
324 surfaceModel.setNullSelection();
325 setTimeout( () => {
326 surfaceModel.getDocument().rebuildTree();
327 surfaceModel.setSelection( selection );
328 } );
329 };
330
331 /**
332 * Handle click events on the filibuster toggle button
333 *
334 * @param {jQuery.Event} e
335 */
336 ve.ui.DebugBar.prototype.onFilibusterToggleClick = function () {
337 const value = this.filibusterToggle.getValue();
338 if ( value ) {
339 this.filibusterToggle.setLabel( ve.msg( 'visualeditor-debugbar-stopfilibuster' ) );
340 this.$filibuster.off( 'click' );
341 this.$filibuster.empty();
342 ve.initFilibuster();
343 ve.filibuster.start();
344 } else {
345 ve.filibuster.stop();
346 // eslint-disable-next-line no-jquery/no-html
347 this.$filibuster.html( ve.filibuster.getObservationsHtml() );
348 this.$filibuster.on( 'click', ( e ) => {
349 const $li = $( e.target ).closest( '.ve-filibuster-frame' );
350
351 // eslint-disable-next-line no-jquery/no-class-state
352 if ( $li.hasClass( 've-filibuster-frame-expandable' ) ) {
353 $li.removeClass( 've-filibuster-frame-expandable' );
354 const path = $li.data( 've-filibuster-frame' );
355 if ( !path ) {
356 return;
357 }
358 $li.children( 'span' ).replaceWith(
359 $( ve.filibuster.getObservationsHtml( path ) )
360 );
361 // eslint-disable-next-line no-jquery/no-class-state
362 $li.toggleClass( 've-filibuster-frame-expanded' );
363 } else if ( $li.children( 'ul' ).length ) {
364 // eslint-disable-next-line no-jquery/no-class-state
365 $li.toggleClass( 've-filibuster-frame-collapsed' );
366 // eslint-disable-next-line no-jquery/no-class-state
367 $li.toggleClass( 've-filibuster-frame-expanded' );
368 }
369 } );
370 this.filibusterToggle.setLabel( ve.msg( 'visualeditor-debugbar-startfilibuster' ) );
371 }
372 this.$filibuster.toggleClass( 'oo-ui-element-hidden', !!value );
373 };
374
375 /**
376 * Handle click events on the filibuster toggle button
377 *
378 * @param {boolean} value
379 */
380 ve.ui.DebugBar.prototype.onTransactionsToggleChange = function ( value ) {
381 if ( value ) {
382 this.updateTransactions();
383 }
384 this.$transactions.toggleClass( 'oo-ui-element-hidden', !value );
385 };
386
387 /**
388 * Handle click events on the test squasher toggle button
389 *
390 * @param {boolean} value
391 */
392 ve.ui.DebugBar.prototype.onTestSquasherToggleChange = function ( value ) {
393 const doc = this.getSurface().getModel().getDocument();
394 if ( value ) {
395 doc.connect( this, { transact: 'testSquasher' } );
396 this.testSquasher();
397 } else {
398 doc.disconnect( this, { transact: 'testSquasher' } );
399 }
400 };
401
402 /**
403 * Update the transaction dump
404 */
405 ve.ui.DebugBar.prototype.updateTransactions = function () {
406 const surface = this.getSurface(),
407 $transactionsList = $( '<ol>' );
408
409 surface.getModel().getHistory().forEach( ( item ) => {
410 const $state = $( '<ol>' ).appendTo( $( '<li>' ).appendTo( $transactionsList ) );
411 item.transactions.forEach( ( tx ) => {
412 $state.append( $( '<li>' ).text( ve.summarizeTransaction( tx ) ) );
413 } );
414 } );
415
416 this.$transactions.empty().append( $transactionsList );
417 };
418
419 ve.ui.DebugBar.prototype.testSquasher = function () {
420 function squashTransactions( txs ) {
421 return new ve.dm.Change(
422 0,
423 txs.map( ( tx ) => tx.clone() ),
424 txs.map( () => new ve.dm.HashValueStore() ),
425 {}
426 ).squash().txs;
427 }
428
429 const transactions = this.getSurface().getModel().getDocument().completeHistory.transactions;
430 if ( transactions.length < 3 ) {
431 // Nothing interesting here
432 return;
433 }
434
435 const squashed = squashTransactions( transactions );
436 for ( let i = 1, iLen = transactions.length - 1; i < iLen; i++ ) {
437 const squashedBefore = squashTransactions( transactions.slice( 0, i ) );
438 const squashedAfter = squashTransactions( transactions.slice( i ) );
439 const doubleSquashed = squashTransactions( [].concat(
440 squashedBefore,
441 squashedAfter
442 ) );
443 const dump = JSON.stringify( squashed );
444 const doubleDump = JSON.stringify( doubleSquashed );
445 if ( dump !== doubleDump ) {
446 throw new Error( 'Discrepancy splitting at i=' + i );
447 }
448 }
449 };
450
451 /**
452 * Destroy the debug bar
453 */
454 ve.ui.DebugBar.prototype.destroy = function () {
455 this.getSurface().getModel().disconnect();
456 this.$element.remove();
457 };