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-09 11:36:11 +08:00
|
|
|
import {isMobile, isWindow} from "../util/functions";
|
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-09 19:17:16 +08:00
|
|
|
public data: any;
|
2023-05-08 21:00:41 +08:00
|
|
|
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");
|
2023-05-09 11:36:11 +08:00
|
|
|
if (isMobile()) {
|
|
|
|
|
iconElement.className = "b3-menu__item";
|
|
|
|
|
iconElement.setAttribute("aria-label", options.title);
|
|
|
|
|
iconElement.setAttribute("data-menu", "true");
|
|
|
|
|
iconElement.innerHTML = (options.icon.startsWith("icon") ? `<svg class="b3-menu__icon"><use xlink:href="#${options.icon}"></use></svg>` : options.icon) +
|
|
|
|
|
`<span class="b3-menu__label">${options.title}</span>`;
|
|
|
|
|
iconElement.addEventListener("click", options.callback);
|
|
|
|
|
document.querySelector("#menuAbout").after(iconElement);
|
|
|
|
|
} else if (!isWindow()) {
|
|
|
|
|
iconElement.className = "toolbar__item b3-tooltips b3-tooltips__sw";
|
|
|
|
|
iconElement.setAttribute("aria-label", options.title);
|
|
|
|
|
iconElement.setAttribute("data-menu", "true");
|
|
|
|
|
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);
|
|
|
|
|
}
|
2023-05-08 10:56:52 +08:00
|
|
|
return iconElement;
|
2023-05-04 18:28:57 +08:00
|
|
|
}
|
|
|
|
|
|
2023-05-08 21:00:41 +08:00
|
|
|
public openSetting() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public loadData(storageName: string) {
|
2023-05-09 19:17:16 +08:00
|
|
|
if (!this.data) {
|
|
|
|
|
this.data = {}
|
|
|
|
|
}
|
2023-05-08 21:00:41 +08:00
|
|
|
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) => {
|
2023-05-09 19:17:16 +08:00
|
|
|
this.data[storageName] = data;
|
2023-05-08 21:00:41 +08:00
|
|
|
resolve(response);
|
|
|
|
|
});
|
|
|
|
|
});
|
2023-05-04 18:28:57 +08:00
|
|
|
}
|
|
|
|
|
}
|