From 2c4dea94fe5609c1cd4c2ed297a21e1b78409786 Mon Sep 17 00:00:00 2001 From: jedwoj Date: Tue, 5 Dec 2023 23:28:57 +0100 Subject: [PATCH] Simulation of Enter press after pasting to editor --- system/libs/figa-ui/src/lib/code/setup.ts | 27 +++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/system/libs/figa-ui/src/lib/code/setup.ts b/system/libs/figa-ui/src/lib/code/setup.ts index a70cfc325..320befb13 100644 --- a/system/libs/figa-ui/src/lib/code/setup.ts +++ b/system/libs/figa-ui/src/lib/code/setup.ts @@ -1,6 +1,10 @@ import { basicSetup } from 'codemirror'; import { EditorView, keymap } from '@codemirror/view'; -import { EditorState, type Extension } from '@codemirror/state'; +import { + EditorState, + type Extension, + EditorSelection, +} from '@codemirror/state'; import type { SetupConfig } from './defs'; import { indentWithTab } from '@codemirror/commands'; import { HighlightStyle, syntaxHighlighting } from '@codemirror/language'; @@ -37,7 +41,6 @@ const createBasicExtensions = ({ ), ]; const extensions: Extension[] = []; - extensions.push(themeSetup); extensions.push(keymap.of([indentWithTab])); @@ -54,6 +57,26 @@ const createBasicExtensions = ({ extensions.push(setup); + const handlePasteExtension = EditorView.domEventHandlers({ + paste: (_, view) => { + // setTimeout allows the paste to complete before we get the cursor position + setTimeout(() => { + const { state } = view; + const cursorPosition = state.selection.main.head; + view.dispatch({ + // Insert a newline at the cursor position + changes: { from: cursorPosition, insert: '\n' }, + // Move the cursor to the next line + selection: EditorSelection.cursor(cursorPosition + 1), + // Scroll the editor to the cursor position + scrollIntoView: true, + }); + }, 0); + }, + }); + + extensions.push(handlePasteExtension); + return extensions; };