Skip to content

Commit

Permalink
0.7.41-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
haouarihk committed Sep 21, 2024
1 parent 1d18aee commit f8bc80a
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 135 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
276 changes: 143 additions & 133 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,134 @@ export default class TextGeneratorPlugin extends Plugin {
temp: Record<string, any> = {};

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();
Expand All @@ -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)
Expand All @@ -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);
}
Expand All @@ -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();
Expand Down

0 comments on commit f8bc80a

Please sign in to comment.