Debian Codegraph @

extensions/DiscussionTools/modules/dt.ui.ReplyWidgetPlain.js

raw symbols SCIP
1 const utils = require( 'ext.discussionTools.init' ).utils;
2
3 /**
4 * DiscussionTools ReplyWidgetPlain class
5 *
6 * @class mw.dt.ReplyWidgetPlain
7 * @extends mw.dt.ReplyWidget
8 * @constructor
9 * @param {CommentController} commentController
10 * @param {CommentDetails} commentDetails
11 * @param {Object} [config]
12 */
13 function ReplyWidgetPlain() {
14 // Parent constructor
15 ReplyWidgetPlain.super.apply( this, arguments );
16
17 if ( OO.ui.isMobile() ) {
18 const toolFactory = new OO.ui.ToolFactory(),
19 toolGroupFactory = new OO.ui.ToolGroupFactory();
20
21 toolFactory.register( mw.libs.ve.MWEditModeVisualTool );
22 toolFactory.register( mw.libs.ve.MWEditModeSourceTool );
23 this.switchToolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory, {
24 classes: [ 'ext-discussiontools-ui-replyWidget-editSwitch' ]
25 } );
26
27 this.switchToolbar.on( 'switchEditor', ( mode ) => {
28 this.switch( mode );
29 } );
30
31 this.switchToolbar.setup( [ {
32 name: 'editMode',
33 type: 'list',
34 icon: 'edit',
35 title: mw.msg( 'visualeditor-mweditmode-tooltip' ),
36 label: mw.msg( 'visualeditor-mweditmode-tooltip' ),
37 invisibleLabel: true,
38 include: [ 'editModeVisual', 'editModeSource' ]
39 } ] );
40
41 this.switchToolbar.emit( 'updateState' );
42
43 this.$headerWrapper.append( this.switchToolbar.$element );
44 }
45
46 this.$element.addClass( 'ext-discussiontools-ui-replyWidget-plain' );
47 }
48
49 /* Inheritance */
50
51 OO.inheritClass( ReplyWidgetPlain, require( './dt.ui.ReplyWidget.js' ) );
52
53 /* Methods */
54
55 /**
56 * @inheritdoc
57 */
58 ReplyWidgetPlain.prototype.createReplyBodyWidget = function ( config ) {
59 const textInput = new OO.ui.MultilineTextInputWidget( Object.assign( {
60 rows: 3,
61 // TODO: Fix upstream to support a value meaning no max limit (e.g. Infinity)
62 maxRows: 999,
63 autosize: true,
64 // The following classes are used here:
65 // * mw-editfont-monospace
66 // * mw-editfont-sans-serif
67 // * mw-editfont-serif
68 classes: [ 'mw-editfont-' + mw.user.options.get( 'editfont' ) ]
69 }, config ) );
70 textInput.$input.attr( 'aria-label', config.placeholder );
71 // Fix jquery.ime position (T255191)
72 textInput.$input.addClass( 'ime-position-inside' );
73
74 return textInput;
75 };
76
77 /**
78 * @inheritdoc
79 */
80 ReplyWidgetPlain.prototype.focus = function () {
81 this.replyBodyWidget.focus();
82
83 return this;
84 };
85
86 /**
87 * @inheritdoc
88 */
89 ReplyWidgetPlain.prototype.clear = function ( preserveStorage ) {
90 this.replyBodyWidget.setValue( '' );
91
92 if ( !preserveStorage ) {
93 this.storage.remove( 'body' );
94 }
95
96 // Parent method
97 ReplyWidgetPlain.super.prototype.clear.apply( this, arguments );
98 };
99
100 /**
101 * @inheritdoc
102 */
103 ReplyWidgetPlain.prototype.isEmpty = function () {
104 return utils.htmlTrim( this.replyBodyWidget.getValue() ) === '';
105 };
106
107 /**
108 * @inheritdoc
109 */
110 ReplyWidgetPlain.prototype.getMode = function () {
111 return 'source';
112 };
113
114 /**
115 * @inheritdoc
116 */
117 ReplyWidgetPlain.prototype.onInputChange = function () {
118 if ( this.isTornDown ) {
119 // Ignore calls after teardown, which would clear the auto-save or crash
120 return;
121 }
122
123 // Parent method
124 ReplyWidgetPlain.super.prototype.onInputChange.apply( this, arguments );
125
126 const wikitext = this.getValue();
127 this.storage.set( 'body', wikitext );
128 };
129
130 /**
131 * @inheritdoc
132 */
133 ReplyWidgetPlain.prototype.setup = function ( data ) {
134 const autosaveValue = this.storage.get( 'body' );
135
136 data = data || {};
137
138 // Parent method
139 ReplyWidgetPlain.super.prototype.setup.apply( this, arguments );
140
141 // Events
142 this.replyBodyWidget.connect( this, { change: this.onInputChangeThrottled } );
143 this.replyBodyWidget.$input.on( 'focus', this.emit.bind( this, 'bodyFocus' ) );
144
145 this.replyBodyWidget.setValue( data.value || autosaveValue );
146
147 // needs to bind after the initial setValue:
148 this.replyBodyWidget.once( 'change', this.onFirstChange.bind( this ) );
149
150 this.afterSetup();
151
152 return this;
153 };
154
155 /**
156 * @inheritdoc
157 */
158 ReplyWidgetPlain.prototype.teardown = function () {
159 this.replyBodyWidget.disconnect( this );
160 this.replyBodyWidget.off( 'change' );
161
162 // Parent method
163 return ReplyWidgetPlain.super.prototype.teardown.apply( this, arguments );
164 };
165
166 /**
167 * @inheritdoc
168 */
169 ReplyWidgetPlain.prototype.getValue = function () {
170 return this.replyBodyWidget.getValue();
171 };
172
173 module.exports = ReplyWidgetPlain;