-
-
Notifications
You must be signed in to change notification settings - Fork 361
/
cursor.ts
184 lines (169 loc) · 5.21 KB
/
cursor.ts
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
import {
caretRangeFromPoint,
clamp,
getCurrentRange,
isMultiLineRange,
resetNativeSelection,
SelectionEvent,
} from '../../__internal__/index.js';
export function repairContextMenuRange(e: SelectionEvent) {
const currentRange = window.getSelection()?.getRangeAt(0);
const pointRange = caretRangeFromPoint(e.raw.x, e.raw.y);
// repair browser context menu change selection can not go through blocks
if (
currentRange &&
pointRange &&
currentRange.isPointInRange(
pointRange.startContainer,
pointRange.startOffset
) &&
currentRange.isPointInRange(pointRange.endContainer, pointRange.endOffset)
) {
requestAnimationFrame(() => {
resetNativeSelection(currentRange);
});
}
}
export type DragDirection =
| 'right-bottom'
| 'right-top'
| 'left-bottom'
| 'left-top'
| 'center-bottom'
// no select direction, for example select all by `ctrl + a`
| 'directionless';
// XXX The type is not clear enough and needs to be redesigned
// text can be applied both text and paragraph formatting actions, while others can only be applied paragraph actions
export type SelectedBlockType = 'Text' | 'Caret' | 'Other';
export function getDragDirection(e: SelectionEvent): DragDirection {
const startX = e.start.x;
const startY = e.start.y;
const endX = e.x;
const endY = e.y;
// selection direction
const isForwards = endX > startX;
const range = getCurrentRange();
const selectedOneLine = !isMultiLineRange(range);
if (isForwards) {
if (selectedOneLine || endY >= startY) {
return 'right-bottom';
} else {
return 'right-top';
}
} else {
// backwards
if (selectedOneLine || endY <= startY) {
return 'left-top';
} else {
return 'left-bottom';
}
}
}
/**
* Return a base range for element positioning based on the current selection and selection info
*
* @example
* ```ts
* const { selectedType, direction } = getNativeSelectionMouseDragInfo(e);
* if (selectedType === 'Caret') {
* return;
* }
* ```
*/
export function getNativeSelectionMouseDragInfo(e: SelectionEvent) {
const curRange = getCurrentRange();
const direction = getDragDirection(e);
const isSelectedNothing =
curRange.collapsed ||
// If you try to drag from back to front on an empty line,
// you will get a empty range, but the `range.collapsed` is false,
// and the range has different startContainer and endContainer.
// So we need to check the length of the range
curRange.toString().length === 0;
const selectedType: SelectedBlockType = isSelectedNothing ? 'Caret' : 'Text';
return { selectedType, direction };
}
export function calcPositionPointByRange(
range: Range,
direction: DragDirection
) {
const { startContainer, startOffset, endContainer, endOffset } = range;
const isStartAnchor = direction.includes('top');
const [targetNode, offset] = isStartAnchor
? [startContainer, startOffset]
: [endContainer, endOffset];
const lineRange = new Range();
if (direction.includes('center')) {
// Use whole range
lineRange.setStart(startContainer, startOffset);
lineRange.setEnd(endContainer, endOffset);
} else {
lineRange.setStart(targetNode, offset);
}
// XXX the workaround is very ugly, please improve after you find a way to identify the range is empty
// Workaround select to empty line will get empty range
// If range is empty, range.getBoundingClientRect of range will return the empty value({ x: 0, y: 0 ...})
const tmpRect = lineRange.getBoundingClientRect();
const isWrongRect =
tmpRect.x === 0 &&
tmpRect.y === 0 &&
tmpRect.width === 0 &&
tmpRect.height === 0;
if (isWrongRect) {
// Fallback to use whole range
lineRange.setStart(startContainer, startOffset);
lineRange.setEnd(endContainer, endOffset);
}
// resetNativeSelection(lineRange);
const lineRect = lineRange.getBoundingClientRect();
const isBottom = direction.includes('bottom');
const positioningPoint = {
// Use the center of the position rect
x: lineRect.x + lineRect.width / 2,
// if the direction is bottom, use the bottom of the position rect
y: lineRect.y + (isBottom ? lineRect.height : 0),
};
return positioningPoint;
}
type CollisionBox = {
/**
* The point that the objRect is positioned to.
*/
positioningPoint: { x: number; y: number };
/**
* The boundary rect of the obj that is being positioned.
*/
objRect?: { height: number; width: number };
/**
* The boundary rect of the container that the obj is in.
*/
boundaryRect?: DOMRect;
offsetX?: number;
offsetY?: number;
edgeGap?: number;
};
export function calcSafeCoordinate({
positioningPoint,
objRect = { width: 0, height: 0 },
boundaryRect = document.body.getBoundingClientRect(),
offsetX = 0,
offsetY = 0,
edgeGap = 20,
}: CollisionBox) {
const safeX = clamp(
positioningPoint.x + offsetX,
edgeGap,
boundaryRect.width - objRect.width - edgeGap
);
const y = positioningPoint.y + offsetY;
// Not use clamp for y coordinate to avoid the quick bar always showing after scrolling
// const safeY = clamp(
// positioningPoint.y + offsetY,
// edgeGap,
// boundaryRect.height - objRect.height - edgeGap
// );
return {
x: safeX,
y,
};
}