## ファイル内ヘッダの取得方法 [[ファイルのヘッダ一覧を取得 (Obsidian)]] ## スクロール ハイライト ```ts app.workspace.activeLeaf.openFile(app.workspace.getActiveFile(), {eState: {line: 154}}) ``` これだと長い文書で正確に移動しない。 ```ts moveTo(to: Pos | number, editor?: Editor) { const isToOffset = typeof to === "number"; const subView = this.getMarkdownViewInActiveLeaf()?.currentMode; if (!subView) { return; } const targetEditor = editor ?? this.getCurrentEditor(); if (!targetEditor) { return; } subView.applyScroll( isToOffset ? targetEditor.offsetToPos(to).line : to.start.line ); targetEditor.setCursor( targetEditor.offsetToPos(isToOffset ? to : to.start.offset) ); } ``` ### #2022/07/09時点の最新 ```ts async moveTo(to: Pos | number, editor?: Editor) { const isToOffset = typeof to === "number"; const activeFile = this.getActiveFile(); const activeLeaf = this.app.workspace.activeLeaf; if (!activeFile || !activeLeaf) { return; } const subView = this.getMarkdownViewInActiveLeaf()?.currentMode; if (!subView) { return; } const targetEditor = editor ?? this.getCurrentEditor(); if (!targetEditor) { return; } const line = isToOffset ? targetEditor.offsetToPos(to).line : to.start.line; targetEditor.setCursor( targetEditor.offsetToPos(isToOffset ? to : to.start.offset) ); await activeLeaf.openFile(activeFile, { eState: { line, }, active: false, }); } ``` `activeLeaf.openFile`の第2引数で`{active: false}`を指定しないとエディタにカーソルを奪われて、ダイアログの入力がエディタまで貫通するので注意。 > [Through input keyboard events to editor unintentionally in "Header floating search in file" dialog\. · Issue \#76 · tadashi\-aikawa/obsidian\-another\-quick\-switcher](https://github.com/tadashi-aikawa/obsidian-another-quick-switcher/issues/76)