| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
ve.ui.AnnotationAction = function VeUiAnnotationAction() { |
| 18 |
|
| 19 |
ve.ui.AnnotationAction.super.apply( this, arguments ); |
| 20 |
}; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
OO.inheritClass( ve.ui.AnnotationAction, ve.ui.Action ); |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
ve.ui.AnnotationAction.static.name = 'annotation'; |
| 29 |
|
| 30 |
ve.ui.AnnotationAction.static.methods = [ 'set', 'clear', 'toggle', 'clearAll' ]; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
ve.ui.AnnotationAction.prototype.set = function ( name, data ) { |
| 42 |
ve.track( 'activity.' + name, { action: 'set' } ); |
| 43 |
return this.setInternal( name, data ); |
| 44 |
}; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
ve.ui.AnnotationAction.prototype.clear = function ( name, data ) { |
| 54 |
ve.track( 'activity.' + name, { action: 'clear' } ); |
| 55 |
this.surface.getModel().getFragment().annotateContent( 'clear', name, data ); |
| 56 |
return true; |
| 57 |
}; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
ve.ui.AnnotationAction.prototype.toggle = function ( name, data ) { |
| 70 |
const surfaceModel = this.surface.getModel(), |
| 71 |
fragment = surfaceModel.getFragment(), |
| 72 |
annotation = ve.dm.annotationFactory.create( name, data ); |
| 73 |
|
| 74 |
if ( !fragment.getSelection().isCollapsed() ) { |
| 75 |
ve.track( 'activity.' + name, { action: 'toggle-selection' } ); |
| 76 |
if ( !fragment.getAnnotations().containsComparable( annotation ) ) { |
| 77 |
this.setInternal( name, data ); |
| 78 |
} else { |
| 79 |
fragment.annotateContent( 'clear', name ); |
| 80 |
} |
| 81 |
} else if ( surfaceModel.sourceMode ) { |
| 82 |
return false; |
| 83 |
} else { |
| 84 |
ve.track( 'activity.' + name, { action: 'toggle-insertion' } ); |
| 85 |
const insertionAnnotations = surfaceModel.getInsertionAnnotations(); |
| 86 |
const existingAnnotations = insertionAnnotations.getAnnotationsByName( annotation.name ); |
| 87 |
const removes = annotation.constructor.static.removes; |
| 88 |
if ( existingAnnotations.isEmpty() ) { |
| 89 |
const removesAnnotations = insertionAnnotations.filter( ( ann ) => removes.indexOf( ann.name ) !== -1 ); |
| 90 |
surfaceModel.removeInsertionAnnotations( removesAnnotations ); |
| 91 |
surfaceModel.addInsertionAnnotations( annotation ); |
| 92 |
} else { |
| 93 |
surfaceModel.removeInsertionAnnotations( existingAnnotations ); |
| 94 |
} |
| 95 |
} |
| 96 |
return true; |
| 97 |
}; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
ve.ui.AnnotationAction.prototype.clearAll = function () { |
| 105 |
const surfaceModel = this.surface.getModel(), |
| 106 |
fragment = surfaceModel.getFragment(), |
| 107 |
annotations = fragment.getAnnotations( true ); |
| 108 |
|
| 109 |
ve.track( 'activity.allAnnotations', { action: 'clear-all' } ); |
| 110 |
|
| 111 |
const arr = annotations.get(); |
| 112 |
|
| 113 |
|
| 114 |
for ( let i = 0, len = arr.length; i < len; i++ ) { |
| 115 |
fragment.annotateContent( 'clear', arr[ i ].name, arr[ i ].data ); |
| 116 |
} |
| 117 |
surfaceModel.setInsertionAnnotations( null ); |
| 118 |
return true; |
| 119 |
}; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
ve.ui.AnnotationAction.prototype.setInternal = function ( name, data ) { |
| 130 |
const annotationClass = ve.dm.annotationFactory.lookup( name ); |
| 131 |
let fragment = this.surface.getModel().getFragment(); |
| 132 |
|
| 133 |
if ( fragment.getSelection() instanceof ve.dm.LinearSelection ) { |
| 134 |
const trimmedFragment = fragment.trimLinearSelection(); |
| 135 |
if ( !trimmedFragment.getSelection().isCollapsed() ) { |
| 136 |
fragment = trimmedFragment; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
const removes = annotationClass.static.removes; |
| 141 |
for ( let i = removes.length - 1; i >= 0; i-- ) { |
| 142 |
fragment.annotateContent( 'clear', removes[ i ] ); |
| 143 |
} |
| 144 |
fragment.annotateContent( 'set', name, data ); |
| 145 |
return true; |
| 146 |
}; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
ve.ui.actionFactory.register( ve.ui.AnnotationAction ); |