-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathcodemirror.ts
69 lines (65 loc) · 2.41 KB
/
codemirror.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
import { Selectors } from '../compass';
import type { CompassBrowser } from '../compass-browser';
export async function getCodemirrorEditorText(
browser: CompassBrowser,
selector: string = Selectors.DocumentJSONEntry
) {
// Codemirror uses virtual rendering, to get full text content of the editor,
// we have to find an instance of the editor and get the text directly from
// its state
const editorContents = await browser.execute(function (selector) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Accessing private Codemirror state
const node: any =
// eslint-disable-next-line no-restricted-globals
document.querySelector(`${selector} [data-codemirror]`) ??
// eslint-disable-next-line no-restricted-globals
document.querySelector(`${selector}[data-codemirror]`);
return node._cm.state.sliceDoc() as string;
}, selector);
return editorContents;
}
export async function getCodemirrorEditorTextAll(
browser: CompassBrowser,
selector: string = Selectors.DocumentJSONEntry
) {
// Codemirror uses virtual rendering, to get full text content of the editor,
// we have to find an instance of the editor and get the text directly from
// its state
const editorContents = await browser.execute(function (selector) {
const editors = Array.from(
// eslint-disable-next-line no-restricted-globals
document.querySelectorAll(`${selector} [data-codemirror]`)
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Accessing private Codemirror state
return editors.map((node: any) => {
return node._cm.state.sliceDoc() as string;
});
}, selector);
return editorContents;
}
export async function setCodemirrorEditorValue(
browser: CompassBrowser,
selector: string,
text: string
) {
await browser.execute(
function (selector, text) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Accessing private Codemirror state
const node: any =
// eslint-disable-next-line no-restricted-globals
document.querySelector(`${selector} [data-codemirror]`) ??
// eslint-disable-next-line no-restricted-globals
document.querySelector(`${selector}[data-codemirror]`);
const editor = node._cm;
editor.dispatch({
changes: {
from: 0,
to: editor.state.doc.length,
insert: text,
},
});
},
selector,
text
);
}