From e6e7a508e554283cefe94e53f4e0b8d15b24236c Mon Sep 17 00:00:00 2001 From: Zak <97318732+zakhij@users.noreply.github.com> Date: Sun, 21 Jul 2024 16:06:04 -0700 Subject: [PATCH] add summarize to context menu (#482) --- src/main.ts | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index c6058f09..2f8c1fd1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -29,7 +29,7 @@ import { } from "@/utils"; import VectorDBManager, { VectorStoreDocument } from "@/vectorDBManager"; import { MD5 } from "crypto-js"; -import { Editor, Notice, Plugin, TFile, WorkspaceLeaf } from "obsidian"; +import { Editor, Notice, Plugin, TFile, WorkspaceLeaf, Menu } from "obsidian"; import PouchDB from "pouchdb"; export default class CopilotPlugin extends Plugin { @@ -446,6 +446,11 @@ export default class CopilotPlugin extends Plugin { new Notice("An error occurred while saving vault to vector store."); } } + + this.registerEvent( + this.app.workspace.on("editor-menu", this.handleContextMenu) + ); + } private getVaultIdentifier(): string { @@ -699,6 +704,28 @@ export default class CopilotPlugin extends Plugin { } } + handleContextMenu = ( + menu: Menu, + editor: Editor + ): void => { + this.addContextMenu(menu, editor, this); + }; + + addContextMenu = ( + menu: Menu, + editor: Editor, + plugin: this, + ): void => { + menu.addItem((item) => { + item + .setTitle("Copilot: Summarize Selection") + .setIcon("bot") + .onClick(async (e) => { + plugin.processSelection(editor, "summarizeSelection"); + }); + }); + } + getChainManagerParams(): LangChainParams { const { openAIApiKey,