Skip to content

Commit 8902aa7

Browse files
committed
fix: ensure all usages of markdown leaves check whether view is deferred
1 parent d96968f commit 8902aa7

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

src/editor/renderers/post-process/util.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@ import type {BlockInfo} from "@codemirror/view";
44

55
export function codeBlockPostProcessorUpdate(app: App, language: string) {
66
for (const leaf of app.workspace.getLeavesOfType("markdown")) {
7-
const view = <MarkdownView> leaf.view;
8-
if (view.editor.cm) {
9-
const widgets = view.editor.cm.viewportLineBlocks.filter((block: BlockInfo) =>
10-
block.widget && block.widget.lang === language
11-
);
12-
const original_selection = view.editor.cm.state.selection;
13-
view.editor.cm.dispatch({
14-
selection: EditorSelection.create(widgets.map((block: BlockInfo) => EditorSelection.range(block.from, block.to))),
15-
scrollIntoView: false,
16-
});
17-
view.editor.cm.dispatch({
18-
selection: original_selection,
19-
scrollIntoView: false,
20-
});
7+
if (leaf.view instanceof MarkdownView) {
8+
const { view } = leaf;
9+
if (view.editor.cm) {
10+
const widgets = view.editor.cm.viewportLineBlocks.filter((block: BlockInfo) =>
11+
block.widget && block.widget.lang === language
12+
);
13+
const original_selection = view.editor.cm.state.selection;
14+
view.editor.cm.dispatch({
15+
selection: EditorSelection.create(widgets.map((block: BlockInfo) => EditorSelection.range(block.from, block.to))),
16+
scrollIntoView: false,
17+
});
18+
view.editor.cm.dispatch({
19+
selection: original_selection,
20+
scrollIntoView: false,
21+
});
22+
}
2123
}
2224
}
2325
}
@@ -39,5 +41,7 @@ export function postProcessorUpdate(app: App) {
3941

4042
export function postProcessorRerender(app: App) {
4143
for (const leaf of app.workspace.getLeavesOfType("markdown"))
42-
(leaf.view as MarkdownView).previewMode.rerender(true);
44+
if (leaf.view instanceof MarkdownView) {
45+
leaf.view.previewMode.rerender(true);
46+
}
4347
}

src/editor/view-header/header-button.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ export class HeaderButton {
3333
this.has_label = render;
3434

3535
for (const leaf of this.plugin.app.workspace.getLeavesOfType("markdown")) {
36-
const view = leaf.view as MarkdownView;
36+
if (!(leaf.view instanceof MarkdownView)) continue;
37+
const { view } = leaf;
38+
3739
const { text } = this.states[this.getvalue(view)];
3840
const elements = this.active_mapping.get(view);
3941
if (!elements) continue;
@@ -72,9 +74,8 @@ export class HeaderButton {
7274
this.changeEvent = this.plugin.app.workspace.on("layout-change", this.attachButtons.bind(this));
7375

7476
for (const leaf of this.plugin.app.workspace.getLeavesOfType("markdown")) {
75-
// Check if the view is deferred
76-
let { view } = leaf;
77-
if (!(view instanceof MarkdownView)) continue;
77+
if (!(leaf.view instanceof MarkdownView)) continue;
78+
const { view } = leaf;
7879

7980
if (this.active_mapping.has(view)) continue;
8081
const event = leaf.on("history-change", () => {
@@ -137,8 +138,10 @@ export class HeaderButton {
137138
detachButtons() {
138139
if (!this.changeEvent) return;
139140

140-
for (const leaf of this.plugin.app.workspace.getLeavesOfType("markdown"))
141+
for (const leaf of this.plugin.app.workspace.getLeavesOfType("markdown")) {
142+
if (!(leaf.view instanceof MarkdownView)) continue;
141143
this.detachButton(leaf);
144+
}
142145
this.plugin.app.workspace.offref(this.changeEvent!);
143146
this.changeEvent = null;
144147
}

src/ui/pages/view/ViewPage.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,8 @@
566566

567567
const file = plugin.app.vault.getAbstractFileByPath(row.path);
568568
if (!file) return;
569+
570+
await plugin.app.workspace.revealLeaf(lastActiveLeaf);
569571
const view = lastActiveLeaf.view;
570572

571573
if (file !== view.file) await lastActiveLeaf.openFile(file);

0 commit comments

Comments
 (0)