mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-20 00:20:12 +01:00
This commit is contained in:
parent
3babe0a5bb
commit
110eab8a1a
2 changed files with 15 additions and 15 deletions
|
|
@ -12,7 +12,7 @@ import {hasClosestByAttribute} from "../protyle/util/hasClosest";
|
||||||
export class Plugin {
|
export class Plugin {
|
||||||
public i18n: IObject;
|
public i18n: IObject;
|
||||||
public eventBus: EventBus;
|
public eventBus: EventBus;
|
||||||
public data: any;
|
public data: any = {};
|
||||||
public name: string;
|
public name: string;
|
||||||
public topBarIcons: Element[] = [];
|
public topBarIcons: Element[] = [];
|
||||||
public models: {
|
public models: {
|
||||||
|
|
@ -75,17 +75,12 @@ export class Plugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
public loadData(storageName: string) {
|
public loadData(storageName: string) {
|
||||||
if (!this.data) {
|
|
||||||
this.data = {};
|
|
||||||
}
|
|
||||||
if (typeof this.data[storageName] === "undefined") {
|
if (typeof this.data[storageName] === "undefined") {
|
||||||
this.data[storageName] = "";
|
this.data[storageName] = "";
|
||||||
}
|
}
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
fetchPost("/api/file/getFile", {path: `/data/storage/petal/${this.name}/${storageName}`}, (response) => {
|
fetchPost("/api/file/getFile", {path: `/data/storage/petal/${this.name}/${storageName}`}, (response) => {
|
||||||
if (response.code === 404) {
|
if (response.code !== 404) {
|
||||||
this.data[storageName] = "";
|
|
||||||
} else {
|
|
||||||
this.data[storageName] = response;
|
this.data[storageName] = response;
|
||||||
}
|
}
|
||||||
resolve(this.data[storageName]);
|
resolve(this.data[storageName]);
|
||||||
|
|
@ -95,11 +90,15 @@ export class Plugin {
|
||||||
|
|
||||||
public saveData(storageName: string, data: any) {
|
public saveData(storageName: string, data: any) {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
if (!this.data) {
|
|
||||||
this.data = {};
|
|
||||||
}
|
|
||||||
const pathString = `/data/storage/petal/${this.name}/${storageName}`;
|
const pathString = `/data/storage/petal/${this.name}/${storageName}`;
|
||||||
const file = new File([new Blob([data])], pathString.split("/").pop());
|
let file: File;
|
||||||
|
if (typeof data === "object") {
|
||||||
|
file = new File([new Blob([JSON.stringify(data)], {
|
||||||
|
type: "application/json"
|
||||||
|
})], pathString.split("/").pop());
|
||||||
|
} else {
|
||||||
|
file = new File([new Blob([data])], pathString.split("/").pop());
|
||||||
|
}
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append("path", pathString);
|
formData.append("path", pathString);
|
||||||
formData.append("file", file);
|
formData.append("file", file);
|
||||||
|
|
|
||||||
|
|
@ -48,17 +48,18 @@ export const fetchPost = (url: string, data?: any, cb?: (response: IWebSocketDat
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (typeof response.msg === "undefined") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (["/api/search/searchRefBlock", "/api/graph/getGraph", "/api/graph/getLocalGraph"].includes(url)) {
|
if (["/api/search/searchRefBlock", "/api/graph/getGraph", "/api/graph/getLocalGraph"].includes(url)) {
|
||||||
if (response.data.reqId && window.siyuan.reqIds[url] && window.siyuan.reqIds[url] > response.data.reqId) {
|
if (response.data.reqId && window.siyuan.reqIds[url] && window.siyuan.reqIds[url] > response.data.reqId) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (typeof response === "object" && typeof response.msg === "string" && typeof response.code === "number") {
|
||||||
if (processMessage(response) && cb) {
|
if (processMessage(response) && cb) {
|
||||||
cb(response);
|
cb(response);
|
||||||
}
|
}
|
||||||
|
} else if (cb) {
|
||||||
|
cb(response);
|
||||||
|
}
|
||||||
}).catch((e) => {
|
}).catch((e) => {
|
||||||
console.warn("fetch post error", e);
|
console.warn("fetch post error", e);
|
||||||
if (url === "/api/transactions" && (e.message === "Failed to fetch" || e.message === "Unexpected end of JSON input")) {
|
if (url === "/api/transactions" && (e.message === "Failed to fetch" || e.message === "Unexpected end of JSON input")) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue