From f8bc80ac011ecdffea0f2df6449eac35e21c5532 Mon Sep 17 00:00:00 2001 From: Haouari haitam Kouider <57036855+haouarihk@users.noreply.github.com> Date: Sat, 21 Sep 2024 15:27:04 +0100 Subject: [PATCH] 0.7.41-beta --- manifest.json | 2 +- package.json | 2 +- src/main.ts | 276 ++++++++++++++++++++++++++------------------------ 3 files changed, 145 insertions(+), 135 deletions(-) diff --git a/manifest.json b/manifest.json index 307d67dc..6f2cb1d3 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-textgenerator-plugin", "name": "Text Generator", - "version": "0.7.38", + "version": "0.7.41-beta", "minAppVersion": "1.6.0", "description": "Text generation using AI", "author": "Noureddine Haouari", diff --git a/package.json b/package.json index 277714d1..c8f65334 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-textgenerator-plugin", - "version": "0.7.39-beta", + "version": "0.7.41-beta", "description": "Text generator is a handy plugin that helps you generate text content using GPT-3 (OpenAI).", "obsidian": { "minAppVersion": "1.6.0" diff --git a/src/main.ts b/src/main.ts index 018ed675..10358734 100644 --- a/src/main.ts +++ b/src/main.ts @@ -83,15 +83,134 @@ export default class TextGeneratorPlugin extends Plugin { temp: Record = {}; async onload() { - this.app.workspace.onLayoutReady(async () => { - try { - logger("loading textGenerator plugin"); - addIcon("GENERATE_ICON", GENERATE_ICON); - addIcon("GENERATE_META_ICON", GENERATE_META_ICON); + logger("loading textGenerator plugin"); + addIcon("GENERATE_ICON", GENERATE_ICON); + addIcon("GENERATE_META_ICON", GENERATE_META_ICON); + + this.defaultSettings = DEFAULT_SETTINGS; + await this.loadSettings(); + await this.addStatusBar(); + + // This adds a settings tab so the user can configure various aspects of the plugin + this.addSettingTab(new TextGeneratorSettingTab(this.app, this)); + + // registering different views + // Playground view + this.registerView( + VIEW_Playground_ID, + (leaf) => new PlaygroundView(leaf, this) + ); + + // "open template as tool" view + this.registerView(VIEW_TOOL_ID, (leaf) => new ToolView(leaf, this)); + + + // register events such as right click + if (this.settings.options["generate-in-right-click-menu"]) + this.registerEvent( + this.app.workspace.on("editor-menu", async (menu) => { + menu.addItem((item) => { + item.setIcon("GENERATE_META_ICON"); + item.setTitle("Generate"); + item.onClick(async () => { + try { + if (this.processing) + return this.textGenerator.signalController?.abort(); + const activeView = await this.getActiveView(); + const CM = ContentManagerCls.compile(activeView, this); + await this.textGenerator.generateInEditor({}, false, CM); + } catch (error) { + this.handelError(error); + } + }); + }); + }) + ); + + if (this.settings.options["batch-generate-in-right-click-files-menu"]) + this.registerEvent( + this.app.workspace.on( + "files-menu", + async (menu, files, source, leaf) => { + menu.addItem((item) => { + item.setIcon("GENERATE_META_ICON"); + item.setTitle("Generate"); + item.onClick(() => { + try { + new TemplatesModal( + this.app, + this, + async (result) => { + if (!result.path) + return this.handelError("couldn't find path"); + await this.textGenerator.generateBatchFromTemplate( + files.filter( + // @ts-ignore + (f) => !f.children && f.path.endsWith(".md") + ) as TFile[], + {}, + result.path, + true + ); + }, + "Generate and Create a New Note From Template" + ).open(); + } catch (error) { + this.handelError(error); + } + }); + }); + } + ) + ); + + + + // tg codeblock + if (this.settings.options["tg-block-processor"]) { + new TGBlock(this); + } + + // This creates an icon in the left ribbon. + if (!this.settings.options["disable-ribbon-icons"]) { + this.addRibbonIcon( + "GENERATE_ICON", + "Generate Text!", + async (evt: MouseEvent) => { + // Called when the user clicks the icon. + // const activeFile = this.app.workspace.getActiveFile(); + const activeView = this.getActiveViewMD(); + if (activeView !== null) { + const CM = ContentManagerCls.compile(activeView, this); + try { + await this.textGenerator.generateInEditor({}, false, CM); + } catch (error) { + this.handelError(error); + } + } + } + ); + + this.addRibbonIcon( + "boxes", + "Text Generator: Templates Packages Manager", + async (evt: MouseEvent) => { + new PackageManagerUI( + this.app, + this, + async (result: string) => { } + ).open(); + } + ); + } + + this.pluginAPIService = new PluginServiceAPI(this); + + registerAPI("tg", this.pluginAPIService, this as any); - this.defaultSettings = DEFAULT_SETTINGS; - await this.loadSettings(); + this.app.workspace.onLayoutReady(async () => { + try { // register managers this.versionManager = new VersionManager(this); await this.versionManager.load(); @@ -102,7 +221,6 @@ export default class TextGeneratorPlugin extends Plugin { // Register Services // text generator this.textGenerator = new TextGenerator(this.app, this); - await this.textGenerator.load(); // auto suggest if (this.settings.autoSuggestOptions?.isEnabled) @@ -113,151 +231,29 @@ export default class TextGeneratorPlugin extends Plugin { this.registerEditorSuggest(new SlashSuggest(this.app, this)); } - // This adds a settings tab so the user can configure various aspects of the plugin - this.addSettingTab(new TextGeneratorSettingTab(this.app, this)); - // register scopes this.commands = new Commands(this); this.tokensScope = new TokensScope(this); - await this.tokensScope.setup(); // add loading spinner this.registerEditorExtension(spinnersPlugin); this.app.workspace.updateOptions(); - // add status bar items - this.textGeneratorIconItem = this.addStatusBarItem(); - this.statusBarTokens = this.addStatusBarItem(); - this.statusBarItemEl = this.addStatusBarItem(); - - this.updateStatusBar(``); - - if (this.settings.autoSuggestOptions.showStatus) - this.autoSuggest?.AddStatusBar(); - - // registering different views - // Playground view - this.registerView( - VIEW_Playground_ID, - (leaf) => new PlaygroundView(leaf, this) - ); - - // "open template as tool" view - this.registerView(VIEW_TOOL_ID, (leaf) => new ToolView(leaf, this)); - - // register events such as right click - if (this.settings.options["generate-in-right-click-menu"]) - this.registerEvent( - this.app.workspace.on("editor-menu", async (menu) => { - menu.addItem((item) => { - item.setIcon("GENERATE_META_ICON"); - item.setTitle("Generate"); - item.onClick(async () => { - try { - if (this.processing) - return this.textGenerator.signalController?.abort(); - const activeView = await this.getActiveView(); - const CM = ContentManagerCls.compile(activeView, this); - await this.textGenerator.generateInEditor({}, false, CM); - } catch (error) { - this.handelError(error); - } - }); - }); - }) - ); - - if (this.settings.options["batch-generate-in-right-click-files-menu"]) - this.registerEvent( - this.app.workspace.on( - "files-menu", - async (menu, files, source, leaf) => { - menu.addItem((item) => { - item.setIcon("GENERATE_META_ICON"); - item.setTitle("Generate"); - item.onClick(() => { - try { - new TemplatesModal( - this.app, - this, - async (result) => { - if (!result.path) - return this.handelError("couldn't find path"); - await this.textGenerator.generateBatchFromTemplate( - files.filter( - // @ts-ignore - (f) => !f.children && f.path.endsWith(".md") - ) as TFile[], - {}, - result.path, - true - ); - }, - "Generate and Create a New Note From Template" - ).open(); - } catch (error) { - this.handelError(error); - } - }); - }); - } - ) - ); - - // tg codeblock - if (this.settings.options["tg-block-processor"]) { - new TGBlock(this); - } - - // This creates an icon in the left ribbon. - if (!this.settings.options["disable-ribbon-icons"]) { - this.addRibbonIcon( - "GENERATE_ICON", - "Generate Text!", - async (evt: MouseEvent) => { - // Called when the user clicks the icon. - // const activeFile = this.app.workspace.getActiveFile(); - const activeView = this.getActiveViewMD(); - if (activeView !== null) { - const CM = ContentManagerCls.compile(activeView, this); - try { - await this.textGenerator.generateInEditor({}, false, CM); - } catch (error) { - this.handelError(error); - } - } - } - ); - - this.addRibbonIcon( - "boxes", - "Text Generator: Templates Packages Manager", - async (evt: MouseEvent) => { - new PackageManagerUI( - this.app, - this, - async (result: string) => { } - ).open(); - } - ); - } - // add commands try { await Promise.all([ + this.tokensScope.setup(), + this.textGenerator.load(), this.commands.addCommands(), this.packageManager.load(), ]); } catch (err: any) { - console.trace("error in packageManager or commands", err); + console.trace("[TG:Error] in Loading a Service", err); } - this.pluginAPIService = new PluginServiceAPI(this); - - registerAPI("tg", this.pluginAPIService, this as any); } catch (err: any) { this.handelError(err); } @@ -273,7 +269,21 @@ export default class TextGeneratorPlugin extends Plugin { }); } + + async addStatusBar() { + // add status bar items + this.textGeneratorIconItem = this.addStatusBarItem(); + this.statusBarTokens = this.addStatusBarItem(); + this.statusBarItemEl = this.addStatusBarItem(); + + this.updateStatusBar(``); + + if (this.settings.autoSuggestOptions.showStatus) + this.autoSuggest?.AddStatusBar(); + } + async onunload() { + this.app.workspace.detachLeavesOfType(VIEW_TOOL_ID); this.app.workspace.detachLeavesOfType(VIEW_Playground_ID); await this.textGenerator.unload();