wrapselection to work with foreachselection

This commit is contained in:
Víctor Losada Hernández
2026-09-27 22:58:08 +02:00
parent 3ec85b6f52
commit 7b1c576eb7
@@ -97,53 +97,41 @@ const insertTab = (view)=>{
return true;
};
const wrapSelection = (prefix, suffix)=>(view)=>{
view.dispatch(
view.state.changeByRange((range)=>{
const wrapSelection = (prefix, suffix)=>(view)=>forEachSelection(
view,
(state, range)=>{
const { from, to } = range;
const noSelection = from === to;
const doc = view.state.doc;
const selected = state.doc.sliceString(from, to);
if(noSelection) {
const insert = prefix + suffix;
return {
changes : {
from,
to,
insert : prefix + suffix
},
changes : { from, to, insert },
range : EditorSelection.cursor(from + prefix.length)
};
}
const before = doc.sliceString(
Math.max(0, from),
from + prefix.length
);
const hasWrapper =
selected.startsWith(prefix) &&
selected.endsWith(suffix);
const after = doc.sliceString(
to - suffix.length,
to
);
const alreadyWrapped = before === prefix && after === suffix;
if(alreadyWrapped) {
if(hasWrapper) {
return {
changes : [
{
from : from,
to : from + prefix.length,
from : to - suffix.length,
to,
insert : ''
},
{
from : to - suffix.length,
to : to,
from,
to : from + prefix.length,
insert : ''
}
],
range : EditorSelection.range(
from,
to - prefix.length - suffix.length,
)
range : EditorSelection.range(from, to - prefix.length - suffix.length)
};
}
@@ -151,18 +139,16 @@ const wrapSelection = (prefix, suffix)=>(view)=>{
changes : {
from,
to,
insert : prefix + doc.sliceString(from, to) + suffix
insert : prefix + selected + suffix
},
range : EditorSelection.range(
from,
to + suffix.length + prefix.length
to + prefix.length + suffix.length
)
};
})
);
}
);
return true;
};
const makeNbsp = (view)=>forEachSelection(view, (state, range)=>{
const { from, to } = range;