Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/ve.ui.MWWikitextSurface.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MWWikitextSurface class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * @class
9 * @extends ve.ui.Surface
10 *
11 * @constructor
12 * @param {ve.init.Target} target
13 * @param {HTMLDocument|Array|ve.dm.LinearData|ve.dm.Document} dataOrDoc Document data to edit
14 * @param {Object} [config] Configuration options
15 */
16 ve.ui.MWWikitextSurface = function VeUiMWWikitextSurface() {
17 // Parent constructor
18 ve.ui.MWWikitextSurface.super.apply( this, arguments );
19
20 // Initialization
21 this.$element.addClass( 've-ui-mwWikitextSurface' );
22
23 this.getView().$element.add( this.$placeholder )
24 .removeClass( 've-ui-surface-source-font' )
25 // The following classes are used here:
26 // * mw-editfont-monospace
27 // * mw-editfont-sans-serif
28 // * mw-editfont-serif
29 .addClass( 'mw-editfont-' + mw.user.options.get( 'editfont' ) );
30
31 // eslint-disable-next-line no-jquery/no-global-selector
32 this.$textbox = $( '#wpTextbox1' );
33 if ( !this.$textbox.length ) {
34 this.$textbox = $( '<textarea>' )
35 .attr( 'id', 'wpTextbox1' )
36 .addClass( 've-dummyTextbox' );
37 // Append a dummy textbox to the surface, so it gets destroyed with it. Wrap it in a hidden
38 // element, so that UI of extensions/gadgets that add stuff to the real MediaWiki textbox
39 // (e.g. WikiEditor) remains mercifully hidden (T211898).
40 this.$element.append(
41 $( '<div>' )
42 .addClass( 've-dummyTextbox-wrapper oo-ui-element-hidden' )
43 .append( this.$textbox )
44 );
45 } else {
46 // Existing textbox may have an API registered
47 this.$textbox.textSelection( 'unregister' );
48 }
49
50 // Note: Chainable methods (setContents/setSelection) must use
51 // non-arrow functions that return this. We can't just return
52 // this.$textbox either as the collection could be cloned.
53 /* eslint es-x/no-arrow-functions: error */
54 const surface = this;
55 // Backwards support for the textSelection API
56 this.$textbox.textSelection( 'register', {
57 getContents: function () {
58 return surface.getDom();
59 },
60 setContents: function ( content ) {
61 surface.getModel().getLinearFragment( new ve.Range( 0 ), true ).expandLinearSelection( 'root' ).insertContent( content );
62 return this;
63 },
64 getSelection: function () {
65 const range = surface.getModel().getSelection().getCoveringRange();
66 if ( !range ) {
67 return '';
68 }
69 return surface.getModel().getDocument().data.getSourceText( range );
70 },
71 setSelection: function ( options ) {
72 surface.getModel().setLinearSelection(
73 surface.getModel().getRangeFromSourceOffsets( options.start, options.end )
74 );
75 return this;
76 },
77 getCaretPosition: function ( options ) {
78 const range = surface.getModel().getSelection().getCoveringRange(),
79 surfaceModel = surface.getModel(),
80 caretPos = range ? surfaceModel.getSourceOffsetFromOffset( range.start ) : 0;
81
82 return options.startAndEnd ?
83 [ caretPos, range ? surfaceModel.getSourceOffsetFromOffset( range.end ) : 0 ] :
84 caretPos;
85 },
86 replaceSelection: function ( value ) {
87 surface.getModel().getFragment().insertContent( value );
88 return this;
89 },
90 // encapsulateSelection works automatically when we implement the overrides above
91 scrollToCaretPosition: function () {
92 surface.scrollSelectionIntoView();
93 return this;
94 }
95 } );
96 /* eslint es-x/no-arrow-functions: off */
97 };
98
99 /* Inheritance */
100
101 OO.inheritClass( ve.ui.MWWikitextSurface, ve.ui.MWSurface );
102
103 /* Methods */
104
105 /**
106 * @inheritdoc
107 */
108 ve.ui.MWWikitextSurface.prototype.createModel = function ( doc ) {
109 return new ve.dm.MWWikitextSurface( doc );
110 };
111
112 /**
113 * @inheritdoc
114 */
115 ve.ui.MWWikitextSurface.prototype.createView = function ( model ) {
116 return new ve.ce.MWWikitextSurface( model, this );
117 };
118
119 /**
120 * @inheritdoc
121 */
122 ve.ui.MWWikitextSurface.prototype.destroy = function () {
123 this.$textbox.textSelection( 'unregister' );
124 // Parent method
125 return ve.ui.MWWikitextSurface.super.prototype.destroy.apply( this, arguments );
126 };