Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/inspectors/ve.ui.MWWikitextLinkAnnotationInspector.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MWWikitextLinkAnnotationInspector class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * Inspector for applying and editing labeled MediaWiki internal and external links.
10 *
11 * @class
12 * @extends ve.ui.MWLinkAnnotationInspector
13 *
14 * @constructor
15 * @param {Object} [config] Configuration options
16 */
17 ve.ui.MWWikitextLinkAnnotationInspector = function VeUiMWWikitextLinkAnnotationInspector( config ) {
18 // Parent constructor
19 ve.ui.MWWikitextLinkAnnotationInspector.super.call( this, config );
20 };
21
22 /* Inheritance */
23
24 OO.inheritClass( ve.ui.MWWikitextLinkAnnotationInspector, ve.ui.MWLinkAnnotationInspector );
25
26 /* Static properties */
27
28 ve.ui.MWWikitextLinkAnnotationInspector.static.name = 'wikitextLink';
29
30 ve.ui.MWWikitextLinkAnnotationInspector.static.modelClasses = [];
31
32 ve.ui.MWWikitextLinkAnnotationInspector.static.handlesSource = true;
33
34 // TODO: Support [[linktrail]]s & [[pipe trick|]]
35 ve.ui.MWWikitextLinkAnnotationInspector.static.internalLinkParser = ( function () {
36 const openLink = '\\[\\[',
37 closeLink = '\\]\\]',
38 noCloseLink = '(?:(?!' + closeLink + ').)*',
39 noCloseLinkOrPipe = '(?:(?!' + closeLink + ')[^|])*';
40
41 return new RegExp(
42 openLink +
43 '(' + noCloseLinkOrPipe + ')' +
44 '(?:\\|(' + noCloseLink + '))?' +
45 closeLink,
46 'g'
47 );
48 }() );
49
50 /* Methods */
51
52 /**
53 * @inheritdoc
54 */
55 ve.ui.MWWikitextLinkAnnotationInspector.prototype.getSetupProcess = function ( data ) {
56 // Annotation inspector stages the annotation, so call its parent
57 // Call grand-parent
58 return ve.ui.AnnotationInspector.super.prototype.getSetupProcess.call( this, data )
59 .next( () => {
60 const wgNamespaceIds = mw.config.get( 'wgNamespaceIds' ),
61 internalLinkParser = this.constructor.static.internalLinkParser;
62
63 // Only supports linear selections
64 if ( !( this.initialFragment && this.initialFragment.getSelection() instanceof ve.dm.LinearSelection ) ) {
65 return ve.createDeferred().reject().promise();
66 }
67
68 let fragment = this.getFragment();
69 let linkMatches;
70 // Initialize range
71 if ( !data.noExpand ) {
72 if ( !fragment.getSelection().isCollapsed() ) {
73 // Trim whitespace
74 fragment = fragment.trimLinearSelection();
75 }
76 // Expand to existing link, if present
77 // Find all links in the paragraph and see which one contains
78 // the current selection.
79 const contextFragment = fragment.expandLinearSelection( 'siblings' );
80 const contextRange = contextFragment.getSelection().getCoveringRange();
81 const range = fragment.getSelection().getCoveringRange();
82 const text = contextFragment.getText();
83 internalLinkParser.lastIndex = 0;
84 let matches;
85 while ( ( matches = internalLinkParser.exec( text ) ) !== null ) {
86 const matchTitle = mw.Title.newFromText( matches[ 1 ] );
87 if ( !matchTitle ) {
88 continue;
89 }
90 const linkRange = new ve.Range(
91 contextRange.start + matches.index,
92 contextRange.start + matches.index + matches[ 0 ].length
93 );
94 const namespaceId = mw.Title.newFromText( matches[ 1 ] ).getNamespaceId();
95 if (
96 linkRange.containsRange( range ) && !(
97 // Ignore File:/Category:, but not :File:/:Category:
98 (
99 namespaceId === wgNamespaceIds.file ||
100 namespaceId === wgNamespaceIds.category
101 ) &&
102 matches[ 1 ].indexOf( ':' ) !== 0
103 )
104 ) {
105 linkMatches = matches;
106 fragment = fragment.getSurface().getLinearFragment( linkRange );
107 break;
108 }
109 }
110 }
111 if ( !linkMatches ) {
112 if ( !data.noExpand && fragment.getSelection().isCollapsed() ) {
113 // expand to nearest word
114 fragment = fragment.expandLinearSelection( 'word' );
115 } else {
116 // Trim whitespace
117 fragment = fragment.trimLinearSelection();
118 }
119 }
120
121 // Update selection
122 fragment.select();
123
124 this.initialSelection = fragment.getSelection();
125 this.fragment = fragment;
126 this.initialLabelText = this.fragment.getText();
127
128 let title;
129 if ( linkMatches ) {
130 // Group 1 is the link target, group 2 is the label after | if present
131 title = mw.Title.newFromText( linkMatches[ 1 ] );
132 this.initialLabelText = linkMatches[ 2 ] || linkMatches[ 1 ];
133 // HACK: Remove escaping probably added by this tool.
134 // We should really do a full parse from wikitext to HTML if
135 // we see any syntax
136 this.initialLabelText = this.initialLabelText.replace( /<nowiki>(\]{2,})<\/nowiki>/g, '$1' );
137 } else {
138 title = mw.Title.newFromText( this.initialLabelText );
139 }
140 if ( title ) {
141 this.initialAnnotation = this.newInternalLinkAnnotationFromTitle( title );
142 }
143
144 const inspectorTitle = ve.msg(
145 this.isReadOnly() ?
146 'visualeditor-linkinspector-title' : (
147 !linkMatches ?
148 'visualeditor-linkinspector-title-add' :
149 'visualeditor-linkinspector-title-edit'
150 )
151 );
152
153 this.title.setLabel( inspectorTitle ).setTitle( inspectorTitle );
154 this.annotationInput.setReadOnly( this.isReadOnly() );
155
156 this.actions.setMode( this.getMode() );
157 this.linkTypeIndex.setTabPanel(
158 this.initialAnnotation instanceof ve.dm.MWExternalLinkAnnotation ? 'external' : 'internal'
159 );
160 this.annotationInput.setAnnotation( this.initialAnnotation );
161
162 this.updateActions();
163 } );
164 };
165
166 /**
167 * @inheritdoc
168 */
169 ve.ui.MWWikitextLinkAnnotationInspector.prototype.getTeardownProcess = function ( data ) {
170 data = data || {};
171 // Call grand-parent
172 return ve.ui.FragmentInspector.prototype.getTeardownProcess.call( this, data )
173 .first( () => {
174 const wgNamespaceIds = mw.config.get( 'wgNamespaceIds' ),
175 annotation = this.getAnnotation(),
176 fragment = this.getFragment(),
177 insertion = this.getInsertionText();
178
179 if ( data && data.action === 'done' && annotation ) {
180 const insert = this.initialSelection.isCollapsed() && insertion.length;
181 let labelText;
182 if ( insert ) {
183 fragment.insertContent( insertion );
184 labelText = insertion;
185 } else {
186 labelText = this.initialLabelText;
187 }
188
189 // Build internal links locally
190 if ( annotation instanceof ve.dm.MWInternalLinkAnnotation ) {
191 if ( labelText.indexOf( ']]' ) !== -1 ) {
192 labelText = labelText.replace( /(\]{2,})/g, '<nowiki>$1</nowiki>' );
193 }
194 const labelTitle = mw.Title.newFromText( labelText );
195 let targetText;
196 if ( !labelTitle || labelTitle.getPrefixedText() !== annotation.getAttribute( 'normalizedTitle' ) ) {
197 targetText = annotation.getAttribute( 'normalizedTitle' ) + '|';
198 } else {
199 targetText = '';
200 }
201 const targetTitle = mw.Title.newFromText( annotation.getAttribute( 'normalizedTitle' ) );
202 const namespaceId = targetTitle.getNamespaceId();
203 let prefix;
204 if (
205 ( targetText + labelText )[ 0 ] !== ':' && (
206 namespaceId === wgNamespaceIds.file ||
207 namespaceId === wgNamespaceIds.category
208 )
209 ) {
210 prefix = ':';
211 } else {
212 prefix = '';
213 }
214
215 fragment.insertContent( '[[' + prefix + targetText + labelText + ']]' );
216 } else {
217 // Annotating the surface will send the content to Parsoid before
218 // it is inserted into the wikitext document. It is slower but it
219 // will handle all cases.
220 // Where possible we should generate the wikitext locally.
221 fragment.annotateContent( 'set', annotation );
222 }
223
224 // Fix selection after annotating is complete
225 fragment.getPending().then( () => {
226 if ( insert ) {
227 fragment.collapseToEnd().select();
228 } else {
229 fragment.select();
230 }
231 } );
232 } else if ( !data.action ) {
233 // Restore selection to what it was before we expanded it
234 this.initialFragment.select();
235 }
236 } )
237 .next( () => {
238 // Reset state
239 this.initialSelection = null;
240 this.initialAnnotation = null;
241
242 // Parent resets
243 this.allowProtocolInInternal = false;
244 this.internalAnnotationInput.setAnnotation( null );
245 this.externalAnnotationInput.setAnnotation( null );
246 } );
247 };
248
249 /* Registration */
250
251 ve.ui.windowFactory.register( ve.ui.MWWikitextLinkAnnotationInspector );
252
253 ve.ui.wikitextCommandRegistry.register(
254 new ve.ui.Command(
255 'link', 'window', 'open',
256 { args: [ 'wikitextLink' ], supportedSelections: [ 'linear' ] }
257 )
258 );