| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
ve.ui.AnnotationContextItem = function VeUiAnnotationContextItem( context, model, config ) { |
| 19 |
|
| 20 |
ve.ui.AnnotationContextItem.super.call( this, context, model, config ); |
| 21 |
|
| 22 |
|
| 23 |
this.$element.addClass( 've-ui-annotationContextItem' ); |
| 24 |
|
| 25 |
if ( this.context.isMobile() ) { |
| 26 |
this.clearButton = new OO.ui.ButtonWidget( { |
| 27 |
framed: false, |
| 28 |
label: this.constructor.static.clearMsg, |
| 29 |
icon: this.constructor.static.clearIcon, |
| 30 |
flags: [ 'destructive' ] |
| 31 |
} ); |
| 32 |
if ( this.isClearable() && !this.isReadOnly() ) { |
| 33 |
this.$foot.append( this.clearButton.$element ); |
| 34 |
} |
| 35 |
} else { |
| 36 |
this.clearButton = new OO.ui.ButtonWidget( { |
| 37 |
title: this.constructor.static.clearMsg, |
| 38 |
icon: this.constructor.static.clearIcon, |
| 39 |
flags: [ 'destructive' ] |
| 40 |
} ); |
| 41 |
if ( this.isClearable() && !this.isReadOnly() ) { |
| 42 |
this.actionButtons.addItems( [ this.clearButton ], 0 ); |
| 43 |
} |
| 44 |
} |
| 45 |
this.clearButton.connect( this, { click: 'onClearButtonClick' } ); |
| 46 |
}; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
OO.inheritClass( ve.ui.AnnotationContextItem, ve.ui.LinearContextItem ); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
ve.ui.AnnotationContextItem.static.clearable = true; |
| 55 |
ve.ui.AnnotationContextItem.static.clearIcon = 'cancel'; |
| 56 |
ve.ui.AnnotationContextItem.static.clearMsg = OO.ui.deferMsg( 'visualeditor-clearbutton-tooltip' ); |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
ve.ui.AnnotationContextItem.prototype.isClearable = function () { |
| 66 |
return this.constructor.static.clearable; |
| 67 |
}; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
ve.ui.AnnotationContextItem.prototype.onClearButtonClick = function () { |
| 77 |
ve.track( 'activity.' + this.constructor.static.name, { action: 'context-clear' } ); |
| 78 |
this.applyToAnnotations( ( fragment, annotation ) => { |
| 79 |
fragment.annotateContent( 'clear', annotation ); |
| 80 |
} ); |
| 81 |
}; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
ve.ui.AnnotationContextItem.prototype.applyToAnnotations = function ( callback ) { |
| 89 |
const modelClasses = this.constructor.static.modelClasses; |
| 90 |
|
| 91 |
let fragment = this.getFragment(), |
| 92 |
annotations = fragment.getAnnotations( true ).filter( ( annotation ) => ve.isInstanceOfAny( annotation, modelClasses ) ).get(); |
| 93 |
if ( |
| 94 |
!annotations.length && |
| 95 |
fragment.getSelection().isCollapsed() && |
| 96 |
fragment.getDocument().data.isContentOffset( fragment.getSelection().getRange().start ) |
| 97 |
) { |
| 98 |
|
| 99 |
fragment = fragment.expandLinearSelection( 'word' ); |
| 100 |
|
| 101 |
annotations = fragment.getAnnotations( true ).filter( ( annotation ) => ve.isInstanceOfAny( annotation, modelClasses ) ).get(); |
| 102 |
} |
| 103 |
for ( let i = 0, len = annotations.length; i < len; i++ ) { |
| 104 |
callback( fragment.expandLinearSelection( 'annotation', annotations[ i ] ), annotations[ i ] ); |
| 105 |
} |
| 106 |
}; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
ve.ui.AnnotationContextItem.prototype.getAnnotationView = function () { |
| 117 |
const model = this.model, |
| 118 |
surfaceView = this.context.getSurface().getView(); |
| 119 |
|
| 120 |
function isThisModel( annotationView ) { |
| 121 |
return model === annotationView.model; |
| 122 |
} |
| 123 |
|
| 124 |
let annotations = []; |
| 125 |
|
| 126 |
|
| 127 |
if ( surfaceView.contexedAnnotations ) { |
| 128 |
annotations = surfaceView.contexedAnnotations.filter( isThisModel ); |
| 129 |
} |
| 130 |
if ( !annotations.length ) { |
| 131 |
annotations = surfaceView.annotationsAtModelSelection( isThisModel ); |
| 132 |
} |
| 133 |
|
| 134 |
return annotations.length ? annotations[ 0 ] : undefined; |
| 135 |
}; |