| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
ve.ui.AnnotationInspector = function VeUiAnnotationInspector() { |
| 18 |
|
| 19 |
ve.ui.AnnotationInspector.super.apply( this, arguments ); |
| 20 |
|
| 21 |
|
| 22 |
this.initialSelection = null; |
| 23 |
this.initialAnnotation = null; |
| 24 |
this.initialAnnotationIsCovering = false; |
| 25 |
}; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
OO.inheritClass( ve.ui.AnnotationInspector, ve.ui.FragmentInspector ); |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
ve.ui.AnnotationInspector.static.modelClasses = []; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
ve.ui.AnnotationInspector.prototype.shouldRemoveAnnotation = function () { |
| 51 |
return false; |
| 52 |
}; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
ve.ui.AnnotationInspector.prototype.shouldInsertText = function () { |
| 64 |
return !this.isEditing(); |
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
ve.ui.AnnotationInspector.prototype.getInsertionData = function () { |
| 75 |
return this.getInsertionText().split( '' ); |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
ve.ui.AnnotationInspector.prototype.getInsertionText = function () { |
| 84 |
if ( this.sourceMode ) { |
| 85 |
return OO.ui.resolveMsg( this.constructor.static.title ); |
| 86 |
} |
| 87 |
return ''; |
| 88 |
}; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
ve.ui.AnnotationInspector.prototype.getAnnotation = null; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
ve.ui.AnnotationInspector.prototype.getAnnotationFromFragment = null; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
ve.ui.AnnotationInspector.prototype.getMatchingAnnotations = function ( fragment, all ) { |
| 119 |
const modelClasses = this.constructor.static.modelClasses; |
| 120 |
|
| 121 |
return fragment.getAnnotations( all ).filter( ( annotation ) => ve.isInstanceOfAny( annotation, modelClasses ) ); |
| 122 |
}; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
ve.ui.AnnotationInspector.prototype.isEditing = function () { |
| 129 |
|
| 130 |
|
| 131 |
return !this.initialSelection || !this.initialSelection.isCollapsed(); |
| 132 |
}; |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
ve.ui.AnnotationInspector.prototype.getSetupProcess = function ( data ) { |
| 149 |
return ve.ui.AnnotationInspector.super.prototype.getSetupProcess.call( this, data ) |
| 150 |
.next( () => { |
| 151 |
let fragment = this.getFragment(), |
| 152 |
|
| 153 |
annotation = this.getMatchingAnnotations( fragment, true ).get( 0 ); |
| 154 |
|
| 155 |
const surfaceModel = fragment.getSurface(); |
| 156 |
surfaceModel.pushStaging(); |
| 157 |
|
| 158 |
|
| 159 |
if ( !( this.initialFragment && this.initialFragment.getSelection() instanceof ve.dm.LinearSelection ) ) { |
| 160 |
return ve.createDeferred().reject().promise(); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
if ( !annotation ) { |
| 165 |
|
| 166 |
|
| 167 |
if ( |
| 168 |
fragment.getSelection().isCollapsed() && |
| 169 |
fragment.getDocument().data.isContentOffset( fragment.getSelection().getRange().start ) |
| 170 |
) { |
| 171 |
|
| 172 |
if ( !data.noExpand ) { |
| 173 |
fragment = fragment.expandLinearSelection( 'word' ); |
| 174 |
|
| 175 |
if ( !fragment.getSelection().isCollapsed() ) { |
| 176 |
annotation = this.getMatchingAnnotations( fragment, true ).get( 0 ); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
} else { |
| 183 |
|
| 184 |
fragment = fragment.trimLinearSelection(); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
if ( !fragment.getSelection().isCollapsed() && !annotation ) { |
| 189 |
this.isNew = true; |
| 190 |
annotation = this.getAnnotationFromFragment( fragment ); |
| 191 |
if ( annotation ) { |
| 192 |
fragment.annotateContent( 'set', annotation ); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
if ( annotation && !data.noExpand ) { |
| 199 |
fragment = fragment.expandLinearSelection( 'annotation', annotation ); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
fragment.select(); |
| 204 |
this.initialSelection = fragment.getSelection(); |
| 205 |
|
| 206 |
|
| 207 |
this.initialAnnotation = this.getMatchingAnnotations( fragment, true ).get( 0 ); |
| 208 |
const initialCoveringAnnotation = this.getMatchingAnnotations( fragment ).get( 0 ); |
| 209 |
|
| 210 |
if ( !this.initialAnnotation ) { |
| 211 |
this.isNew = true; |
| 212 |
this.initialAnnotation = this.getAnnotationFromFragment( fragment ); |
| 213 |
} else if ( |
| 214 |
initialCoveringAnnotation && |
| 215 |
initialCoveringAnnotation.compareTo( this.initialAnnotation ) |
| 216 |
) { |
| 217 |
|
| 218 |
|
| 219 |
this.initialAnnotationIsCovering = true; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
this.fragment = fragment; |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
this.actions.setMode( this.getMode() ); |
| 228 |
|
| 229 |
|
| 230 |
if ( this.isNew && this.isReadOnly() ) { |
| 231 |
return ve.createDeferred().reject().promise(); |
| 232 |
} |
| 233 |
}, this ); |
| 234 |
}; |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
ve.ui.AnnotationInspector.prototype.getTeardownProcess = function ( data ) { |
| 240 |
data = data || {}; |
| 241 |
return ve.ui.AnnotationInspector.super.prototype.getTeardownProcess.call( this, data ) |
| 242 |
.first( () => { |
| 243 |
const annotation = this.getAnnotation(), |
| 244 |
remove = data.action === 'done' && this.shouldRemoveAnnotation(), |
| 245 |
surfaceModel = this.fragment.getSurface(), |
| 246 |
surfaceView = this.manager.getSurface().getView(), |
| 247 |
fragment = surfaceModel.getFragment( this.initialSelection, false ), |
| 248 |
isEditing = this.isEditing(), |
| 249 |
insertText = !remove && this.shouldInsertText(); |
| 250 |
|
| 251 |
let insertionAnnotation = false; |
| 252 |
let replace = false; |
| 253 |
let annotations; |
| 254 |
let insertion; |
| 255 |
|
| 256 |
const clear = () => { |
| 257 |
|
| 258 |
annotations = this.getMatchingAnnotations( fragment, true ).get(); |
| 259 |
for ( let i = 0, len = annotations.length; i < len; i++ ) { |
| 260 |
fragment.annotateContent( 'clear', annotations[ i ] ); |
| 261 |
} |
| 262 |
}; |
| 263 |
|
| 264 |
if ( remove ) { |
| 265 |
surfaceModel.popStaging(); |
| 266 |
if ( !isEditing ) { |
| 267 |
return; |
| 268 |
} |
| 269 |
clear(); |
| 270 |
} else { |
| 271 |
if ( data.action !== 'done' ) { |
| 272 |
surfaceModel.popStaging(); |
| 273 |
if ( this.initialFragment ) { |
| 274 |
this.initialFragment.select(); |
| 275 |
} |
| 276 |
return; |
| 277 |
} |
| 278 |
if ( annotation ) { |
| 279 |
|
| 280 |
|
| 281 |
if ( |
| 282 |
!this.initialAnnotationIsCovering || |
| 283 |
!this.initialAnnotation || |
| 284 |
!this.initialAnnotation.compareTo( annotation ) |
| 285 |
) { |
| 286 |
replace = true; |
| 287 |
} |
| 288 |
} |
| 289 |
if ( replace || insertText ) { |
| 290 |
surfaceModel.popStaging(); |
| 291 |
if ( insertText ) { |
| 292 |
insertion = this.getInsertionData(); |
| 293 |
if ( insertion.length ) { |
| 294 |
fragment.insertContent( insertion, true ); |
| 295 |
if ( !isEditing ) { |
| 296 |
|
| 297 |
this.initialFragment = fragment.collapseToEnd(); |
| 298 |
} else { |
| 299 |
this.initialFragment = fragment; |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
if ( replace ) { |
| 306 |
clear(); |
| 307 |
|
| 308 |
if ( fragment.getSelection().isCollapsed() ) { |
| 309 |
insertionAnnotation = true; |
| 310 |
} else { |
| 311 |
fragment.annotateContent( 'set', annotation ); |
| 312 |
} |
| 313 |
} |
| 314 |
} else { |
| 315 |
surfaceModel.applyStaging(); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
let selection; |
| 322 |
if ( this.initialFragment && ( !data.action || insertText ) ) { |
| 323 |
|
| 324 |
selection = this.initialFragment.getSelection(); |
| 325 |
} else { |
| 326 |
selection = fragment.getSelection(); |
| 327 |
} |
| 328 |
|
| 329 |
if ( data.action ) { |
| 330 |
|
| 331 |
|
| 332 |
if ( !( insertion && insertion.length && ve.dm.LinearData.static.isElementData( insertion[ 0 ] ) ) ) { |
| 333 |
surfaceModel.setSelection( selection ); |
| 334 |
} |
| 335 |
|
| 336 |
surfaceView.updateActiveAnnotations( true ); |
| 337 |
|
| 338 |
|
| 339 |
surfaceView.previousActiveAnnotations = surfaceView.activeAnnotations; |
| 340 |
if ( OO.ui.isMobile() ) { |
| 341 |
|
| 342 |
surfaceView.deactivate( false, false, true ); |
| 343 |
} else { |
| 344 |
|
| 345 |
|
| 346 |
surfaceView.selectAnnotation( ( annView ) => ve.isInstanceOfAny( annView.getModel(), this.constructor.static.modelClasses ) ); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
if ( insertionAnnotation ) { |
| 351 |
surfaceModel.addInsertionAnnotations( annotation ); |
| 352 |
} |
| 353 |
} ) |
| 354 |
.next( () => { |
| 355 |
|
| 356 |
this.initialSelection = null; |
| 357 |
this.initialAnnotation = null; |
| 358 |
this.initialAnnotationIsCovering = false; |
| 359 |
this.isNew = false; |
| 360 |
} ); |
| 361 |
}; |