Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/preinit/ve.init.mw.TempWikitextEditorWidget.js

raw symbols SCIP
1 /*!
2 * VisualEditor MediaWiki temporary wikitext editor widget
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 mw.libs.ve = mw.libs.ve || {};
9
10 /**
11 * MediaWiki temporary wikitext editor widget
12 *
13 * This widget can be used to show the user a basic editing interface
14 * while VE libraries are still loading.
15 *
16 * It has a similar API to OO.ui.InputWidget, but is designed to
17 * be loaded before any core VE code or dependencies, e.g. OOUI.
18 *
19 * @class
20 *
21 * @constructor
22 * @param {Object} config Configuration options
23 * @param {string} config.value Raw wikitext to edit
24 */
25 mw.libs.ve.MWTempWikitextEditorWidget = function VeUiMwTempWikitextEditorWidget( config ) {
26 const conf = mw.config.get( 'wgVisualEditor' ),
27 dir = conf.pageLanguageDir,
28 lang = conf.pageLanguageCode;
29
30 this.$element = $( '<textarea>' )
31 .addClass( 've-init-mw-tempWikitextEditorWidget ' )
32 // The following classes are used here:
33 // * mw-editfont-monospace
34 // * mw-editfont-sans-serif
35 // * mw-editfont-serif
36 .addClass( 'mw-editfont-' + mw.user.options.get( 'editfont' ) )
37 // The following classes are used here:
38 // * mw-content-ltr
39 // * mw-content-rtl
40 .addClass( 'mw-content-' + dir )
41 .attr( {
42 lang: lang,
43 dir: dir
44 } )
45 .val( config.value );
46 };
47
48 /**
49 * Focus the input and move the cursor to the start.
50 *
51 * @return {mw.libs.ve.MWTempWikitextEditorWidget}
52 * @chainable
53 */
54 mw.libs.ve.MWTempWikitextEditorWidget.prototype.moveCursorToStart = function () {
55 // Move cursor to start
56 this.$element[ 0 ].setSelectionRange( 0, 0 );
57 this.focus();
58 return this;
59 };
60
61 /**
62 * Expand the height of the text to fit the contents
63 *
64 * @return {mw.libs.ve.MWTempWikitextEditorWidget}
65 * @chainable
66 */
67 mw.libs.ve.MWTempWikitextEditorWidget.prototype.adjustSize = function () {
68 // Don't bother with reducing height for simplicity
69 this.$element.height( this.$element[ 0 ].scrollHeight );
70 return this;
71 };
72
73 /**
74 * Focus the input
75 *
76 * @return {mw.libs.ve.MWTempWikitextEditorWidget}
77 * @chainable
78 */
79 mw.libs.ve.MWTempWikitextEditorWidget.prototype.focus = function () {
80 this.$element[ 0 ].focus();
81 return this;
82 };
83
84 /**
85 * @return {string} Raw, possibly edited wikitext
86 */
87 mw.libs.ve.MWTempWikitextEditorWidget.prototype.getValue = function () {
88 return this.$element.val();
89 };
90
91 /**
92 * Get the selection range
93 *
94 * @return {Object} Object containing numbers 'from' and 'to'
95 */
96 mw.libs.ve.MWTempWikitextEditorWidget.prototype.getRange = function () {
97 const input = this.$element[ 0 ],
98 start = input.selectionStart,
99 end = input.selectionEnd,
100 isBackwards = input.selectionDirection === 'backward';
101
102 return {
103 from: isBackwards ? end : start,
104 to: isBackwards ? start : end
105 };
106 };