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

92.3% Statements 36/39
70% Branches 7/10
100% Functions 6/6
92.3% Lines 36/39

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                              1x   16x         1x       1x   1x                 1x 23x 23x               1x 16x               1x 9x 9x   9x       9x           9x 9x       9x     9x     9x 19x   5x       9x   9x               1x 7x 7x   7x       7x       7x           7x 7x       7x   7x           7x         1x  
/*!
 * VisualEditor UserInterface BlockquoteAction class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Blockquote action.
 *
 * @class
 * @extends ve.ui.Action
 * @constructor
 * @param {ve.ui.Surface} surface Surface to act on
 * @param {string} [source]
 */
ve.ui.BlockquoteAction = function VeUiBlockquoteAction() {
	// Parent constructor
	ve.ui.BlockquoteAction.super.apply( this, arguments );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.BlockquoteAction, ve.ui.Action );
 
/* Static Properties */
 
ve.ui.BlockquoteAction.static.name = 'blockquote';
 
ve.ui.BlockquoteAction.static.methods = [ 'wrap', 'unwrap', 'toggle' ];
 
/* Methods */
 
/**
 * Check if the current selection is wrapped in a blockquote.
 *
 * @return {boolean} Current selection is wrapped in a blockquote
 */
ve.ui.BlockquoteAction.prototype.isWrapped = function () {
	const fragment = this.surface.getModel().getFragment();
	return fragment.hasMatchingAncestor( 'blockquote' );
};
 
/**
 * Toggle a blockquote around content.
 *
 * @return {boolean} Action was executed
 */
ve.ui.BlockquoteAction.prototype.toggle = function () {
	return this[ this.isWrapped() ? 'unwrap' : 'wrap' ]();
};
 
/**
 * Add a blockquote around content (only if it has no blockquote already).
 *
 * @return {boolean} Action was executed
 */
ve.ui.BlockquoteAction.prototype.wrap = function () {
	const surfaceModel = this.surface.getModel(),
		selection = surfaceModel.getSelection();
 
	Iif ( !( selection instanceof ve.dm.LinearSelection ) ) {
		return false;
	}
 
	let fragment = surfaceModel.getFragment( null, true );
	// Trim the selection range to the range of leaf nodes in the selection,
	// to avoid covering whole nodes where only start/end tag was selected.
	// For example:
	//     <p>asdf</p><p>qwer</p>   -->   <p>asdf</p><p>qwer</p>
	//        ^^^^^^^^^^^                    ^^^^
	const leaves = fragment.getSelectedLeafNodes();
	const leavesRange = new ve.Range(
		leaves[ 0 ].getRange().start,
		leaves[ leaves.length - 1 ].getRange().end
	);
	fragment = surfaceModel.getLinearFragment( leavesRange, true );
 
	// Expand to cover entire nodes
	fragment = fragment.expandLinearSelection( 'siblings' );
 
	// If the nodes can't be wrapped (e.g. they are list items), wrap the parent
	while (
		fragment.getCoveredNodes().some( ( nodeInfo ) => !nodeInfo.node.isAllowedParentNodeType( 'blockquote' ) || nodeInfo.node.isContent() )
	) {
		fragment = fragment.expandLinearSelection( 'parent' );
	}
 
	// Wrap everything in a blockquote
	fragment.wrapAllNodes( { type: 'blockquote' } );
 
	return true;
};
 
/**
 * Remove blockquote around content (if present).
 *
 * @return {boolean} Action was executed
 */
ve.ui.BlockquoteAction.prototype.unwrap = function () {
	const surfaceModel = this.surface.getModel(),
		selection = surfaceModel.getSelection();
 
	Iif ( !( selection instanceof ve.dm.LinearSelection ) ) {
		return false;
	}
 
	Iif ( !this.isWrapped() ) {
		return false;
	}
 
	let fragment = surfaceModel.getFragment( null, true );
	// Trim the selection range to the range of leaf nodes in the selection,
	// to avoid covering whole nodes where only start/end tag was selected.
	// For example:
	//     <bq><p>asdf</p></bq><p>qwer</p>   -->   <bq><p>asdf</p></bq><p>qwer</p>
	//            ^^^^^^^^^^^^^^^^                        ^^^^
	const leaves = fragment.getSelectedLeafNodes();
	const leavesRange = new ve.Range(
		leaves[ 0 ].getRange().start,
		leaves[ leaves.length - 1 ].getRange().end
	);
	fragment = surfaceModel.getLinearFragment( leavesRange, true );
 
	fragment
		// Expand to cover entire blockquote
		.expandLinearSelection( 'closest', ve.dm.BlockquoteNode )
		// Unwrap it
		.unwrapNodes( 0, 1 );
 
	return true;
};
 
/* Registration */
 
ve.ui.actionFactory.register( ve.ui.BlockquoteAction );