基础设置项

This commit is contained in:
Chen 2025-04-22 15:45:58 +08:00
parent fea8d3af9e
commit ed8222dfcb
5 changed files with 145 additions and 2416 deletions

1
.gitignore vendored
View file

@ -20,3 +20,4 @@ data.json
# Exclude macOS Finder (System Explorer) View States
.DS_Store
package-lock.json

33
main.ts
View file

@ -1,21 +1,16 @@
import { Notice, Plugin } from "obsidian";
import { SettingTab } from "./setting/SettingTab";
import {
TypechoPluginSettings,
DEFAULT_SETTINGS,
} from "./setting/PluginSettings";
// Remember to rename these classes and interfaces!
interface MyPluginSettings {
mySetting: string;
}
const DEFAULT_SETTINGS: MyPluginSettings = {
mySetting: "default",
};
export default class MyPlugin extends Plugin {
settings: MyPluginSettings;
let settings: TypechoPluginSettings;
export default class TypechoPlugin extends Plugin {
async onload() {
console.log("loading plugin");
await this.loadSettings();
this.addSettingTab(new SettingTab(this.app, this, settings));
// This creates an icon in the left ribbon.
const ribbonIconEl = this.addRibbonIcon(
@ -33,14 +28,14 @@ export default class MyPlugin extends Plugin {
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
await this.saveData(settings);
}
}
export function getSettings() {
return settings;
}

2397
package-lock.json generated

File diff suppressed because it is too large Load diff

21
setting/PluginSettings.ts Normal file
View file

@ -0,0 +1,21 @@
export interface TypechoPluginSettings {
Host: string;
Token: string;
User: {
uid: string;
url: string;
screenName: string;
mail: string;
};
}
export const DEFAULT_SETTINGS: TypechoPluginSettings = {
Host: "",
Token: "",
User: {
uid: "",
url: "",
screenName: "",
mail: "",
},
};

109
setting/SettingTab.ts Normal file
View file

@ -0,0 +1,109 @@
import plugin from "../main";
import { App, PluginSettingTab, Setting, requestUrl, Notice } from "obsidian";
import { TypechoPluginSettings } from "./PluginSettings";
export class SettingTab extends PluginSettingTab {
plugin: plugin;
settings: TypechoPluginSettings;
constructor(app: App, plugin: plugin, settings: TypechoPluginSettings) {
super(app, plugin);
this.plugin = plugin;
this.settings = settings;
}
async display() {
const { containerEl } = this;
this.initTab(containerEl);
await this.refreshUserList(containerEl);
}
private async initTab(containerEl: HTMLElement): Promise<void> {
containerEl.empty();
new Setting(containerEl)
.setName("Host")
.setDesc("Typecho域名")
.addText((text) =>
text
.setPlaceholder("域名/index.php/api或者配置了伪静态请自行检查")
.setValue(this.settings.Host)
.onChange(async (value) => {
this.settings.Host = value;
await this.plugin.saveSettings();
})
)
.then((setting) => {
setting.controlEl.addEventListener("change", async () => {
await this.refreshUserList(containerEl);
});
});
new Setting(containerEl)
.setName("Token")
.setDesc("插件设置的ApiToken")
.addText((text) =>
text
.setPlaceholder("ApiToken")
.setValue(this.settings.Token)
.onChange(async (value) => {
this.settings.Token = value;
await this.plugin.saveSettings();
})
)
.then((setting) => {
setting.controlEl.addEventListener("change", async () => {
await this.refreshUserList(containerEl);
});
});
}
private async refreshUserList(containerEl: HTMLElement): Promise<void> {
let userList: string | any[] = [];
console.log(this.settings);
if (this.settings.Host && this.settings.Token) {
this.initTab(containerEl);
await requestUrl({
url: this.settings.Host + "/userList",
method: "get",
headers: {
token: this.settings.Token,
"Content-Type": "application/json",
},
})
.then(async (res) => {
console.log(res);
const data = res.json;
if (data.status == "success") {
userList = data.data;
await this.plugin.saveSettings();
} else {
new Notice(data.message);
}
})
.catch((err) => {
console.log(err);
new Notice(err);
});
}
new Setting(containerEl)
.setName("User")
.setDesc("Typecho用户,操作时使用的用户")
.addDropdown((dropdown) => {
for (let i = 0; i < userList.length; i++) {
dropdown.addOption(userList[i].uid, userList[i].screenName);
}
dropdown.setValue(this.settings.User.uid);
dropdown.onChange(async (value) => {
for (let i = 0; i < userList.length; i++) {
if (userList[i].uid == value) {
this.settings.User = userList[i];
await this.plugin.saveSettings();
break;
}
}
});
});
}
}