2023-05-06 11:29:46 +08:00
|
|
|
import {App} from "../index";
|
2023-05-07 17:31:50 +08:00
|
|
|
import {EventBus} from "./EventBus";
|
2023-05-08 21:00:41 +08:00
|
|
|
import {fetchPost} from "../util/fetch";
|
2023-05-04 18:28:57 +08:00
|
|
|
|
2023-05-06 11:29:46 +08:00
|
|
|
export class Plugin {
|
|
|
|
|
public i18n: IObject;
|
2023-05-07 17:31:50 +08:00
|
|
|
public eventBus: EventBus;
|
2023-05-08 21:00:41 +08:00
|
|
|
public data: any = {};
|
|
|
|
|
public name: string;
|
2023-05-04 18:28:57 +08:00
|
|
|
|
2023-05-06 11:29:46 +08:00
|
|
|
constructor(options: {
|
|
|
|
|
app: App,
|
2023-05-07 17:31:50 +08:00
|
|
|
name: string,
|
2023-05-06 11:29:46 +08:00
|
|
|
i18n: IObject
|
|
|
|
|
}) {
|
|
|
|
|
this.i18n = options.i18n;
|
2023-05-08 21:00:41 +08:00
|
|
|
this.name = options.name;
|
2023-05-07 17:31:50 +08:00
|
|
|
this.eventBus = new EventBus(options.name);
|
2023-05-04 18:28:57 +08:00
|
|
|
}
|
|
|
|
|
|
2023-05-08 21:00:41 +08:00
|
|
|
public onload() {
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-08 10:56:52 +08:00
|
|
|
public addTopBar(options: {
|
|
|
|
|
icon: string,
|
|
|
|
|
title: string,
|
2023-05-08 15:28:33 +08:00
|
|
|
position?: "right",
|
2023-05-08 10:56:52 +08:00
|
|
|
callback: (evt: MouseEvent) => void
|
|
|
|
|
}) {
|
|
|
|
|
const iconElement = document.createElement("div");
|
|
|
|
|
iconElement.className = "toolbar__item b3-tooltips b3-tooltips__sw";
|
|
|
|
|
iconElement.setAttribute("aria-label", options.title);
|
2023-05-08 12:35:56 +08:00
|
|
|
iconElement.setAttribute("data-menu", "true");
|
2023-05-08 10:56:52 +08:00
|
|
|
iconElement.innerHTML = options.icon.startsWith("icon") ? `<svg><use xlink:href="#${options.icon}"></use></svg>` : options.icon;
|
|
|
|
|
iconElement.addEventListener("click", options.callback);
|
|
|
|
|
document.querySelector("#" + (options.position === "right" ? "barSearch" : "drag")).before(iconElement);
|
|
|
|
|
return iconElement;
|
2023-05-04 18:28:57 +08:00
|
|
|
}
|
|
|
|
|
|
2023-05-08 21:00:41 +08:00
|
|
|
public openSetting() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public loadData(storageName: string) {
|
|
|
|
|
if (typeof this.data[storageName] === "undefined") {
|
|
|
|
|
this.data[storageName] = "";
|
|
|
|
|
}
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
fetchPost("/api/file/getFile", {path: `/data/storage/petal/${this.name}/${storageName}`}, (response) => {
|
|
|
|
|
if (response.code === 404) {
|
|
|
|
|
this.data[storageName] = "";
|
|
|
|
|
} else {
|
|
|
|
|
this.data[storageName] = response;
|
|
|
|
|
}
|
|
|
|
|
resolve(this.data[storageName]);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public saveData(storageName: string, data: any) {
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
const pathString = `/data/storage/petal/${this.name}/${storageName}`;
|
|
|
|
|
const file = new File([new Blob([data])], pathString.split('/').pop());
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append('path', pathString);
|
|
|
|
|
formData.append('file', file);
|
|
|
|
|
formData.append('isDir', "false");
|
|
|
|
|
fetchPost("/api/file/putFile", formData, (response) => {
|
|
|
|
|
resolve(response);
|
|
|
|
|
});
|
|
|
|
|
});
|
2023-05-04 18:28:57 +08:00
|
|
|
}
|
|
|
|
|
}
|