Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add actions to toggle completions #3

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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