aqeja_ClipperMaster-for-Obs.../main.ts

108 lines
2.7 KiB
TypeScript
Raw Normal View History

2025-03-25 02:03:10 +00:00
import { texts, upsertFile, Welcome } from "common";
2025-04-11 08:36:43 +00:00
import { App, Plugin, PluginSettingTab, Setting, addIcon } from "obsidian";
2025-03-14 13:46:28 +00:00
import { server } from "server";
2025-03-19 08:16:43 +00:00
interface ClipperMasterPluginSettings {
2025-03-14 13:46:28 +00:00
port: number;
}
2025-03-19 08:16:43 +00:00
const DEFAULT_SETTINGS: ClipperMasterPluginSettings = {
2025-03-14 13:46:28 +00:00
port: 8282,
};
export default class ClipperMasterPlugin extends Plugin {
2025-03-19 08:16:43 +00:00
settings: ClipperMasterPluginSettings;
2025-03-14 13:46:28 +00:00
async onload() {
addIcon(
"clippermaster",
`
<rect x="4" y="5" width="3" height="16" rx="1.5" transform="rotate(-10 4 5)"/>
<rect x="11" y="8" width="3" height="11" rx="1.5" transform="rotate(-90 11 8)"/>
<rect x="11" y="21" width="3" height="11" rx="1.5" transform="rotate(-90 11 21)"/>
<rect x="11" y="9" width="11" height="6" rx="2" stroke-opacity="0.6"/>
`
);
await this.loadSettings();
// This adds a settings tab so the user can configure various aspects of the plugin
2025-03-25 02:03:10 +00:00
this.addSettingTab(new ClipperMasterSettingTab(this.app, this));
2025-03-14 13:46:28 +00:00
server.start(this.app);
}
2025-04-11 08:36:43 +00:00
onUserEnable() {
upsertFile(this.app, texts.welcome, Welcome);
}
2025-03-14 13:46:28 +00:00
onunload() {
server.close();
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
}
2025-03-25 02:03:10 +00:00
class ClipperMasterSettingTab extends PluginSettingTab {
2025-03-14 13:46:28 +00:00
plugin: ClipperMasterPlugin;
constructor(app: App, plugin: ClipperMasterPlugin) {
super(app, plugin);
this.plugin = plugin;
2025-03-25 02:03:10 +00:00
}
2025-03-14 13:46:28 +00:00
display(): void {
const { containerEl } = this;
containerEl.empty();
2025-04-11 08:36:43 +00:00
// Port setting
2025-03-14 13:46:28 +00:00
const portSetting = new Setting(containerEl)
2025-03-25 02:03:10 +00:00
.setName(texts.listeningPort)
2025-04-11 08:36:43 +00:00
.setDesc(texts.desc);
2025-03-14 13:46:28 +00:00
const invalidPortElement = portSetting.infoEl.createDiv();
invalidPortElement.hide();
invalidPortElement
.createSpan("settings-error-element")
2025-03-25 02:03:10 +00:00
.setText(texts.error);
2025-03-14 13:46:28 +00:00
portSetting.addText((text) => {
2025-04-11 08:36:43 +00:00
text.setPlaceholder(texts.enterPort);
2025-03-14 13:46:28 +00:00
text.setValue(String(this.plugin.settings.port));
text.onChange(async (value) => {
const numValue = Number(value);
if (isNaN(numValue) || numValue < 1 || numValue > 65535) {
invalidPortElement.show();
return;
}
invalidPortElement.hide();
this.plugin.settings.port = numValue;
await this.saveAndReload();
});
});
2025-04-11 08:36:43 +00:00
// Guidance button setting
new Setting(containerEl)
.setName(texts.guidanceButton)
.setDesc(texts.guidanceDesc)
.addButton((btn) => {
btn.setButtonText(texts.openGuidance)
.onClick(() => {
window.open("https://docs.clippermaster.com/docs/integrations/obsidian", "_blank");
});
});
2025-03-14 13:46:28 +00:00
}
async saveAndReload() {
await this.plugin.saveSettings();
2025-04-11 08:36:43 +00:00
await server.close();
server.start(this.plugin.app);
2025-03-14 13:46:28 +00:00
}
}