Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface BlockquoteAction class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Blockquote action.
9 *
10 * @class
11 * @extends ve.ui.Action
12 * @constructor
13 * @param {ve.ui.Surface} surface Surface to act on
14 * @param {string} [source]
15 */
16 ve.ui.BlockquoteAction = function VeUiBlockquoteAction() {
17 // Parent constructor
18 ve.ui.BlockquoteAction.super.apply( this, arguments );
19 };
20
21 /* Inheritance */
22
23 OO.inheritClass( ve.ui.BlockquoteAction, ve.ui.Action );
24
25 /* Static Properties */
26
27 ve.ui.BlockquoteAction.static.name = 'blockquote';
28
29 ve.ui.BlockquoteAction.static.methods = [ 'wrap', 'unwrap', 'toggle' ];
30
31 /* Methods */
32
33 /**
34 * Check if the current selection is wrapped in a blockquote.
35 *
36 * @return {boolean} Current selection is wrapped in a blockquote
37 */
38 ve.ui.BlockquoteAction.prototype.isWrapped = function () {
39 const fragment = this.surface.getModel().getFragment();
40 return fragment.hasMatchingAncestor( 'blockquote' );
41 };
42
43 /**
44 * Toggle a blockquote around content.
45 *
46 * @return {boolean} Action was executed
47 */
48 ve.ui.BlockquoteAction.prototype.toggle = function () {
49 return this[ this.isWrapped() ? 'unwrap' : 'wrap' ]();
50 };
51
52 /**
53 * Add a blockquote around content (only if it has no blockquote already).
54 *
55 * @return {boolean} Action was executed
56 */
57 ve.ui.BlockquoteAction.prototype.wrap = function () {
58 const surfaceModel = this.surface.getModel(),
59 selection = surfaceModel.getSelection();
60
61 if ( !( selection instanceof ve.dm.LinearSelection ) ) {
62 return false;
63 }
64
65 let fragment = surfaceModel.getFragment( null, true );
66 // Trim the selection range to the range of leaf nodes in the selection,
67 // to avoid covering whole nodes where only start/end tag was selected.
68 // For example:
69 // <p>asdf</p><p>qwer</p> --> <p>asdf</p><p>qwer</p>
70 // ^^^^^^^^^^^ ^^^^
71 const leaves = fragment.getSelectedLeafNodes();
72 const leavesRange = new ve.Range(
73 leaves[ 0 ].getRange().start,
74 leaves[ leaves.length - 1 ].getRange().end
75 );
76 fragment = surfaceModel.getLinearFragment( leavesRange, true );
77
78 // Expand to cover entire nodes
79 fragment = fragment.expandLinearSelection( 'siblings' );
80
81 // If the nodes can't be wrapped (e.g. they are list items), wrap the parent
82 while (
83 fragment.getCoveredNodes().some( ( nodeInfo ) => !nodeInfo.node.isAllowedParentNodeType( 'blockquote' ) || nodeInfo.node.isContent() )
84 ) {
85 fragment = fragment.expandLinearSelection( 'parent' );
86 }
87
88 // Wrap everything in a blockquote
89 fragment.wrapAllNodes( { type: 'blockquote' } );
90
91 return true;
92 };
93
94 /**
95 * Remove blockquote around content (if present).
96 *
97 * @return {boolean} Action was executed
98 */
99 ve.ui.BlockquoteAction.prototype.unwrap = function () {
100 const surfaceModel = this.surface.getModel(),
101 selection = surfaceModel.getSelection();
102
103 if ( !( selection instanceof ve.dm.LinearSelection ) ) {
104 return false;
105 }
106
107 if ( !this.isWrapped() ) {
108 return false;
109 }
110
111 let fragment = surfaceModel.getFragment( null, true );
112 // Trim the selection range to the range of leaf nodes in the selection,
113 // to avoid covering whole nodes where only start/end tag was selected.
114 // For example:
115 // <bq><p>asdf</p></bq><p>qwer</p> --> <bq><p>asdf</p></bq><p>qwer</p>
116 // ^^^^^^^^^^^^^^^^ ^^^^
117 const leaves = fragment.getSelectedLeafNodes();
118 const leavesRange = new ve.Range(
119 leaves[ 0 ].getRange().start,
120 leaves[ leaves.length - 1 ].getRange().end
121 );
122 fragment = surfaceModel.getLinearFragment( leavesRange, true );
123
124 fragment
125 // Expand to cover entire blockquote
126 .expandLinearSelection( 'closest', ve.dm.BlockquoteNode )
127 // Unwrap it
128 .unwrapNodes( 0, 1 );
129
130 return true;
131 };
132
133 /* Registration */
134
135 ve.ui.actionFactory.register( ve.ui.BlockquoteAction );