mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-05-09 20:38:40 +00:00
make wrapselection work with multiple selections
This commit is contained in:
@@ -29,27 +29,25 @@ const indentLess = (view)=>{
|
|||||||
};
|
};
|
||||||
|
|
||||||
const wrapSelection = (prefix, suffix) => (view) => {
|
const wrapSelection = (prefix, suffix) => (view) => {
|
||||||
const { from, to } = view.state.selection.main;
|
const changes = [];
|
||||||
|
|
||||||
|
for(const range of view.state.selection.ranges) {
|
||||||
|
const { from, to } = range;
|
||||||
const selected = view.state.doc.sliceString(from, to);
|
const selected = view.state.doc.sliceString(from, to);
|
||||||
|
|
||||||
let text, selection;
|
let text;
|
||||||
|
|
||||||
if(from === to) {
|
if(from === to) { text = prefix + suffix }
|
||||||
text = prefix + suffix;
|
|
||||||
selection = { anchor: from + prefix.length, head: from + prefix.length };
|
|
||||||
}
|
|
||||||
else if(selected.startsWith(prefix) && selected.endsWith(suffix)) {
|
else if(selected.startsWith(prefix) && selected.endsWith(suffix)) {
|
||||||
text = selected.slice(prefix.length, -suffix.length);
|
text = selected.slice(prefix.length, -suffix.length);
|
||||||
selection = { anchor: from, head: from + text.length };
|
|
||||||
}
|
}
|
||||||
else {
|
else {text = `${prefix}${selected}${suffix}`}
|
||||||
text = `${prefix}${selected}${suffix}`;
|
|
||||||
selection = { anchor: from, head: from + text.length };
|
changes.push({ from, to, insert: text });
|
||||||
}
|
}
|
||||||
|
|
||||||
view.dispatch({
|
view.dispatch({
|
||||||
changes : { from, to, insert: text },
|
changes
|
||||||
selection
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in New Issue
Block a user