diff --git a/docs/Actions/Navigation.md b/docs/Actions/Navigation.md index d66386f..cb0f64b 100644 --- a/docs/Actions/Navigation.md +++ b/docs/Actions/Navigation.md @@ -11,6 +11,7 @@ | save current workspace | saveworkspace=true | Saves the current workspace. (Can be combined with `workspace` to open a new workspace afterwards) | | | file | | Opens file | | | line and/or column in file | , line, column | Opens `column` in `line` in file (1 indexed) | | +| offset in file | , offset | Sets the cursor at `offset` in file. Offset is the character count from the start | | heading in file | , heading | Opens the `heading` in file | | | block reference in file | , block | Opens the `block` in file | | | global block reference | block | Searches the whole vault for that block id and uses that file for | | diff --git a/src/handlers.ts b/src/handlers.ts index 47ad777..425f6fc 100644 --- a/src/handlers.ts +++ b/src/handlers.ts @@ -176,7 +176,8 @@ export default class Handlers { } } else if ( parameters.line != undefined || - parameters.column != undefined + parameters.column != undefined || + parameters.offset != undefined ) { await this.plugin.open({ file: parameters.filepath, @@ -259,7 +260,8 @@ export default class Handlers { } } else if ( parameters.line != undefined || - parameters.column != undefined + parameters.column != undefined || + parameters.offset != undefined ) { await this.plugin.open({ file: parameters.filepath, @@ -459,7 +461,8 @@ export default class Handlers { }); if ( parameters.line != undefined || - parameters.column != undefined + parameters.column != undefined || + parameters.offset != undefined ) { await this.plugin.setCursorInLine(parameters); } diff --git a/src/main.ts b/src/main.ts index 6c4a16d..725666a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -528,7 +528,8 @@ export default class AdvancedURI extends Plugin { }); if ( parameters.line != undefined || - parameters.column != undefined + parameters.column != undefined || + parameters.offset != undefined ) { await this.setCursorInLine(parameters); } @@ -666,21 +667,28 @@ export default class AdvancedURI extends Plugin { viewState.state.mode = "source"; await view.leaf.setViewState(viewState); - const line = - rawLine != undefined - ? Math.min(rawLine - 1, view.editor.lineCount() - 1) - : view.editor.getCursor().line; - const maxColumn = view.editor.getLine(line).length - 1; - const column = Math.min(rawColumn - 1, maxColumn); + let line: number, column: number; + if (parameters.offset != undefined) { + const pos = view.editor.offsetToPos(Number(parameters.offset)); + line = pos.line; + column = pos.ch; + } else { + line = + rawLine != undefined + ? Math.min(rawLine - 1, view.editor.lineCount() - 1) + : view.editor.getCursor().line; + const maxColumn = view.editor.getLine(line).length - 1; + column = Math.min(rawColumn - 1, maxColumn); + } view.editor.focus(); view.editor.setCursor({ line: line, - ch: column ?? maxColumn, + ch: column, }); view.editor.scrollIntoView( { - from: { line: line, ch: column ?? maxColumn }, - to: { line: line, ch: column ?? maxColumn }, + from: { line: line, ch: column }, + to: { line: line, ch: column }, }, true ); diff --git a/src/types.ts b/src/types.ts index 59f7722..bb1c172 100644 --- a/src/types.ts +++ b/src/types.ts @@ -165,6 +165,7 @@ export interface Parameters { */ canvasviewport?: string; confirm?: string; + offset?: string; } export type OpenMode = "silent" | "popover" | PaneType | "true" | "false";