Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/dialogs/ve.ui.MWExtensionPreviewDialog.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MWExtensionPreviewDialog class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * Dialog for editing generic MediaWiki extensions with a preview.
10 *
11 * @class
12 * @abstract
13 * @extends ve.ui.MWExtensionDialog
14 *
15 * @constructor
16 * @param {Object} [config] Configuration options
17 */
18 ve.ui.MWExtensionPreviewDialog = function VeUiMWExtensionPreviewDialog() {
19 // Parent constructor
20 ve.ui.MWExtensionPreviewDialog.super.apply( this, arguments );
21
22 this.updatePreviewDebounced = ve.debounce( this.updatePreview.bind( this ), 1000 );
23 };
24
25 /* Inheritance */
26
27 OO.inheritClass( ve.ui.MWExtensionPreviewDialog, ve.ui.MWExtensionDialog );
28
29 /* Methods */
30
31 /**
32 * @inheritdoc
33 */
34 ve.ui.MWExtensionPreviewDialog.prototype.initialize = function () {
35 // Parent method
36 ve.ui.MWExtensionPreviewDialog.super.prototype.initialize.call( this );
37
38 // Properties
39 this.previewNode = null;
40 this.previewElement = new ve.ui.MWPreviewElement();
41
42 // Initialization
43 this.$element.addClass( 've-ui-mwExtensionPreviewDialog' );
44 };
45
46 /**
47 * @inheritdoc
48 */
49 ve.ui.MWExtensionPreviewDialog.prototype.getSetupProcess = function ( data ) {
50 return ve.ui.MWExtensionPreviewDialog.super.prototype.getSetupProcess.call( this, data )
51 .next( () => {
52 let element;
53 if ( this.selectedNode ) {
54 element = this.selectedNode.getClonedElement();
55 } else {
56 element = this.getNewElement();
57 }
58 const linearData = [ element, { type: '/' + element.type } ];
59 if ( ve.dm.nodeFactory.isNodeContent( element.type ) ) {
60 linearData.unshift( { type: 'paragraph' } );
61 linearData.push( { type: '/paragraph' } );
62 }
63 linearData.push(
64 { type: 'internalList' },
65 { type: '/internalList' }
66 );
67 // We assume that WindowAction pass
68 const doc = data.fragment.getDocument().cloneWithData( linearData );
69
70 const rootNode = doc.getDocumentNode().children[ 0 ];
71 this.previewNode = doc.getNodesByType( element.type )[ 0 ];
72 this.previewElement.setModel( rootNode );
73 } );
74 };
75
76 /**
77 * @inheritdoc
78 */
79 ve.ui.MWExtensionPreviewDialog.prototype.onChange = function () {
80 // Parent method
81 ve.ui.MWExtensionPreviewDialog.super.prototype.onChange.call( this );
82
83 this.updatePreviewDebounced();
84 };
85
86 /**
87 * Update the node rendering to reflect the current content in the dialog.
88 */
89 ve.ui.MWExtensionPreviewDialog.prototype.updatePreview = function () {
90 const mwData = ve.copy( this.previewNode.getAttribute( 'mw' ) ),
91 doc = this.previewNode.getDocument();
92
93 this.updateMwData( mwData );
94
95 doc.commit(
96 ve.dm.TransactionBuilder.static.newFromAttributeChanges(
97 doc, this.previewNode.getOuterRange().start, { mw: mwData }
98 )
99 );
100 this.previewElement.updatePreview();
101 };