mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-27 18:12:29 +08:00
refactor(codemirror): split custom tab functionality into more methods
This commit is contained in:
parent
9e3909a5f7
commit
56b7965c9a
@ -1,5 +1,5 @@
|
|||||||
import { indentLess, indentMore } from "@codemirror/commands";
|
import { indentLess, indentMore } from "@codemirror/commands";
|
||||||
import { EditorSelection, EditorState, SelectionRange, type ChangeSpec } from "@codemirror/state";
|
import { EditorSelection, EditorState, SelectionRange, type Transaction, type ChangeSpec } from "@codemirror/state";
|
||||||
import type { KeyBinding } from "@codemirror/view";
|
import type { KeyBinding } from "@codemirror/view";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -19,8 +19,6 @@ const smartIndentWithTab: KeyBinding[] = [
|
|||||||
}
|
}
|
||||||
|
|
||||||
const { selection } = state;
|
const { selection } = state;
|
||||||
const changes: ChangeSpec[] = [];
|
|
||||||
const newSelections: SelectionRange[] = [];
|
|
||||||
|
|
||||||
// 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)) {
|
||||||
@ -40,55 +38,69 @@ const smartIndentWithTab: KeyBinding[] = [
|
|||||||
// Multiple lines are selected, indent each line.
|
// Multiple lines are selected, indent each line.
|
||||||
return indentMore({ state, dispatch });
|
return indentMore({ state, dispatch });
|
||||||
} else {
|
} else {
|
||||||
// Single line selection, replace with tab.
|
return handleSingleLineSelection(state, dispatch);
|
||||||
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"
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 2: Handle empty selections
|
// Step 2: Handle empty selections
|
||||||
for (let range of selection.ranges) {
|
return handleEmptySelections(state, dispatch);
|
||||||
const line = state.doc.lineAt(range.head);
|
|
||||||
const beforeCursor = state.doc.sliceString(line.from, range.head);
|
|
||||||
|
|
||||||
if (/^\s*$/.test(beforeCursor)) {
|
|
||||||
// Only whitespace before cursor → indent line
|
|
||||||
return indentMore({ state, dispatch });
|
|
||||||
} else {
|
|
||||||
// Insert tab character at cursor
|
|
||||||
changes.push({ from: range.head, to: range.head, insert: "\t" });
|
|
||||||
newSelections.push(EditorSelection.cursor(range.head + 1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (changes.length) {
|
|
||||||
dispatch(
|
|
||||||
state.update({
|
|
||||||
changes,
|
|
||||||
selection: EditorSelection.create(newSelections),
|
|
||||||
scrollIntoView: true,
|
|
||||||
userEvent: "input"
|
|
||||||
})
|
|
||||||
);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
},
|
},
|
||||||
shift: indentLess
|
shift: indentLess
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
export default smartIndentWithTab;
|
export default smartIndentWithTab;
|
||||||
|
|
||||||
|
function handleSingleLineSelection(state: EditorState, dispatch: (transaction: Transaction) => void) {
|
||||||
|
const changes: ChangeSpec[] = [];
|
||||||
|
const newSelections: SelectionRange[] = [];
|
||||||
|
|
||||||
|
// Single line selection, replace with tab.
|
||||||
|
for (let range of state.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"
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleEmptySelections(state: EditorState, dispatch: (transaction: Transaction) => void) {
|
||||||
|
const changes: ChangeSpec[] = [];
|
||||||
|
const newSelections: SelectionRange[] = [];
|
||||||
|
|
||||||
|
for (let range of state.selection.ranges) {
|
||||||
|
const line = state.doc.lineAt(range.head);
|
||||||
|
const beforeCursor = state.doc.sliceString(line.from, range.head);
|
||||||
|
|
||||||
|
if (/^\s*$/.test(beforeCursor)) {
|
||||||
|
// Only whitespace before cursor → indent line
|
||||||
|
return indentMore({ state, dispatch });
|
||||||
|
} else {
|
||||||
|
// Insert tab character at cursor
|
||||||
|
changes.push({ from: range.head, to: range.head, insert: "\t" });
|
||||||
|
newSelections.push(EditorSelection.cursor(range.head + 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (changes.length) {
|
||||||
|
dispatch(
|
||||||
|
state.update({
|
||||||
|
changes,
|
||||||
|
selection: EditorSelection.create(newSelections),
|
||||||
|
scrollIntoView: true,
|
||||||
|
userEvent: "input"
|
||||||
|
})
|
||||||
|
);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user