All files / src/ce ve.ce.DragDropHandler.js

36.36% Statements 52/143
12.5% Branches 11/88
70% Functions 7/10
37.41% Lines 52/139

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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322                      1x 372x   372x 372x 372x 372x 372x 372x   372x                             1x 388x                 1x 2x 2x               1x                                                                                                                                                                                                                                                               1x 1x 1x                             1x   2x 2x 2x 2x     2x   2x           2x                         2x 2x 1x     1x   1x       1x     1x   1x   1x                           1x       1x       1x 1x     1x   1x 1x                           1x                   1x     2x 2x 2x               1x 1x 1x   1x 1x    
/*!
 * VisualEditor ContentEditable DragDropHandler class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Drag and drop handler
 *
 * @param {ve.ce.Surface} surface
 */
ve.ce.DragDropHandler = function VeCeDragDropHandler( surface ) {
	this.surface = surface;
 
	this.$dropMarker = $( '<div>' ).addClass( 've-ce-surface-dropMarker oo-ui-element-hidden' );
	this.$lastDropTarget = null;
	this.lastDropPosition = null;
	this.relocatingSelection = null;
	this.relocatingNode = null;
	this.allowedFile = null;
 
	this.getSurface().$element.on( {
		dragstart: this.onDocumentDragStart.bind( this ),
		dragover: this.onDocumentDragOver.bind( this ),
		dragleave: this.onDocumentDragLeave.bind( this ),
		drop: this.onDocumentDrop.bind( this )
	} );
};
 
/* Methods */
 
/**
 * Get the handled surface
 *
 * @return {ve.ce.Surface}
 */
ve.ce.DragDropHandler.prototype.getSurface = function () {
	return this.surface;
};
 
/**
 * Handle document drag start events.
 *
 * @param {jQuery.Event} e Drag start event
 * @fires ve.ce.Surface#relocationStart
 */
ve.ce.DragDropHandler.prototype.onDocumentDragStart = function ( e ) {
	this.getSurface().getClipboardHandler().onCopy( e );
	this.startRelocation();
};
 
/**
 * Handle document drag over events.
 *
 * @param {jQuery.Event} e Drag over event
 */
ve.ce.DragDropHandler.prototype.onDocumentDragOver = function ( e ) {
	const dataTransferHandlerFactory = this.getSurface().getSurface().dataTransferHandlerFactory,
		dataTransfer = e.originalEvent.dataTransfer;
	let isContent = true;
 
	if ( this.getSurface().isReadOnly() ) {
		return;
	}
 
	let nodeType;
	if ( this.relocatingNode ) {
		isContent = this.relocatingNode.isContent();
		nodeType = this.relocatingNode.getType();
	} else {
		if ( this.allowedFile === null ) {
			this.allowedFile = false;
			// If we can get file metadata, check if there is a DataTransferHandler registered
			// to handle it.
			if ( dataTransfer.items ) {
				for ( let i = 0, l = dataTransfer.items.length; i < l; i++ ) {
					const item = dataTransfer.items[ i ];
					if ( item.kind !== 'string' ) {
						const fakeItem = new ve.ui.DataTransferItem( item.kind, item.type );
						if ( dataTransferHandlerFactory.getHandlerNameForItem( fakeItem ) ) {
							this.allowedFile = true;
							break;
						}
					}
				}
			} else if ( dataTransfer.files && dataTransfer.files.length ) {
				for ( let i = 0, l = dataTransfer.files.length; i < l; i++ ) {
					const item = dataTransfer.items[ i ];
					const fakeItem = new ve.ui.DataTransferItem( item.kind, item.type );
					if ( dataTransferHandlerFactory.getHandlerNameForItem( fakeItem ) ) {
						this.allowedFile = true;
						break;
					}
				}
			} else if ( Array.prototype.indexOf.call( dataTransfer.types || [], 'Files' ) !== -1 ) {
				// Support: Firefox
				// If we have no metadata (e.g. in Firefox) assume it is droppable
				this.allowedFile = true;
			}
		}
		// this.allowedFile is cached until the next dragleave event
		if ( this.allowedFile ) {
			isContent = false;
			nodeType = 'alienBlock';
		}
	}
 
	function getNearestDropTarget( node ) {
		while ( node.parent && !node.parent.isAllowedChildNodeType( nodeType ) ) {
			node = node.parent;
		}
		if ( node.parent ) {
			node.parent.traverseUpstream( ( n ) => {
				if ( n.shouldIgnoreChildren() ) {
					node = null;
					return false;
				}
			} );
			return node;
		}
	}
 
	if ( !isContent ) {
		e.preventDefault();
		const $target = $( e.target ).closest( '.ve-ce-branchNode, .ve-ce-leafNode' );
		let $dropTarget;
		let dropPosition;
		if ( $target.length ) {
			// Find the nearest node which will accept this node type
			let dropTargetNode = getNearestDropTarget( $target.data( 'view' ) );
			if ( dropTargetNode ) {
				$dropTarget = dropTargetNode.$element;
				dropPosition = e.originalEvent.pageY - $dropTarget.offset().top > $dropTarget.outerHeight() / 2 ? 'bottom' : 'top';
			} else {
				const targetOffset = this.getSurface().getOffsetFromEventCoords( e.originalEvent );
				if ( targetOffset !== -1 ) {
					dropTargetNode = getNearestDropTarget( this.getSurface().getDocument().getBranchNodeFromOffset( targetOffset ) );
					if ( dropTargetNode ) {
						$dropTarget = dropTargetNode.$element;
						dropPosition = 'top';
					}
				}
				if ( !$dropTarget ) {
					$dropTarget = this.$lastDropTarget;
					dropPosition = this.lastDropPosition;
				}
			}
		}
		if ( this.$lastDropTarget && (
			!this.$lastDropTarget.is( $dropTarget ) || dropPosition !== this.lastDropPosition
		) ) {
			this.$dropMarker.addClass( 'oo-ui-element-hidden' );
			$dropTarget = null;
		}
		if ( $dropTarget && (
			!$dropTarget.is( this.$lastDropTarget ) || dropPosition !== this.lastDropPosition
		) ) {
			const targetPosition = $dropTarget.position();
			// Go beyond margins as they can overlap
			let top = targetPosition.top + parseFloat( $dropTarget.css( 'margin-top' ) );
			const left = targetPosition.left + parseFloat( $dropTarget.css( 'margin-left' ) );
			if ( dropPosition === 'bottom' ) {
				top += $dropTarget.outerHeight();
			}
			this.$dropMarker
				.css( {
					top: top,
					left: left
				} )
				.width( $dropTarget.outerWidth() )
				.removeClass( 'oo-ui-element-hidden' );
		}
		if ( $dropTarget !== undefined ) {
			this.$lastDropTarget = $dropTarget;
			this.lastDropPosition = dropPosition;
		}
	}
};
 
/**
 * Handle document drag leave events.
 *
 * @param {jQuery.Event} e Drag leave event
 */
ve.ce.DragDropHandler.prototype.onDocumentDragLeave = function () {
	this.allowedFile = null;
	Iif ( this.$lastDropTarget ) {
		this.$dropMarker.addClass( 'oo-ui-element-hidden' );
		this.$lastDropTarget = null;
		this.lastDropPosition = null;
	}
};
 
/**
 * Handle document drop events.
 *
 * Limits native drag and drop behaviour.
 *
 * @param {jQuery.Event} e Drop event
 * @fires ve.ce.Surface#relocationEnd
 */
ve.ce.DragDropHandler.prototype.onDocumentDrop = function ( e ) {
	// Properties may be nullified by other events, so cache before setTimeout
	const surfaceModel = this.getSurface().getModel(),
		$dropTarget = this.$lastDropTarget,
		dropPosition = this.lastDropPosition,
		platformKey = ve.getSystemPlatform() === 'mac' ? 'mac' : 'pc';
 
	// Prevent native drop event from modifying view
	e.preventDefault();
 
	Iif ( this.getSurface().isReadOnly() ) {
		return;
	}
 
	let targetOffset;
	// Determine drop position
	Iif ( $dropTarget ) {
		// Block level drag and drop: use the lastDropTarget to get the targetOffset
		if ( $dropTarget ) {
			const targetRange = $dropTarget.data( 'view' ).getModel().getOuterRange();
			if ( dropPosition === 'top' ) {
				targetOffset = targetRange.start;
			} else {
				targetOffset = targetRange.end;
			}
		} else {
			return;
		}
	} else {
		targetOffset = this.getSurface().getOffsetFromEventCoords( e.originalEvent );
		if ( targetOffset === -1 ) {
			return;
		}
	}
	const targetFragment = surfaceModel.getLinearFragment( new ve.Range( targetOffset ) );
 
	const targetViewNode = this.getSurface().getDocument().getBranchNodeFromOffset(
		targetFragment.getSelection().getCoveringRange().from
	);
	// TODO: Support sanitized drop on a single line node (removing line breaks)
	const isMultiline = targetViewNode.isMultiline();
 
	// Internal drop
	if ( this.relocatingSelection ) {
		// Get a fragment and data of the node being dragged
		const originFragment = surfaceModel.getFragment( this.relocatingSelection );
		let originData;
		Iif ( !isMultiline ) {
			// Data needs to be balanced to be sanitized
			const slice = this.getSurface().getModel().getDocument().shallowCloneFromRange( originFragment.getSelection().getCoveringRange() );
			const linearData = new ve.dm.ElementLinearData(
				originFragment.getDocument().getStore(),
				slice.getBalancedData()
			);
			linearData.sanitize( { singleLine: true } );
			originData = linearData.data;
			// Unwrap CBN
			if ( originData[ 0 ].type && ve.dm.nodeFactory.canNodeContainContent( originData[ 0 ].type ) ) {
				originData = originData.slice( 1, originData.length - 1 );
			}
		} else {
			originData = originFragment.getData();
		}
 
		// Start staging so we can abort in the catch later
		surfaceModel.pushStaging();
 
		// Dragging performs cut-and-paste by default (remove content from old location).
		// If Ctrl on PC, or Opt (alt) on Mac, is held, it performs copy-and-paste instead.
		Eif ( ( platformKey === 'pc' && !e.ctrlKey ) || ( platformKey === 'mac' && !e.altKey ) ) {
			originFragment.removeContent();
		}
 
		try {
			// Re-insert data at new location
			targetFragment.insertContent( originData );
			surfaceModel.applyStaging();
		} catch ( error ) {
			// Insert content may throw an exception if it can't find a way
			// to fixup the insertion sensibly
			surfaceModel.popStaging();
		}
 
	} else E{
		// External drop
		// TODO: Support sanitized external drop into single line contexts
		if ( isMultiline ) {
			this.getSurface().getClipboardHandler().onPaste( e );
		}
	}
	this.endRelocation();
};
 
/* Relocation */
 
/**
 * Start a relocation action.
 *
 * @fires ve.ce.Surface#relocationStart
 */
ve.ce.DragDropHandler.prototype.startRelocation = function () {
	// Cache the selection and selectedNode when the drag starts, to
	// avoid having to recompute them while dragging.
	this.relocatingSelection = this.getSurface().getModel().getSelection();
	this.relocatingNode = this.getSurface().getModel().getSelectedNode();
	this.getSurface().emit( 'relocationStart' );
};
 
/**
 * Complete a relocation action.
 *
 * @fires ve.ce.Surface#relocationEnd
 */
ve.ce.DragDropHandler.prototype.endRelocation = function () {
	this.relocatingSelection = null;
	this.relocatingNode = null;
	// Trigger a drag leave event to clear markers
	this.onDocumentDragLeave();
	this.getSurface().emit( 'relocationEnd' );
};