mediawiki-extensions-Visual.../modules/ve-mw/ce/ve.ce.MWWikitextSurface.js
Ed Sanders c452e134cc Wikitext surface alpha feature
Edit wikitext with the VE interace.

Bug: T104479
Bug: T142138
Change-Id: Ic95b47e0dd378578555c4a2342ca9c87064ed1d5
2016-08-12 18:37:29 +00:00

72 lines
2 KiB
JavaScript

/*!
* VisualEditor DataModel Surface class.
*
* @copyright 2011-2016 VisualEditor Team and others; see http://ve.mit-license.org
*/
/**
* DataModel surface.
*
* @class
* @extends ve.ce.Surface
*
* @constructor
* @param {ve.dm.Surface} model
* @param {ve.ui.Surface} ui
* @param {Object} [config]
*/
ve.ce.MWWikitextSurface = function VeCeMwWikitextSurface() {
// Parent constructors
ve.ce.MWWikitextSurface.super.apply( this, arguments );
this.pasteTargetInput = new OO.ui.TextInputWidget( { multiline: true } );
};
/* Inheritance */
OO.inheritClass( ve.ce.MWWikitextSurface, ve.ce.Surface );
/**
* @inheritdoc
*/
ve.ce.MWWikitextSurface.prototype.onCopy = function ( e ) {
var originalSelection, scrollTop,
view = this,
clipboardData = e.originalEvent.clipboardData,
text = this.getModel().getFragment().getText( true ).replace( /\n\n/g, '\n' );
if ( clipboardData ) {
// Disable the default event so we can override the data
e.preventDefault();
clipboardData.setData( 'text/plain', text );
} else {
originalSelection = new ve.SelectionState( this.nativeSelection );
// Save scroll position before changing focus to "offscreen" paste target
scrollTop = this.$window.scrollTop();
// Prevent surface observation due to native range changing
this.surfaceObserver.disable();
this.$pasteTarget.append( this.pasteTargetInput.$element );
this.pasteTargetInput.setValue( text ).select();
// Restore scroll position after changing focus
this.$window.scrollTop( scrollTop );
// setTimeout: postpone until after the default copy action
setTimeout( function () {
// Change focus back
view.$documentNode[ 0 ].focus();
view.showSelectionState( originalSelection );
// Restore scroll position
view.$window.scrollTop( scrollTop );
view.surfaceObserver.clear();
view.surfaceObserver.enable();
// Detach input
this.pasteTargetInput.$element.detach();
} );
}
};
ve.ui.dataTransferHandlerFactory.unregister( ve.ui.MWWikitextStringTransferHandler );