Make it so the settings save only after we stop editing

This commit is contained in:
Barrca 2025-01-29 13:30:20 -08:00
parent 75da491f42
commit d052bd0bb3

View file

@ -8,11 +8,11 @@ export interface InlineAISettings {
provider: "openai" | "ollama" | "custom";
model: string;
apiKey?: string;
customURL?: string; // Add a custom URL field
customURL?: string;
selectionPrompt: string;
cursorPrompt: string;
customCommands: SlashCommand[];
commandPrefix: string; // Add command prefix setting
commandPrefix: string;
}
// Default settings values
@ -23,11 +23,10 @@ export const DEFAULT_SETTINGS: InlineAISettings = {
customURL: "",
selectionPrompt: selectionPrompt,
cursorPrompt: cursorPrompt,
customCommands: [], // Default is an empty array
commandPrefix: "/" // Default command prefix
customCommands: [],
commandPrefix: "/"
};
// Settings tab class to display settings in Obsidian UI
export class InlineAISettingsTab extends PluginSettingTab {
plugin: MyPlugin;
@ -36,6 +35,12 @@ export class InlineAISettingsTab extends PluginSettingTab {
this.plugin = plugin;
}
/** Function to save settings after user finishes editing */
private async saveSettings() {
await this.plugin.saveSettings();
this.plugin.chatapi.updateSettings(this.plugin.settings);
}
display(): void {
const { containerEl } = this;
containerEl.empty();
@ -52,9 +57,8 @@ export class InlineAISettingsTab extends PluginSettingTab {
.setValue(this.plugin.settings.provider)
.onChange(async (value) => {
this.plugin.settings.provider = value as "openai" | "ollama" | "custom";
await this.plugin.saveSettings();
this.plugin.chatapi.updateSettings(this.plugin.settings);
this.display(); // Refresh to update the API key field visibility
await this.saveSettings();
this.display(); // Refresh UI to show/hide API key field
})
);
@ -62,91 +66,59 @@ export class InlineAISettingsTab extends PluginSettingTab {
new Setting(containerEl)
.setName("Model")
.setDesc("Specify the model to use.")
.addText(text =>
text
.setPlaceholder("e.g., gpt-4o-mini")
.addText((text) => {
text.setPlaceholder("e.g., gpt-4o-mini")
.setValue(this.plugin.settings.model)
.onChange(async (value) => {
this.plugin.settings.model = value;
await this.plugin.saveSettings();
this.plugin.chatapi.updateSettings(this.plugin.settings);
})
);
.inputEl.addEventListener("blur", async () => {
this.plugin.settings.model = text.getValue();
await this.saveSettings();
});
});
// API Key setting (conditionally displayed for OpenAI suported endpoints
if (this.plugin.settings.provider === "openai" ||
this.plugin.settings.provider === "custom"
) {
// API Key setting (conditionally displayed for OpenAI-supported endpoints)
if (this.plugin.settings.provider === "openai" || this.plugin.settings.provider === "custom") {
new Setting(containerEl)
.setName("API key")
.setDesc("Enter your API key.")
.addText((text) =>
text
.setPlaceholder("sk-...")
.addText((text) => {
text.setPlaceholder("sk-...")
.setValue(this.plugin.settings.apiKey || "")
.onChange(async (value) => {
this.plugin.settings.apiKey = value;
await this.plugin.saveSettings();
this.plugin.chatapi.updateSettings(this.plugin.settings);
})
);
.inputEl.addEventListener("blur", async () => {
this.plugin.settings.apiKey = text.getValue();
await this.saveSettings();
});
});
}
/**
* If the user chooses 'custom', show a field for the base endpoint URL.
*/
// Custom endpoint setting (only for "custom" provider)
if (this.plugin.settings.provider === "custom") {
new Setting(containerEl)
.setName("Custom endpoint")
.setDesc("Enter your OpenAI-compatible base URL (e.g. https://api.groq.com/openai/v1).")
.addText((text) =>
text
.setPlaceholder("https://api.mycustomhost.com/v1")
.setValue(this.plugin.settings.customURL || "")
.onChange(async (value) => {
this.plugin.settings.customURL = value;
await this.plugin.saveSettings();
this.plugin.chatapi.updateSettings(this.plugin.settings);
})
);
.setName("Custom endpoint")
.setDesc("Enter your OpenAI-compatible base URL (e.g. https://api.groq.com/openai/v1).")
.addText((text) => {
text.setPlaceholder("https://api.mycustomhost.com/v1")
.setValue(this.plugin.settings.customURL || "")
.inputEl.addEventListener("blur", async () => {
this.plugin.settings.customURL = text.getValue();
await this.saveSettings();
});
});
}
// Advanced Section
new Setting(containerEl)
.setName("Advanced")
.setHeading();
// Command Prefix setting
new Setting(containerEl)
.setName("Command Prefix")
.setDesc("The prefix used to trigger custom commands (e.g., /, !, #)")
.addText((text) =>
text
.setPlaceholder("/")
.setValue(this.plugin.settings.commandPrefix)
.onChange(async (value) => {
// Ensure the prefix is a single character
this.plugin.settings.commandPrefix = value[0];
await this.plugin.saveSettings();
this.display();
})
);
new Setting(containerEl).setName("Advanced").setHeading();
// Selection Prompt setting
new Setting(containerEl)
.setName("Selection prompt")
.setDesc("System Prompt used when the tooltip is triggered with selected text.")
.addTextArea((textarea) => {
textarea
.setPlaceholder("e.g., Summarize the selected text.")
textarea.setPlaceholder("e.g., Summarize the selected text.")
.setValue(this.plugin.settings.selectionPrompt)
.onChange(async (value) => {
this.plugin.settings.selectionPrompt = value;
await this.plugin.saveSettings();
this.plugin.chatapi.updateSettings(this.plugin.settings);
.inputEl.addEventListener("blur", async () => {
this.plugin.settings.selectionPrompt = textarea.getValue();
await this.saveSettings();
});
// Add a CSS class for styling
textarea.inputEl.classList.add("wide-text-settings");
});
@ -155,70 +127,75 @@ export class InlineAISettingsTab extends PluginSettingTab {
.setName("Cursor prompt")
.setDesc("System Prompt used when the tooltip is triggered with selected text.")
.addTextArea((textarea) => {
textarea
.setPlaceholder("e.g., Generate text based on cursor position.")
textarea.setPlaceholder("e.g., Generate text based on cursor position.")
.setValue(this.plugin.settings.cursorPrompt)
.onChange(async (value) => {
this.plugin.settings.cursorPrompt = value;
await this.plugin.saveSettings();
this.plugin.chatapi.updateSettings(this.plugin.settings);
.inputEl.addEventListener("blur", async () => {
this.plugin.settings.cursorPrompt = textarea.getValue();
await this.saveSettings();
});
// Add a CSS class for styling
textarea.inputEl.classList.add("wide-text-settings");
});
// Custom Commands Section
containerEl.createEl("h3", { text: "Custom Commands" });
// Add a description
containerEl.createEl("p", { text: "Add your own custom commands. Triggered with /" });
// Display existing commands
this.plugin.settings.customCommands.forEach((command, index) => {
const setting = new Setting(containerEl)
new Setting(containerEl)
.setName(`Command: ${command.keyword}`)
.setDesc("Edit the command prompt.")
.addText((text) =>
text
.setValue(command.keyword)
.addText((text) => {
text.setValue(command.keyword)
.setPlaceholder("Command name")
.onChange(async (value) => {
this.plugin.settings.customCommands[index].keyword = value;
await this.plugin.saveSettings();
})
)
.addTextArea((textarea) =>
textarea
.setValue(command.prompt)
.inputEl.addEventListener("blur", async () => {
this.plugin.settings.customCommands[index].keyword = text.getValue();
await this.saveSettings();
});
})
.addTextArea((textarea) => {
textarea.setValue(command.prompt)
.setPlaceholder("Command prompt")
.onChange(async (value) => {
this.plugin.settings.customCommands[index].prompt = value;
await this.plugin.saveSettings();
})
)
.inputEl.addEventListener("blur", async () => {
this.plugin.settings.customCommands[index].prompt = textarea.getValue();
await this.saveSettings();
});
})
.addExtraButton((btn) =>
btn
.setIcon("trash")
.setTooltip("Delete this command")
.onClick(async () => {
this.plugin.settings.customCommands.splice(index, 1);
await this.plugin.saveSettings();
this.display(); // Refresh the display
await this.saveSettings();
this.display();
})
);
});
// Add new command button
// Command Prefix setting
new Setting(containerEl)
.addButton((btn) =>
btn
.setButtonText("Add Command")
.setCta()
.onClick(async () => {
this.plugin.settings.customCommands.push({ keyword: "new_command", prompt: "" });
await this.plugin.saveSettings();
this.display(); // Refresh the display
})
);
.setName("Command Prefix")
.setDesc("The prefix used to trigger custom commands (e.g., /, !, #)")
.addText((text) => {
text.setPlaceholder("/")
.setValue(this.plugin.settings.commandPrefix)
.inputEl.addEventListener("blur", async () => {
this.plugin.settings.commandPrefix = text.getValue().charAt(0);
await this.saveSettings();
});
});
// Add new command button
new Setting(containerEl).addButton((btn) =>
btn
.setButtonText("Add Command")
.setCta()
.onClick(async () => {
this.plugin.settings.customCommands.push({ keyword: "new_command", prompt: "" });
await this.saveSettings();
this.display();
})
);
}
}