- 新增 CORS 中间件 (middleware/cors.go): 支持 app://obsidian.md 跨域 - 新增 Obsidian 插件: MemoryView(分页记忆列表) + GraphView(D3力导向图) + SearchModal(语义搜索) - Go API CORS: Access-Control-Allow-Origin: app://obsidian.md - Makefile 新增 build-obsidian / install-obsidian 目标 - 插件安装至 ~/.obsidian/plugins/zhiyi-memory/
134 lines
4.9 KiB
TypeScript
134 lines
4.9 KiB
TypeScript
import { App, Plugin, PluginSettingTab, Setting, addIcon } from "obsidian";
|
||
import { MemoryView, MEMORY_VIEW_TYPE } from "./src/MemoryView";
|
||
import { GraphView, GRAPH_VIEW_TYPE } from "./src/GraphView";
|
||
import { SearchModal } from "./src/SearchModal";
|
||
|
||
export default class ZhiYiPlugin extends Plugin {
|
||
private ribbonIcon: HTMLElement | null = null;
|
||
|
||
async onload() {
|
||
// ─── 注册视图 ─────────────────────────────────────────────────────
|
||
this.registerView(MEMORY_VIEW_TYPE, (leaf) => new MemoryView(leaf));
|
||
this.registerView(GRAPH_VIEW_TYPE, (leaf) => new GraphView(leaf));
|
||
|
||
// ─── 侧边栏命令 ───────────────────────────────────────────────────
|
||
// 记忆面板
|
||
this.addCommand({
|
||
id: "zhiyi-open-memory-view",
|
||
name: "打开织忆记忆面板",
|
||
callback: () => this.openMemoryView(),
|
||
});
|
||
|
||
// 图谱面板
|
||
this.addCommand({
|
||
id: "zhiyi-open-graph-view",
|
||
name: "打开织忆图谱面板",
|
||
callback: () => this.openGraphView(),
|
||
});
|
||
|
||
// 搜索模态框
|
||
this.addCommand({
|
||
id: "zhiyi-search-memories",
|
||
name: "搜索织忆记忆",
|
||
callback: () => this.openSearchModal(),
|
||
});
|
||
|
||
// ─── 状态栏 ────────────────────────────────────────────────────────
|
||
this.addStatusBarItem().setText("🧠 织忆");
|
||
this.addStatusBarItem().setText("Ctrl+P → 织忆");
|
||
|
||
// ─── 设置页 ────────────────────────────────────────────────────────
|
||
this.addSettingTab(new ZhiYiSettingTab(this.app, this));
|
||
}
|
||
|
||
onunload() {
|
||
// 关闭所有视图
|
||
this.app.workspace.getLeavesOfType(MEMORY_VIEW_TYPE).forEach((leaf) => leaf.detach());
|
||
this.app.workspace.getLeavesOfType(GRAPH_VIEW_TYPE).forEach((leaf) => leaf.detach());
|
||
}
|
||
|
||
async openMemoryView() {
|
||
const leaf = this.app.workspace.getLeaf("right");
|
||
await leaf.setViewState({ type: MEMORY_VIEW_TYPE, active: true });
|
||
this.app.workspace.revealLeaf(leaf);
|
||
}
|
||
|
||
async openGraphView() {
|
||
const leaf = this.app.workspace.getLeaf("right");
|
||
await leaf.setViewState({ type: GRAPH_VIEW_TYPE, active: true });
|
||
this.app.workspace.revealLeaf(leaf);
|
||
}
|
||
|
||
openSearchModal() {
|
||
const modal = new SearchModal(this.app);
|
||
modal.onSelect = (id, content) => {
|
||
// 选好后复制到剪贴板并在通知中显示
|
||
navigator.clipboard.writeText(content).catch(() => {});
|
||
new (this.app as unknown as { notify: (m: string) => void }).notify(
|
||
`已复制: ${content.slice(0, 80)}…`
|
||
);
|
||
};
|
||
modal.open();
|
||
}
|
||
}
|
||
|
||
// ─── 设置页 ────────────────────────────────────────────────────────────────
|
||
|
||
class ZhiYiSettingTab extends PluginSettingTab {
|
||
constructor(app: App, plugin: ZhiYiPlugin) {
|
||
super(app, plugin);
|
||
}
|
||
|
||
display() {
|
||
const { containerEl } = this;
|
||
containerEl.empty();
|
||
containerEl.createEl("h2", { text: "🧠 织忆设置" });
|
||
|
||
new Setting(containerEl)
|
||
.setName("API 地址")
|
||
.setDesc("织忆 Go API 服务地址(默认 http://localhost:7821)")
|
||
.addText((text) =>
|
||
text
|
||
.setPlaceholder("http://localhost:7821")
|
||
.setValue("http://localhost:7821")
|
||
.onChange((val) => {
|
||
// API 地址暂存(后续在 api.ts 中使用)
|
||
(window as unknown as Record<string, string>)["zhiyi_api_url"] = val;
|
||
})
|
||
);
|
||
|
||
new Setting(containerEl)
|
||
.setName("API Key")
|
||
.setDesc("织忆认证密钥")
|
||
.addText((text) =>
|
||
text.setPlaceholder("zhiyi-dev-key-2026").onChange((val) => {
|
||
(window as unknown as Record<string, string>)["zhiyi_api_key"] = val;
|
||
})
|
||
);
|
||
|
||
new Setting(containerEl)
|
||
.setName("默认 Namespace")
|
||
.setDesc("搜索和查询使用的默认命名空间")
|
||
.addText((text) =>
|
||
text.setValue("hermes-main").onChange((val) => {
|
||
(window as unknown as Record<string, string>)["zhiyi_namespace"] = val;
|
||
})
|
||
);
|
||
|
||
new Setting(containerEl)
|
||
.setName("打开记忆面板")
|
||
.addButton((btn) =>
|
||
btn.setButtonText("打开 🧠").onClick(() => {
|
||
(this.plugin as unknown as { openMemoryView: () => void }).openMemoryView();
|
||
})
|
||
);
|
||
|
||
new Setting(containerEl)
|
||
.setName("打开图谱面板")
|
||
.addButton((btn) =>
|
||
btn.setButtonText("打开 📐").onClick(() => {
|
||
(this.plugin as unknown as { openGraphView: () => void }).openGraphView();
|
||
})
|
||
);
|
||
}
|
||
} |