File: ve.ui.MWExtensionPreviewDialog.js

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (101 lines) | stat: -rw-r--r-- 2,737 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*!
 * VisualEditor UserInterface MWExtensionPreviewDialog class.
 *
 * @copyright See AUTHORS.txt
 * @license The MIT License (MIT); see LICENSE.txt
 */

/**
 * Dialog for editing generic MediaWiki extensions with a preview.
 *
 * @class
 * @abstract
 * @extends ve.ui.MWExtensionDialog
 *
 * @constructor
 * @param {Object} [config] Configuration options
 */
ve.ui.MWExtensionPreviewDialog = function VeUiMWExtensionPreviewDialog() {
	// Parent constructor
	ve.ui.MWExtensionPreviewDialog.super.apply( this, arguments );

	this.updatePreviewDebounced = ve.debounce( this.updatePreview.bind( this ), 1000 );
};

/* Inheritance */

OO.inheritClass( ve.ui.MWExtensionPreviewDialog, ve.ui.MWExtensionDialog );

/* Methods */

/**
 * @inheritdoc
 */
ve.ui.MWExtensionPreviewDialog.prototype.initialize = function () {
	// Parent method
	ve.ui.MWExtensionPreviewDialog.super.prototype.initialize.call( this );

	// Properties
	this.previewNode = null;
	this.previewElement = new ve.ui.MWPreviewElement();

	// Initialization
	this.$element.addClass( 've-ui-mwExtensionPreviewDialog' );
};

/**
 * @inheritdoc
 */
ve.ui.MWExtensionPreviewDialog.prototype.getSetupProcess = function ( data ) {
	return ve.ui.MWExtensionPreviewDialog.super.prototype.getSetupProcess.call( this, data )
		.next( () => {
			let element;
			if ( this.selectedNode ) {
				element = this.selectedNode.getClonedElement();
			} else {
				element = this.getNewElement();
			}
			const linearData = [ element, { type: '/' + element.type } ];
			if ( ve.dm.nodeFactory.isNodeContent( element.type ) ) {
				linearData.unshift( { type: 'paragraph' } );
				linearData.push( { type: '/paragraph' } );
			}
			linearData.push(
				{ type: 'internalList' },
				{ type: '/internalList' }
			);
			// We assume that WindowAction pass
			const doc = data.fragment.getDocument().cloneWithData( linearData );

			const rootNode = doc.getDocumentNode().children[ 0 ];
			this.previewNode = doc.getNodesByType( element.type )[ 0 ];
			this.previewElement.setModel( rootNode );
		} );
};

/**
 * @inheritdoc
 */
ve.ui.MWExtensionPreviewDialog.prototype.onChange = function () {
	// Parent method
	ve.ui.MWExtensionPreviewDialog.super.prototype.onChange.call( this );

	this.updatePreviewDebounced();
};

/**
 * Update the node rendering to reflect the current content in the dialog.
 */
ve.ui.MWExtensionPreviewDialog.prototype.updatePreview = function () {
	const mwData = ve.copy( this.previewNode.getAttribute( 'mw' ) ),
		doc = this.previewNode.getDocument();

	this.updateMwData( mwData );

	doc.commit(
		ve.dm.TransactionBuilder.static.newFromAttributeChanges(
			doc, this.previewNode.getOuterRange().start, { mw: mwData }
		)
	);
	this.previewElement.updatePreview();
};