mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 21:11:30 +08:00 
			
		
		
		
	chore(code): replace tab if pressed while selected
This commit is contained in:
		
							parent
							
								
									8d18823608
								
							
						
					
					
						commit
						44069b8ccb
					
				| @ -2,20 +2,41 @@ import { indentLess, indentMore } from "@codemirror/commands"; | |||||||
| import { EditorSelection, type ChangeSpec } from "@codemirror/state"; | import { EditorSelection, type ChangeSpec } from "@codemirror/state"; | ||||||
| import type { KeyBinding } from "@codemirror/view"; | import type { KeyBinding } from "@codemirror/view"; | ||||||
| 
 | 
 | ||||||
|  | /** | ||||||
|  |  * Custom key binding for indentation: | ||||||
|  |  * | ||||||
|  |  * - <kbd>Tab</kbd> while at the beginning of a line will indent the line. | ||||||
|  |  * - <kbd>Tab</kbd> while not at the beginning of a line will insert a tab character. | ||||||
|  |  * - <kbd>Tab</kbd> while not at the beginning of a line while text is selected will replace the txt with a tab character. | ||||||
|  |  * - <kbd>Shift</kbd>+<kbd>Tab</kbd> will always unindent. | ||||||
|  |  */ | ||||||
| const smartIndentWithTab: KeyBinding[] = [ | const smartIndentWithTab: KeyBinding[] = [ | ||||||
|     { |     { | ||||||
|         key: "Tab", |         key: "Tab", | ||||||
|         run({ state, dispatch }) { |         run({ state, dispatch }) { | ||||||
|             const { selection } = state; |             const { selection } = state; | ||||||
| 
 |  | ||||||
|             // Handle selection indenting normally
 |  | ||||||
|             if (selection.ranges.some(range => !range.empty)) { |  | ||||||
|                 return indentMore({ state, dispatch }); |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             const changes = []; |             const changes = []; | ||||||
|             const newSelections = []; |             const newSelections = []; | ||||||
| 
 | 
 | ||||||
|  |             // Step 1: Handle non-empty selections → replace with tab
 | ||||||
|  |             if (selection.ranges.some(range => !range.empty)) { | ||||||
|  |                 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
 | ||||||
|             for (let range of selection.ranges) { |             for (let range of selection.ranges) { | ||||||
|                 const line = state.doc.lineAt(range.head); |                 const line = state.doc.lineAt(range.head); | ||||||
|                 const beforeCursor = state.doc.sliceString(line.from, range.head); |                 const beforeCursor = state.doc.sliceString(line.from, range.head); | ||||||
| @ -24,7 +45,7 @@ const smartIndentWithTab: KeyBinding[] = [ | |||||||
|                     // Only whitespace before cursor → indent line
 |                     // Only whitespace before cursor → indent line
 | ||||||
|                     return indentMore({ state, dispatch }); |                     return indentMore({ state, dispatch }); | ||||||
|                 } else { |                 } else { | ||||||
|                     // Insert a tab character at cursor
 |                     // Insert tab character at cursor
 | ||||||
|                     changes.push({ from: range.head, to: range.head, insert: "\t" }); |                     changes.push({ from: range.head, to: range.head, insert: "\t" }); | ||||||
|                     newSelections.push(EditorSelection.cursor(range.head + 1)); |                     newSelections.push(EditorSelection.cursor(range.head + 1)); | ||||||
|                 } |                 } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Elian Doran
						Elian Doran