|
| 1 | +import { useMonaco } from '@guolao/vue-monaco-editor'; |
| 2 | +import { editor, Range } from 'monaco-editor'; |
| 3 | +import type { MonacoTheme, SerializedRange } from 'shared/src'; |
| 4 | +import { shallowRef, watch } from 'vue'; |
| 5 | + |
| 6 | +export function useMonacoEditor() { |
| 7 | + const currentEditor = shallowRef<editor.IStandaloneCodeEditor>(); |
| 8 | + const monaco = useMonaco(); |
| 9 | + |
| 10 | + const MONACO_EDITOR_OPTIONS = { |
| 11 | + minimap: { enabled: false }, |
| 12 | + readOnly: true, |
| 13 | + lineNumbers: "on", |
| 14 | + scrollbar: { vertical: "hidden", horizontal: "auto" }, |
| 15 | + scrollBeyondLastLine: false, |
| 16 | + stickyScrolling: false, |
| 17 | + contextmenu: false, |
| 18 | + } as editor.IEditorOptions; |
| 19 | + |
| 20 | + function setEditor(edit: editor.IStandaloneCodeEditor) { |
| 21 | + currentEditor.value = edit; |
| 22 | + } |
| 23 | + |
| 24 | + function setDecorators(editor: editor.IStandaloneCodeEditor, position: SerializedRange) { |
| 25 | + try { |
| 26 | + const currentDecorations = editor.getDecorationsInRange(new Range(1, 1, 9999999, 999999)); |
| 27 | + editor.removeDecorations(currentDecorations?.map(e => e.id) ?? []); |
| 28 | + } catch { |
| 29 | + /* nop */ |
| 30 | + } |
| 31 | + |
| 32 | + const { startLine, startCharacter, endLine, endCharacter } = position; |
| 33 | + editor.createDecorationsCollection([{ |
| 34 | + range: new Range( |
| 35 | + (startLine ?? 0) + 1, |
| 36 | + startCharacter ?? 1, |
| 37 | + endLine ?? (startLine ?? 0) + 1, |
| 38 | + endCharacter ?? 9999), |
| 39 | + |
| 40 | + options: { |
| 41 | + isWholeLine: true, |
| 42 | + className: 'highlight' |
| 43 | + } |
| 44 | + }]); |
| 45 | + } |
| 46 | + |
| 47 | + function layoutEditor(editor: editor.IStandaloneCodeEditor) { |
| 48 | + const lineCount = editor.getModel()?.getLineCount() ?? 0; |
| 49 | + const lineHeight = 18; // Standard line height for Monaco |
| 50 | + |
| 51 | + // Calculate height based on line count, not scroll height |
| 52 | + const contentHeight = lineCount * lineHeight; |
| 53 | + const targetHeight = Math.min(Math.max(contentHeight + 10, 50), 400); // min 50px, max 400px |
| 54 | + |
| 55 | + // Use CSS-based width instead of calculating container width |
| 56 | + editor.layout({ width: 100, height: targetHeight }); |
| 57 | + } |
| 58 | + |
| 59 | + function setTheme(monacoTheme: MonacoTheme) { |
| 60 | + try { |
| 61 | + const themeName = 'tmp'; |
| 62 | + monaco.monacoRef.value?.editor.defineTheme(themeName, monacoTheme as editor.IStandaloneThemeData); |
| 63 | + monaco.monacoRef.value?.editor.setTheme(themeName); |
| 64 | + } catch (e) { |
| 65 | + console.error("error setting theme", e); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + function setupScrollPrevention(editor: editor.IStandaloneCodeEditor) { |
| 70 | + const domNode = editor?.getDomNode(); |
| 71 | + domNode?.addEventListener("scroll", e => e.stopImmediatePropagation(), { capture: true }); |
| 72 | + domNode?.addEventListener("wheel", e => e.stopImmediatePropagation(), { capture: true }); |
| 73 | + } |
| 74 | + |
| 75 | + function setupResizeObserver(editor: editor.IStandaloneCodeEditor) { |
| 76 | + const domNode = editor?.getDomNode(); |
| 77 | + if (!domNode) return; |
| 78 | + |
| 79 | + let timeoutId: number; |
| 80 | + const resizeObserver = new ResizeObserver(() => { |
| 81 | + clearTimeout(timeoutId); |
| 82 | + timeoutId = window.setTimeout(() => { |
| 83 | + const container = domNode.parentElement; |
| 84 | + if (container) { |
| 85 | + const containerRect = container.getBoundingClientRect(); |
| 86 | + const targetWidth = Math.max(containerRect.width - 10, 100); |
| 87 | + const currentLayout = editor.getLayoutInfo(); |
| 88 | + editor.layout({ width: targetWidth, height: currentLayout.height }); |
| 89 | + } |
| 90 | + }, 150); |
| 91 | + }); |
| 92 | + |
| 93 | + const container = domNode.parentElement; |
| 94 | + if (container) { |
| 95 | + resizeObserver.observe(container); |
| 96 | + } |
| 97 | + |
| 98 | + editor.onDidDispose(() => { |
| 99 | + clearTimeout(timeoutId); |
| 100 | + resizeObserver.disconnect(); |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + // Watch for theme changes |
| 105 | + watch([monaco], () => { |
| 106 | + // Theme watching logic can be added here if needed |
| 107 | + }); |
| 108 | + |
| 109 | + return { |
| 110 | + currentEditor, |
| 111 | + monaco, |
| 112 | + MONACO_EDITOR_OPTIONS, |
| 113 | + setEditor, |
| 114 | + setDecorators, |
| 115 | + layoutEditor, |
| 116 | + setTheme, |
| 117 | + setupScrollPrevention, |
| 118 | + setupResizeObserver |
| 119 | + }; |
| 120 | +} |
0 commit comments