Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface ContentAction class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Content 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.ContentAction = function VeUiContentAction() {
18 // Parent constructor
19 ve.ui.ContentAction.super.apply( this, arguments );
20 };
21
22 /* Inheritance */
23
24 OO.inheritClass( ve.ui.ContentAction, ve.ui.Action );
25
26 /* Static Properties */
27
28 ve.ui.ContentAction.static.name = 'content';
29
30 ve.ui.ContentAction.static.methods = [ 'insert', 'remove', 'select', 'pasteSpecial', 'selectAll', 'changeDirectionality', 'submit', 'cancel', 'focusContext' ];
31
32 /* Methods */
33
34 /**
35 * Insert content.
36 *
37 * @param {string|Array} content Content to insert, can be either a string or array of data
38 * @param {boolean} [annotate] Content should be automatically annotated to match surrounding content
39 * @param {boolean} [collapseToEnd] Collapse selection to end after inserting
40 * @return {boolean} Action was executed
41 */
42 ve.ui.ContentAction.prototype.insert = function ( content, annotate, collapseToEnd ) {
43 const fragment = this.surface.getModel().getFragment();
44 fragment.insertContent( content, annotate );
45 if ( collapseToEnd ) {
46 fragment.collapseToEnd().select();
47 }
48 return true;
49 };
50
51 /**
52 * Remove content.
53 *
54 * @param {string} [key] Trigger remove as if a key were pressed, either 'backspace' or 'delete'
55 * @return {boolean} Action was executed
56 */
57 ve.ui.ContentAction.prototype.remove = function ( key ) {
58 let defaultPrevented = false;
59 if ( key ) {
60 const e = {
61 keyCode: key === 'delete' ? OO.ui.Keys.DELETE : OO.ui.Keys.BACKSPACE,
62 preventDefault: function () {
63 defaultPrevented = true;
64 }
65 };
66 ve.ce.keyDownHandlerFactory.executeHandlersForKey(
67 e.keyCode,
68 this.surface.getModel().getSelection().getName(),
69 this.surface.getView(),
70 e
71 );
72 return defaultPrevented;
73 } else {
74 this.surface.getModel().getFragment().removeContent();
75 return true;
76 }
77 };
78
79 /**
80 * Select content.
81 *
82 * @param {ve.dm.Selection} selection
83 * @return {boolean} Action was executed
84 */
85 ve.ui.ContentAction.prototype.select = function ( selection ) {
86 this.surface.getModel().setSelection( selection );
87 return true;
88 };
89
90 /**
91 * Select all content.
92 *
93 * @return {boolean} Action was executed
94 */
95 ve.ui.ContentAction.prototype.selectAll = function () {
96 this.surface.getView().selectAll();
97 return true;
98 };
99
100 /**
101 * Paste special.
102 *
103 * @return {boolean} Action was executed
104 */
105 ve.ui.ContentAction.prototype.pasteSpecial = function () {
106 this.surface.getView().pasteSpecial = true;
107 // Return false to allow the paste event to occur
108 return false;
109 };
110
111 /**
112 * Change directionality
113 *
114 * @return {boolean} Action was executed
115 */
116 ve.ui.ContentAction.prototype.changeDirectionality = function () {
117 const documentView = this.surface.getView().getDocument();
118 documentView.setDir( documentView.getDir() === 'ltr' ? 'rtl' : 'ltr' );
119 this.surface.getModel().emit( 'contextChange' );
120 this.surface.getView().emit( 'position' );
121 return true;
122 };
123
124 /**
125 * Emit a surface submit event
126 *
127 * @return {boolean} Action was executed
128 */
129 ve.ui.ContentAction.prototype.submit = function () {
130 this.surface.emit( 'submit' );
131 return true;
132 };
133
134 /**
135 * Emit a surface cancel event
136 *
137 * @return {boolean} Action was executed
138 */
139 ve.ui.ContentAction.prototype.cancel = function () {
140 if ( this.surface.context.isVisible() ) {
141 // T97350
142 this.surface.context.hide();
143 } else {
144 this.surface.emit( 'cancel' );
145 }
146 return true;
147 };
148
149 /**
150 * Move keyboard focus to the context menu.
151 *
152 * @return {boolean} Action was executed
153 */
154 ve.ui.ContentAction.prototype.focusContext = function () {
155 if ( this.surface.getContext().isVisible() ) {
156 // Disable $focusTrapBefore so it doesn't get matched as the first
157 // focusable item.
158 this.surface.getContext().$focusTrapBefore.prop( 'disabled', true );
159 const $focusable = OO.ui.findFocusable( this.surface.getContext().$element );
160 this.surface.getContext().$focusTrapBefore.prop( 'disabled', false );
161 if ( $focusable.length ) {
162 this.surface.getView().deactivate();
163 $focusable[ 0 ].focus();
164 return true;
165 }
166 }
167 return false;
168 };
169
170 /* Registration */
171
172 ve.ui.actionFactory.register( ve.ui.ContentAction );