mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-09-28 04:32:58 +00:00
inserttab to use foreachselection
This commit is contained in:
@@ -65,36 +65,25 @@ const forEachSelection = (view, fn)=>{
|
|||||||
const insertTab = (view)=>{
|
const insertTab = (view)=>{
|
||||||
// If any selection spans multiple lines, delegates to CodeMirror's indentMore
|
// If any selection spans multiple lines, delegates to CodeMirror's indentMore
|
||||||
// Otherwise inserts two spaces at each cursor/selection
|
// Otherwise inserts two spaces at each cursor/selection
|
||||||
const shouldIndent = view.state.selection.ranges.some((range)=>view.state.doc.lineAt(range.from).number !==
|
const shouldIndent = view.state.selection.ranges.some((range)=>
|
||||||
|
view.state.doc.lineAt(range.from).number !==
|
||||||
view.state.doc.lineAt(range.to).number
|
view.state.doc.lineAt(range.to).number
|
||||||
);
|
);
|
||||||
|
|
||||||
if(shouldIndent) return indentMore(view);
|
if(shouldIndent) return indentMore(view);
|
||||||
|
|
||||||
const changes = [];
|
return forEachSelection(view, (state, range)=>{
|
||||||
|
const { from, to } = range;
|
||||||
|
|
||||||
for (const range of view.state.selection.ranges) {
|
return {
|
||||||
changes.push({
|
changes : {
|
||||||
from : range.from,
|
from,
|
||||||
to : range.to,
|
to,
|
||||||
insert : ' ' // Insert two spaces, not a tab char!
|
insert : ' ' // Insert two spaces, not a tab char!
|
||||||
|
},
|
||||||
|
range : EditorSelection.cursor(from + 2)
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
|
||||||
// Create a transaction so we can map old positions to
|
|
||||||
// their new positions after the edits are applied
|
|
||||||
const mappedChanges = view.state.update({ changes });
|
|
||||||
|
|
||||||
view.dispatch({
|
|
||||||
changes,
|
|
||||||
selection : EditorSelection.create(
|
|
||||||
view.state.selection.ranges.map((range)=>EditorSelection.cursor(
|
|
||||||
mappedChanges.changes.mapPos(range.from, -1) + 2
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
return true;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const wrapSelection = (prefix, suffix)=>(view)=>forEachSelection(
|
const wrapSelection = (prefix, suffix)=>(view)=>forEachSelection(
|
||||||
|
|||||||
Reference in New Issue
Block a user