Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor Wikitext command registry
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Command registry.
9 *
10 * @class
11 * @extends ve.ui.CommandRegistry
12 * @constructor
13 * @param {ve.ui.CommandRegistry} fallbackRegistry Fallback registry
14 */
15 ve.ui.MWWikitextCommandRegistry = function VeUiMwWikitextCommandRegistry( fallbackRegistry ) {
16 // Parent constructor
17 ve.ui.MWWikitextCommandRegistry.super.apply( this, arguments );
18
19 this.fallbackRegistry = fallbackRegistry;
20 };
21
22 /* Inheritance */
23
24 OO.inheritClass( ve.ui.MWWikitextCommandRegistry, ve.ui.CommandRegistry );
25
26 /* Methods */
27
28 /**
29 * Get data for a given symbolic name.
30 *
31 * See https://doc.wikimedia.org/oojs/master/OO.Registry.html
32 *
33 * @param {string} name Symbolic name
34 * @return {any|undefined} Data associated with symbolic name
35 */
36 ve.ui.MWWikitextCommandRegistry.prototype.lookup = function ( name ) {
37 // Parent method
38 const data = ve.ui.MWWikitextCommandRegistry.super.prototype.lookup.call( this, name );
39 if ( data !== undefined ) {
40 return data;
41 }
42 // Fall through to original command registry if not overridden.
43 return this.fallbackRegistry.lookup( name );
44 };
45
46 /**
47 * @inheritdoc
48 */
49 ve.ui.MWWikitextCommandRegistry.prototype.getNames = function () {
50 return OO.simpleArrayUnion(
51 // Parent method
52 ve.ui.MWWikitextCommandRegistry.super.prototype.getNames.call( this ),
53 this.fallbackRegistry.getNames()
54 );
55 };
56
57 /* Initialization */
58
59 ve.ui.wikitextCommandRegistry = new ve.ui.MWWikitextCommandRegistry( ve.ui.commandRegistry );
60
61 /* Registrations */
62
63 ve.ui.wikitextCommandRegistry.register(
64 new ve.ui.Command(
65 'bold', 'mwWikitext', 'toggleWrapSelection',
66 { args: [ '\'\'\'', '\'\'\'', OO.ui.deferMsg( 'visualeditor-annotationbutton-bold-tooltip' ) ], supportedSelections: [ 'linear' ] }
67 )
68 );
69 ve.ui.wikitextCommandRegistry.register(
70 new ve.ui.Command(
71 'italic', 'mwWikitext', 'toggleWrapSelection',
72 {
73 args: [
74 '\'\'', '\'\'',
75 OO.ui.deferMsg( 'visualeditor-annotationbutton-italic-tooltip' ),
76 /* expandOffsetsCallback */
77 function ( textBefore, textAfter ) {
78 let matches, lengthBefore, lengthAfter;
79 if ( ( matches = textBefore.match( /('+)$/ ) ) ) {
80 lengthBefore = -matches[ 1 ].length;
81 }
82 if ( ( matches = textAfter.match( /^('+)/ ) ) ) {
83 lengthAfter = matches[ 1 ].length;
84 }
85 return lengthBefore || lengthAfter ? [ lengthBefore, lengthAfter ] : null;
86 },
87 /* unwrapOffsetsCallback */
88 function ( text ) {
89 /* Text is only italic if there are 2 or 5+ apostrophes */
90 const matches = /^(''([^'].*|.*[^'])''|'{5,}([^'].*|.*[^'])'{5,})$/.test( text );
91
92 return matches ? [ 2, 2 ] : null;
93 }
94 ],
95 supportedSelections: [ 'linear' ] }
96 )
97 );
98 ve.ui.wikitextCommandRegistry.register(
99 new ve.ui.Command(
100 'code', 'mwWikitext', 'toggleWrapSelection',
101 { args: [ '<code>', '</code>', OO.ui.deferMsg( 'visualeditor-annotationbutton-code-tooltip' ) ], supportedSelections: [ 'linear' ] }
102 )
103 );
104 ve.ui.wikitextCommandRegistry.register(
105 new ve.ui.Command(
106 'strikethrough', 'mwWikitext', 'toggleWrapSelection',
107 { args: [ '<s>', '</s>', OO.ui.deferMsg( 'visualeditor-annotationbutton-strikethrough-tooltip' ) ], supportedSelections: [ 'linear' ] }
108 )
109 );
110 ve.ui.wikitextCommandRegistry.register(
111 new ve.ui.Command(
112 'underline', 'mwWikitext', 'toggleWrapSelection',
113 { args: [ '<u>', '</u>', OO.ui.deferMsg( 'visualeditor-annotationbutton-underline-tooltip' ) ], supportedSelections: [ 'linear' ] }
114 )
115 );
116 ve.ui.wikitextCommandRegistry.register(
117 new ve.ui.Command(
118 'subscript', 'mwWikitext', 'toggleWrapSelection',
119 { args: [ '<sub>', '</sub>', OO.ui.deferMsg( 'visualeditor-annotationbutton-subscript-tooltip' ) ], supportedSelections: [ 'linear' ] }
120 )
121 );
122 ve.ui.wikitextCommandRegistry.register(
123 new ve.ui.Command(
124 'superscript', 'mwWikitext', 'toggleWrapSelection',
125 { args: [ '<sup>', '</sup>', OO.ui.deferMsg( 'visualeditor-annotationbutton-superscript-tooltip' ) ], supportedSelections: [ 'linear' ] }
126 )
127 );
128 ve.ui.wikitextCommandRegistry.register(
129 new ve.ui.Command(
130 'big', 'mwWikitext', 'toggleWrapSelection',
131 { args: [ '<big>', '</big>', OO.ui.deferMsg( 'visualeditor-annotationbutton-big-tooltip' ) ], supportedSelections: [ 'linear' ] }
132 )
133 );
134 ve.ui.wikitextCommandRegistry.register(
135 new ve.ui.Command(
136 'small', 'mwWikitext', 'toggleWrapSelection',
137 { args: [ '<small>', '</small>', OO.ui.deferMsg( 'visualeditor-annotationbutton-small-tooltip' ) ], supportedSelections: [ 'linear' ] }
138 )
139 );
140
141 ( function () {
142
143 function unformat( text ) {
144 /* Use lazy .+? in the middle so whitespace is matched to wrappers */
145 let headings;
146 if ( ( headings = text.match( /^((={1,6})\s*).+?(\s*\2\s*)$/ ) ) ) {
147 return [ headings[ 1 ].length, headings[ 3 ].length ];
148 }
149 let pre;
150 if ( ( pre = text.match( /^ +/ ) ) ) {
151 return [ pre[ 0 ].length, 0 ];
152 }
153 }
154
155 let heading = '';
156 for ( let i = 1; i <= 6; i++ ) {
157 heading += '=';
158 ve.ui.wikitextCommandRegistry.register(
159 new ve.ui.Command(
160 'heading' + i, 'mwWikitext', 'wrapLine',
161 {
162 args: [
163 heading + ' ',
164 ' ' + heading,
165 // The following messages can be used here:
166 // * visualeditor-formatdropdown-format-heading1
167 // * visualeditor-formatdropdown-format-heading2
168 // * visualeditor-formatdropdown-format-heading3
169 // * visualeditor-formatdropdown-format-heading4
170 // * visualeditor-formatdropdown-format-heading5
171 // * visualeditor-formatdropdown-format-heading6
172 OO.ui.deferMsg( 'visualeditor-formatdropdown-format-heading' + i ),
173 unformat
174 ],
175 supportedSelections: [ 'linear' ]
176 }
177 )
178 );
179 }
180
181 ve.ui.wikitextCommandRegistry.register(
182 new ve.ui.Command(
183 'paragraph', 'mwWikitext', 'wrapLine',
184 { args: [ '', '', '', unformat ], supportedSelections: [ 'linear' ] }
185 )
186 );
187 ve.ui.wikitextCommandRegistry.register(
188 new ve.ui.Command(
189 'preformatted', 'mwWikitext', 'wrapLine',
190 { args: [ ' ', '', OO.ui.deferMsg( 'visualeditor-formatdropdown-format-preformatted' ), unformat ], supportedSelections: [ 'linear' ] }
191 )
192 );
193 ve.ui.wikitextCommandRegistry.register(
194 new ve.ui.Command(
195 'blockquote', 'mwWikitext', 'toggleWrapSelection',
196 { args: [ '<blockquote>', '</blockquote>', OO.ui.deferMsg( 'visualeditor-formatdropdown-format-blockquote' ) ], supportedSelections: [ 'linear' ] }
197 )
198 );
199
200 function unlist( keepType, text ) {
201 let matches;
202 if ( ( matches = text.match( /^[*#] */ ) ) && text.slice( 0, 1 ) !== keepType ) {
203 return [ matches[ 0 ].length, 0 ];
204 }
205 }
206
207 ve.ui.wikitextCommandRegistry.register(
208 new ve.ui.Command(
209 'number', 'mwWikitext', 'wrapLine',
210 { args: [ '# ', '', OO.ui.deferMsg( 'visualeditor-listbutton-number-tooltip' ), unlist.bind( this, '#' ) ], supportedSelections: [ 'linear' ] }
211 )
212 );
213 ve.ui.wikitextCommandRegistry.register(
214 new ve.ui.Command(
215 'bullet', 'mwWikitext', 'wrapLine',
216 { args: [ '* ', '', OO.ui.deferMsg( 'visualeditor-listbutton-bullet-tooltip' ), unlist.bind( this, '*' ) ], supportedSelections: [ 'linear' ] }
217 )
218 );
219
220 }() );
221
222 ve.ui.wikitextCommandRegistry.register(
223 new ve.ui.Command(
224 'comment', 'mwWikitext', 'toggleWrapSelection',
225 { args: [ '<!-- ', ' -->', OO.ui.deferMsg( 'visualeditor-commentinspector-title' ) ], supportedSelections: [ 'linear' ] }
226 )
227 );
228 ve.ui.wikitextCommandRegistry.register(
229 new ve.ui.Command(
230 'insertTable', 'mwWikitext', 'toggleWrapSelection',
231 {
232 args: [
233 [ { type: 'paragraph' }, ...'{| class="wikitable"', { type: '/paragraph' } ],
234 [ { type: 'paragraph' }, ...'|}', { type: '/paragraph' } ],
235 function () {
236 return '' +
237 '|+ ' + ve.msg( 'visualeditor-dialog-table-caption' ) +
238 '\n' +
239 '! ' + ve.msg( 'visualeditor-table-format-header' ) + ' !! ' + ve.msg( 'visualeditor-table-format-header' ) +
240 '\n' +
241 '|-' +
242 '\n' +
243 '| ' + ve.msg( 'visualeditor-table-format-data' ) + ' || ' + ve.msg( 'visualeditor-table-format-data' );
244 }
245 ],
246 supportedSelections: [ 'linear' ]
247 }
248 )
249 );
250
251 ve.ui.wikitextCommandRegistry.register(
252 new ve.ui.Command( 'mwNonBreakingSpace', 'content', 'insert', {
253 args: [
254 '&nbsp;',
255 // annotate
256 true,
257 // collapseToEnd
258 true
259 ],
260 supportedSelections: [ 'linear' ]
261 } )
262 );