2015-11-18 07:25:01 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWExtensionPreviewDialog class.
|
|
|
|
*
|
2017-01-03 16:58:33 +00:00
|
|
|
* @copyright 2011-2017 VisualEditor Team and others; see AUTHORS.txt
|
2015-11-18 07:25:01 +00:00
|
|
|
* @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 );
|
|
|
|
|
2017-01-14 23:03:36 +00:00
|
|
|
this.updatePreviewDebounced = ve.debounce( this.updatePreview.bind( this ), 250 );
|
2015-11-18 07:25:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* 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;
|
2015-11-23 22:28:57 +00:00
|
|
|
this.previewElement = new ve.ui.MWPreviewElement();
|
2015-11-18 07:25:01 +00:00
|
|
|
|
|
|
|
// 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( function () {
|
2016-07-26 15:06:07 +00:00
|
|
|
var doc, element, rootNode, linearData;
|
2015-11-18 07:25:01 +00:00
|
|
|
if ( this.selectedNode ) {
|
2016-07-26 15:06:07 +00:00
|
|
|
element = this.selectedNode.getClonedElement();
|
2015-11-18 07:25:01 +00:00
|
|
|
} else {
|
|
|
|
element = this.getNewElement();
|
|
|
|
}
|
2016-07-26 15:06:07 +00:00
|
|
|
linearData = [ element, { type: '/' + element.type } ];
|
|
|
|
if ( ve.dm.nodeFactory.isNodeContent( element.type ) ) {
|
|
|
|
linearData = [ { type: 'paragraph' } ].concat( linearData, { type: '/paragraph' } );
|
|
|
|
}
|
|
|
|
// We assume that WindowAction pass
|
|
|
|
doc = data.fragment.getDocument().cloneWithData( linearData.concat( [
|
|
|
|
{ type: 'internalList' },
|
|
|
|
{ type: '/internalList' }
|
|
|
|
] ) );
|
|
|
|
|
|
|
|
rootNode = doc.getDocumentNode().children[ 0 ];
|
|
|
|
this.previewNode = doc.getNodesByType( element.type )[ 0 ];
|
|
|
|
this.previewElement.setModel( rootNode );
|
2015-11-18 07:25:01 +00:00
|
|
|
}, this );
|
|
|
|
};
|
|
|
|
|
2017-01-14 23:03:36 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionPreviewDialog.prototype.onChange = function () {
|
|
|
|
// Parent method
|
|
|
|
ve.ui.MWExtensionPreviewDialog.super.prototype.onChange.call( this );
|
|
|
|
|
|
|
|
this.updatePreviewDebounced();
|
|
|
|
};
|
|
|
|
|
2015-11-18 07:25:01 +00:00
|
|
|
/**
|
|
|
|
* Update the node rendering to reflect the current content in the dialog.
|
|
|
|
*/
|
|
|
|
ve.ui.MWExtensionPreviewDialog.prototype.updatePreview = function () {
|
|
|
|
var mwData = ve.copy( this.previewNode.getAttribute( 'mw' ) ),
|
|
|
|
doc = this.previewNode.getDocument();
|
|
|
|
|
|
|
|
this.updateMwData( mwData );
|
|
|
|
|
|
|
|
doc.commit(
|
2016-11-30 22:03:03 +00:00
|
|
|
ve.dm.TransactionBuilder.static.newFromAttributeChanges(
|
2015-11-18 07:25:01 +00:00
|
|
|
doc, this.previewNode.getOuterRange().start, { mw: mwData }
|
|
|
|
)
|
|
|
|
);
|
|
|
|
this.previewElement.updatePreview();
|
|
|
|
};
|