mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	chore(code): support multiple selections in tab
This commit is contained in:
		| @@ -1,4 +1,5 @@ | ||||
| import { indentLess, indentMore } from "@codemirror/commands"; | ||||
| import { EditorSelection, type ChangeSpec } from "@codemirror/state"; | ||||
| import type { KeyBinding } from "@codemirror/view"; | ||||
|  | ||||
| const smartIndentWithTab: KeyBinding[] = [ | ||||
| @@ -7,33 +8,39 @@ const smartIndentWithTab: KeyBinding[] = [ | ||||
|         run({ state, dispatch }) { | ||||
|             const { selection } = state; | ||||
|  | ||||
|             for (const range of selection.ranges) { | ||||
|                 if (!range.empty) { | ||||
|                     // Allow default behaviour. | ||||
|                     return false; | ||||
|             // Handle selection indenting normally | ||||
|             if (selection.ranges.some(range => !range.empty)) { | ||||
|                 return indentMore({ state, dispatch }); | ||||
|             } | ||||
|  | ||||
|             const changes = []; | ||||
|             const newSelections = []; | ||||
|  | ||||
|             for (let range of 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 | ||||
|                     // Only whitespace before cursor → indent line | ||||
|                     return indentMore({ state, dispatch }); | ||||
|                 } else { | ||||
|                     // Insert a tab character | ||||
|                     const cursor = range.head; | ||||
|                     dispatch(state.update({ | ||||
|                         changes: { | ||||
|                             from: cursor, | ||||
|                             insert: "\t" | ||||
|                         }, | ||||
|                         selection: { anchor: cursor + 1 }, | ||||
|                     // Insert a 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; | ||||
|         }, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user