From 358ddca2f08c311136a49e0a6f78d0da15cc68c6 Mon Sep 17 00:00:00 2001 From: olivier Date: Fri, 27 Feb 2026 10:12:27 -0500 Subject: [PATCH] fix typescript --- src/BetterFormatting.ts | 3 +- src/DirectionalCopy.ts | 4 +- src/DirectionalMove.ts | 4 +- src/FileHelper.ts | 14 +- src/SelectionHelper.ts | 6 +- src/SentenceNavigator.ts | 12 +- src/ToggleHeading.ts | 5 +- src/main.ts | 790 ++++++++++++++++++--------------------- 8 files changed, 386 insertions(+), 452 deletions(-) diff --git a/src/BetterFormatting.ts b/src/BetterFormatting.ts index 779c3c7..236877f 100644 --- a/src/BetterFormatting.ts +++ b/src/BetterFormatting.ts @@ -7,6 +7,7 @@ import { EditorRangeOrCaret, EditorSelection, EditorTransaction, + MarkdownFileInfo, MarkdownView, } from 'obsidian' import BindThemPlugin from './main' @@ -244,7 +245,7 @@ export class BetterFormatting { */ public toggleWrapper( editor: Editor, - view: MarkdownView, + view: MarkdownView | MarkdownFileInfo, symbolStart: string, symbolEnd?: string ): void { diff --git a/src/DirectionalCopy.ts b/src/DirectionalCopy.ts index 3aaff6e..007b264 100644 --- a/src/DirectionalCopy.ts +++ b/src/DirectionalCopy.ts @@ -1,4 +1,4 @@ -import { App, Editor, EditorChange, EditorTransaction, MarkdownView } from 'obsidian' +import { App, Editor, EditorChange, EditorTransaction, MarkdownFileInfo, MarkdownView } from 'obsidian' import BindThemPlugin from './main' import { Direction } from './Entities' import { selectionToLine, selectionToRange } from './Utils' @@ -20,7 +20,7 @@ export class DirectionalCopy { */ public directionalCopy( editor: Editor, - view: MarkdownView, + view: MarkdownView | MarkdownFileInfo, direction: Direction ): void { const selections = editor.listSelections() diff --git a/src/DirectionalMove.ts b/src/DirectionalMove.ts index 38f43f2..073baeb 100644 --- a/src/DirectionalMove.ts +++ b/src/DirectionalMove.ts @@ -1,4 +1,4 @@ -import { App, Editor, EditorChange, EditorTransaction, MarkdownView } from 'obsidian' +import { App, Editor, EditorChange, EditorTransaction, MarkdownFileInfo, MarkdownView } from 'obsidian' import BindThemPlugin from './main' import { Direction } from './Entities' import { selectionToRange } from './Utils' @@ -20,7 +20,7 @@ export class DirectionalMove { */ public directionalMove( editor: Editor, - view: MarkdownView, + view: MarkdownView | MarkdownFileInfo, direction: Direction.Left | Direction.Right ): void { const selections = editor.listSelections() diff --git a/src/FileHelper.ts b/src/FileHelper.ts index 4b0219a..8c8c9eb 100644 --- a/src/FileHelper.ts +++ b/src/FileHelper.ts @@ -1,4 +1,4 @@ -import { App, Editor, MarkdownView, Notice } from 'obsidian' +import { App, Editor, MarkdownFileInfo, MarkdownView, Notice } from 'obsidian' import BindThemPlugin from './main' /** @@ -16,7 +16,7 @@ export class FileHelper { /** * Duplicate the current file */ - public async duplicateFile(editor: Editor, view: MarkdownView): Promise { + public async duplicateFile(editor: Editor, view: MarkdownView | MarkdownFileInfo): Promise { const activeFile = this.app.workspace.getActiveFile() if (activeFile === null) { @@ -30,7 +30,9 @@ export class FileHelper { try { const newFile = await this.app.vault.copy(activeFile, newFilePath) - await view.leaf.openFile(newFile) + if (view instanceof MarkdownView) { + await view.leaf.openFile(newFile) + } editor.setSelections(selections) } catch (e) { @@ -41,7 +43,7 @@ export class FileHelper { /** * Create a new file in the same directory as the current file */ - public async newAdjacentFile(editor: Editor, view: MarkdownView): Promise { + public async newAdjacentFile(editor: Editor, view: MarkdownView | MarkdownFileInfo): Promise { const activeFile = this.app.workspace.getActiveFile() if (activeFile === null) { @@ -53,7 +55,9 @@ export class FileHelper { try { const newFile = await this.app.vault.create(newFilePath, '') - await view.leaf.openFile(newFile) + if (view instanceof MarkdownView) { + await view.leaf.openFile(newFile) + } } catch (e) { new Notice(String(e)) } diff --git a/src/SelectionHelper.ts b/src/SelectionHelper.ts index 616eec3..eeff96e 100644 --- a/src/SelectionHelper.ts +++ b/src/SelectionHelper.ts @@ -1,4 +1,4 @@ -import { App, Editor, EditorRange, EditorTransaction, MarkdownView } from 'obsidian' +import { App, Editor, EditorRange, EditorTransaction, MarkdownFileInfo, MarkdownView } from 'obsidian' import BindThemPlugin from './main' import { selectionToLine, selectionToRange } from './Utils' @@ -17,7 +17,7 @@ export class SelectionHelper { /** * Select the current line(s) */ - public selectLine(editor: Editor, view: MarkdownView): void { + public selectLine(editor: Editor, view: MarkdownView | MarkdownFileInfo): void { const selections = editor.listSelections() const newSelectionRanges: Array = [] @@ -36,7 +36,7 @@ export class SelectionHelper { /** * Select the current word(s) */ - public selectWord(editor: Editor, view: MarkdownView): void { + public selectWord(editor: Editor, view: MarkdownView | MarkdownFileInfo): void { const selections = editor.listSelections() const newSelections: Array = [] diff --git a/src/SentenceNavigator.ts b/src/SentenceNavigator.ts index c59bb0e..b2ecb95 100644 --- a/src/SentenceNavigator.ts +++ b/src/SentenceNavigator.ts @@ -1,4 +1,4 @@ -import { App, Editor, EditorPosition, MarkdownView } from 'obsidian' +import { App, Editor, EditorPosition, MarkdownFileInfo, MarkdownView } from 'obsidian' import BindThemPlugin from './main' import { LIST_CHARACTER_REGEX } from './Constants' @@ -97,7 +97,7 @@ export class SentenceNavigator { /** * Delete text from cursor to sentence boundary */ - public deleteToBoundary(editor: Editor, view: MarkdownView, boundary: 'start' | 'end'): void { + public deleteToBoundary(editor: Editor, view: MarkdownView | MarkdownFileInfo, boundary: 'start' | 'end'): void { let { cursorPosition, paragraphText } = this.getCursorAndParagraphText(editor) const originalCursorPosition = cursorPosition @@ -149,7 +149,7 @@ export class SentenceNavigator { /** * Select text from cursor to sentence boundary */ - public selectToBoundary(editor: Editor, view: MarkdownView, boundary: 'start' | 'end'): void { + public selectToBoundary(editor: Editor, view: MarkdownView | MarkdownFileInfo, boundary: 'start' | 'end'): void { const { cursorPosition, paragraphText } = this.getCursorAndParagraphText(editor) this.forEachSentence(paragraphText, (sentence) => { const idx = sentence.index ?? 0 @@ -183,7 +183,7 @@ export class SentenceNavigator { /** * Move cursor to the start of the current sentence */ - public moveToStartOfCurrentSentence(editor: Editor, view: MarkdownView): void { + public moveToStartOfCurrentSentence(editor: Editor, view: MarkdownView | MarkdownFileInfo): void { let { cursorPosition, paragraphText } = this.getCursorAndParagraphText(editor) if (cursorPosition.ch === 0 || this.isAtStartOfListItem(cursorPosition, paragraphText)) { @@ -214,7 +214,7 @@ export class SentenceNavigator { /** * Move cursor to the start of the next sentence */ - public moveToStartOfNextSentence(editor: Editor, view: MarkdownView): void { + public moveToStartOfNextSentence(editor: Editor, view: MarkdownView | MarkdownFileInfo): void { let { cursorPosition, paragraphText } = this.getCursorAndParagraphText(editor) if (cursorPosition.ch === paragraphText.length) { @@ -252,7 +252,7 @@ export class SentenceNavigator { /** * Select the current sentence */ - public selectSentence(editor: Editor, view: MarkdownView): void { + public selectSentence(editor: Editor, view: MarkdownView | MarkdownFileInfo): void { const { cursorPosition, paragraphText } = this.getCursorAndParagraphText(editor) let offset = 0 let text = paragraphText diff --git a/src/ToggleHeading.ts b/src/ToggleHeading.ts index d128c09..1d780f1 100644 --- a/src/ToggleHeading.ts +++ b/src/ToggleHeading.ts @@ -4,6 +4,7 @@ import { EditorChange, EditorPosition, EditorTransaction, + MarkdownFileInfo, MarkdownView, } from 'obsidian' import BindThemPlugin from './main' @@ -69,7 +70,7 @@ export class ToggleHeading { */ public toggleHeading( editor: Editor, - view: MarkdownView, + view: MarkdownView | MarkdownFileInfo, heading: Heading ): void { const selections = editor.listSelections() @@ -111,7 +112,7 @@ export class ToggleHeading { */ public toggleHeadingWithStrip( editor: Editor, - view: MarkdownView, + view: MarkdownView | MarkdownFileInfo, level: number ): void { const cursor = editor.getCursor() diff --git a/src/main.ts b/src/main.ts index 3b12562..0adce36 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,66 +1,67 @@ -import { App, Editor, MarkdownView, MarkdownFileInfo, Notice, Plugin } from 'obsidian' +import { Editor, MarkdownView, MarkdownFileInfo, Plugin } from 'obsidian'; + import { BindThemSettings, BindThemSettingTab, DEFAULT_SETTINGS, createDefaultEnabledCommands, -} from './settings' -import { BetterFormatting } from './BetterFormatting' -import { DirectionalCopy } from './DirectionalCopy' -import { DirectionalMove } from './DirectionalMove' -import { ToggleHeading } from './ToggleHeading' -import { SelectionHelper } from './SelectionHelper' -import { FileHelper } from './FileHelper' -import { SentenceNavigator } from './SentenceNavigator' -import { Direction, Heading, CaseType } from './Entities' -import { DEBUG_HEAD, DEFAULT_SENTENCE_REGEX } from './Constants' +} from './settings'; +import { BetterFormatting } from './BetterFormatting'; +import { DirectionalCopy } from './DirectionalCopy'; +import { DirectionalMove } from './DirectionalMove'; +import { ToggleHeading } from './ToggleHeading'; +import { SelectionHelper } from './SelectionHelper'; +import { FileHelper } from './FileHelper'; +import { SentenceNavigator } from './SentenceNavigator'; +import { Direction, Heading, CaseType } from './Entities'; +import { DEBUG_HEAD } from './Constants'; import { getLeadingWhitespace, wordRangeAtPos, toTitleCase, selectionToRange, -} from './Utils' +} from './Utils'; // ============================================================ // Main Plugin Class // ============================================================ export default class BindThemPlugin extends Plugin { - public settings!: BindThemSettings + public settings!: BindThemSettings; - private betterFormatting!: BetterFormatting - private directionalCopy!: DirectionalCopy - private directionalMove!: DirectionalMove - private toggleHeading!: ToggleHeading - private selectionHelper!: SelectionHelper - private fileHelper!: FileHelper - private sentenceNavigator!: SentenceNavigator + private betterFormatting!: BetterFormatting; + private directionalCopy!: DirectionalCopy; + private directionalMove!: DirectionalMove; + private toggleHeading!: ToggleHeading; + private selectionHelper!: SelectionHelper; + private fileHelper!: FileHelper; + private sentenceNavigator!: SentenceNavigator; async onload(): Promise { - await this.loadSettings() - this.debug('Loading BindThem...') + await this.loadSettings(); + this.debug('Loading BindThem...'); // Initialize feature classes - this.betterFormatting = new BetterFormatting(this.app, this) - this.directionalCopy = new DirectionalCopy(this.app, this) - this.directionalMove = new DirectionalMove(this.app, this) - this.toggleHeading = new ToggleHeading(this.app, this) - this.selectionHelper = new SelectionHelper(this.app, this) - this.fileHelper = new FileHelper(this.app, this) + this.betterFormatting = new BetterFormatting(this.app, this); + this.directionalCopy = new DirectionalCopy(this.app, this); + this.directionalMove = new DirectionalMove(this.app, this); + this.toggleHeading = new ToggleHeading(this.app, this); + this.selectionHelper = new SelectionHelper(this.app, this); + this.fileHelper = new FileHelper(this.app, this); this.sentenceNavigator = new SentenceNavigator( this.app, this, this.settings.sentenceRegexSource - ) + ); // Register commands - this.registerCommands() + this.registerCommands(); - this.addSettingTab(new BindThemSettingTab(this.app, this)) + this.addSettingTab(new BindThemSettingTab(this.app, this)); } onunload(): void { - console.log('Unloading BindThem...') + console.debug('Unloading BindThem...'); } /** @@ -68,7 +69,7 @@ export default class BindThemPlugin extends Plugin { */ public debug(str: string): void { if (this.settings.debug) { - console.log(DEBUG_HEAD + str) + console.debug(DEBUG_HEAD + str); } } @@ -76,18 +77,18 @@ export default class BindThemPlugin extends Plugin { * Check if a command is enabled in settings */ public isCommandEnabled(commandId: string): boolean { - return this.settings.enabledCommands[commandId] !== false + return this.settings.enabledCommands[commandId] !== false; } /** * Register a command only if it's enabled in settings */ private addConditionalCommand(options: { - id: string - name: string - icon?: string - editorCallback?: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => void - callback?: () => void + id: string; + name: string; + icon?: string; + editorCallback?: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => void; + callback?: () => void; }): void { if (this.isCommandEnabled(options.id)) { this.addCommand({ @@ -96,7 +97,7 @@ export default class BindThemPlugin extends Plugin { icon: options.icon, editorCallback: options.editorCallback, callback: options.callback, - }) + }); } } @@ -111,18 +112,16 @@ export default class BindThemPlugin extends Plugin { id: 'select-line', name: 'Select Current Line(s)', icon: 'text-select', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.selectionHelper.selectLine(editor, view) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.selectionHelper.selectLine(editor, view); + }}); this.addConditionalCommand({ id: 'select-word', name: 'Select Current Word(s)', icon: 'text-cursor', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.selectionHelper.selectWord(editor, view) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.selectionHelper.selectWord(editor, view); + }}); // ============================================================ // Better Formatting Commands @@ -131,82 +130,72 @@ export default class BindThemPlugin extends Plugin { id: 'toggle-bold-underscore', name: 'Better Toggle Bold (underscores)', icon: 'bold', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.betterFormatting.toggleWrapper(editor, view, '__', '__') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.betterFormatting.toggleWrapper(editor, view, '__', '__'); + }}); this.addConditionalCommand({ id: 'toggle-bold-asterisk', name: 'Better Toggle Bold (asterisks)', icon: 'bold', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.betterFormatting.toggleWrapper(editor, view, '**', '**') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.betterFormatting.toggleWrapper(editor, view, '**', '**'); + }}); this.addConditionalCommand({ id: 'toggle-italics-underscore', name: 'Better Toggle Italics (underscore)', icon: 'italic', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.betterFormatting.toggleWrapper(editor, view, '_', '_') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.betterFormatting.toggleWrapper(editor, view, '_', '_'); + }}); this.addConditionalCommand({ id: 'toggle-italics-asterisk', name: 'Better Toggle Italics (asterisk)', icon: 'italic', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.betterFormatting.toggleWrapper(editor, view, '*', '*') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.betterFormatting.toggleWrapper(editor, view, '*', '*'); + }}); this.addConditionalCommand({ id: 'toggle-code', name: 'Better Toggle Code', icon: 'code', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.betterFormatting.toggleWrapper(editor, view, '`', '`') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.betterFormatting.toggleWrapper(editor, view, '`', '`'); + }}); this.addConditionalCommand({ id: 'toggle-comment', name: 'Better Toggle Comment', icon: 'message-square', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.betterFormatting.toggleWrapper(editor, view, '%%', '%%') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.betterFormatting.toggleWrapper(editor, view, '%%', '%%'); + }}); this.addConditionalCommand({ id: 'toggle-highlight', name: 'Better Toggle Highlight', icon: 'highlighter', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.betterFormatting.toggleWrapper(editor, view, '==', '==') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.betterFormatting.toggleWrapper(editor, view, '==', '=='); + }}); this.addConditionalCommand({ id: 'toggle-strikethrough', name: 'Better Toggle Strikethrough', icon: 'strikethrough', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.betterFormatting.toggleWrapper(editor, view, '~~', '~~') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.betterFormatting.toggleWrapper(editor, view, '~~', '~~'); + }}); this.addConditionalCommand({ id: 'toggle-math-inline', name: 'Better Toggle Math (Inline)', icon: 'function-square', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.betterFormatting.toggleWrapper(editor, view, '$', '$') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.betterFormatting.toggleWrapper(editor, view, '$', '$'); + }}); this.addConditionalCommand({ id: 'toggle-math-block', name: 'Better Toggle Math (Block)', icon: 'square-function', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.betterFormatting.toggleWrapper(editor, view, '$$', '$$') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.betterFormatting.toggleWrapper(editor, view, '$$', '$$'); + }}); // ============================================================ // Directional Copy Commands @@ -215,34 +204,30 @@ export default class BindThemPlugin extends Plugin { id: 'copy-up', name: 'Copy Current Line(s) Up', icon: 'copy', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.directionalCopy.directionalCopy(editor, view, Direction.Up) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.directionalCopy.directionalCopy(editor, view, Direction.Up); + }}); this.addConditionalCommand({ id: 'copy-down', name: 'Copy Current Line(s) Down', icon: 'copy', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.directionalCopy.directionalCopy(editor, view, Direction.Down) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.directionalCopy.directionalCopy(editor, view, Direction.Down); + }}); this.addConditionalCommand({ id: 'copy-left', name: 'Copy Current Selection(s) Left', icon: 'copy', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.directionalCopy.directionalCopy(editor, view, Direction.Left) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.directionalCopy.directionalCopy(editor, view, Direction.Left); + }}); this.addConditionalCommand({ id: 'copy-right', name: 'Copy Current Selections(s) Right', icon: 'copy', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.directionalCopy.directionalCopy(editor, view, Direction.Right) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.directionalCopy.directionalCopy(editor, view, Direction.Right); + }}); // ============================================================ // Directional Move Commands @@ -251,18 +236,16 @@ export default class BindThemPlugin extends Plugin { id: 'move-left', name: 'Move Selection Left', icon: 'move-left', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.directionalMove.directionalMove(editor, view, Direction.Left) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.directionalMove.directionalMove(editor, view, Direction.Left); + }}); this.addConditionalCommand({ id: 'move-right', name: 'Move Selection Right', icon: 'move-right', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.directionalMove.directionalMove(editor, view, Direction.Right) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.directionalMove.directionalMove(editor, view, Direction.Right); + }}); // ============================================================ // Toggle Heading Commands @@ -271,99 +254,86 @@ export default class BindThemPlugin extends Plugin { id: 'toggle-heading-1', name: 'Toggle Heading - H1', icon: 'heading-1', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.toggleHeading.toggleHeading(editor, view, Heading.H1) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.toggleHeading.toggleHeading(editor, view, Heading.H1); + }}); this.addConditionalCommand({ id: 'toggle-heading-2', name: 'Toggle Heading - H2', icon: 'heading-2', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.toggleHeading.toggleHeading(editor, view, Heading.H2) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.toggleHeading.toggleHeading(editor, view, Heading.H2); + }}); this.addConditionalCommand({ id: 'toggle-heading-3', name: 'Toggle Heading - H3', icon: 'heading-3', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.toggleHeading.toggleHeading(editor, view, Heading.H3) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.toggleHeading.toggleHeading(editor, view, Heading.H3); + }}); this.addConditionalCommand({ id: 'toggle-heading-4', name: 'Toggle Heading - H4', icon: 'heading-4', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.toggleHeading.toggleHeading(editor, view, Heading.H4) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.toggleHeading.toggleHeading(editor, view, Heading.H4); + }}); this.addConditionalCommand({ id: 'toggle-heading-5', name: 'Toggle Heading - H5', icon: 'heading-5', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.toggleHeading.toggleHeading(editor, view, Heading.H5) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.toggleHeading.toggleHeading(editor, view, Heading.H5); + }}); this.addConditionalCommand({ id: 'toggle-heading-6', name: 'Toggle Heading - H6', icon: 'heading-6', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.toggleHeading.toggleHeading(editor, view, Heading.H6) - }, - }) - // Heading with strip formatting + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.toggleHeading.toggleHeading(editor, view, Heading.H6); + }}); this.addConditionalCommand({ id: 'toggle-heading-strip-1', name: 'Toggle Heading - H1 (strip formatting)', icon: 'heading-1', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.toggleHeading.toggleHeadingWithStrip(editor, view, 1) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.toggleHeading.toggleHeadingWithStrip(editor, view, 1); + }}); this.addConditionalCommand({ id: 'toggle-heading-strip-2', name: 'Toggle Heading - H2 (strip formatting)', icon: 'heading-2', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.toggleHeading.toggleHeadingWithStrip(editor, view, 2) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.toggleHeading.toggleHeadingWithStrip(editor, view, 2); + }}); this.addConditionalCommand({ id: 'toggle-heading-strip-3', name: 'Toggle Heading - H3 (strip formatting)', icon: 'heading-3', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.toggleHeading.toggleHeadingWithStrip(editor, view, 3) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.toggleHeading.toggleHeadingWithStrip(editor, view, 3); + }}); this.addConditionalCommand({ id: 'toggle-heading-strip-4', name: 'Toggle Heading - H4 (strip formatting)', icon: 'heading-4', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.toggleHeading.toggleHeadingWithStrip(editor, view, 4) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.toggleHeading.toggleHeadingWithStrip(editor, view, 4); + }}); this.addConditionalCommand({ id: 'toggle-heading-strip-5', name: 'Toggle Heading - H5 (strip formatting)', icon: 'heading-5', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.toggleHeading.toggleHeadingWithStrip(editor, view, 5) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.toggleHeading.toggleHeadingWithStrip(editor, view, 5); + }}); this.addConditionalCommand({ id: 'toggle-heading-strip-6', name: 'Toggle Heading - H6 (strip formatting)', icon: 'heading-6', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.toggleHeading.toggleHeadingWithStrip(editor, view, 6) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.toggleHeading.toggleHeadingWithStrip(editor, view, 6); + }}); // ============================================================ // Sentence Navigator Commands @@ -372,58 +342,51 @@ export default class BindThemPlugin extends Plugin { id: 'delete-to-start-of-sentence', name: 'Delete to Start of Sentence', icon: 'delete', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.sentenceNavigator.deleteToBoundary(editor, view, 'start') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.sentenceNavigator.deleteToBoundary(editor, view, 'start'); + }}); this.addConditionalCommand({ id: 'delete-to-end-of-sentence', name: 'Delete to End of Sentence', icon: 'delete', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.sentenceNavigator.deleteToBoundary(editor, view, 'end') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.sentenceNavigator.deleteToBoundary(editor, view, 'end'); + }}); this.addConditionalCommand({ id: 'select-to-start-of-sentence', name: 'Select to Start of Sentence', icon: 'text-select', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.sentenceNavigator.selectToBoundary(editor, view, 'start') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.sentenceNavigator.selectToBoundary(editor, view, 'start'); + }}); this.addConditionalCommand({ id: 'select-to-end-of-sentence', name: 'Select to End of Sentence', icon: 'text-select', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.sentenceNavigator.selectToBoundary(editor, view, 'end') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.sentenceNavigator.selectToBoundary(editor, view, 'end'); + }}); this.addConditionalCommand({ id: 'move-to-start-of-current-sentence', name: 'Move to Start of Current Sentence', icon: 'arrow-up-left', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.sentenceNavigator.moveToStartOfCurrentSentence(editor, view) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.sentenceNavigator.moveToStartOfCurrentSentence(editor, view); + }}); this.addConditionalCommand({ id: 'move-to-start-of-next-sentence', name: 'Move to Start of Next Sentence', icon: 'arrow-down-right', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.sentenceNavigator.moveToStartOfNextSentence(editor, view) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.sentenceNavigator.moveToStartOfNextSentence(editor, view); + }}); this.addConditionalCommand({ id: 'select-sentence', name: 'Select Current Sentence', icon: 'text-quote', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.sentenceNavigator.selectSentence(editor, view) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.sentenceNavigator.selectSentence(editor, view); + }}); // ============================================================ // File Helper Commands @@ -432,18 +395,16 @@ export default class BindThemPlugin extends Plugin { id: 'duplicate-file', name: 'Duplicate File', icon: 'copy', - editorCallback: (editor: Editor, view: MarkdownView) => { - void this.fileHelper.duplicateFile(editor, view) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + void this.fileHelper.duplicateFile(editor, view); + }}); this.addConditionalCommand({ id: 'new-adjacent-file', name: 'New Adjacent File', icon: 'file-plus', - editorCallback: (editor: Editor, view: MarkdownView) => { - void this.fileHelper.newAdjacentFile(editor, view) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + void this.fileHelper.newAdjacentFile(editor, view); + }}); // ============================================================ // Line Operations @@ -452,74 +413,65 @@ export default class BindThemPlugin extends Plugin { id: 'insert-line-above', name: 'Insert Line Above', icon: 'plus', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.insertLine(editor, view, 'above') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.insertLine(editor, view, 'above'); + }}); this.addConditionalCommand({ id: 'insert-line-below', name: 'Insert Line Below', icon: 'plus', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.insertLine(editor, view, 'below') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.insertLine(editor, view, 'below'); + }}); this.addConditionalCommand({ id: 'delete-line', name: 'Delete Line', icon: 'trash', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.deleteLine(editor) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.deleteLine(editor); + }}); this.addConditionalCommand({ id: 'delete-to-start-of-line', name: 'Delete to Start of Line', icon: 'delete', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.deleteToLineBoundary(editor, 'start') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.deleteToLineBoundary(editor, 'start'); + }}); this.addConditionalCommand({ id: 'delete-to-end-of-line', name: 'Delete to End of Line', icon: 'delete', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.deleteToLineBoundary(editor, 'end') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.deleteToLineBoundary(editor, 'end'); + }}); this.addConditionalCommand({ id: 'join-lines', name: 'Join Lines', icon: 'merge', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.joinLines(editor) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.joinLines(editor); + }}); this.addConditionalCommand({ id: 'duplicate-line', name: 'Duplicate Line', icon: 'copy', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.copyLine(editor, 'down') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.copyLine(editor, 'down'); + }}); this.addConditionalCommand({ id: 'copy-line-up', name: 'Copy Line Up', icon: 'copy', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.copyLine(editor, 'up') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.copyLine(editor, 'up'); + }}); this.addConditionalCommand({ id: 'copy-line-down', name: 'Copy Line Down', icon: 'copy', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.copyLine(editor, 'down') - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.copyLine(editor, 'down'); + }}); // ============================================================ // Case Transformation @@ -528,34 +480,30 @@ export default class BindThemPlugin extends Plugin { id: 'transform-uppercase', name: 'Transform to Uppercase', icon: 'arrow-up', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.transformCase(editor, CaseType.Upper) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.transformCase(editor, CaseType.Upper); + }}); this.addConditionalCommand({ id: 'transform-lowercase', name: 'Transform to Lowercase', icon: 'arrow-down', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.transformCase(editor, CaseType.Lower) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.transformCase(editor, CaseType.Lower); + }}); this.addConditionalCommand({ id: 'transform-titlecase', name: 'Transform to Title Case', icon: 'heading', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.transformCase(editor, CaseType.Title) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.transformCase(editor, CaseType.Title); + }}); this.addConditionalCommand({ id: 'toggle-case', name: 'Toggle Case', icon: 'case-sensitive', - editorCallback: (editor: Editor, view: MarkdownView) => { - this.transformCase(editor, CaseType.Next) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + this.transformCase(editor, CaseType.Next); + }}); // ============================================================ // Navigation Commands @@ -564,61 +512,54 @@ export default class BindThemPlugin extends Plugin { id: 'go-to-line-start', name: 'Go to Line Start', icon: 'arrow-left', - editorCallback: (editor: Editor) => { - editor.setCursor({ line: editor.getCursor().line, ch: 0 }) - }, - }) + editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { + editor.setCursor({ line: editor.getCursor().line, ch: 0 }); + }}); this.addConditionalCommand({ id: 'go-to-line-end', name: 'Go to Line End', icon: 'arrow-right', editorCallback: (editor: Editor) => { - editor.setCursor({ - line: editor.getCursor().line, - ch: editor.getLine(editor.getCursor().line).length, - }) - }, - }) + editor.setCursor({ + line: editor.getCursor().line, + ch: editor.getLine(editor.getCursor().line).length, + }); + }}); this.addConditionalCommand({ id: 'go-to-first-line', name: 'Go to First Line', icon: 'arrow-up-from-line', editorCallback: (editor: Editor) => { - editor.setCursor({ line: 0, ch: 0 }) - }, - }) + editor.setCursor({ line: 0, ch: 0 }); + }}); this.addConditionalCommand({ id: 'go-to-last-line', name: 'Go to Last Line', icon: 'arrow-down-to-line', editorCallback: (editor: Editor) => { - editor.setCursor({ line: editor.lineCount() - 1, ch: 0 }) - }, - }) + editor.setCursor({ line: editor.lineCount() - 1, ch: 0 }); + }}); this.addConditionalCommand({ id: 'go-to-line-number', name: 'Go to Line Number', icon: 'text', editorCallback: (editor: Editor) => { - this.showGoToLineModal(editor) - }, - }) + this.showGoToLineModal(editor); + }}); this.addConditionalCommand({ id: 'go-to-next-heading', name: 'Go to Next Heading', icon: 'arrow-down', editorCallback: (editor: Editor) => { - this.goToHeading(editor, 'next') - }, - }) + this.goToHeading(editor, 'next'); + }}); this.addConditionalCommand({ id: 'go-to-prev-heading', name: 'Go to Previous Heading', icon: 'arrow-up', editorCallback: (editor: Editor) => { - this.goToHeading(editor, 'prev') - }, - }) + this.goToHeading(editor, 'prev'); + }}); // ============================================================ // Cursor Movement Commands @@ -628,81 +569,71 @@ export default class BindThemPlugin extends Plugin { name: 'Move Cursor Up', icon: 'arrow-up', editorCallback: (editor: Editor) => { - editor.exec('goUp') - }, - }) + editor.exec('goUp'); + }}); this.addConditionalCommand({ id: 'move-cursor-down', name: 'Move Cursor Down', icon: 'arrow-down', editorCallback: (editor: Editor) => { - editor.exec('goDown') - }, - }) + editor.exec('goDown'); + }}); this.addConditionalCommand({ id: 'move-cursor-left', name: 'Move Cursor Left', icon: 'arrow-left', editorCallback: (editor: Editor) => { - editor.exec('goLeft') - }, - }) + editor.exec('goLeft'); + }}); this.addConditionalCommand({ id: 'move-cursor-right', name: 'Move Cursor Right', icon: 'arrow-right', editorCallback: (editor: Editor) => { - editor.exec('goRight') - }, - }) + editor.exec('goRight'); + }}); this.addConditionalCommand({ id: 'go-to-prev-word', name: 'Go to Previous Word', icon: 'arrow-left', editorCallback: (editor: Editor) => { - editor.exec('goWordLeft') - }, - }) + editor.exec('goWordLeft'); + }}); this.addConditionalCommand({ id: 'go-to-next-word', name: 'Go to Next Word', icon: 'arrow-right', editorCallback: (editor: Editor) => { - editor.exec('goWordRight') - }, - }) + editor.exec('goWordRight'); + }}); this.addConditionalCommand({ id: 'insert-cursor-above', name: 'Insert Cursor Above', icon: 'plus-circle', editorCallback: (editor: Editor) => { - this.insertCursor(editor, -1) - }, - }) + this.insertCursor(editor, -1); + }}); this.addConditionalCommand({ id: 'insert-cursor-below', name: 'Insert Cursor Below', icon: 'plus-circle', editorCallback: (editor: Editor) => { - this.insertCursor(editor, 1) - }, - }) + this.insertCursor(editor, 1); + }}); this.addConditionalCommand({ id: 'select-next-occurrence', name: 'Select Next Occurrence', icon: 'text-cursor', editorCallback: (editor: Editor) => { - this.selectOccurrence(editor, 'next') - }, - }) + this.selectOccurrence(editor, 'next'); + }}); this.addConditionalCommand({ id: 'select-prev-occurrence', name: 'Select Previous Occurrence', icon: 'text-cursor', editorCallback: (editor: Editor) => { - this.selectOccurrence(editor, 'prev') - }, - }) + this.selectOccurrence(editor, 'prev'); + }}); // ============================================================ // Utility Commands @@ -713,29 +644,26 @@ export default class BindThemPlugin extends Plugin { icon: 'list', callback: () => { const vaultWithConfig = this.app.vault as unknown as { - getConfig: (k: string) => unknown - setConfig: (k: string, v: unknown) => void + getConfig: (k: string) => unknown; + setConfig: (k: string, v: unknown) => void; } - const currentValue = vaultWithConfig.getConfig('showLineNumber') - vaultWithConfig.setConfig('showLineNumber', !currentValue) - }, - }) + const currentValue = vaultWithConfig.getConfig('showLineNumber') as boolean | undefined; + vaultWithConfig.setConfig('showLineNumber', !currentValue); + }}); this.addConditionalCommand({ id: 'undo', name: 'Undo', icon: 'undo', editorCallback: (editor: Editor) => { - editor.undo() - }, - }) + editor.undo(); + }}); this.addConditionalCommand({ id: 'redo', name: 'Redo', icon: 'redo', editorCallback: (editor: Editor) => { - editor.redo() - }, - }) + editor.redo(); + }}); } // ============================================================ @@ -744,112 +672,112 @@ export default class BindThemPlugin extends Plugin { private insertLine( editor: Editor, - view: MarkdownView, + view: MarkdownView | MarkdownFileInfo, where: 'above' | 'below' ): void { - const cursor = editor.getCursor() - const indent = getLeadingWhitespace(editor.getLine(cursor.line)) + const cursor = editor.getCursor(); + const indent = getLeadingWhitespace(editor.getLine(cursor.line)); if (where === 'above') { - editor.replaceRange(indent + '\n', { line: cursor.line, ch: 0 }) - editor.setCursor({ line: cursor.line, ch: indent.length }) + editor.replaceRange(indent + '\n', { line: cursor.line, ch: 0 }); + editor.setCursor({ line: cursor.line, ch: indent.length }); } else { editor.replaceRange( '\n' + indent, { line: cursor.line, ch: editor.getLine(cursor.line).length } - ) - editor.setCursor({ line: cursor.line + 1, ch: indent.length }) + ); + editor.setCursor({ line: cursor.line + 1, ch: indent.length }); } } private deleteLine(editor: Editor): void { - const cursor = editor.getCursor() - const isLastLine = cursor.line === editor.lineCount() - 1 + const cursor = editor.getCursor(); + const isLastLine = cursor.line === editor.lineCount() - 1; editor.replaceRange( '', { line: cursor.line, ch: 0 }, isLastLine ? { line: cursor.line, ch: editor.getLine(cursor.line).length } : { line: cursor.line + 1, ch: 0 } - ) + ); } private deleteToLineBoundary(editor: Editor, boundary: 'start' | 'end'): void { - const cursor = editor.getCursor() + const cursor = editor.getCursor(); editor.replaceRange( '', boundary === 'start' ? { line: cursor.line, ch: 0 } : cursor, boundary === 'start' ? cursor : { line: cursor.line, ch: editor.getLine(cursor.line).length } - ) + ); } private joinLines(editor: Editor): void { - const cursor = editor.getCursor() - if (cursor.line >= editor.lineCount() - 1) return - const curr = editor.getLine(cursor.line) - const next = editor.getLine(cursor.line + 1).replace(/^\s*[-+*>\d.]+\s*/, '') + const cursor = editor.getCursor(); + if (cursor.line >= editor.lineCount() - 1) return; + const curr = editor.getLine(cursor.line); + const next = editor.getLine(cursor.line + 1).replace(/^\s*[-+*>\d.]+\s*/, ''); editor.replaceRange( (curr.endsWith(' ') || next.startsWith(' ') ? '' : ' ') + next, { line: cursor.line, ch: curr.length }, { line: cursor.line + 1, ch: editor.getLine(cursor.line + 1).length } - ) + ); } private copyLine(editor: Editor, direction: 'up' | 'down'): void { - const cursor = editor.getCursor() - const line = editor.getLine(cursor.line) + const cursor = editor.getCursor(); + const line = editor.getLine(cursor.line); if (direction === 'up') { - editor.replaceRange(line + '\n', { line: cursor.line, ch: 0 }) + editor.replaceRange(line + '\n', { line: cursor.line, ch: 0 }); } else { editor.replaceRange('\n' + line, { line: cursor.line, ch: line.length, - }) - editor.setCursor({ line: cursor.line + 1, ch: cursor.ch }) + }); + editor.setCursor({ line: cursor.line + 1, ch: cursor.ch }); } } private transformCase(editor: Editor, caseType: CaseType): void { - const sel = editor.getSelection() - const cursor = editor.getCursor() - const lineContent = editor.getLine(cursor.line) - const wordRange = wordRangeAtPos(cursor, lineContent) + const sel = editor.getSelection(); + const cursor = editor.getCursor(); + const lineContent = editor.getLine(cursor.line); + const wordRange = wordRangeAtPos(cursor, lineContent); - const text = sel || editor.getRange(wordRange.anchor, wordRange.head) - let result: string + const text = sel || editor.getRange(wordRange.anchor, wordRange.head); + let result: string; switch (caseType) { case CaseType.Upper: - result = text.toUpperCase() - break + result = text.toUpperCase(); + break; case CaseType.Lower: - result = text.toLowerCase() - break + result = text.toLowerCase(); + break; case CaseType.Title: - result = toTitleCase(text) - break + result = toTitleCase(text); + break; case CaseType.Next: result = text === text.toUpperCase() ? text.toLowerCase() : text === text.toLowerCase() ? toTitleCase(text) - : text.toUpperCase() - break + : text.toUpperCase(); + break; } if (sel) { - editor.replaceSelection(result) + editor.replaceSelection(result); } else { - editor.replaceRange(result, wordRange.anchor, wordRange.head) + editor.replaceRange(result, wordRange.anchor, wordRange.head); } } private insertCursor(editor: Editor, offset: number): void { - const newSels: Array<{ anchor: { line: number; ch: number }; head: { line: number; ch: number } }> = [] + const newSels: Array<{ anchor: { line: number; ch: number }; head: { line: number; ch: number } }> = []; for (const sel of editor.listSelections()) { - const newLine = sel.head.line + offset + const newLine = sel.head.line + offset; if (newLine >= 0 && newLine < editor.lineCount()) { newSels.push({ anchor: { @@ -860,94 +788,94 @@ export default class BindThemPlugin extends Plugin { line: newLine, ch: Math.min(sel.head.ch, editor.getLine(newLine).length), }, - }) + }); } } - editor.setSelections([...editor.listSelections(), ...newSels]) + editor.setSelections([...editor.listSelections(), ...newSels]); } private selectOccurrence(editor: Editor, direction: 'next' | 'prev'): void { - const selections = editor.listSelections() - if (selections.length === 0) return + const selections = editor.listSelections(); + if (selections.length === 0) return; - const lastSelection = direction === 'next' ? selections[selections.length - 1] : selections[0] - const range = selectionToRange(lastSelection) - const selectedText = editor.getRange(range.from, range.to) + const lastSelection = direction === 'next' ? selections[selections.length - 1] : selections[0]; + const range = selectionToRange(lastSelection); + const selectedText = editor.getRange(range.from, range.to); if (!selectedText) { - this.selectionHelper.selectWord(editor, null as unknown as MarkdownView) - return + this.selectionHelper.selectWord(editor, null as unknown as MarkdownView); + return; } - const fullText = editor.getValue() - const occurrences: { from: number; to: number }[] = [] - let searchPos = 0 + const fullText = editor.getValue(); + const occurrences: { from: number; to: number }[] = []; + let searchPos = 0; while (true) { - const idx = fullText.indexOf(selectedText, searchPos) - if (idx === -1) break - occurrences.push({ from: idx, to: idx + selectedText.length }) - searchPos = idx + 1 + const idx = fullText.indexOf(selectedText, searchPos); + if (idx === -1) break; + occurrences.push({ from: idx, to: idx + selectedText.length }); + searchPos = idx + 1; } - if (occurrences.length <= 1) return + if (occurrences.length <= 1) return; - const currentOffset = editor.posToOffset(range.from) - let targetOccurrence: { from: number; to: number } | null = null + const currentOffset = editor.posToOffset(range.from); + let targetOccurrence: { from: number; to: number } | null = null; if (direction === 'next') { for (const occ of occurrences) { if (occ.from > currentOffset) { - targetOccurrence = occ - break + targetOccurrence = occ; + break; } } if (!targetOccurrence) { - targetOccurrence = occurrences[0] + targetOccurrence = occurrences[0]; } } else { for (let i = occurrences.length - 1; i >= 0; i--) { if (occurrences[i].from < currentOffset) { - targetOccurrence = occurrences[i] - break + targetOccurrence = occurrences[i]; + break; } } if (!targetOccurrence) { - targetOccurrence = occurrences[occurrences.length - 1] + targetOccurrence = occurrences[occurrences.length - 1]; } } if (targetOccurrence) { - const from = editor.offsetToPos(targetOccurrence.from) - const to = editor.offsetToPos(targetOccurrence.to) - const newSelections = [...selections, { anchor: from, head: to }] - editor.setSelections(newSelections) + const from = editor.offsetToPos(targetOccurrence.from); + const to = editor.offsetToPos(targetOccurrence.to); + const newSelections = [...selections, { anchor: from, head: to }]; + editor.setSelections(newSelections); } } private goToHeading(editor: Editor, direction: 'next' | 'prev'): void { - const file = this.app.workspace.getActiveFile() - const cache = file ? this.app.metadataCache.getFileCache(file) : null - if (!cache?.headings?.length) return + const file = this.app.workspace.getActiveFile(); + const cache = file ? this.app.metadataCache.getFileCache(file) : null; + if (!cache?.headings?.length) return; - const cursorLine = editor.getCursor().line - let target = direction === 'next' ? editor.lineCount() - 1 : 0 + const cursorLine = editor.getCursor().line; + let target = direction === 'next' ? editor.lineCount() - 1 : 0; for (const h of cache.headings) { - const line = h.position.end.line - if (direction === 'next' && line > cursorLine && line < target) target = line - if (direction === 'prev' && line < cursorLine && line > target) target = line + const line = h.position.end.line; + if (direction === 'next' && line > cursorLine && line < target) target = line; + if (direction === 'prev' && line < cursorLine && line > target) target = line; } - editor.setCursor({ line: target, ch: 0 }) - editor.scrollIntoView({ from: { line: target, ch: 0 }, to: { line: target, ch: 0 } }) + editor.setCursor({ line: target, ch: 0 }); + editor.scrollIntoView({ from: { line: target, ch: 0 }, to: { line: target, ch: 0 } }); } private showGoToLineModal(editor: Editor): void { - const lineCount = editor.lineCount() + const lineCount = editor.lineCount(); // Create modal elements - const overlay = document.createElement('div') - overlay.className = 'modal-bg' + const overlay = document.createElement('div'); + overlay.className = 'modal-bg'; overlay.setCssStyles({ position: 'fixed', top: '0', @@ -956,10 +884,10 @@ export default class BindThemPlugin extends Plugin { height: '100%', background: 'rgba(0,0,0,0.5)', zIndex: '1000', - }) + }); - const modalEl = document.createElement('div') - modalEl.className = 'modal-container bindthem-modal' + const modalEl = document.createElement('div'); + modalEl.className = 'modal-container bindthem-modal'; modalEl.setCssStyles({ position: 'fixed', top: '50%', @@ -970,40 +898,40 @@ export default class BindThemPlugin extends Plugin { borderRadius: '8px', zIndex: '1001', maxWidth: '300px', - }) + }); - const title = document.createElement('h2') - title.textContent = 'Go to line' - title.setCssStyles({ marginTop: '0' }) - modalEl.appendChild(title) + const title = document.createElement('h2'); + title.textContent = 'Go to line'; + title.setCssStyles({ marginTop: '0' }); + modalEl.appendChild(title); - const inputEl = document.createElement('input') - inputEl.type = 'number' - inputEl.min = '1' - inputEl.max = String(lineCount) - inputEl.setCssStyles({ width: '100%', marginBottom: '10px', padding: '8px' }) - modalEl.appendChild(inputEl) + const inputEl = document.createElement('input'); + inputEl.type = 'number'; + inputEl.min = '1'; + inputEl.max = String(lineCount); + inputEl.setCssStyles({ width: '100%', marginBottom: '10px', padding: '8px' }); + modalEl.appendChild(inputEl); - const btn = document.createElement('button') - btn.textContent = 'Go' - btn.setCssStyles({ padding: '8px 16px' }) + const btn = document.createElement('button'); + btn.textContent = 'Go'; + btn.setCssStyles({ padding: '8px 16px' }); btn.onclick = () => { - const n = parseInt(inputEl.value) - 1 + const n = parseInt(inputEl.value) - 1; if (n >= 0 && n < lineCount) { - editor.setCursor({ line: n, ch: 0 }) - overlay.remove() + editor.setCursor({ line: n, ch: 0 }); + overlay.remove(); } - } - modalEl.appendChild(btn) + }; + modalEl.appendChild(btn); inputEl.onkeydown = (e) => { - if (e.key === 'Enter') btn.click() - if (e.key === 'Escape') overlay.remove() - } + if (e.key === 'Enter') btn.click(); + if (e.key === 'Escape') overlay.remove(); + }; - overlay.appendChild(modalEl) - document.body.appendChild(overlay) - inputEl.focus() + overlay.appendChild(modalEl); + document.body.appendChild(overlay); + inputEl.focus(); } // ============================================================ @@ -1011,9 +939,9 @@ export default class BindThemPlugin extends Plugin { // ============================================================ async loadSettings(): Promise { - const savedData = (await this.loadData()) as Partial | null + const savedData = (await this.loadData()) as Partial | null; - const defaultCommands = createDefaultEnabledCommands() + const defaultCommands = createDefaultEnabledCommands(); this.settings = { ...DEFAULT_SETTINGS, @@ -1022,11 +950,11 @@ export default class BindThemPlugin extends Plugin { ...defaultCommands, ...savedData?.enabledCommands, }, - } + }; } async saveSettings(): Promise { - await this.saveData(this.settings) - this.sentenceNavigator.updateRegex(this.settings.sentenceRegexSource) + await this.saveData(this.settings); + this.sentenceNavigator.updateRegex(this.settings.sentenceRegexSource); } } \ No newline at end of file