From 6243c27b0827875d697d4a4119d084108dd8f294 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Losada=20Hern=C3=A1ndez?= Date: Sun, 20 Sep 2026 20:43:14 +0200 Subject: [PATCH] fix wrapselection to apply cursor in center of selection on every instance of cursors correctly. --- .../codeEditor/extensions/customKeyMaps.js | 72 +++++++++++++++---- 1 file changed, 57 insertions(+), 15 deletions(-) diff --git a/client/components/codeEditor/extensions/customKeyMaps.js b/client/components/codeEditor/extensions/customKeyMaps.js index a5858150d..768a66f81 100644 --- a/client/components/codeEditor/extensions/customKeyMaps.js +++ b/client/components/codeEditor/extensions/customKeyMaps.js @@ -1,4 +1,4 @@ -/* eslint max-lines: ["error", { "max": 300 }] */ +/* eslint max-lines: ["error", { "max": 400 }] */ import { keymap } from '@codemirror/view'; import { undo, redo, indentMore, indentLess, deleteLine } from '@codemirror/commands'; import { EditorSelection } from '@codemirror/state'; @@ -92,25 +92,67 @@ const insertTab = (view)=>{ return true; }; -const wrapSelection = (prefix, suffix)=>(view)=>{ - const changes = []; +const wrapSelection = (prefix, suffix) => (view) => { + view.dispatch( + view.state.changeByRange((range) => { + const { from, to } = range; + const doc = view.state.doc; - for (const range of view.state.selection.ranges) { - const { from, to } = range; - const selected = view.state.doc.sliceString(from, to); + if (from === to) { + return { + changes: { + from, + to, + insert: prefix + suffix + }, + range: EditorSelection.cursor(from + prefix.length) + }; + } - let text; + const before = doc.sliceString( + Math.max(0, from - prefix.length), + from + ); - if(from === to) { text = prefix + suffix; } else if(selected.startsWith(prefix) && selected.endsWith(suffix)) { - text = selected.slice(prefix.length, -suffix.length); - } else {text = `${prefix}${selected}${suffix}`;} + const after = doc.sliceString( + to, + to + suffix.length + ); - changes.push({ from, to, insert: text }); - } + if (before === prefix && after === suffix) { + return { + changes: [ + { + from: from - prefix.length, + to, + insert: "" + }, + { + from: to, + to: to + suffix.length, + insert: "" + } + ], + range: EditorSelection.range( + from - prefix.length, + to - prefix.length + ) + }; + } - view.dispatch({ - changes - }); + return { + changes: { + from, + to, + insert: prefix + doc.sliceString(from, to) + suffix + }, + range: EditorSelection.range( + from + prefix.length, + to + prefix.length + ) + }; + }) + ); return true; };