import type { InsertResult } from '@admin/utils/editor/insert-result' import type { EditorView } from '@codemirror/view' export function insertComponentAtCursor( view: EditorView | null, currentValue: string, snippet: string ): InsertResult { const wrapped = `\n${snippet}\n` if (view) { const { from } = view.state.selection.main view.dispatch({ changes: { from, insert: wrapped }, selection: { anchor: from + wrapped.length } }) return { newValue: currentValue.slice(0, from) + wrapped + currentValue.slice(from), newCursorPos: from + wrapped.length } } return { newValue: currentValue + wrapped, newCursorPos: currentValue.length + wrapped.length } }