siyuan/app/src/plugin/API.ts

185 lines
5.3 KiB
TypeScript
Raw Normal View History

import {confirmDialog} from "../dialog/confirmDialog";
import {Plugin} from "./index";
import {showMessage} from "../dialog/message";
import {Dialog} from "../dialog";
import {fetchGet, fetchPost, fetchSyncPost} from "../util/fetch";
import {getBackend, getFrontend} from "../util/functions";
2023-05-10 16:11:02 +08:00
/// #if !MOBILE
import {openFile, openFileById} from "../editor/util";
import {openNewWindow, openNewWindowById} from "../window/openNewWindow";
import {Tab} from "../layout/Tab";
2023-05-10 16:11:02 +08:00
/// #endif
import {updateHotkeyTip} from "../protyle/util/compatibility";
import {App} from "../index";
import {Constants} from "../constants";
import {Setting} from "./Setting";
2023-06-12 12:42:22 +08:00
import {Menu} from "./Menu";
import {Protyle} from "../protyle";
import {openMobileFileById} from "../mobile/editor";
2023-05-10 16:11:02 +08:00
let openTab;
let openWindow;
2023-05-10 16:11:02 +08:00
/// #if MOBILE
openTab = () => {
// TODO: Mobile
2023-05-10 16:11:51 +08:00
};
openWindow = () => {
// TODO: Mobile
};
2023-05-10 16:11:02 +08:00
/// #else
openWindow = (options: {
position?: IPosition,
height?: number,
width?: number,
tab?: Tab,
doc?: {
id: string, // 块 id
},
}) => {
if (options.doc && options.doc.id) {
openNewWindowById(options.doc.id, {position: options.position, width: options.width, height: options.height});
return;
}
if (options.tab) {
openNewWindow(options.tab, {position: options.position, width: options.width, height: options.height});
return;
}
};
openTab = (options: {
app: App,
doc?: {
id: string, // 块 id
action?: string [] // cb-get-all获取所有内容cb-get-focus打开后光标定位在 id 所在的块cb-get-hl: 打开后 id 块高亮
zoomIn?: boolean // 是否缩放
},
pdf?: {
path: string,
page?: number, // pdf 页码
id?: string, // File Annotation id
},
asset?: {
path: string,
},
search?: ISearchOption
card?: {
type: TCardType,
id?: string, // cardType 为 all 时不传,否则传文档或笔记本 id
title?: string // cardType 为 all 时不传,否则传文档或笔记本名称
},
custom?: {
title: string,
icon: string,
data?: any
id: string
}
position?: "right" | "bottom",
keepCursor?: boolean // 是否跳转到新 tab 上
removeCurrentTab?: boolean // 在当前页签打开时需移除原有页签
afterOpen?: () => void // 打开后回调
}) => {
if (options.doc) {
if (options.doc.zoomIn) {
if (options.doc.action && !options.doc.action.includes(Constants.CB_GET_ALL)) {
options.doc.action.push(Constants.CB_GET_ALL);
} else {
options.doc.action = [Constants.CB_GET_ALL];
}
}
if (!options.doc.action) {
2023-06-01 20:50:49 +08:00
options.doc.action = [];
}
return openFileById({
app: options.app,
keepCursor: options.keepCursor,
removeCurrentTab: options.removeCurrentTab,
position: options.position,
afterOpen: options.afterOpen,
id: options.doc.id,
action: options.doc.action,
zoomIn: options.doc.zoomIn
});
}
if (options.asset) {
return openFile({
app: options.app,
keepCursor: options.keepCursor,
removeCurrentTab: options.removeCurrentTab,
position: options.position,
afterOpen: options.afterOpen,
assetPath: options.asset.path,
});
}
if (options.pdf) {
return openFile({
app: options.app,
keepCursor: options.keepCursor,
removeCurrentTab: options.removeCurrentTab,
position: options.position,
afterOpen: options.afterOpen,
assetPath: options.pdf.path,
page: options.pdf.id || options.pdf.page,
});
}
if (options.search) {
if (!options.search.idPath) {
options.search.idPath = [];
}
if (!options.search.hPath) {
options.search.hPath = "";
}
return openFile({
app: options.app,
keepCursor: options.keepCursor,
removeCurrentTab: options.removeCurrentTab,
position: options.position,
afterOpen: options.afterOpen,
searchData: options.search,
});
}
if (options.card) {
return openFile({
app: options.app,
keepCursor: options.keepCursor,
removeCurrentTab: options.removeCurrentTab,
position: options.position,
afterOpen: options.afterOpen,
custom: {
icon: "iconRiffCard",
title: window.siyuan.languages.spaceRepetition,
data: {
cardType: options.card.type,
id: options.card.id || "",
title: options.card.title,
},
id: "siyuan-card"
},
});
}
if (options.custom) {
return openFile(options);
}
};
2023-05-10 16:11:02 +08:00
/// #endif
export const API = {
confirm: confirmDialog,
showMessage,
adaptHotkey: updateHotkeyTip,
fetchPost,
fetchSyncPost,
fetchGet,
getFrontend,
getBackend,
2023-05-10 16:11:02 +08:00
openTab,
openWindow,
Protyle,
Plugin,
Dialog,
Menu,
Setting,
Constants,
openMobileFileById
};