create better structure inspired by ObsidianTweaks'
Some checks failed
Node.js build / build (20.x) (push) Has been cancelled
Node.js build / build (22.x) (push) Has been cancelled

This commit is contained in:
2026-02-26 15:19:52 -05:00
parent 580f2d8c2b
commit 7022dadd83
12 changed files with 2105 additions and 654 deletions

63
src/SelectionHelper.ts Normal file
View File

@@ -0,0 +1,63 @@
import { App, Editor, EditorRange, EditorTransaction, MarkdownView } from 'obsidian'
import BindThemPlugin from './main'
import { selectionToLine, selectionToRange } from './Utils'
/**
* SelectionHelper provides commands for manipulating text selections
*/
export class SelectionHelper {
public app: App
private plugin: BindThemPlugin
constructor(app: App, plugin: BindThemPlugin) {
this.app = app
this.plugin = plugin
}
/**
* Select the current line(s)
*/
public selectLine(editor: Editor, view: MarkdownView): void {
const selections = editor.listSelections()
const newSelectionRanges: Array<EditorRange> = []
for (const selection of selections) {
const newSelection = selectionToLine(editor, selection)
newSelectionRanges.push(selectionToRange(newSelection))
}
const transaction: EditorTransaction = {
selections: newSelectionRanges,
}
editor.transaction(transaction, 'SelectionHelper_Line')
}
/**
* Select the current word(s)
*/
public selectWord(editor: Editor, view: MarkdownView): void {
const selections = editor.listSelections()
const newSelections: Array<EditorRange> = []
for (const selection of selections) {
const range = selectionToRange(selection)
const wordStart = editor.wordAt(range.from)?.from ?? range.from
const wordEnd = editor.wordAt(range.to)?.to ?? range.from
const newSelection: EditorRange = {
from: { line: wordStart.line, ch: wordStart.ch },
to: { line: wordEnd.line, ch: wordEnd.ch },
}
newSelections.push(newSelection)
}
const transaction: EditorTransaction = {
selections: newSelections,
}
editor.transaction(transaction, 'SelectionHelper_Word')
}
}