2016-05-26 12:08:26 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel Surface class.
|
|
|
|
*
|
2019-01-01 13:24:23 +00:00
|
|
|
* @copyright 2011-2019 VisualEditor Team and others; see http://ve.mit-license.org
|
2016-05-26 12:08:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 );
|
|
|
|
|
2017-07-03 21:02:00 +00:00
|
|
|
this.pasteTargetInput = new OO.ui.MultilineTextInputWidget();
|
2016-05-26 12:08:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ce.MWWikitextSurface, ve.ce.Surface );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ce.MWWikitextSurface.prototype.onCopy = function ( e ) {
|
2018-01-26 01:40:26 +00:00
|
|
|
var originalSelection, scrollTop, slice, clipboardKey,
|
2016-05-26 12:08:26 +00:00
|
|
|
view = this,
|
|
|
|
clipboardData = e.originalEvent.clipboardData,
|
|
|
|
text = this.getModel().getFragment().getText( true ).replace( /\n\n/g, '\n' );
|
|
|
|
|
2017-11-07 15:35:14 +00:00
|
|
|
if ( !text ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-26 12:08:26 +00:00
|
|
|
if ( clipboardData ) {
|
|
|
|
// Disable the default event so we can override the data
|
|
|
|
e.preventDefault();
|
|
|
|
clipboardData.setData( 'text/plain', text );
|
2018-01-26 01:40:26 +00:00
|
|
|
// We're not going to set HTML, but for browsers that support custom data, set a clipboard key
|
|
|
|
if ( ve.isClipboardDataFormatsSupported( e, true ) ) {
|
|
|
|
slice = this.model.documentModel.shallowCloneFromSelection( this.getModel().getSelection() );
|
|
|
|
this.clipboardIndex++;
|
|
|
|
clipboardKey = this.clipboardId + '-' + this.clipboardIndex;
|
|
|
|
this.clipboard = { slice: slice, hash: null };
|
|
|
|
// Clone the elements in the slice
|
|
|
|
slice.data.cloneElements( true );
|
|
|
|
clipboardData.setData( 'text/xcustom', clipboardKey );
|
2018-04-23 21:04:49 +00:00
|
|
|
|
|
|
|
// Explicitly store wikitext as text/x-wiki, so that wikitext-aware paste
|
|
|
|
// contexts can accept it without having to do any content-
|
|
|
|
// sniffing.
|
|
|
|
clipboardData.setData( 'text/x-wiki', text );
|
2018-01-26 01:40:26 +00:00
|
|
|
}
|
2016-05-26 12:08:26 +00:00
|
|
|
} 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();
|
2018-03-29 12:58:00 +00:00
|
|
|
this.$pasteTarget.empty().append( this.pasteTargetInput.$element );
|
2016-05-26 12:08:26 +00:00
|
|
|
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
|
2019-02-13 13:20:46 +00:00
|
|
|
view.$attachedRootNode[ 0 ].focus();
|
2016-05-26 12:08:26 +00:00
|
|
|
view.showSelectionState( originalSelection );
|
|
|
|
// Restore scroll position
|
|
|
|
view.$window.scrollTop( scrollTop );
|
|
|
|
view.surfaceObserver.clear();
|
|
|
|
view.surfaceObserver.enable();
|
|
|
|
// Detach input
|
2018-03-29 12:58:00 +00:00
|
|
|
view.pasteTargetInput.$element.detach();
|
2016-05-26 12:08:26 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
};
|
2018-04-11 04:23:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ce.MWWikitextSurface.prototype.afterPasteInsertExternalData = function ( targetFragment, pastedDocumentModel, contextRange ) {
|
2018-06-13 12:28:15 +00:00
|
|
|
var windowAction, deferred,
|
|
|
|
view = this;
|
2018-04-11 04:23:47 +00:00
|
|
|
|
2019-11-07 14:49:57 +00:00
|
|
|
function makePlain() {
|
|
|
|
pastedDocumentModel = pastedDocumentModel.shallowCloneFromRange( contextRange );
|
|
|
|
pastedDocumentModel.data.sanitize( { plainText: true, keepEmptyContentBranches: true } );
|
|
|
|
// We just turned this into plaintext, which probably
|
|
|
|
// affected the content-length. Luckily, because of
|
|
|
|
// the earlier clone, we know we just want the whole
|
|
|
|
// document, and because of the major change to
|
|
|
|
// plaintext, the difference between originalRange and
|
|
|
|
// balancedRange don't really apply. As such, clear
|
|
|
|
// out newDocRange. (Can't just make it undefined;
|
|
|
|
// need to exclude the internal list, and since we're
|
|
|
|
// from a paste we also have to exclude the
|
|
|
|
// opening/closing paragraph.)
|
|
|
|
contextRange = new ve.Range( pastedDocumentModel.getDocumentRange().from + 1, pastedDocumentModel.getDocumentRange().to - 1 );
|
|
|
|
view.pasteSpecial = true;
|
|
|
|
}
|
|
|
|
|
2018-04-11 04:23:47 +00:00
|
|
|
if ( !pastedDocumentModel.data.isPlainText( contextRange, true, undefined, true ) ) {
|
|
|
|
// Not plaintext. We need to ask whether we should convert it to
|
|
|
|
// wikitext, or just strip the formatting out.
|
2019-11-02 05:06:28 +00:00
|
|
|
deferred = ve.createDeferred();
|
2018-06-13 12:28:15 +00:00
|
|
|
windowAction = ve.ui.actionFactory.create( 'window', this.getSurface() );
|
|
|
|
windowAction.open( 'wikitextconvertconfirm', { deferred: deferred } );
|
|
|
|
return deferred.promise().then( function ( usePlain ) {
|
|
|
|
if ( usePlain ) {
|
2019-11-07 14:49:57 +00:00
|
|
|
makePlain();
|
2018-06-13 12:28:15 +00:00
|
|
|
}
|
|
|
|
return ve.ce.MWWikitextSurface.super.prototype.afterPasteInsertExternalData.call( view, targetFragment, pastedDocumentModel, contextRange );
|
|
|
|
} );
|
2018-04-11 04:23:47 +00:00
|
|
|
}
|
2019-11-07 14:49:57 +00:00
|
|
|
// isPlainText is true but we still need sanitize (e.g. remove lists)
|
|
|
|
makePlain();
|
2018-04-11 04:23:47 +00:00
|
|
|
return ve.ce.MWWikitextSurface.super.prototype.afterPasteInsertExternalData.call( this, targetFragment, pastedDocumentModel, contextRange );
|
|
|
|
};
|