2022-05-26 15:18:53 +08:00
|
|
|
|
import {Constants} from "../../constants";
|
|
|
|
|
import {hideElements} from "../ui/hideElements";
|
|
|
|
|
import {fetchPost} from "../../util/fetch";
|
|
|
|
|
import {processRender} from "./processCode";
|
2023-06-07 10:36:20 +08:00
|
|
|
|
import {highlightRender} from "../render/highlightRender";
|
|
|
|
|
import {blockRender} from "../render/blockRender";
|
2023-12-20 10:10:49 +08:00
|
|
|
|
import {bgFade, scrollCenter} from "../../util/highlightById";
|
2022-06-29 20:25:30 +08:00
|
|
|
|
/// #if !MOBILE
|
2022-05-26 15:18:53 +08:00
|
|
|
|
import {pushBack} from "../../util/backForward";
|
2022-06-29 20:25:30 +08:00
|
|
|
|
/// #endif
|
2023-05-13 22:16:22 +08:00
|
|
|
|
import {focusBlock, focusByOffset} from "./selection";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
import {hasClosestByAttribute, hasClosestByClassName} from "./hasClosest";
|
|
|
|
|
import {preventScroll} from "../scroll/preventScroll";
|
2022-08-29 10:32:54 +08:00
|
|
|
|
import {removeLoading} from "../ui/initUI";
|
2023-02-07 11:58:24 +08:00
|
|
|
|
import {isMobile} from "../../util/functions";
|
2023-02-07 12:40:45 +08:00
|
|
|
|
import {foldPassiveType} from "../wysiwyg/renderBacklink";
|
2023-04-10 11:25:25 +08:00
|
|
|
|
import {showMessage} from "../../dialog/message";
|
2023-06-08 22:55:31 +08:00
|
|
|
|
import {avRender} from "../render/av/render";
|
2023-09-10 15:31:09 +08:00
|
|
|
|
import {hideTooltip} from "../../dialog/tooltip";
|
2023-12-08 21:21:57 +08:00
|
|
|
|
import {stickyRow} from "../render/av/row";
|
2024-05-08 10:39:26 +08:00
|
|
|
|
import {updateReadonly as updateReadonlyMethod} from "../breadcrumb/action";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
2023-06-01 14:56:21 +08:00
|
|
|
|
export const onGet = (options: {
|
|
|
|
|
data: IWebSocketData,
|
|
|
|
|
protyle: IProtyle,
|
|
|
|
|
action?: string[],
|
|
|
|
|
scrollAttr?: IScrollAttr
|
2024-01-23 13:08:49 +08:00
|
|
|
|
updateReadonly?: boolean
|
2023-07-18 23:46:08 +08:00
|
|
|
|
afterCB?: () => void
|
2023-06-01 14:56:21 +08:00
|
|
|
|
}) => {
|
|
|
|
|
if (!options.action) {
|
|
|
|
|
options.action = [];
|
|
|
|
|
}
|
|
|
|
|
options.protyle.wysiwyg.element.removeAttribute("data-top");
|
|
|
|
|
if (options.data.code === 1) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
// 其他报错
|
2024-04-06 11:19:12 +08:00
|
|
|
|
if (!options.action.includes(Constants.CB_GET_APPEND)) { // 向下加载时块可能还没有创建 https://github.com/siyuan-note/siyuan/issues/10851
|
|
|
|
|
if (options.protyle.model) {
|
|
|
|
|
options.protyle.model.parent.parent.removeTab(options.protyle.model.parent.id, false);
|
|
|
|
|
} else {
|
|
|
|
|
options.protyle.element.innerHTML = `<div class="ft__smaller ft__secondary b3-form__space--small" contenteditable="false">${window.siyuan.languages.refExpired}</div>`;
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-06-01 14:56:21 +08:00
|
|
|
|
if (options.data.code === 3) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
// block not found
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-06-01 14:56:21 +08:00
|
|
|
|
options.protyle.notebookId = options.data.data.box;
|
|
|
|
|
options.protyle.path = options.data.data.path;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
2023-06-01 14:56:21 +08:00
|
|
|
|
if (options.data.data.eof && !options.scrollAttr) {
|
|
|
|
|
if (options.action.includes(Constants.CB_GET_BEFORE)) {
|
|
|
|
|
options.protyle.wysiwyg.element.firstElementChild.setAttribute("data-eof", "1");
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} else {
|
2023-06-01 14:56:21 +08:00
|
|
|
|
options.protyle.wysiwyg.element.lastElementChild.setAttribute("data-eof", "2");
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2023-06-01 14:56:21 +08:00
|
|
|
|
if (options.data.data.mode !== 4) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-25 13:12:12 +08:00
|
|
|
|
hideElements(["gutterOnly"], options.protyle);
|
2023-06-01 14:56:21 +08:00
|
|
|
|
options.protyle.block.parentID = options.data.data.parentID;
|
|
|
|
|
options.protyle.block.parent2ID = options.data.data.parent2ID;
|
|
|
|
|
options.protyle.block.rootID = options.data.data.rootID;
|
|
|
|
|
options.protyle.block.showAll = false;
|
|
|
|
|
options.protyle.block.mode = options.data.data.mode;
|
|
|
|
|
options.protyle.block.blockCount = options.data.data.blockCount;
|
|
|
|
|
options.protyle.block.scroll = options.data.data.scroll;
|
|
|
|
|
options.protyle.block.action = options.action;
|
|
|
|
|
if (!options.action.includes(Constants.CB_GET_UNCHANGEID)) {
|
|
|
|
|
options.protyle.block.id = options.data.data.id; // 非缩放情况时不一定是 rootID(搜索打开页签);缩放时必为缩放 id,否则需查看代码
|
|
|
|
|
options.protyle.scroll.lastScrollTop = 0;
|
|
|
|
|
options.protyle.contentElement.scrollTop = 0;
|
|
|
|
|
options.protyle.wysiwyg.element.setAttribute("data-doc-type", options.data.data.type);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 17:57:52 +08:00
|
|
|
|
// 防止动态加载加载过多的内容
|
2023-06-01 14:56:21 +08:00
|
|
|
|
if (options.action.includes(Constants.CB_GET_APPEND) || options.action.includes(Constants.CB_GET_BEFORE) || options.action.includes(Constants.CB_GET_HTML)) {
|
2022-08-08 11:36:51 +08:00
|
|
|
|
setHTML({
|
2023-06-01 14:56:21 +08:00
|
|
|
|
content: options.data.data.content,
|
|
|
|
|
expand: options.data.data.isBacklinkExpand,
|
|
|
|
|
action: options.action,
|
|
|
|
|
scrollAttr: options.scrollAttr,
|
2024-01-23 13:08:49 +08:00
|
|
|
|
updateReadonly: options.updateReadonly,
|
2023-06-01 14:56:21 +08:00
|
|
|
|
isSyncing: options.data.data.isSyncing,
|
2023-07-18 23:46:08 +08:00
|
|
|
|
afterCB: options.afterCB,
|
2023-06-01 20:50:49 +08:00
|
|
|
|
}, options.protyle);
|
2023-06-01 14:56:21 +08:00
|
|
|
|
removeLoading(options.protyle);
|
2022-08-08 11:36:51 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-07 21:58:41 +08:00
|
|
|
|
fetchPost("/api/block/getDocInfo", {
|
2023-06-01 14:56:21 +08:00
|
|
|
|
id: options.protyle.block.rootID
|
2022-08-07 21:58:41 +08:00
|
|
|
|
}, (response) => {
|
2023-06-01 14:56:21 +08:00
|
|
|
|
if (options.protyle.options.render.title) {
|
2022-08-07 21:58:41 +08:00
|
|
|
|
// 页签没有打开
|
2023-06-01 14:56:21 +08:00
|
|
|
|
options.protyle.title.render(options.protyle, response);
|
2023-09-17 22:49:10 +08:00
|
|
|
|
} else {
|
|
|
|
|
if (options.protyle.options.render.background) {
|
|
|
|
|
options.protyle.background.render(response.data.ial, options.protyle.block.rootID);
|
|
|
|
|
}
|
2023-06-01 14:56:21 +08:00
|
|
|
|
options.protyle.wysiwyg.renderCustom(response.data.ial);
|
2022-08-07 21:58:41 +08:00
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
2022-08-07 21:58:41 +08:00
|
|
|
|
setHTML({
|
2023-06-01 14:56:21 +08:00
|
|
|
|
content: options.data.data.content,
|
|
|
|
|
expand: options.data.data.isBacklinkExpand,
|
|
|
|
|
action: options.action,
|
|
|
|
|
scrollAttr: options.scrollAttr,
|
2024-01-23 13:08:49 +08:00
|
|
|
|
updateReadonly: options.updateReadonly,
|
2023-06-01 14:56:21 +08:00
|
|
|
|
isSyncing: options.data.data.isSyncing,
|
2023-07-18 23:46:08 +08:00
|
|
|
|
afterCB: options.afterCB,
|
2023-06-01 20:50:49 +08:00
|
|
|
|
}, options.protyle);
|
2023-06-01 14:56:21 +08:00
|
|
|
|
removeLoading(options.protyle);
|
2022-08-07 21:58:41 +08:00
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
2022-10-22 12:06:59 +08:00
|
|
|
|
const setHTML = (options: {
|
|
|
|
|
content: string,
|
|
|
|
|
action?: string[],
|
|
|
|
|
isSyncing: boolean,
|
2022-12-24 12:13:50 +08:00
|
|
|
|
expand: boolean,
|
2024-01-23 13:08:49 +08:00
|
|
|
|
updateReadonly?: boolean,
|
2023-05-13 22:16:22 +08:00
|
|
|
|
scrollAttr?: IScrollAttr
|
2023-07-18 23:46:08 +08:00
|
|
|
|
afterCB?: () => void
|
2023-06-01 20:50:49 +08:00
|
|
|
|
}, protyle: IProtyle) => {
|
2023-06-18 00:40:36 +08:00
|
|
|
|
if (protyle.contentElement.classList.contains("fn__none") && protyle.wysiwyg.element.innerHTML !== "") {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
protyle.block.showAll = options.action.includes(Constants.CB_GET_ALL);
|
2022-06-25 22:55:56 +08:00
|
|
|
|
const REMOVED_OVER_HEIGHT = protyle.contentElement.clientHeight * 8;
|
2024-01-23 13:08:49 +08:00
|
|
|
|
const updateReadonly = typeof options.updateReadonly === "undefined" ? protyle.wysiwyg.element.innerHTML === "" : options.updateReadonly;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (options.action.includes(Constants.CB_GET_APPEND)) {
|
2022-06-24 10:59:23 +08:00
|
|
|
|
// 动态加载移除
|
2022-07-07 23:10:48 +08:00
|
|
|
|
if (!protyle.wysiwyg.element.querySelector(".protyle-wysiwyg--select") && !protyle.scroll.keepLazyLoad && protyle.contentElement.scrollHeight > REMOVED_OVER_HEIGHT) {
|
2022-06-24 10:59:23 +08:00
|
|
|
|
let removeElement = protyle.wysiwyg.element.firstElementChild as HTMLElement;
|
|
|
|
|
const removeElements = [];
|
2022-07-17 11:29:45 +08:00
|
|
|
|
while (protyle.wysiwyg.element.childElementCount > 2 && removeElements && !protyle.wysiwyg.element.lastElementChild.isSameNode(removeElement)) {
|
2022-06-24 10:59:23 +08:00
|
|
|
|
if (protyle.contentElement.scrollHeight - removeElement.offsetTop > REMOVED_OVER_HEIGHT) {
|
|
|
|
|
removeElements.push(removeElement);
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
removeElement = removeElement.nextElementSibling as HTMLElement;
|
|
|
|
|
}
|
|
|
|
|
const lastRemoveTop = removeElement.getBoundingClientRect().top;
|
|
|
|
|
removeElements.forEach(item => {
|
|
|
|
|
item.remove();
|
|
|
|
|
});
|
|
|
|
|
protyle.contentElement.scrollTop = protyle.contentElement.scrollTop + (removeElement.getBoundingClientRect().top - lastRemoveTop);
|
2022-07-12 16:35:46 +08:00
|
|
|
|
protyle.scroll.lastScrollTop = protyle.contentElement.scrollTop;
|
2022-08-03 18:08:28 +08:00
|
|
|
|
hideElements(["toolbar"], protyle);
|
2022-06-24 10:59:23 +08:00
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
protyle.wysiwyg.element.insertAdjacentHTML("beforeend", options.content);
|
|
|
|
|
} else if (options.action.includes(Constants.CB_GET_BEFORE)) {
|
2023-09-18 11:27:55 +08:00
|
|
|
|
const firstElement = protyle.wysiwyg.element.firstElementChild as HTMLElement;
|
|
|
|
|
const lastTop = firstElement.getBoundingClientRect().top;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
protyle.wysiwyg.element.insertAdjacentHTML("afterbegin", options.content);
|
2023-09-18 11:27:55 +08:00
|
|
|
|
protyle.contentElement.scrollTop = protyle.contentElement.scrollTop + (firstElement.getBoundingClientRect().top - lastTop);
|
2022-07-12 16:35:46 +08:00
|
|
|
|
protyle.scroll.lastScrollTop = protyle.contentElement.scrollTop;
|
2022-06-24 10:59:23 +08:00
|
|
|
|
// 动态加载移除
|
2022-07-07 23:10:48 +08:00
|
|
|
|
if (!protyle.wysiwyg.element.querySelector(".protyle-wysiwyg--select") && !protyle.scroll.keepLazyLoad) {
|
2023-09-19 09:46:34 +08:00
|
|
|
|
const removeElements: Element[] = [];
|
2023-09-18 11:27:55 +08:00
|
|
|
|
let childCount = protyle.wysiwyg.element.childElementCount;
|
|
|
|
|
let scrollHeight = protyle.contentElement.scrollHeight;
|
|
|
|
|
let lastElement = protyle.wysiwyg.element.lastElementChild;
|
|
|
|
|
while (childCount > 2 && scrollHeight > REMOVED_OVER_HEIGHT && lastElement.getBoundingClientRect().top > window.innerHeight) {
|
|
|
|
|
removeElements.push(lastElement);
|
|
|
|
|
lastElement = lastElement.previousElementSibling;
|
|
|
|
|
childCount--;
|
|
|
|
|
scrollHeight -= lastElement.clientHeight + 8; // 大部分元素的 margin
|
2022-07-07 23:10:48 +08:00
|
|
|
|
}
|
2023-09-18 11:27:55 +08:00
|
|
|
|
removeElements.forEach((item) => {
|
|
|
|
|
item.remove();
|
|
|
|
|
});
|
2022-08-03 18:08:28 +08:00
|
|
|
|
hideElements(["toolbar"], protyle);
|
2022-06-24 10:59:23 +08:00
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} else {
|
|
|
|
|
protyle.wysiwyg.element.innerHTML = options.content;
|
|
|
|
|
}
|
2022-12-24 12:13:50 +08:00
|
|
|
|
if (options.action.includes(Constants.CB_GET_BACKLINK)) {
|
2023-02-07 12:40:45 +08:00
|
|
|
|
foldPassiveType(options.expand, protyle.wysiwyg.element);
|
2022-12-24 12:13:50 +08:00
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
processRender(protyle.wysiwyg.element);
|
|
|
|
|
highlightRender(protyle.wysiwyg.element);
|
2023-09-09 23:21:46 +08:00
|
|
|
|
avRender(protyle.wysiwyg.element, protyle);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
blockRender(protyle, protyle.wysiwyg.element);
|
2022-08-31 01:14:45 +08:00
|
|
|
|
if (options.action.includes(Constants.CB_GET_HISTORY)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (protyle.options.render.scroll) {
|
2022-09-03 12:53:41 +08:00
|
|
|
|
protyle.scroll.update(protyle);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2023-12-12 01:09:24 +08:00
|
|
|
|
if (options.action.includes(Constants.CB_GET_FOCUSFIRST)) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
// settimeout 时间需短一点,否则定位后快速滚动无效
|
2022-08-08 18:16:18 +08:00
|
|
|
|
const headerHeight = protyle.wysiwyg.element.offsetTop - 16;
|
|
|
|
|
preventScroll(protyle, headerHeight, 256);
|
|
|
|
|
protyle.contentElement.scrollTop = headerHeight;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2022-10-22 12:06:59 +08:00
|
|
|
|
if (options.isSyncing) {
|
2022-10-22 12:14:26 +08:00
|
|
|
|
disabledForeverProtyle(protyle);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} else {
|
2023-07-30 12:40:32 +08:00
|
|
|
|
if (protyle.breadcrumb) {
|
|
|
|
|
protyle.breadcrumb.element.nextElementSibling.textContent = "";
|
|
|
|
|
}
|
2023-04-26 11:23:08 +08:00
|
|
|
|
protyle.element.removeAttribute("disabled-forever");
|
2024-01-23 13:08:49 +08:00
|
|
|
|
setReadonlyByConfig(protyle, updateReadonly);
|
2024-05-08 10:39:26 +08:00
|
|
|
|
if (options.action.includes(Constants.CB_GET_OPENNEW)) {
|
|
|
|
|
updateReadonlyMethod(protyle.breadcrumb.element.parentElement.querySelector('.block__icon[data-type="readonly"]'), protyle);
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2023-12-12 00:53:15 +08:00
|
|
|
|
|
|
|
|
|
focusElementById(protyle, options.action, options.scrollAttr);
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (options.action.includes(Constants.CB_GET_SETID)) {
|
|
|
|
|
// 点击大纲后,如果需要动态加载,在定位后,需要重置 block.id https://github.com/siyuan-note/siyuan/issues/4487
|
|
|
|
|
protyle.block.id = protyle.block.rootID;
|
|
|
|
|
protyle.wysiwyg.element.setAttribute("data-doc-type", "NodeDocument");
|
|
|
|
|
}
|
|
|
|
|
if (protyle.options.defId) {
|
|
|
|
|
protyle.wysiwyg.element.querySelectorAll(`[data-id="${protyle.options.defId}"]`).forEach(item => {
|
|
|
|
|
item.classList.add("def--mark");
|
|
|
|
|
});
|
|
|
|
|
protyle.options.defId = undefined;
|
|
|
|
|
}
|
|
|
|
|
if (options.action.includes(Constants.CB_GET_APPEND) || options.action.includes(Constants.CB_GET_BEFORE)) {
|
2023-08-24 12:19:55 +08:00
|
|
|
|
protyle.app.plugins.forEach(item => {
|
|
|
|
|
item.eventBus.emit("loaded-protyle-dynamic", {
|
|
|
|
|
protyle,
|
2024-05-07 22:28:29 +08:00
|
|
|
|
position: options.action.includes(Constants.CB_GET_APPEND) ? "afterend" : "beforebegin"
|
2023-08-24 12:19:55 +08:00
|
|
|
|
});
|
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-12-12 00:53:15 +08:00
|
|
|
|
|
2022-08-31 01:14:45 +08:00
|
|
|
|
if (protyle.options.render.breadcrumb) {
|
2023-07-04 20:39:43 +08:00
|
|
|
|
protyle.breadcrumb.toggleExit(!options.action.includes(Constants.CB_GET_ALL));
|
2022-08-31 01:14:45 +08:00
|
|
|
|
protyle.breadcrumb.render(protyle);
|
|
|
|
|
}
|
2023-07-18 23:46:08 +08:00
|
|
|
|
if (options.afterCB) {
|
|
|
|
|
options.afterCB();
|
|
|
|
|
}
|
2023-12-12 01:09:24 +08:00
|
|
|
|
// 需等待 afterCB 执行后 resize 计算出高度后再进行计算
|
|
|
|
|
// 屏幕太高的页签 https://github.com/siyuan-note/siyuan/issues/5018
|
|
|
|
|
if (options.scrollAttr && !protyle.scroll.element.classList.contains("fn__none") &&
|
|
|
|
|
!protyle.element.classList.contains("block__edit") && // 不能为浮窗,否则悬浮为根文档无法打开整个文档 https://github.com/siyuan-note/siyuan/issues/9082
|
|
|
|
|
protyle.wysiwyg.element.lastElementChild.getAttribute("data-eof") !== "2" &&
|
|
|
|
|
protyle.contentElement.scrollHeight > 0 && // 没有激活的页签 https://github.com/siyuan-note/siyuan/issues/5255
|
|
|
|
|
!options.action.includes(Constants.CB_GET_FOCUSFIRST) && // 防止 eof 为true https://github.com/siyuan-note/siyuan/issues/5291
|
|
|
|
|
protyle.contentElement.scrollHeight <= protyle.contentElement.clientHeight) {
|
|
|
|
|
fetchPost("/api/filetree/getDoc", {
|
|
|
|
|
id: protyle.wysiwyg.element.lastElementChild.getAttribute("data-node-id"),
|
|
|
|
|
mode: 2,
|
|
|
|
|
size: window.siyuan.config.editor.dynamicLoadBlocks,
|
|
|
|
|
}, getResponse => {
|
|
|
|
|
onGet({data: getResponse, protyle, action: [Constants.CB_GET_APPEND, Constants.CB_GET_UNCHANGEID]});
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-12-20 16:45:06 +08:00
|
|
|
|
if (options.scrollAttr && !protyle.scroll.element.classList.contains("fn__none") && !protyle.element.classList.contains("fn__none")) {
|
2023-12-12 01:09:24 +08:00
|
|
|
|
// 使用动态滚动条定位到最后一个块,重启后无法触发滚动事件,需要再次更新 index
|
2024-03-27 20:11:04 +08:00
|
|
|
|
protyle.scroll.updateIndex(protyle, options.scrollAttr.startId, (index) => {
|
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/8224
|
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/10716
|
|
|
|
|
if (index > 1 && protyle.block.blockCount > 1 && protyle.contentElement.scrollHeight <= protyle.contentElement.clientHeight) {
|
|
|
|
|
showMessage(window.siyuan.languages.scrollGetMore);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2023-12-12 01:09:24 +08:00
|
|
|
|
}
|
2023-06-01 20:50:49 +08:00
|
|
|
|
protyle.app.plugins.forEach(item => {
|
2023-10-21 21:00:10 +08:00
|
|
|
|
item.eventBus.emit("loaded-protyle", protyle); // 准备废弃
|
|
|
|
|
item.eventBus.emit("loaded-protyle-static", {protyle});
|
2023-06-01 15:03:03 +08:00
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
2022-10-22 12:06:59 +08:00
|
|
|
|
export const disabledForeverProtyle = (protyle: IProtyle) => {
|
|
|
|
|
disabledProtyle(protyle);
|
2023-04-10 11:25:25 +08:00
|
|
|
|
if (protyle.breadcrumb && !isMobile()) {
|
2022-10-22 16:56:30 +08:00
|
|
|
|
protyle.breadcrumb.element.nextElementSibling.textContent = window.siyuan.languages["_kernel"][81];
|
2023-04-10 11:25:25 +08:00
|
|
|
|
} else {
|
|
|
|
|
showMessage(window.siyuan.languages["_kernel"][81]);
|
2022-10-22 12:12:38 +08:00
|
|
|
|
}
|
2022-10-22 12:06:59 +08:00
|
|
|
|
protyle.element.setAttribute("disabled-forever", "true");
|
2022-10-22 12:14:26 +08:00
|
|
|
|
};
|
2022-10-22 12:06:59 +08:00
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
/** 禁用编辑器 */
|
|
|
|
|
export const disabledProtyle = (protyle: IProtyle) => {
|
2022-10-17 11:43:33 +08:00
|
|
|
|
window.siyuan.menus.menu.remove();
|
2022-05-26 15:18:53 +08:00
|
|
|
|
hideElements(["gutter", "toolbar", "select", "hint", "util"], protyle);
|
|
|
|
|
protyle.disabled = true;
|
2022-10-11 23:38:14 +08:00
|
|
|
|
if (protyle.title) {
|
2022-10-15 14:25:04 +08:00
|
|
|
|
const titleElement = protyle.title.element.querySelector(".protyle-title__input") as HTMLElement;
|
2022-10-13 16:03:02 +08:00
|
|
|
|
titleElement.setAttribute("contenteditable", "false");
|
|
|
|
|
titleElement.style.userSelect = "text";
|
2022-10-11 23:38:14 +08:00
|
|
|
|
}
|
2023-09-09 14:54:05 +08:00
|
|
|
|
/// #if MOBILE
|
|
|
|
|
document.getElementById("toolbarName").setAttribute("readonly", "readonly");
|
|
|
|
|
/// #endif
|
2022-10-17 11:43:33 +08:00
|
|
|
|
if (protyle.background) {
|
|
|
|
|
protyle.background.element.classList.remove("protyle-background--enable");
|
2022-10-28 12:27:44 +08:00
|
|
|
|
protyle.background.element.classList.remove("protyle-background--mobileshow");
|
2022-10-17 11:43:33 +08:00
|
|
|
|
}
|
2022-10-28 12:27:44 +08:00
|
|
|
|
protyle.wysiwyg.element.querySelectorAll(".protyle-icons--show").forEach(item => {
|
|
|
|
|
item.classList.remove("protyle-icons--show");
|
2022-10-30 00:16:48 +08:00
|
|
|
|
});
|
2024-02-25 22:27:59 +08:00
|
|
|
|
protyle.wysiwyg.element.querySelectorAll(".render-node .protyle-action__edit").forEach(item => {
|
|
|
|
|
item.classList.add("fn__none");
|
|
|
|
|
if (item.classList.contains("protyle-icon--first")) {
|
2024-02-25 22:28:35 +08:00
|
|
|
|
item.nextElementSibling?.classList.add("protyle-icon--first");
|
2024-02-25 22:27:59 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2022-10-13 16:03:02 +08:00
|
|
|
|
protyle.wysiwyg.element.style.userSelect = "text";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
protyle.wysiwyg.element.setAttribute("contenteditable", "false");
|
2024-03-08 16:45:00 +08:00
|
|
|
|
// 用于区分移动端样式
|
|
|
|
|
protyle.wysiwyg.element.setAttribute("data-readonly", "true");
|
2022-12-30 17:31:28 +08:00
|
|
|
|
protyle.wysiwyg.element.querySelectorAll('[contenteditable="true"][spellcheck]').forEach(item => {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
item.setAttribute("contenteditable", "false");
|
|
|
|
|
});
|
2023-12-07 22:09:51 +08:00
|
|
|
|
protyle.wysiwyg.element.querySelectorAll('.protyle-action[draggable="true"]').forEach(item => {
|
|
|
|
|
item.setAttribute("draggable", "false");
|
|
|
|
|
});
|
2023-12-08 21:36:32 +08:00
|
|
|
|
protyle.wysiwyg.element.querySelectorAll(".av").forEach((item: HTMLElement) => {
|
2023-12-08 21:21:57 +08:00
|
|
|
|
const headerElement = item.querySelector(".av__row--header") as HTMLElement;
|
|
|
|
|
if (headerElement) {
|
|
|
|
|
headerElement.style.transform = "";
|
|
|
|
|
(item.querySelector(".av__row--footer") as HTMLElement).style.transform = "";
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-09-12 17:34:46 +08:00
|
|
|
|
if (protyle.breadcrumb) {
|
|
|
|
|
protyle.breadcrumb.element.parentElement.querySelector('[data-type="readonly"] use').setAttribute("xlink:href", "#iconLock");
|
2023-11-25 17:08:22 +08:00
|
|
|
|
protyle.breadcrumb.element.parentElement.querySelector('[data-type="readonly"]').setAttribute("aria-label", window.siyuan.config.editor.readOnly ? window.siyuan.languages.tempUnlock : window.siyuan.languages.unlockEdit);
|
2024-02-03 12:32:56 +08:00
|
|
|
|
const undoElement = protyle.breadcrumb.element.parentElement.querySelector('[data-type="undo"]');
|
2024-02-02 11:52:53 +08:00
|
|
|
|
if (undoElement && !undoElement.classList.contains("fn__none")) {
|
2024-02-03 12:32:56 +08:00
|
|
|
|
undoElement.classList.add("fn__none");
|
|
|
|
|
protyle.breadcrumb.element.parentElement.querySelector('[data-type="redo"]').classList.add("fn__none");
|
|
|
|
|
protyle.breadcrumb.element.parentElement.querySelector('[data-type="indent"]').classList.add("fn__none");
|
|
|
|
|
protyle.breadcrumb.element.parentElement.querySelector('[data-type="outdent"]').classList.add("fn__none");
|
2024-02-02 11:52:53 +08:00
|
|
|
|
}
|
2023-09-12 17:34:46 +08:00
|
|
|
|
}
|
2023-09-10 15:31:09 +08:00
|
|
|
|
hideTooltip();
|
2022-05-26 15:18:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** 解除编辑器禁用 */
|
|
|
|
|
export const enableProtyle = (protyle: IProtyle) => {
|
2022-10-22 12:06:59 +08:00
|
|
|
|
if (protyle.element.getAttribute("disabled-forever") === "true") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
protyle.disabled = false;
|
2023-07-19 22:45:19 +08:00
|
|
|
|
if (isMobile()) {
|
|
|
|
|
// Android 端空块输入法弹出会收起 https://ld246.com/article/1689713888289
|
2022-10-17 11:43:33 +08:00
|
|
|
|
// iPhone,iPad 端 protyle.wysiwyg.element contenteditable 为 true 时,输入会在块中间插入 span 导致保存失败 https://ld246.com/article/1643473862873/comment/1643813765839#comments
|
2023-09-09 14:54:05 +08:00
|
|
|
|
document.getElementById("toolbarName").removeAttribute("readonly");
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} else {
|
|
|
|
|
protyle.wysiwyg.element.setAttribute("contenteditable", "true");
|
2022-10-13 16:03:02 +08:00
|
|
|
|
protyle.wysiwyg.element.style.userSelect = "";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2024-03-08 16:45:00 +08:00
|
|
|
|
// 用于区分移动端样式
|
|
|
|
|
protyle.wysiwyg.element.setAttribute("data-readonly", "false");
|
2022-10-17 11:43:33 +08:00
|
|
|
|
if (protyle.title) {
|
|
|
|
|
const titleElement = protyle.title.element.querySelector(".protyle-title__input") as HTMLElement;
|
|
|
|
|
titleElement.setAttribute("contenteditable", "true");
|
|
|
|
|
titleElement.style.userSelect = "";
|
|
|
|
|
}
|
|
|
|
|
if (protyle.background) {
|
|
|
|
|
protyle.background.element.classList.add("protyle-background--enable");
|
|
|
|
|
}
|
2024-02-25 22:27:59 +08:00
|
|
|
|
|
|
|
|
|
protyle.wysiwyg.element.querySelectorAll(".render-node .protyle-action__edit").forEach(item => {
|
|
|
|
|
item.classList.remove("fn__none");
|
|
|
|
|
if (item.classList.contains("protyle-icon--first")) {
|
2024-02-25 22:28:35 +08:00
|
|
|
|
item.nextElementSibling?.classList.remove("protyle-icon--first");
|
2024-02-25 22:27:59 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2022-12-30 17:31:28 +08:00
|
|
|
|
protyle.wysiwyg.element.querySelectorAll('[contenteditable="false"][spellcheck]').forEach(item => {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (!hasClosestByClassName(item, "protyle-wysiwyg__embed")) {
|
|
|
|
|
item.setAttribute("contenteditable", "true");
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-12-07 22:09:51 +08:00
|
|
|
|
protyle.wysiwyg.element.querySelectorAll('.protyle-action[draggable="false"]').forEach(item => {
|
|
|
|
|
item.setAttribute("draggable", "true");
|
|
|
|
|
});
|
2023-12-08 21:21:57 +08:00
|
|
|
|
const contentRect = protyle.contentElement.getBoundingClientRect();
|
2023-12-08 21:36:32 +08:00
|
|
|
|
protyle.wysiwyg.element.querySelectorAll(".av").forEach((item: HTMLElement) => {
|
2023-12-08 21:56:48 +08:00
|
|
|
|
if (item.querySelector(".av__title")) {
|
|
|
|
|
stickyRow(item, contentRect, "all");
|
|
|
|
|
}
|
2023-12-08 21:21:57 +08:00
|
|
|
|
});
|
2023-09-12 17:34:46 +08:00
|
|
|
|
if (protyle.breadcrumb) {
|
|
|
|
|
protyle.breadcrumb.element.parentElement.querySelector('[data-type="readonly"] use').setAttribute("xlink:href", "#iconUnlock");
|
2023-11-25 17:08:22 +08:00
|
|
|
|
protyle.breadcrumb.element.parentElement.querySelector('[data-type="readonly"]').setAttribute("aria-label", window.siyuan.config.editor.readOnly ? window.siyuan.languages.cancelTempUnlock : window.siyuan.languages.lockEdit);
|
2024-02-03 12:32:56 +08:00
|
|
|
|
const undoElement = protyle.breadcrumb.element.parentElement.querySelector('[data-type="undo"]');
|
2024-02-02 11:52:53 +08:00
|
|
|
|
if (undoElement && undoElement.classList.contains("fn__none")) {
|
2024-02-03 12:32:56 +08:00
|
|
|
|
undoElement.classList.remove("fn__none");
|
|
|
|
|
protyle.breadcrumb.element.parentElement.querySelector('[data-type="redo"]').classList.remove("fn__none");
|
|
|
|
|
protyle.breadcrumb.element.parentElement.querySelector('[data-type="indent"]').classList.remove("fn__none");
|
|
|
|
|
protyle.breadcrumb.element.parentElement.querySelector('[data-type="outdent"]').classList.remove("fn__none");
|
2024-02-02 11:52:53 +08:00
|
|
|
|
}
|
2023-09-12 17:34:46 +08:00
|
|
|
|
}
|
2023-09-10 15:31:09 +08:00
|
|
|
|
hideTooltip();
|
2022-05-26 15:18:53 +08:00
|
|
|
|
};
|
2023-05-13 22:16:22 +08:00
|
|
|
|
|
2023-12-12 00:53:15 +08:00
|
|
|
|
const focusElementById = (protyle: IProtyle, action: string[], scrollAttr?: IScrollAttr) => {
|
2023-05-13 22:16:22 +08:00
|
|
|
|
let focusElement: Element;
|
2023-12-12 00:53:15 +08:00
|
|
|
|
if (scrollAttr && scrollAttr.focusId) {
|
2023-12-12 20:53:42 +08:00
|
|
|
|
focusElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${scrollAttr.focusId}"]`);
|
2023-12-12 00:53:15 +08:00
|
|
|
|
} else {
|
|
|
|
|
Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${protyle.block.id}"]`)).find((item: HTMLElement) => {
|
|
|
|
|
if (!hasClosestByAttribute(item, "data-type", "block-render", true)) {
|
|
|
|
|
focusElement = item;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-05-13 22:16:22 +08:00
|
|
|
|
if (protyle.block.mode === 4) {
|
|
|
|
|
preventScroll(protyle);
|
|
|
|
|
focusElement = protyle.wysiwyg.element.lastElementChild;
|
2023-12-12 00:53:15 +08:00
|
|
|
|
} else if (!focusElement || action.includes(Constants.CB_GET_FOCUSFIRST)) {
|
2023-12-12 20:53:42 +08:00
|
|
|
|
focusElement = protyle.wysiwyg.element.firstElementChild;
|
2023-12-12 00:53:15 +08:00
|
|
|
|
}
|
|
|
|
|
if (action.includes(Constants.CB_GET_HL)) {
|
|
|
|
|
preventScroll(protyle); // 搜索页签滚动会导致再次请求
|
|
|
|
|
bgFade(focusElement);
|
2023-05-13 22:16:22 +08:00
|
|
|
|
}
|
2023-12-12 00:53:15 +08:00
|
|
|
|
if (action.includes(Constants.CB_GET_FOCUS) || action.includes(Constants.CB_GET_FOCUSFIRST)) {
|
2023-12-12 20:40:39 +08:00
|
|
|
|
let range: Range;
|
|
|
|
|
if (scrollAttr && scrollAttr.focusId) {
|
|
|
|
|
range = focusByOffset(focusElement, scrollAttr.focusStart, scrollAttr.focusEnd) as Range;
|
|
|
|
|
} else {
|
|
|
|
|
focusBlock(focusElement);
|
|
|
|
|
}
|
2023-05-13 22:16:22 +08:00
|
|
|
|
/// #if !MOBILE
|
2023-05-17 12:37:56 +08:00
|
|
|
|
if (!action.includes(Constants.CB_GET_UNUNDO)) {
|
2023-12-12 00:53:15 +08:00
|
|
|
|
pushBack(protyle, range, focusElement);
|
2023-05-13 22:16:22 +08:00
|
|
|
|
}
|
|
|
|
|
/// #endif
|
2023-12-12 00:53:15 +08:00
|
|
|
|
}
|
2024-01-01 23:08:28 +08:00
|
|
|
|
const hasScrollTop = scrollAttr && typeof scrollAttr.scrollTop === "number";
|
2023-12-31 11:13:05 +08:00
|
|
|
|
if (hasScrollTop) {
|
2023-12-12 00:53:15 +08:00
|
|
|
|
protyle.contentElement.scrollTop = scrollAttr.scrollTop;
|
2023-12-20 10:10:49 +08:00
|
|
|
|
}
|
2024-01-08 12:39:48 +08:00
|
|
|
|
// 下一个请求过来前需断开,否则 observerLoad 重新赋值后无法 disconnect https://ld246.com/article/1704612002446
|
|
|
|
|
protyle.observerLoad?.disconnect();
|
2023-12-31 16:51:31 +08:00
|
|
|
|
if (action.includes(Constants.CB_GET_FOCUS) || action.includes(Constants.CB_GET_SCROLL) || action.includes(Constants.CB_GET_HL) || action.includes(Constants.CB_GET_FOCUSFIRST)) {
|
2023-12-20 10:10:49 +08:00
|
|
|
|
const contentRect = protyle.contentElement.getBoundingClientRect();
|
|
|
|
|
const focusRect = focusElement.getBoundingClientRect();
|
2023-12-31 12:27:18 +08:00
|
|
|
|
if (!hasScrollTop && (contentRect.top > focusRect.top || contentRect.bottom < focusRect.bottom)) {
|
|
|
|
|
scrollCenter(protyle, focusElement, true);
|
2023-12-20 10:10:49 +08:00
|
|
|
|
}
|
2023-12-15 19:53:18 +08:00
|
|
|
|
} else {
|
|
|
|
|
return;
|
2023-12-12 00:53:15 +08:00
|
|
|
|
}
|
|
|
|
|
// 加强定位
|
2023-12-12 20:53:42 +08:00
|
|
|
|
protyle.observerLoad = new ResizeObserver(() => {
|
2023-12-31 11:13:05 +08:00
|
|
|
|
if (hasScrollTop) {
|
2023-12-12 00:53:15 +08:00
|
|
|
|
protyle.contentElement.scrollTop = scrollAttr.scrollTop;
|
2023-05-13 22:16:22 +08:00
|
|
|
|
}
|
2023-12-20 10:10:49 +08:00
|
|
|
|
if (action.includes(Constants.CB_GET_FOCUS) || action.includes(Constants.CB_GET_HL) || action.includes(Constants.CB_GET_FOCUSFIRST)) {
|
|
|
|
|
const contentRect = protyle.contentElement.getBoundingClientRect();
|
|
|
|
|
const focusRect = focusElement.getBoundingClientRect();
|
2023-12-31 12:27:18 +08:00
|
|
|
|
if (!hasScrollTop && (contentRect.top > focusRect.top || contentRect.bottom < focusRect.bottom)) {
|
|
|
|
|
scrollCenter(protyle, focusElement, true);
|
2023-12-20 10:10:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-12 00:53:15 +08:00
|
|
|
|
});
|
|
|
|
|
protyle.observerLoad.observe(protyle.wysiwyg.element);
|
|
|
|
|
protyle.observer.unobserve(protyle.wysiwyg.element);
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
protyle.observerLoad.disconnect();
|
|
|
|
|
protyle.observer.observe(protyle.wysiwyg.element);
|
2023-12-15 19:53:18 +08:00
|
|
|
|
}, 1000 * 3);
|
2023-12-12 00:53:15 +08:00
|
|
|
|
|
2023-12-31 17:02:00 +08:00
|
|
|
|
if (focusElement.isSameNode(protyle.wysiwyg.element.firstElementChild) && !hasScrollTop) {
|
2023-12-12 00:53:15 +08:00
|
|
|
|
protyle.observerLoad.disconnect();
|
2023-05-13 22:16:22 +08:00
|
|
|
|
}
|
2023-05-16 00:05:18 +08:00
|
|
|
|
};
|
2023-11-28 21:17:19 +08:00
|
|
|
|
|
2024-01-23 13:08:49 +08:00
|
|
|
|
export const setReadonlyByConfig = (protyle: IProtyle, updateReadonly: boolean) => {
|
2023-11-28 21:17:19 +08:00
|
|
|
|
let readOnly = window.siyuan.config.readonly ? "true" : "false";
|
2024-01-23 13:08:49 +08:00
|
|
|
|
if (!updateReadonly) {
|
2024-01-11 22:38:38 +08:00
|
|
|
|
readOnly = protyle.disabled ? "true" : "false";
|
|
|
|
|
} else if (readOnly === "false") {
|
2023-11-28 21:17:19 +08:00
|
|
|
|
readOnly = window.siyuan.config.editor.readOnly ? "true" : "false";
|
|
|
|
|
if (readOnly === "false") {
|
|
|
|
|
readOnly = protyle.wysiwyg.element.getAttribute(Constants.CUSTOM_SY_READONLY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (readOnly === "true") {
|
|
|
|
|
disabledProtyle(protyle);
|
|
|
|
|
} else {
|
|
|
|
|
enableProtyle(protyle);
|
|
|
|
|
}
|
2023-11-29 10:38:54 +08:00
|
|
|
|
};
|