1 import {MarkdownEditorInput, MarkdownEditorInputSelection} from "./interface";
2 import {EditorView} from "@codemirror/view";
3 import {ChangeSpec, TransactionSpec} from "@codemirror/state";
6 export class CodemirrorInput implements MarkdownEditorInput {
7 protected cm: EditorView;
9 constructor(cm: EditorView) {
14 if (!this.cm.hasFocus) {
19 getSelection(): MarkdownEditorInputSelection {
20 return this.cm.state.selection.main;
23 getSelectionText(selection?: MarkdownEditorInputSelection): string {
24 selection = selection || this.getSelection();
25 return this.cm.state.sliceDoc(selection.from, selection.to);
28 setSelection(selection: MarkdownEditorInputSelection, scrollIntoView: boolean = false) {
30 selection: {anchor: selection.from, head: selection.to},
36 return this.cm.state.doc.toString();
39 getTextAboveView(): string {
40 const blockInfo = this.cm.lineBlockAtHeight(this.cm.scrollDOM.scrollTop);
41 return this.cm.state.sliceDoc(0, blockInfo.from);
44 setText(text: string, selection?: MarkdownEditorInputSelection) {
45 selection = selection || this.getSelection();
46 const newDoc = this.cm.state.toText(text);
47 const newSelectFrom = Math.min(selection.from, newDoc.length);
48 const scrollTop = this.cm.scrollDOM.scrollTop;
49 this.dispatchChange(0, this.cm.state.doc.length, text, newSelectFrom);
51 window.requestAnimationFrame(() => {
52 this.cm.scrollDOM.scrollTop = scrollTop;
56 spliceText(from: number, to: number, newText: string, selection: Partial<MarkdownEditorInputSelection> | null = null) {
57 const end = (selection?.from === selection?.to) ? null : selection?.to;
58 this.dispatchChange(from, to, newText, selection?.from, end)
61 appendText(text: string) {
62 const end = this.cm.state.doc.length;
63 this.dispatchChange(end, end, `\n${text}`);
66 getLineText(lineIndex: number = -1): string {
67 const index = lineIndex > -1 ? lineIndex : this.getSelection().from;
68 return this.cm.state.doc.lineAt(index).text;
71 coordsToSelection(x: number, y: number): MarkdownEditorInputSelection {
72 const cursorPos = this.cm.posAtCoords({x, y}, false);
73 return {from: cursorPos, to: cursorPos};
76 getLineRangeFromPosition(position: number): MarkdownEditorInputSelection {
77 const line = this.cm.state.doc.lineAt(position);
78 return {from: line.from, to: line.to};
81 searchForLineContaining(text: string): MarkdownEditorInputSelection | null {
82 const docText = this.cm.state.doc;
84 let scrollToLine = -1;
85 for (const line of docText.iterLines()) {
86 if (line.includes(text)) {
87 scrollToLine = lineCount;
93 if (scrollToLine === -1) {
97 const line = docText.line(scrollToLine);
98 return {from: line.from, to: line.to};
102 * Dispatch changes to the editor.
104 protected dispatchChange(from: number, to: number|null = null, text: string|null = null, selectFrom: number|null = null, selectTo: number|null = null): void {
105 const change: ChangeSpec = {from};
110 change.insert = text;
112 const tr: TransactionSpec = {changes: change};
115 tr.selection = {anchor: selectFrom};
117 tr.selection.head = selectTo;
121 this.cm.dispatch(tr);