Fix file close and select

This commit is contained in:
Mateusz Tymek
2026-02-01 15:07:36 +01:00
parent 7940371ee2
commit b55fbc9a06
3 changed files with 91 additions and 19 deletions

View File

@@ -20,6 +20,22 @@ export class WorkspaceContext {
this.app = app;
}
trackViewSelection(view: MarkdownView | null): void {
if (view) {
this.lastMarkdownView = view;
}
const sourcePath = view?.file?.path;
const selection = view?.editor?.getSelection() ?? "";
if (sourcePath && selection.trim()) {
this.lastSelection = {
text: selection,
sourcePath,
};
}
}
gatherContext(maxNotes: number, maxSelectionLength: number): WorkspaceContextSnapshot {
const leaves = this.app.workspace.getLeavesOfType("markdown");
const paths = new Set<string>();
@@ -35,9 +51,7 @@ export class WorkspaceContext {
const openNotePaths = Array.from(paths).slice(0, Math.max(0, maxNotes));
const view = this.app.workspace.getActiveViewOfType(MarkdownView) ?? this.lastMarkdownView;
if (view) {
this.lastMarkdownView = view;
}
this.trackViewSelection(view);
const sourcePath = view?.file?.path;
const selection = view?.editor?.getSelection() ?? "";
@@ -49,10 +63,8 @@ export class WorkspaceContext {
sourcePath,
};
this.lastSelection = selectionContext;
} else if (!sourcePath) {
} else if (this.lastSelection) {
selectionContext = this.lastSelection;
} else {
this.lastSelection = null;
}
if (selectionContext && selectionContext.text.length > maxSelectionLength) {

View File

@@ -1,4 +1,4 @@
import { Plugin, WorkspaceLeaf, Notice, EventRef } from "obsidian";
import { Plugin, WorkspaceLeaf, Notice, EventRef, MarkdownView } from "obsidian";
import { OpenCodeSettings, DEFAULT_SETTINGS, OPENCODE_VIEW_TYPE } from "./types";
import { OpenCodeView } from "./OpenCodeView";
import { OpenCodeSettingTab } from "./SettingsTab";
@@ -285,7 +285,10 @@ export default class OpenCodePlugin extends Plugin {
return;
}
const activeLeafRef = this.app.workspace.on("active-leaf-change", () => {
const activeLeafRef = this.app.workspace.on("active-leaf-change", (leaf) => {
if (leaf?.view instanceof MarkdownView) {
this.workspaceContext.trackViewSelection(leaf.view);
}
this.scheduleContextRefresh(0);
});
const fileOpenRef = this.app.workspace.on("file-open", () => {
@@ -294,11 +297,33 @@ export default class OpenCodePlugin extends Plugin {
const fileCloseRef = (this.app.workspace as any).on("file-close", () => {
this.scheduleContextRefresh();
});
const editorChangeRef = this.app.workspace.on("editor-change", () => {
const layoutChangeRef = this.app.workspace.on("layout-change", () => {
this.scheduleContextRefresh();
});
const editorChangeRef = this.app.workspace.on("editor-change", (_editor, view) => {
if (view instanceof MarkdownView) {
this.workspaceContext.trackViewSelection(view);
}
this.scheduleContextRefresh(500);
});
const selectionChangeRef = (this.app.workspace as any).on(
"editor-selection-change",
(_editor: unknown, view: unknown) => {
if (view instanceof MarkdownView) {
this.workspaceContext.trackViewSelection(view);
}
this.scheduleContextRefresh(200);
}
);
this.contextEventRefs = [activeLeafRef, fileOpenRef, fileCloseRef, editorChangeRef];
this.contextEventRefs = [
activeLeafRef,
fileOpenRef,
fileCloseRef,
layoutChangeRef,
editorChangeRef,
selectionChangeRef,
];
this.contextEventRefs.forEach((ref) => this.registerEvent(ref));
}