forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlineNumberSelection.ts
More file actions
63 lines (56 loc) · 1.63 KB
/
Copy pathlineNumberSelection.ts
File metadata and controls
63 lines (56 loc) · 1.63 KB
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
import { EditorSelection } from "@codemirror/state";
import type { BlockInfo, EditorView } from "@codemirror/view";
type LineInfo = Pick<BlockInfo, "from" | "to"> | null | undefined;
type LineNumberClickEvent = Pick<
MouseEvent,
| "button"
| "shiftKey"
| "altKey"
| "ctrlKey"
| "metaKey"
| "preventDefault"
| "defaultPrevented"
>;
/**
* Resolve the selection range for a clicked document line.
* Includes the trailing line break when one exists to mirror Ace's
* full-line selection behavior.
*/
export function getLineSelectionRange(
state: EditorView["state"],
line: LineInfo,
): { from: number; to: number } | null {
if (!line) return null;
const from = Math.max(0, Number(line.from) || 0);
const to = Math.max(from, Number(line.to) || from);
return {
from,
to: Math.min(to + 1, state.doc.length),
};
}
/**
* Select the clicked line from the line-number gutter.
* Ignores modified and non-primary clicks so it doesn't interfere with
* context menus or alternate selection gestures.
*/
export function handleLineNumberClick(
view: EditorView | null | undefined,
line: LineInfo,
event: LineNumberClickEvent | null | undefined,
): boolean {
if (!view || !event || event.defaultPrevented) return false;
if ((event.button ?? 0) !== 0) return false;
if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {
return false;
}
const range = getLineSelectionRange(view.state, line);
if (!range) return false;
event.preventDefault();
view.dispatch({
selection: EditorSelection.single(range.from, range.to),
userEvent: "select.pointer",
});
view.focus();
return true;
}
export default handleLineNumberClick;