| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
ve.ui.MWWikitextLinkAnnotationInspector = function VeUiMWWikitextLinkAnnotationInspector( config ) { |
| 18 |
|
| 19 |
ve.ui.MWWikitextLinkAnnotationInspector.super.call( this, config ); |
| 20 |
}; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
OO.inheritClass( ve.ui.MWWikitextLinkAnnotationInspector, ve.ui.MWLinkAnnotationInspector ); |
| 25 |
|
| 26 |
|
| 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 |
|
| 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 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
ve.ui.MWWikitextLinkAnnotationInspector.prototype.getSetupProcess = function ( data ) { |
| 56 |
|
| 57 |
|
| 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 |
|
| 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 |
|
| 71 |
if ( !data.noExpand ) { |
| 72 |
if ( !fragment.getSelection().isCollapsed() ) { |
| 73 |
|
| 74 |
fragment = fragment.trimLinearSelection(); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 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 |
|
| 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 |
|
| 114 |
fragment = fragment.expandLinearSelection( 'word' ); |
| 115 |
} else { |
| 116 |
|
| 117 |
fragment = fragment.trimLinearSelection(); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 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 |
|
| 131 |
title = mw.Title.newFromText( linkMatches[ 1 ] ); |
| 132 |
this.initialLabelText = linkMatches[ 2 ] || linkMatches[ 1 ]; |
| 133 |
|
| 134 |
|
| 135 |
|
| 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 |
|
| 168 |
|
| 169 |
ve.ui.MWWikitextLinkAnnotationInspector.prototype.getTeardownProcess = function ( data ) { |
| 170 |
data = data || {}; |
| 171 |
|
| 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 |
|
| 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 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
fragment.annotateContent( 'set', annotation ); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
fragment.getPending().then( () => { |
| 226 |
if ( insert ) { |
| 227 |
fragment.collapseToEnd().select(); |
| 228 |
} else { |
| 229 |
fragment.select(); |
| 230 |
} |
| 231 |
} ); |
| 232 |
} else if ( !data.action ) { |
| 233 |
|
| 234 |
this.initialFragment.select(); |
| 235 |
} |
| 236 |
} ) |
| 237 |
.next( () => { |
| 238 |
|
| 239 |
this.initialSelection = null; |
| 240 |
this.initialAnnotation = null; |
| 241 |
|
| 242 |
|
| 243 |
this.allowProtocolInInternal = false; |
| 244 |
this.internalAnnotationInput.setAnnotation( null ); |
| 245 |
this.externalAnnotationInput.setAnnotation( null ); |
| 246 |
} ); |
| 247 |
}; |
| 248 |
|
| 249 |
|
| 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 |
); |