Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/actions/ve.ui.AnnotationAction.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface AnnotationAction class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Annotation action.
9 *
10 * @class
11 * @extends ve.ui.Action
12 *
13 * @constructor
14 * @param {ve.ui.Surface} surface Surface to act on
15 * @param {string} [source]
16 */
17 ve.ui.AnnotationAction = function VeUiAnnotationAction() {
18 // Parent constructor
19 ve.ui.AnnotationAction.super.apply( this, arguments );
20 };
21
22 /* Inheritance */
23
24 OO.inheritClass( ve.ui.AnnotationAction, ve.ui.Action );
25
26 /* Static Properties */
27
28 ve.ui.AnnotationAction.static.name = 'annotation';
29
30 ve.ui.AnnotationAction.static.methods = [ 'set', 'clear', 'toggle', 'clearAll' ];
31
32 /* Methods */
33
34 /**
35 * Set an annotation.
36 *
37 * @param {string} name Annotation name, for example: 'textStyle/bold'
38 * @param {Object} [data] Additional annotation data
39 * @return {boolean} Action was executed
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 * Clear an annotation.
48 *
49 * @param {string} name Annotation name, for example: 'textStyle/bold'
50 * @param {Object} [data] Additional annotation data
51 * @return {boolean} Action was executed
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 * Toggle an annotation.
61 *
62 * If the selected text is completely covered with the annotation already the annotation will be
63 * cleared. Otherwise the annotation will be set.
64 *
65 * @param {string} name Annotation name, for example: 'textStyle/bold'
66 * @param {Object} [data] Additional annotation data
67 * @return {boolean} Action was executed
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 * Clear all annotations.
101 *
102 * @return {boolean} Action was executed
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 // TODO: Allow multiple annotations to be set or cleared by ve.dm.SurfaceFragment, probably
113 // using an annotation set and ideally building a single transaction
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 * Internal implementation of set(). Do not use this, use set() instead.
123 *
124 * @private
125 * @param {string} name Annotation name, for example: 'textStyle/bold'
126 * @param {Object} [data] Additional annotation data
127 * @return {boolean} Action was executed
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 /* Registration */
149
150 ve.ui.actionFactory.register( ve.ui.AnnotationAction );