https://github.com/tadashi-aikawa/obsidian-another-quick-switcher/issues/178
`this.initialState`のターゲットが異なるのが原因ぽい。
`chooseCurrentSuggestion()`にて
```ts
if (!option.keepOpen) {
this.close();
this.navigate(() => this.isClosed); // wait for close to finish before navigating
} else if (leaf === "same-tab") {
// Previewing in same tab; save state if not already saved
this.initialState ??= this.appHelper.captureState();
}
this.navigate(() =>
this.appHelper.openFile(
fileToOpened,
{ leaf, offset, inplace: option.keepOpen },
this.initialState
)
);
return fileToOpened;
```
のコメントがある部分が怪しい。
これでとりあえず動くけど、ピン止めの挙動かな... もうちょいリファクタ
```ts
captureState(): CaptureState {
const oldLeaf = this.unsafeApp.workspace.activeLeaf;
const existing = new WeakSet<WorkspaceLeaf>();
app.workspace.iterateAllLeaves((leaf) => existing.add(leaf));
const leaf: WorkspaceLeaf | undefined = app.workspace.getLeaf();
const state = leaf.getViewState();
const eState = leaf.getEphemeralState();
return {
leaf,
async restore() {
if (!leaf) {
return;
}
if (existing.has(leaf)) {
await leaf.setViewState(
{ ...state, active: leaf === oldLeaf, popstate: true } as ViewState,
eState
);
if (oldLeaf && leaf !== oldLeaf) {
app.workspace.setActiveLeaf(oldLeaf, { focus: true });
}
} else {
// Newly opened leaf: close it and drop references
// leaf.detach();
await leaf.setViewState(
{ ...state, active: leaf === oldLeaf, popstate: true } as ViewState,
eState
);
if (oldLeaf && leaf !== oldLeaf) {
app.workspace.setActiveLeaf(oldLeaf, { focus: true });
}
}
// leaf = this.leaf = undefined;
},
};
}
```
ピン止めについては[[🦉Another Quick Switcher]]を起動したときのLeafを`initialLeaf`として持たせておき、そのピン止め情報を参照して最後に消しこめばいい。あと `this.leaf = undefined` を入れないと、決定したときにファイルが変わらないので必要っぽい。仕組みはちゃんとわかっていない...
ということで `captureState` の最終系。
```ts
captureState(initialLeaf: WorkspaceLeaf | null): CaptureState {
const currentLeaf = this.unsafeApp.workspace.activeLeaf;
const newLeaf = app.workspace.getLeaf();
const newState = newLeaf.getViewState();
const newEState = newLeaf.getEphemeralState();
return {
leaf: newLeaf,
async restore() {
if (!newLeaf) {
return;
}
if (!initialLeaf || initialLeaf.getViewState().pinned) {
newLeaf.detach();
} else {
await newLeaf.setViewState(
{
...newState,
active: newLeaf === currentLeaf,
popstate: true,
} as ViewState,
newEState
);
if (newLeaf !== currentLeaf) {
app.workspace.setActiveLeaf(currentLeaf!, { focus: true });
}
}
this.leaf = undefined;
},
};
}
}
```
## 動作確認
- [x] タブが空の状態で
- [x] ファイルを普通に開ける
- [x] ESCできる
- [x] プレビューできる
- [x] プレビュー後にESCで消える
- [x] プレビュー後に決定で変わる
- [x] 1つだけタブがある状態で
- [x] ファイルを普通に開ける
- [x] ESCできる
- [x] プレビューできる
- [x] プレビュー後にESCで戻る
- [x] プレビュー後に決定で変わる
- [x] 2つタブがあり、左側のタブがアクティブな状態で
- [x] ファイルを普通に開ける
- [x] ESCできる
- [x] プレビューできる
- [x] プレビュー後にESCで戻る
- [x] プレビュー後に決定で変わる
- [x] 2つタブがあり、右側のタブがアクティブな状態で
- [x] ファイルを普通に開ける
- [x] ESCできる
- [x] プレビューできる
- [x] プレビュー後にESCで戻る
- [x] プレビュー後に決定で変わる
- [x] 1つだけピン止めのタブがある状態で
- [x] ファイルを普通に開ける
- [x] ESCできる
- [x] プレビューできる
- [x] プレビュー後にESCで消える
- [x] プレビュー後に決定で変わる