2016-05-26 12:08:26 +00:00
|
|
|
/*!
|
2017-01-17 22:00:30 +00:00
|
|
|
* VisualEditor UserInterface MWWikitextSurface class.
|
2016-05-26 12:08:26 +00:00
|
|
|
*
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see http://ve.mit-license.org
|
2016-05-26 12:08:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class
|
2017-01-17 22:00:30 +00:00
|
|
|
* @extends ve.ui.Surface
|
2016-05-26 12:08:26 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {HTMLDocument|Array|ve.dm.LinearData|ve.dm.Document} dataOrDoc Document data to edit
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
2017-01-17 22:00:30 +00:00
|
|
|
ve.ui.MWWikitextSurface = function VeUiMWWikitextSurface() {
|
2018-01-22 18:37:51 +00:00
|
|
|
var surface = this;
|
|
|
|
|
2016-05-26 12:08:26 +00:00
|
|
|
// Parent constructor
|
2017-01-17 22:00:30 +00:00
|
|
|
ve.ui.MWWikitextSurface.super.apply( this, arguments );
|
2016-05-26 12:08:26 +00:00
|
|
|
|
|
|
|
// Initialization
|
2019-10-24 13:31:38 +00:00
|
|
|
this.$element.addClass( 've-ui-mwWikitextSurface' );
|
2021-09-09 20:30:07 +00:00
|
|
|
|
|
|
|
this.getView().$element.add( this.$placeholder )
|
|
|
|
.removeClass( 've-ui-surface-source-font' )
|
|
|
|
// The following classes are used here:
|
|
|
|
// * mw-editfont-monospace
|
|
|
|
// * mw-editfont-sans-serif
|
|
|
|
// * mw-editfont-serif
|
|
|
|
.addClass( 'mw-editfont-' + mw.user.options.get( 'editfont' ) );
|
|
|
|
|
2019-02-20 20:23:43 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
2018-01-22 18:37:51 +00:00
|
|
|
this.$textbox = $( '#wpTextbox1' );
|
|
|
|
if ( !this.$textbox.length ) {
|
|
|
|
this.$textbox = $( '<textarea>' )
|
|
|
|
.attr( 'id', 'wpTextbox1' )
|
2019-05-06 20:56:39 +00:00
|
|
|
.addClass( 've-dummyTextbox' );
|
|
|
|
// Append a dummy textbox to the surface, so it gets destroyed with it. Wrap it in a hidden
|
|
|
|
// element, so that UI of extensions/gadgets that add stuff to the real MediaWiki textbox
|
|
|
|
// (e.g. WikiEditor) remains mercifully hidden (T211898).
|
|
|
|
this.$element.append(
|
|
|
|
$( '<div>' )
|
|
|
|
.addClass( 've-dummyTextbox-wrapper oo-ui-element-hidden' )
|
|
|
|
.append( this.$textbox )
|
|
|
|
);
|
2018-02-07 20:12:51 +00:00
|
|
|
} else {
|
|
|
|
// Existing textbox may have an API registered
|
|
|
|
this.$textbox.textSelection( 'unregister' );
|
2018-01-22 18:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Backwards support for the textSelection API
|
|
|
|
this.$textbox.textSelection( 'register', {
|
|
|
|
getContents: function () {
|
|
|
|
return surface.getDom();
|
|
|
|
},
|
|
|
|
setContents: function ( content ) {
|
|
|
|
surface.getModel().getLinearFragment( new ve.Range( 0 ), true ).expandLinearSelection( 'root' ).insertContent( content );
|
2018-02-08 18:47:53 +00:00
|
|
|
return this;
|
2018-01-22 18:37:51 +00:00
|
|
|
},
|
|
|
|
getSelection: function () {
|
|
|
|
var range = surface.getModel().getSelection().getCoveringRange();
|
|
|
|
if ( !range ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return surface.getModel().getDocument().data.getSourceText( range );
|
|
|
|
},
|
|
|
|
setSelection: function ( options ) {
|
|
|
|
surface.getModel().setLinearSelection(
|
|
|
|
surface.getModel().getRangeFromSourceOffsets( options.start, options.end )
|
|
|
|
);
|
2018-02-08 18:47:53 +00:00
|
|
|
return this;
|
2018-01-22 18:37:51 +00:00
|
|
|
},
|
2018-01-27 20:22:44 +00:00
|
|
|
getCaretPosition: function ( options ) {
|
|
|
|
var range = surface.getModel().getSelection().getCoveringRange(),
|
|
|
|
surfaceModel = surface.getModel(),
|
2018-08-08 18:00:14 +00:00
|
|
|
caretPos = range ? surfaceModel.getSourceOffsetFromOffset( range.start ) : 0;
|
2018-01-27 20:22:44 +00:00
|
|
|
|
|
|
|
return options.startAndEnd ?
|
|
|
|
[ caretPos, surfaceModel.getSourceOffsetFromOffset( range.end ) ] :
|
|
|
|
caretPos;
|
2018-01-22 18:37:51 +00:00
|
|
|
},
|
2018-02-05 21:34:07 +00:00
|
|
|
replaceSelection: function ( value ) {
|
|
|
|
surface.getModel().getFragment().insertContent( value );
|
|
|
|
return this;
|
2018-01-22 18:37:51 +00:00
|
|
|
},
|
2018-02-05 21:34:07 +00:00
|
|
|
// encapsulateSelection works automatically when we implement the overrides above
|
2018-01-22 18:37:51 +00:00
|
|
|
scrollToCaretPosition: function () {
|
2019-04-08 11:17:28 +00:00
|
|
|
surface.scrollSelectionIntoView();
|
2018-02-08 18:47:53 +00:00
|
|
|
return this;
|
2018-01-22 18:37:51 +00:00
|
|
|
}
|
|
|
|
} );
|
2016-05-26 12:08:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2018-05-05 15:42:34 +00:00
|
|
|
OO.inheritClass( ve.ui.MWWikitextSurface, ve.ui.MWSurface );
|
2016-05-26 12:08:26 +00:00
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2017-01-17 22:00:30 +00:00
|
|
|
ve.ui.MWWikitextSurface.prototype.createModel = function ( doc ) {
|
2016-05-26 12:08:26 +00:00
|
|
|
return new ve.dm.MWWikitextSurface( doc );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2017-01-17 22:00:30 +00:00
|
|
|
ve.ui.MWWikitextSurface.prototype.createView = function ( model ) {
|
2016-05-26 12:08:26 +00:00
|
|
|
return new ve.ce.MWWikitextSurface( model, this );
|
|
|
|
};
|
2018-01-22 18:37:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWWikitextSurface.prototype.destroy = function () {
|
|
|
|
this.$textbox.textSelection( 'unregister' );
|
|
|
|
// Parent method
|
|
|
|
return ve.ui.MWWikitextSurface.super.prototype.destroy.apply( this, arguments );
|
|
|
|
};
|