mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-27 18:12:29 +08:00
fix(code): pressing tab while multiple lines are selected would replace with tab
This commit is contained in:
parent
03de472a57
commit
9e3909a5f7
@ -24,19 +24,38 @@ const smartIndentWithTab: KeyBinding[] = [
|
|||||||
|
|
||||||
// Step 1: Handle non-empty selections → replace with tab
|
// Step 1: Handle non-empty selections → replace with tab
|
||||||
if (selection.ranges.some(range => !range.empty)) {
|
if (selection.ranges.some(range => !range.empty)) {
|
||||||
for (let range of selection.ranges) {
|
// If multiple lines are selected, insert a tab character at the start of each line
|
||||||
changes.push({ from: range.from, to: range.to, insert: "\t" });
|
// and move the cursor to the position after the tab character.
|
||||||
newSelections.push(EditorSelection.cursor(range.from + 1));
|
const linesCovered = new Set<number>();
|
||||||
|
for (const range of selection.ranges) {
|
||||||
|
const startLine = state.doc.lineAt(range.from);
|
||||||
|
const endLine = state.doc.lineAt(range.to);
|
||||||
|
|
||||||
|
for (let lineNumber = startLine.number; lineNumber <= endLine.number; lineNumber++) {
|
||||||
|
linesCovered.add(lineNumber);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (linesCovered.size > 1) {
|
||||||
|
// Multiple lines are selected, indent each line.
|
||||||
|
return indentMore({ state, dispatch });
|
||||||
|
} else {
|
||||||
|
// Single line selection, replace with tab.
|
||||||
|
for (let range of selection.ranges) {
|
||||||
|
changes.push({ from: range.from, to: range.to, insert: "\t" });
|
||||||
|
newSelections.push(EditorSelection.cursor(range.from + 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
dispatch(
|
||||||
|
state.update({
|
||||||
|
changes,
|
||||||
|
selection: EditorSelection.create(newSelections),
|
||||||
|
scrollIntoView: true,
|
||||||
|
userEvent: "input"
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch(
|
|
||||||
state.update({
|
|
||||||
changes,
|
|
||||||
selection: EditorSelection.create(newSelections),
|
|
||||||
scrollIntoView: true,
|
|
||||||
userEvent: "input"
|
|
||||||
})
|
|
||||||
);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user