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

53
main.js
View File

@@ -787,6 +787,20 @@ var WorkspaceContext = class {
this.lastMarkdownView = null;
this.app = app;
}
trackViewSelection(view) {
var _a, _b, _c;
if (view) {
this.lastMarkdownView = view;
}
const sourcePath = (_a = view == null ? void 0 : view.file) == null ? void 0 : _a.path;
const selection = (_c = (_b = view == null ? void 0 : view.editor) == null ? void 0 : _b.getSelection()) != null ? _c : "";
if (sourcePath && selection.trim()) {
this.lastSelection = {
text: selection,
sourcePath
};
}
}
gatherContext(maxNotes, maxSelectionLength) {
var _a, _b, _c, _d, _e;
const leaves = this.app.workspace.getLeavesOfType("markdown");
@@ -800,9 +814,7 @@ var WorkspaceContext = class {
}
const openNotePaths = Array.from(paths).slice(0, Math.max(0, maxNotes));
const view = (_b = this.app.workspace.getActiveViewOfType(import_obsidian4.MarkdownView)) != null ? _b : this.lastMarkdownView;
if (view) {
this.lastMarkdownView = view;
}
this.trackViewSelection(view);
const sourcePath = (_c = view == null ? void 0 : view.file) == null ? void 0 : _c.path;
const selection = (_e = (_d = view == null ? void 0 : view.editor) == null ? void 0 : _d.getSelection()) != null ? _e : "";
let selectionContext = null;
@@ -812,10 +824,8 @@ var WorkspaceContext = class {
sourcePath
};
this.lastSelection = selectionContext;
} else if (!sourcePath) {
} else if (this.lastSelection) {
selectionContext = this.lastSelection;
} else {
this.lastSelection = null;
}
if (selectionContext && selectionContext.text.length > maxSelectionLength) {
selectionContext = {
@@ -1076,7 +1086,10 @@ var OpenCodePlugin = class extends import_obsidian5.Plugin {
if (this.contextEventRefs.length > 0) {
return;
}
const activeLeafRef = this.app.workspace.on("active-leaf-change", () => {
const activeLeafRef = this.app.workspace.on("active-leaf-change", (leaf) => {
if ((leaf == null ? void 0 : leaf.view) instanceof import_obsidian5.MarkdownView) {
this.workspaceContext.trackViewSelection(leaf.view);
}
this.scheduleContextRefresh(0);
});
const fileOpenRef = this.app.workspace.on("file-open", () => {
@@ -1085,10 +1098,32 @@ var OpenCodePlugin = class extends import_obsidian5.Plugin {
const fileCloseRef = this.app.workspace.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 import_obsidian5.MarkdownView) {
this.workspaceContext.trackViewSelection(view);
}
this.scheduleContextRefresh(500);
});
this.contextEventRefs = [activeLeafRef, fileOpenRef, fileCloseRef, editorChangeRef];
const selectionChangeRef = this.app.workspace.on(
"editor-selection-change",
(_editor, view) => {
if (view instanceof import_obsidian5.MarkdownView) {
this.workspaceContext.trackViewSelection(view);
}
this.scheduleContextRefresh(200);
}
);
this.contextEventRefs = [
activeLeafRef,
fileOpenRef,
fileCloseRef,
layoutChangeRef,
editorChangeRef,
selectionChangeRef
];
this.contextEventRefs.forEach((ref) => this.registerEvent(ref));
}
clearContextListeners() {

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));
}