Skip to content

Commit

Permalink
Merge pull request #3 from taichimaeda/add-actions-to-toggle-completions
Browse files Browse the repository at this point in the history
Add actions to toggle completions
  • Loading branch information
taichimaeda authored Apr 17, 2024
2 parents 442190f + 5a522b2 commit bbf7163
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
2 changes: 2 additions & 0 deletions esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const context = await esbuild.context({
sourcemap: prod ? false : "inline",
treeShaking: true,
outfile: "main.js",
// For loading custom icons:
loader: { ".svg": "text" },
});

if (prod) {
Expand Down
19 changes: 19 additions & 0 deletions src/icons/bot-off.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '*.svg' {
const content: string;
export default content;
}
71 changes: 70 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Extension } from '@codemirror/state';
import { Notice, Plugin } from 'obsidian';
import { addIcon, Notice, Plugin, setIcon } from 'obsidian';
import { MemoryCache } from './api/cache';
import { APIClient, OpenAIClient } from './api/openai';
import { CHAT_VIEW_TYPE, ChatView } from './chat/view';
import { inlineCompletionsExtension } from './editor/extension';
import botOffIcon from './icons/bot-off.svg';
import {
DEFAULT_SETTINGS,
MarkpilotSettings,
Expand All @@ -20,6 +21,8 @@ export default class Markpilot extends Plugin {
await this.loadSettings();
this.addSettingTab(new MarkpilotSettingTab(this.app, this));

// Initialize the OpenAI API client and
// register the editor extension and chat view.
const client = new OpenAIClient(this);
const cache = new MemoryCache(client, this);
this.client = cache;
Expand All @@ -33,6 +36,7 @@ export default class Markpilot extends Plugin {
this.activateView();
}

// Notify the user if the OpenAI API key is not set.
if (
(this.settings.completions.enabled || this.settings.chat.enabled) &&
!this.settings.apiKey?.startsWith('sk')
Expand All @@ -42,6 +46,37 @@ export default class Markpilot extends Plugin {
);
}

this.registerRibbonActions();
this.registerCommands();
}

registerRibbonActions() {
// Register custom icon.
// TODO:
// Remove once this PR gets merged:
// https://github.com/lucide-icons/lucide/pull/2079
addIcon('bot-off', botOffIcon);

// TODO:
// Extract duplicate logic when toggling features.
const toggleCompletionsItem = this.addRibbonIcon(
'bot',
'Toggle inline completions',
() => {
this.settings.completions.enabled = !this.settings.completions.enabled;
this.saveSettings();
setIcon(
toggleCompletionsItem,
this.settings.completions.enabled ? 'bot' : 'bot-off',
);
new Notice(
`Inline completions ${this.settings.completions.enabled ? 'enabled' : 'disabled'}.`,
);
},
);
}

registerCommands() {
this.addCommand({
id: 'enable-completions',
name: 'Enable inline completions',
Expand All @@ -60,6 +95,17 @@ export default class Markpilot extends Plugin {
new Notice('Inline completions disabled.');
},
});
this.addCommand({
id: 'toggle-completions',
name: 'Toggle inline completions',
callback: () => {
this.settings.completions.enabled = !this.settings.completions.enabled;
this.saveSettings();
new Notice(
`Inline completions ${this.settings.completions.enabled ? 'enabled' : 'disabled'}.`,
);
},
});
this.addCommand({
id: 'enable-chat-view',
name: 'Enable chat view',
Expand All @@ -80,6 +126,18 @@ export default class Markpilot extends Plugin {
new Notice('Chat view disabled.');
},
});
this.addCommand({
id: 'toggle-chat-view',
name: 'Toggle chat view',
callback: () => {
this.settings.chat.enabled = !this.settings.chat.enabled;
this.saveSettings();
this.deactivateView();
new Notice(
`Chat view ${this.settings.completions.enabled ? 'enabled' : 'disabled'}.`,
);
},
});
this.addCommand({
id: 'clear-chat-history',
name: 'Clear chat history',
Expand Down Expand Up @@ -111,6 +169,17 @@ export default class Markpilot extends Plugin {
new Notice('Cache disabled.');
},
});
this.addCommand({
id: 'toggle-cache',
name: 'Toggle cache',
callback: () => {
this.settings.cache.enabled = !this.settings.cache.enabled;
this.saveSettings();
new Notice(
`Cache ${this.settings.completions.enabled ? 'enabled' : 'disabled'}.`,
);
},
});
}

getEditorExtension() {
Expand Down

0 comments on commit bbf7163

Please sign in to comment.