| 10 | import { getCodeMirrorView, getProjectId } from "../libs/helpers"; |
| 11 | |
| 12 | export class OverleafAdapter implements DocumentAdapter { |
| 13 | readonly platform = "overleaf" as const; |
| 14 | |
| 15 | private getEditorView(): EditorView | null { |
| 16 | return getCodeMirrorView(); |
| 17 | } |
| 18 | |
| 19 | isReady(): boolean { |
| 20 | return this.getEditorView() !== null; |
| 21 | } |
| 22 | |
| 23 | async getFullText(): Promise<string> { |
| 24 | const view = this.getEditorView(); |
| 25 | if (!view) { |
| 26 | return ""; |
| 27 | } |
| 28 | return view.state.doc.toString(); |
| 29 | } |
| 30 | |
| 31 | async getSelection(): Promise<SelectionInfo | null> { |
| 32 | const view = this.getEditorView(); |
| 33 | if (!view) { |
| 34 | return null; |
| 35 | } |
| 36 | |
| 37 | const selection = view.state.selection.main; |
| 38 | if (selection.empty) { |
| 39 | return null; |
| 40 | } |
| 41 | |
| 42 | const text = view.state.sliceDoc(selection.from, selection.to); |
| 43 | const doc = view.state.doc; |
| 44 | |
| 45 | // Get surrounding context |
| 46 | const contextBefore = doc.sliceString(Math.max(0, selection.from - 100), selection.from); |
| 47 | const contextAfter = doc.sliceString(selection.to, Math.min(doc.length, selection.to + 100)); |
| 48 | const surroundingText = `${contextBefore}[SELECTED_TEXT_START]${text}[SELECTED_TEXT_END]${contextAfter}`; |
| 49 | |
| 50 | // Create a range identifier that can be used later |
| 51 | const rangeId = JSON.stringify({ from: selection.from, to: selection.to }); |
| 52 | |
| 53 | return { |
| 54 | text, |
| 55 | surroundingText, |
| 56 | rangeId, |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | async insertText(text: string, location: "cursor" | "start" | "end" = "cursor"): Promise<void> { |
| 61 | const view = this.getEditorView(); |
| 62 | if (!view) { |
| 63 | throw new Error("Editor not available"); |
| 64 | } |
| 65 | |
| 66 | let pos: number; |
| 67 | switch (location) { |
| 68 | case "start": |
| 69 | pos = 0; |
nothing calls this directly
no outgoing calls
no test coverage detected