File: ve.init.mw.TempWikitextEditorWidget.js

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (106 lines) | stat: -rw-r--r-- 2,731 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*!
 * VisualEditor MediaWiki temporary wikitext editor widget
 *
 * @copyright See AUTHORS.txt
 * @license The MIT License (MIT); see LICENSE.txt
 */

mw.libs.ve = mw.libs.ve || {};

/**
 * MediaWiki temporary wikitext editor widget
 *
 * This widget can be used to show the user a basic editing interface
 * while VE libraries are still loading.
 *
 * It has a similar API to OO.ui.InputWidget, but is designed to
 * be loaded before any core VE code or dependencies, e.g. OOUI.
 *
 * @class
 *
 * @constructor
 * @param {Object} config Configuration options
 * @param {string} config.value Raw wikitext to edit
 */
mw.libs.ve.MWTempWikitextEditorWidget = function VeUiMwTempWikitextEditorWidget( config ) {
	const conf = mw.config.get( 'wgVisualEditor' ),
		dir = conf.pageLanguageDir,
		lang = conf.pageLanguageCode;

	this.$element = $( '<textarea>' )
		.addClass( 've-init-mw-tempWikitextEditorWidget ' )
		// The following classes are used here:
		// * mw-editfont-monospace
		// * mw-editfont-sans-serif
		// * mw-editfont-serif
		.addClass( 'mw-editfont-' + mw.user.options.get( 'editfont' ) )
		// The following classes are used here:
		// * mw-content-ltr
		// * mw-content-rtl
		.addClass( 'mw-content-' + dir )
		.attr( {
			lang: lang,
			dir: dir
		} )
		.val( config.value );
};

/**
 * Focus the input and move the cursor to the start.
 *
 * @return {mw.libs.ve.MWTempWikitextEditorWidget}
 * @chainable
 */
mw.libs.ve.MWTempWikitextEditorWidget.prototype.moveCursorToStart = function () {
	// Move cursor to start
	this.$element[ 0 ].setSelectionRange( 0, 0 );
	this.focus();
	return this;
};

/**
 * Expand the height of the text to fit the contents
 *
 * @return {mw.libs.ve.MWTempWikitextEditorWidget}
 * @chainable
 */
mw.libs.ve.MWTempWikitextEditorWidget.prototype.adjustSize = function () {
	// Don't bother with reducing height for simplicity
	this.$element.height( this.$element[ 0 ].scrollHeight );
	return this;
};

/**
 * Focus the input
 *
 * @return {mw.libs.ve.MWTempWikitextEditorWidget}
 * @chainable
 */
mw.libs.ve.MWTempWikitextEditorWidget.prototype.focus = function () {
	this.$element[ 0 ].focus();
	return this;
};

/**
 * @return {string} Raw, possibly edited wikitext
 */
mw.libs.ve.MWTempWikitextEditorWidget.prototype.getValue = function () {
	return this.$element.val();
};

/**
 * Get the selection range
 *
 * @return {Object} Object containing numbers 'from' and 'to'
 */
mw.libs.ve.MWTempWikitextEditorWidget.prototype.getRange = function () {
	const input = this.$element[ 0 ],
		start = input.selectionStart,
		end = input.selectionEnd,
		isBackwards = input.selectionDirection === 'backward';

	return {
		from: isBackwards ? end : start,
		to: isBackwards ? start : end
	};
};