Refactor AIAgentPlugin to register intervals through the plugin and streamline command handling in the ribbon icon

This commit is contained in:
Andrew Beal 2025-10-03 20:27:49 +01:00
parent abe2caebf6
commit 3a67400427
2 changed files with 13 additions and 31 deletions

View file

@ -5,10 +5,12 @@
import ChatAreaThought from "./ChatAreaThought.svelte";
import StreamingIndicator from "./StreamingIndicator.svelte";
import { Greeting } from "Enums/Greeting";
import type AIAgentPlugin from "main";
export let messages: Array<{id: string, content: string, isUser: boolean, isStreaming: boolean}> = [];
export let chatContainer: HTMLDivElement;
let plugin: AIAgentPlugin = Resolve(Services.AIAgentPlugin);
let streamingMarkdownService: StreamingMarkdownService = Resolve(Services.StreamingMarkdownService);
let messageElements = new Map<string, HTMLElement>();
@ -70,11 +72,11 @@
function startScrolling() {
if (scrollInterval || userScrolledUp) return;
scrollInterval = window.setInterval(() => {
scrollInterval = plugin.registerInterval(window.setInterval(() => {
if (chatContainer && !userScrolledUp) {
chatContainer.scrollTop = chatContainer.scrollHeight;
}
}, 50);
}, 50));
setTimeout(() => {
if (scrollInterval) {

38
main.ts
View file

@ -37,14 +37,17 @@ export default class AIAgentPlugin extends Plugin {
(leaf) => new MainView(leaf)
);
// This creates an icon in the left ribbon.
const ribbonIconEl = this.addRibbonIcon('dice', 'Sample Plugin', (evt: MouseEvent) => {
// Called when the user clicks the icon.
new Notice('This is a notice!');
this.addCommand({
id: 'ai-agent',
name: 'AI Agent',
callback: () => {
this.activateView();
}
});
this.addRibbonIcon('sparkles', 'AI Agent', (evt: MouseEvent) => {
this.activateView();
});
// Perform additional things with the ribbon
ribbonIconEl.addClass('my-plugin-ribbon-class');
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
const statusBarItemEl = this.addStatusBarItem();
@ -59,27 +62,7 @@ export default class AIAgentPlugin extends Plugin {
editor.replaceSelection('Sample Editor Command');
}
});
// This adds a complex command that can check whether the current state of the app allows execution of the command
this.addCommand({
id: 'open-sample-modal-complex',
name: 'Open sample modal (complex)',
checkCallback: (checking: boolean) => {
// Conditions to check
const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (markdownView) {
// If checking is true, we're simply "checking" if the command can be run.
// If checking is false, then we want to actually perform the operation.
if (!checking) {
new SampleModal(this.app).open();
}
// This command will only show up in Command Palette when the check function returns true
return true;
}
}
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new AIAgentSettingTab(this.app, this));
// If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
@ -88,9 +71,6 @@ export default class AIAgentPlugin extends Plugin {
console.log('click', evt);
});
// When registering intervals, this function will automatically clear the interval when the plugin is disabled.
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
let odbCache: OdbCache = Resolve<OdbCache>(Services.OdbCache)
await odbCache.buildCache();