forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-text-around-cursor.ts
More file actions
147 lines (124 loc) · 3.53 KB
/
Copy pathget-text-around-cursor.ts
File metadata and controls
147 lines (124 loc) · 3.53 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
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
import {
Editor,
Node,
Path,
Range,
Text,
Element,
BasePoint,
BaseRange,
Point,
} from "slate";
import { EditorAutocompleteState } from "../types/base/editor-autocomplete-state";
export interface EditorTextState {
selection: BaseRange;
textBeforeCursor: string;
selectedText: string;
textAfterCursor: string;
}
export function getTextAroundCollapsedCursor(
editor: Editor,
): EditorAutocompleteState | null {
const { selection } = editor;
if (!selection || !Range.isCollapsed(selection)) {
return null;
}
const cursorPoint = selection.anchor;
// Create two ranges: one before the anchor and one after
const beforeRange: Range = {
anchor: Editor.start(editor, []),
focus: cursorPoint,
};
const afterRange: Range = {
anchor: cursorPoint,
focus: Editor.end(editor, []),
};
// Extract text for these ranges
const before = extractTextWithNewlines(editor, beforeRange);
const after = extractTextWithNewlines(editor, afterRange);
return {
cursorPoint: cursorPoint,
textBeforeCursor: before,
textAfterCursor: after,
};
}
export function getTextAroundSelection(editor: Editor): EditorTextState | null {
const { selection } = editor;
if (!selection) {
return null;
}
const wellOrderedSelection = wellOrderedRange(selection);
// Create two ranges: one before the anchor and one after
const beforeRange: Range = {
anchor: Editor.start(editor, []),
focus: wellOrderedSelection.anchor,
};
const afterRange: Range = {
anchor: wellOrderedSelection.focus,
focus: Editor.end(editor, []),
};
// Extract text for these ranges
const before = extractTextWithNewlines(editor, beforeRange);
const after = extractTextWithNewlines(editor, afterRange);
const selectedText = extractTextWithNewlines(editor, wellOrderedSelection);
return {
selection: wellOrderedSelection,
textBeforeCursor: before,
selectedText,
textAfterCursor: after,
};
}
export function getFullEditorTextWithNewlines(editor: Editor): string {
const fullDocumentRange: Range = {
anchor: Editor.start(editor, []),
focus: Editor.end(editor, []),
};
return extractTextWithNewlines(editor, fullDocumentRange);
}
// Helper function to extract text with newlines
export function extractTextWithNewlines(editor: Editor, range: Range): string {
const voids = false;
const [start, end] = Range.edges(range);
let text = "";
let lastBlock: Node | null = null;
for (const [node, path] of Editor.nodes(editor, {
at: range,
match: Text.isText,
voids,
})) {
let t = node.text;
// Determine the parent block of the current text node
const [block] = Editor.above(editor, {
at: path,
match: (n) => Element.isElement(n) && n.type === "paragraph",
}) || [null];
// If we encounter a new block, prepend a newline
if (lastBlock !== block && block) {
// check that lastBlock is not null to avoid adding a newline at the beginning
if (lastBlock) {
text += "\n";
}
lastBlock = block;
}
if (Path.equals(path, end.path)) {
t = t.slice(0, end.offset);
}
if (Path.equals(path, start.path)) {
t = t.slice(start.offset);
}
text += t;
}
return text;
}
function wellOrderedRange(range: BaseRange): BaseRange {
const { anchor, focus } = range;
// if anchor is before focus, return range as is
if (Point.isBefore(anchor, focus)) {
return range;
}
// if focus is before anchor, return range with anchor and focus swapped
return {
anchor: focus,
focus: anchor,
};
}