2022-05-26 15:18:53 +08:00
|
|
|
import {Tab} from "../Tab";
|
|
|
|
|
import {Model} from "../Model";
|
|
|
|
|
import {Tree} from "../../util/Tree";
|
|
|
|
|
import {getDockByType, setPanelFocus} from "../util";
|
|
|
|
|
import {fetchPost} from "../../util/fetch";
|
|
|
|
|
import {getAllModels} from "../getAll";
|
|
|
|
|
import {hasClosestByClassName} from "../../protyle/util/hasClosest";
|
|
|
|
|
import {updateHotkeyTip} from "../../protyle/util/compatibility";
|
2022-08-06 18:01:20 +08:00
|
|
|
import {openFileById} from "../../editor/util";
|
2022-05-26 15:18:53 +08:00
|
|
|
import {Constants} from "../../constants";
|
2022-05-28 22:43:27 +08:00
|
|
|
import {escapeHtml} from "../../util/escape";
|
2022-05-31 17:51:34 +08:00
|
|
|
import {unicode2Emoji} from "../../emoji";
|
2022-06-28 18:03:03 +08:00
|
|
|
import {onGet} from "../../protyle/util/onGet";
|
2023-03-01 15:17:45 +08:00
|
|
|
import {getPreviousBlock} from "../../protyle/wysiwyg/getBlock";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
|
export class Outline extends Model {
|
2022-12-07 18:27:24 +08:00
|
|
|
public tree: Tree;
|
2022-05-26 15:18:53 +08:00
|
|
|
public element: HTMLElement;
|
2022-05-31 17:51:34 +08:00
|
|
|
public headerElement: HTMLElement;
|
2022-05-26 15:18:53 +08:00
|
|
|
public type: "pin" | "local";
|
|
|
|
|
public blockId: string;
|
|
|
|
|
private openNodes: { [key: string]: string[] } = {};
|
|
|
|
|
|
|
|
|
|
constructor(options: {
|
|
|
|
|
tab: Tab,
|
|
|
|
|
blockId: string,
|
|
|
|
|
type: "pin" | "local"
|
|
|
|
|
}) {
|
|
|
|
|
super({
|
|
|
|
|
id: options.tab.id,
|
|
|
|
|
callback() {
|
|
|
|
|
if (this.type === "local") {
|
|
|
|
|
fetchPost("/api/block/checkBlockExist", {id: this.blockId}, existResponse => {
|
|
|
|
|
if (!existResponse.data) {
|
|
|
|
|
this.parent.parent.removeTab(this.parent.id);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
msgCallback(data) {
|
|
|
|
|
if (data) {
|
|
|
|
|
switch (data.cmd) {
|
|
|
|
|
case "transactions":
|
|
|
|
|
this.onTransaction(data);
|
|
|
|
|
break;
|
|
|
|
|
case "rename":
|
|
|
|
|
if (this.type === "local" && this.blockId === data.data.id) {
|
|
|
|
|
this.parent.updateTitle(data.data.title);
|
|
|
|
|
} else {
|
2022-05-31 17:51:34 +08:00
|
|
|
this.updateDocTitle({
|
2022-06-06 15:20:01 +08:00
|
|
|
title: data.data.title,
|
|
|
|
|
icon: Constants.ZWSP
|
2022-05-31 17:51:34 +08:00
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "unmount":
|
|
|
|
|
if (this.type === "local") {
|
|
|
|
|
fetchPost("/api/block/checkBlockExist", {id: this.blockId}, existResponse => {
|
|
|
|
|
if (!existResponse.data) {
|
|
|
|
|
this.parent.parent.removeTab(this.parent.id);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
break;
|
2022-11-03 22:47:19 +08:00
|
|
|
case "removeDoc":
|
|
|
|
|
if (data.data.ids.includes(this.blockId) && this.type === "local") {
|
|
|
|
|
this.parent.parent.removeTab(this.parent.id);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
this.blockId = options.blockId;
|
|
|
|
|
this.type = options.type;
|
|
|
|
|
options.tab.panelElement.classList.add("fn__flex-column", "file-tree", "sy__outline");
|
|
|
|
|
options.tab.panelElement.innerHTML = `<div class="block__icons">
|
|
|
|
|
<div class="block__logo">
|
|
|
|
|
<svg><use xlink:href="#iconAlignCenter"></use></svg>
|
|
|
|
|
${window.siyuan.languages.outline}
|
|
|
|
|
</div>
|
|
|
|
|
<span class="fn__flex-1 fn__space"></span>
|
2022-09-06 22:46:34 +08:00
|
|
|
<span data-type="expand" class="block__icon b3-tooltips b3-tooltips__sw" aria-label="${window.siyuan.languages.stickOpen} ${updateHotkeyTip(window.siyuan.config.keymap.editor.general.expand.custom)}">
|
2023-03-13 21:49:30 +08:00
|
|
|
<svg><use xlink:href="#iconExpand"></use></svg>
|
2022-05-26 15:18:53 +08:00
|
|
|
</span>
|
|
|
|
|
<span class="fn__space"></span>
|
2022-09-06 22:46:34 +08:00
|
|
|
<span data-type="collapse" class="block__icon b3-tooltips b3-tooltips__sw" aria-label="${window.siyuan.languages.collapse} ${updateHotkeyTip(window.siyuan.config.keymap.editor.general.collapse.custom)}">
|
2022-05-26 15:18:53 +08:00
|
|
|
<svg><use xlink:href="#iconContract"></use></svg>
|
|
|
|
|
</span>
|
|
|
|
|
<span class="${this.type === "local" ? "fn__none " : ""}fn__space"></span>
|
|
|
|
|
<span data-type="min" class="${this.type === "local" ? "fn__none " : ""}block__icon b3-tooltips b3-tooltips__sw" aria-label="${window.siyuan.languages.min} ${updateHotkeyTip(window.siyuan.config.keymap.general.closeTab.custom)}"><svg><use xlink:href='#iconMin'></use></svg></span>
|
|
|
|
|
</div>
|
2023-04-18 18:57:20 +08:00
|
|
|
<div class="b3-list-item fn__none"></div>
|
2022-10-15 11:35:53 +08:00
|
|
|
<div class="fn__flex-1" style="margin-bottom: 8px"></div>`;
|
2022-05-26 15:18:53 +08:00
|
|
|
this.element = options.tab.panelElement.lastElementChild as HTMLElement;
|
|
|
|
|
this.headerElement = options.tab.panelElement.firstElementChild as HTMLElement;
|
|
|
|
|
this.tree = new Tree({
|
|
|
|
|
element: options.tab.panelElement.lastElementChild as HTMLElement,
|
|
|
|
|
data: null,
|
|
|
|
|
click: (element: HTMLElement) => {
|
2022-08-04 00:11:11 +08:00
|
|
|
const id = element.getAttribute("data-node-id");
|
2022-09-03 19:49:52 +08:00
|
|
|
fetchPost("/api/attr/getBlockAttrs", {id}, (attrResponse) => {
|
2022-08-04 00:11:11 +08:00
|
|
|
openFileById({
|
|
|
|
|
id,
|
2022-09-03 19:49:52 +08:00
|
|
|
action: attrResponse.data["heading-fold"] === "1" ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL, Constants.CB_GET_HTML] : [Constants.CB_GET_FOCUS, Constants.CB_GET_SETID, Constants.CB_GET_CONTEXT, Constants.CB_GET_HTML],
|
2022-08-04 00:11:11 +08:00
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// 为了快捷键的 dispatch
|
|
|
|
|
options.tab.panelElement.querySelector('[data-type="collapse"]').addEventListener("click", () => {
|
2022-05-28 22:43:27 +08:00
|
|
|
this.tree.collapseAll();
|
2022-05-26 15:18:53 +08:00
|
|
|
});
|
2023-03-01 15:17:45 +08:00
|
|
|
options.tab.panelElement.querySelector('[data-type="expand"]').addEventListener("click", (event: MouseEvent & {
|
|
|
|
|
target: Element
|
|
|
|
|
}) => {
|
2022-05-26 15:18:53 +08:00
|
|
|
const iconElement = hasClosestByClassName(event.target, "block__icon");
|
|
|
|
|
if (!iconElement) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (iconElement.classList.contains("block__icon--active")) {
|
|
|
|
|
iconElement.classList.remove("block__icon--active");
|
|
|
|
|
} else {
|
|
|
|
|
iconElement.classList.add("block__icon--active");
|
|
|
|
|
}
|
|
|
|
|
this.tree.expandAll();
|
|
|
|
|
});
|
|
|
|
|
options.tab.panelElement.addEventListener("click", (event: MouseEvent & { target: HTMLElement }) => {
|
|
|
|
|
if (this.type === "local") {
|
|
|
|
|
setPanelFocus(options.tab.panelElement.parentElement.parentElement);
|
|
|
|
|
} else {
|
2022-10-14 22:29:40 +08:00
|
|
|
setPanelFocus(options.tab.panelElement);
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
|
let target = event.target as HTMLElement;
|
|
|
|
|
while (target && !target.isEqualNode(options.tab.panelElement)) {
|
|
|
|
|
if (target.classList.contains("block__icon")) {
|
|
|
|
|
const type = target.getAttribute("data-type");
|
|
|
|
|
switch (type) {
|
|
|
|
|
case "min":
|
|
|
|
|
getDockByType("outline").toggleModel("outline");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-06-03 11:11:33 +08:00
|
|
|
break;
|
|
|
|
|
} else if (target.isSameNode(this.headerElement.nextElementSibling) || target.classList.contains("block__icons")) {
|
|
|
|
|
getAllModels().editor.find(item => {
|
|
|
|
|
if (this.blockId === item.editor.protyle.block.rootID) {
|
2022-06-28 18:03:03 +08:00
|
|
|
if (item.editor.protyle.scroll.element.classList.contains("fn__none")) {
|
|
|
|
|
item.editor.protyle.contentElement.scrollTop = 0;
|
|
|
|
|
} else {
|
|
|
|
|
fetchPost("/api/filetree/getDoc", {
|
|
|
|
|
id: item.editor.protyle.block.rootID,
|
|
|
|
|
mode: 0,
|
2022-10-30 23:13:41 +08:00
|
|
|
size: window.siyuan.config.editor.dynamicLoadBlocks,
|
2022-06-28 18:03:03 +08:00
|
|
|
}, getResponse => {
|
|
|
|
|
onGet(getResponse, item.editor.protyle, [Constants.CB_GET_FOCUS]);
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-06-03 11:11:33 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
break;
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
|
target = target.parentElement;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
fetchPost("/api/outline/getDocOutline", {
|
|
|
|
|
id: this.blockId,
|
|
|
|
|
}, response => {
|
|
|
|
|
this.update(response);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-03 11:11:33 +08:00
|
|
|
public updateDocTitle(ial?: IObject) {
|
2023-04-19 11:28:26 +08:00
|
|
|
const docTitleElement = this.headerElement.nextElementSibling as HTMLElement;
|
2022-05-28 22:43:27 +08:00
|
|
|
if (this.type === "pin") {
|
2022-05-31 17:51:34 +08:00
|
|
|
if (ial) {
|
2022-11-27 16:48:43 +08:00
|
|
|
let iconHTML = `${unicode2Emoji(ial.icon || Constants.SIYUAN_IMAGE_FILE, false, "b3-list-item__graphic", true)}`;
|
2023-04-18 18:57:20 +08:00
|
|
|
if (ial.icon === Constants.ZWSP && docTitleElement.firstElementChild) {
|
|
|
|
|
iconHTML = docTitleElement.firstElementChild.outerHTML;
|
2022-05-31 17:51:34 +08:00
|
|
|
}
|
2023-04-18 18:57:20 +08:00
|
|
|
docTitleElement.innerHTML = `${iconHTML}
|
2022-05-31 17:51:34 +08:00
|
|
|
<span class="b3-list-item__text">${escapeHtml(ial.title)}</span>`;
|
2023-04-18 18:57:20 +08:00
|
|
|
docTitleElement.setAttribute("title", ial.title);
|
2023-04-19 11:28:26 +08:00
|
|
|
docTitleElement.classList.remove("fn__none");
|
2022-05-31 17:51:34 +08:00
|
|
|
} else {
|
2023-04-19 11:28:26 +08:00
|
|
|
docTitleElement.classList.add("fn__none");
|
2022-05-31 17:51:34 +08:00
|
|
|
}
|
2023-04-18 18:57:20 +08:00
|
|
|
} else {
|
2023-04-19 11:28:26 +08:00
|
|
|
docTitleElement.classList.add("fn__none");
|
2022-05-28 22:43:27 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
private onTransaction(data: IWebSocketData) {
|
|
|
|
|
let needReload = false;
|
|
|
|
|
data.data[0].doOperations.forEach((item: IOperation) => {
|
|
|
|
|
if ((item.action === "update" || item.action === "insert") &&
|
2022-12-30 17:31:28 +08:00
|
|
|
(item.data.indexOf('data-type="NodeHeading"') > -1 || item.data.indexOf(`<div contenteditable="true" spellcheck="${window.siyuan.config.editor.spellcheck}"><wbr></div>`) > -1)) {
|
2022-05-26 15:18:53 +08:00
|
|
|
needReload = true;
|
|
|
|
|
} else if (item.action === "delete" || item.action === "move") {
|
|
|
|
|
needReload = true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (data.data[0].undoOperations) {
|
|
|
|
|
data.data[0].undoOperations.forEach((item: IOperation) => {
|
|
|
|
|
if (item.action === "update" && item.data.indexOf('data-type="NodeHeading"') > -1) {
|
|
|
|
|
needReload = true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (needReload) {
|
|
|
|
|
fetchPost("/api/outline/getDocOutline", {
|
|
|
|
|
id: this.blockId,
|
|
|
|
|
}, response => {
|
|
|
|
|
this.update(response);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 15:17:45 +08:00
|
|
|
public setCurrent(nodeElement: HTMLElement) {
|
2023-05-15 11:46:51 +08:00
|
|
|
if (!nodeElement) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-03-01 15:17:45 +08:00
|
|
|
if (nodeElement.getAttribute("data-type") === "NodeHeading") {
|
2023-03-03 10:39:58 +08:00
|
|
|
this.setCurrentById(nodeElement.getAttribute("data-node-id"));
|
2023-03-01 15:17:45 +08:00
|
|
|
} else {
|
|
|
|
|
let previousElement = getPreviousBlock(nodeElement);
|
|
|
|
|
while (previousElement) {
|
|
|
|
|
if (previousElement.getAttribute("data-type") === "NodeHeading") {
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
previousElement = getPreviousBlock(previousElement);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (previousElement) {
|
2023-03-03 10:39:58 +08:00
|
|
|
this.setCurrentById(previousElement.getAttribute("data-node-id"));
|
2023-03-01 15:17:45 +08:00
|
|
|
} else {
|
|
|
|
|
fetchPost("/api/block/getBlockBreadcrumb", {
|
|
|
|
|
id: nodeElement.getAttribute("data-node-id"),
|
|
|
|
|
excludeTypes: []
|
|
|
|
|
}, (response) => {
|
|
|
|
|
response.data.reverse().find((item: IBreadcrumb) => {
|
|
|
|
|
if (item.type === "NodeHeading") {
|
|
|
|
|
this.setCurrentById(item.id);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private setCurrentById(id: string) {
|
2022-05-26 15:18:53 +08:00
|
|
|
this.element.querySelectorAll(".b3-list-item.b3-list-item--focus").forEach(item => {
|
|
|
|
|
item.classList.remove("b3-list-item--focus");
|
|
|
|
|
});
|
|
|
|
|
let currentElement = this.element.querySelector(`.b3-list-item[data-node-id="${id}"]`) as HTMLElement;
|
|
|
|
|
while (currentElement && currentElement.clientHeight === 0) {
|
|
|
|
|
currentElement = currentElement.parentElement.previousElementSibling as HTMLElement;
|
|
|
|
|
}
|
|
|
|
|
if (currentElement) {
|
|
|
|
|
currentElement.classList.add("b3-list-item--focus");
|
2022-10-02 12:44:28 +08:00
|
|
|
this.element.scrollTop = currentElement.offsetTop - this.element.clientHeight / 2 - 30;
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public update(data: IWebSocketData, callbackId?: string) {
|
|
|
|
|
let currentElement = this.element.querySelector(".b3-list-item--focus");
|
|
|
|
|
let currentId;
|
|
|
|
|
if (currentElement) {
|
|
|
|
|
currentId = currentElement.getAttribute("data-node-id");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.openNodes[this.blockId]) {
|
|
|
|
|
this.openNodes[this.blockId] = this.tree.getExpandIds();
|
|
|
|
|
}
|
|
|
|
|
if (typeof callbackId !== "undefined") {
|
|
|
|
|
this.blockId = callbackId;
|
|
|
|
|
}
|
|
|
|
|
this.tree.updateData(data.data);
|
|
|
|
|
if (this.openNodes[this.blockId] && !this.headerElement.querySelector('[data-type="expand"]').classList.contains("block__icon--active")) {
|
|
|
|
|
this.tree.setExpandIds(this.openNodes[this.blockId]);
|
|
|
|
|
} else {
|
|
|
|
|
this.tree.expandAll();
|
|
|
|
|
this.openNodes[this.blockId] = this.tree.getExpandIds();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentId) {
|
|
|
|
|
currentElement = this.element.querySelector(`[data-node-id="${currentId}"]`);
|
|
|
|
|
if (currentElement) {
|
|
|
|
|
currentElement.classList.add("b3-list-item--focus");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|