All files / src/ui/actions ve.ui.ContentAction.js

61.11% Statements 33/54
28.57% Branches 4/14
63.63% Functions 7/11
61.11% Lines 33/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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