fix wrapping of wrapselection for selection to accomodate prefixes and suffixes

This commit is contained in:
Víctor Losada Hernández
2026-09-27 16:48:55 +02:00
parent 304d78526e
commit cf83806759
@@ -96,9 +96,11 @@ const wrapSelection = (prefix, suffix) => (view) => {
view.dispatch( view.dispatch(
view.state.changeByRange((range) => { view.state.changeByRange((range) => {
const { from, to } = range; const { from, to } = range;
const noSelection = from === to;
const doc = view.state.doc; const doc = view.state.doc;
if (from === to) { if (noSelection) {
return { return {
changes: { changes: {
from, from,
@@ -110,32 +112,32 @@ const wrapSelection = (prefix, suffix) => (view) => {
} }
const before = doc.sliceString( const before = doc.sliceString(
Math.max(0, from - prefix.length), Math.max(0, from),
from from + prefix.length
); );
const after = doc.sliceString( const after = doc.sliceString(
to, to - suffix.length,
to + suffix.length to
); );
const alreadyWrapped = before === prefix && after === suffix;
if (before === prefix && after === suffix) { if (alreadyWrapped) {
return { return {
changes: [ changes: [
{ {
from: from - prefix.length, from: from,
to, to: from + prefix.length,
insert: "" insert: ""
}, },
{ {
from: to, from: to - suffix.length,
to: to + suffix.length, to: to,
insert: "" insert: ""
} }
], ],
range: EditorSelection.range( range: EditorSelection.range(
from - prefix.length, from,
to - prefix.length to - prefix.length - suffix.length,
) )
}; };
} }
@@ -147,8 +149,8 @@ const wrapSelection = (prefix, suffix) => (view) => {
insert: prefix + doc.sliceString(from, to) + suffix insert: prefix + doc.sliceString(from, to) + suffix
}, },
range: EditorSelection.range( range: EditorSelection.range(
from + prefix.length, from,
to + prefix.length to + suffix.length*2
) )
}; };
}) })