siyuan/app/src/layout/dock/util.ts

183 lines
6.2 KiB
TypeScript
Raw Normal View History

2022-06-29 10:35:35 +08:00
import {getAllModels} from "../getAll";
import {Tab} from "../Tab";
import {Graph} from "./Graph";
import {Outline} from "./Outline";
import {getInstanceById, getWndByLayout, resizeTabs, switchWnd} from "../util";
import {Backlink} from "./Backlink";
import {App} from "../../index";
import {Wnd} from "../Wnd";
import {fetchSyncPost} from "../../util/fetch";
2022-06-29 10:35:35 +08:00
export const openBacklink = async (options: {
app: App,
blockId: string,
rootId?: string,
title?: string,
useBlockId?: boolean,
}) => {
const backlink = getAllModels().backlink.find(item => {
if (item.blockId === options.blockId && item.type === "local") {
2022-06-29 10:35:35 +08:00
item.parent.parent.removeTab(item.parent.id);
return true;
}
});
if (backlink) {
return;
}
let wnd: Wnd = undefined;
const element = document.querySelector(".layout__wnd--active");
if (element) {
wnd = getInstanceById(element.getAttribute("data-id")) as Wnd;
}
if (!wnd) {
wnd = getWndByLayout(window.siyuan.layout.centerLayout);
}
const newWnd = wnd.split("lr");
if (!options.rootId) {
const response = await fetchSyncPost("api/block/getDocInfo", {id: options.blockId});
options.rootId = response.data.rootID
options.useBlockId = response.data.rootID !== response.data.id
options.title = response.data.name || "Untitled"
} else if (!options.title) {
const response = await fetchSyncPost("api/block/getDocInfo", {id: options.blockId});
options.title = response.data.name || "Untitled"
}
newWnd.addTab(new Tab({
2022-06-29 10:35:35 +08:00
icon: "iconLink",
title: options.title,
2022-06-29 10:35:35 +08:00
callback(tab: Tab) {
tab.addModel(new Backlink({
app: options.app,
2022-06-29 10:35:35 +08:00
type: "local",
tab,
// 通过搜索打开的包含上下文,但不是缩放,因此需要传 rootID https://ld246.com/article/1666786639708
blockId: options.useBlockId ? options.blockId : options.rootId,
rootId: options.rootId,
2022-06-29 10:35:35 +08:00
}));
}
}));
2022-06-29 10:35:35 +08:00
};
export const openGraph = async (options: {
app: App,
blockId: string,
rootId?: string,
title?: string,
useBlockId?: boolean,
}) => {
2022-06-29 10:35:35 +08:00
const graph = getAllModels().graph.find(item => {
if (item.blockId === options.blockId && item.type === "local") {
2022-06-29 10:35:35 +08:00
item.parent.parent.removeTab(item.parent.id);
return true;
}
});
if (graph) {
return;
}
let wnd: Wnd = undefined;
const element = document.querySelector(".layout__wnd--active");
if (element) {
wnd = getInstanceById(element.getAttribute("data-id")) as Wnd;
}
if (!wnd) {
wnd = getWndByLayout(window.siyuan.layout.centerLayout);
}
const newWnd = wnd.split("lr");
if (!options.rootId) {
const response = await fetchSyncPost("api/block/getDocInfo", {id: options.blockId});
options.rootId = response.data.rootID
options.useBlockId = response.data.rootID !== response.data.id
options.title = response.data.name || "Untitled"
} else if (!options.title) {
const response = await fetchSyncPost("api/block/getDocInfo", {id: options.blockId});
options.title = response.data.name || "Untitled"
}
newWnd.addTab(new Tab({
2022-06-29 10:35:35 +08:00
icon: "iconGraph",
title: options.title,
2022-06-29 10:35:35 +08:00
callback(tab: Tab) {
tab.addModel(new Graph({
app: options.app,
2022-06-29 10:35:35 +08:00
type: "local",
tab,
blockId: options.blockId,
rootId: options.rootId,
2022-06-29 10:35:35 +08:00
}));
}
}));
2022-06-29 10:35:35 +08:00
};
export const openOutline = async (protyle: IProtyle) => {
2022-06-29 10:35:35 +08:00
const outlinePanel = getAllModels().outline.find(item => {
if (item.blockId === protyle.block.rootID && item.type === "local") {
item.parent.parent.removeTab(item.parent.id);
return true;
}
});
if (outlinePanel) {
return;
}
let wnd: Wnd = undefined;
const element = document.querySelector(".layout__wnd--active");
if (element) {
wnd = getInstanceById(element.getAttribute("data-id")) as Wnd;
}
if (!wnd) {
wnd = getWndByLayout(window.siyuan.layout.centerLayout);
}
const newWnd = wnd.split("lr");
let title = ""
if (!protyle.title) {
const response = await fetchSyncPost("api/block/getDocInfo", {id: protyle.block.rootID});
title = response.data.name || "Untitled"
} else {
title = protyle.title.editElement.textContent || "Untitled"
}
newWnd.addTab(new Tab({
2022-06-29 10:35:35 +08:00
icon: "iconAlignCenter",
title,
2022-06-29 10:35:35 +08:00
callback(tab: Tab) {
tab.addModel(new Outline({
2023-06-01 20:50:49 +08:00
app: protyle.app,
2022-06-29 10:35:35 +08:00
type: "local",
tab,
blockId: protyle.block.rootID,
2023-06-18 22:40:44 +08:00
isPreview: !protyle.preview.element.classList.contains("fn__none")
2022-06-29 10:35:35 +08:00
}));
}
}));
2022-06-29 10:35:35 +08:00
newWnd.element.classList.remove("fn__flex-1");
newWnd.element.style.width = "200px";
switchWnd(newWnd, wnd);
2022-06-29 10:35:35 +08:00
};
export const resetFloatDockSize = () => {
if (!window.siyuan.layout.leftDock.pin && window.siyuan.layout.leftDock.layout.element.style.opacity === "1") {
window.siyuan.layout.leftDock.showDock(true);
}
if (!window.siyuan.layout.rightDock.pin && window.siyuan.layout.rightDock.layout.element.style.opacity === "1") {
window.siyuan.layout.rightDock.showDock(true);
}
if (!window.siyuan.layout.bottomDock.pin && window.siyuan.layout.bottomDock.layout.element.style.opacity === "1") {
2023-02-17 15:46:46 +08:00
window.siyuan.layout.bottomDock.showDock(true);
}
2023-02-11 21:34:42 +08:00
};
export const toggleDockBar = (useElement: Element) => {
const dockIsShow = useElement.getAttribute("xlink:href") === "#iconHideDock";
if (dockIsShow) {
useElement.setAttribute("xlink:href", "#iconDock");
} else {
useElement.setAttribute("xlink:href", "#iconHideDock");
}
document.querySelectorAll(".dock").forEach(item => {
if (dockIsShow) {
item.classList.add("fn__none");
} else if (item.querySelectorAll(".dock__item").length > 1) {
item.classList.remove("fn__none");
}
});
resizeTabs();
resetFloatDockSize();
2023-08-18 20:03:04 +08:00
};