Files
BindThem/src/DirectionalMove.ts
olivier 358ddca2f0
Some checks failed
Node.js build / build (20.x) (push) Has been cancelled
Node.js build / build (22.x) (push) Has been cancelled
fix typescript
2026-02-27 10:12:27 -05:00

82 lines
1.8 KiB
TypeScript

import { App, Editor, EditorChange, EditorTransaction, MarkdownFileInfo, MarkdownView } from 'obsidian'
import BindThemPlugin from './main'
import { Direction } from './Entities'
import { selectionToRange } from './Utils'
/**
* DirectionalMove provides commands to move selection content left or right
*/
export class DirectionalMove {
public app: App
private plugin: BindThemPlugin
constructor(app: App, plugin: BindThemPlugin) {
this.app = app
this.plugin = plugin
}
/**
* Move the current selection(s) in the specified direction (left or right)
*/
public directionalMove(
editor: Editor,
view: MarkdownView | MarkdownFileInfo,
direction: Direction.Left | Direction.Right
): void {
const selections = editor.listSelections()
const changes: Array<EditorChange> = []
for (const selection of selections) {
const range = selectionToRange(selection)
let additionChange: EditorChange
let deletionChange: EditorChange
switch (direction) {
case Direction.Left: {
deletionChange = {
from: {
line: range.from.line,
ch: range.from.ch - 1,
},
to: range.from,
text: '',
}
additionChange = {
from: range.to,
to: range.to,
text: editor.getRange(deletionChange.from, deletionChange.to!),
}
break
}
case Direction.Right: {
deletionChange = {
from: range.to,
to: {
line: range.to.line,
ch: range.to.ch + 1,
},
text: '',
}
additionChange = {
from: range.from,
to: range.from,
text: editor.getRange(deletionChange.from, deletionChange.to!),
}
break
}
}
changes.push(deletionChange, additionChange)
}
const transaction: EditorTransaction = {
changes: changes,
}
const origin = 'DirectionalMove_' + String(direction)
editor.transaction(transaction, origin)
}
}