Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ce/selections/ve.ce.TableSelection.js

raw symbols SCIP
1 /*!
2 * VisualEditor Table Selection class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * @class
9 * @extends ve.ce.Selection
10 * @constructor
11 * @param {ve.ce.Surface} surface
12 * @param {ve.dm.Selection} model
13 */
14 ve.ce.TableSelection = function VeCeTableSelection() {
15 // Parent constructor
16 ve.ce.TableSelection.super.apply( this, arguments );
17 this.directionality = null;
18 };
19
20 /* Inheritance */
21
22 OO.inheritClass( ve.ce.TableSelection, ve.ce.Selection );
23
24 /* Static Properties */
25
26 ve.ce.TableSelection.static.name = 'table';
27
28 /* Method */
29
30 /**
31 * @inheritdoc
32 */
33 ve.ce.TableSelection.prototype.getSelectionRects = function () {
34 return [ this.getSelectionBoundingRect() ];
35 };
36
37 /**
38 * @inheritdoc
39 */
40 ve.ce.TableSelection.prototype.getSelectionBoundingRect = function () {
41 const surface = this.getSurface(),
42 tableNode = surface.getDocument().getBranchNodeFromOffset( this.model.tableRange.start + 1 ),
43 nodes = tableNode.getCellNodesFromSelection( this.getModel() ),
44 surfaceRect = surface.getSurface().getBoundingClientRect();
45
46 let top = Infinity;
47 let bottom = -Infinity;
48 let left = Infinity;
49 let right = -Infinity;
50
51 const cellElements = [];
52 nodes.forEach( ( node ) => {
53 cellElements.push( ...node.$element.toArray() );
54 } );
55
56 // Compute a bounding box for the given cell elements
57 cellElements.forEach( ( cellElement ) => {
58 if ( !cellElement ) {
59 return null;
60 }
61 const cellOffset = cellElement.getBoundingClientRect();
62
63 top = Math.min( top, cellOffset.top );
64 bottom = Math.max( bottom, cellOffset.bottom );
65 left = Math.min( left, cellOffset.left );
66 right = Math.max( right, cellOffset.right );
67 } );
68
69 // Browser tweaks to adjust for border-collapse:collapse
70 if ( !ve.test ) {
71 switch ( $.client.profile().layout ) {
72 case 'webkit':
73 right += 1;
74 bottom += 1;
75 break;
76 case 'gecko':
77 left -= 1;
78 top -= 1;
79 break;
80 }
81 }
82
83 const boundingRect = {
84 top: top,
85 bottom: bottom,
86 left: left,
87 right: right,
88 width: right - left,
89 height: bottom - top
90 };
91
92 if ( !boundingRect || !surfaceRect ) {
93 return null;
94 }
95 return ve.translateRect( boundingRect, -surfaceRect.left, -surfaceRect.top );
96 };
97
98 /**
99 * Get the bounding rectangle of the parent table
100 *
101 * @return {Object|null} Selection rectangle, with keys top, bottom, left, right, width, height
102 */
103 ve.ce.TableSelection.prototype.getTableBoundingRect = function () {
104 const surface = this.getSurface(),
105 tableNode = surface.getDocument().getBranchNodeFromOffset( this.model.tableRange.start + 1 );
106
107 if ( !tableNode ) {
108 return null;
109 }
110
111 const surfaceRect = surface.getSurface().getBoundingClientRect();
112 const boundingRect = tableNode.$element[ 0 ].getBoundingClientRect();
113
114 if ( !boundingRect || !surfaceRect ) {
115 return null;
116 }
117 return ve.translateRect( boundingRect, -surfaceRect.left, -surfaceRect.top );
118 };
119
120 /**
121 * @inheritdoc
122 */
123 ve.ce.TableSelection.prototype.isFocusedNode = function () {
124 return true;
125 };
126
127 /**
128 * @inheritdoc
129 */
130 ve.ce.TableSelection.prototype.isNativeCursor = function () {
131 return false;
132 };
133
134 /**
135 * @inheritdoc
136 */
137 ve.ce.TableSelection.prototype.getDirectionality = function ( doc ) {
138 if ( !this.directionality ) {
139 this.directionality = doc.getDirectionalityFromRange( this.getModel().tableRange );
140 }
141 return this.directionality;
142 };
143
144 /* Registration */
145
146 ve.ce.selectionFactory.register( ve.ce.TableSelection );