2022-05-26 15:18:53 +08:00
|
|
|
import {setEditMode} from "../util/setEditMode";
|
|
|
|
|
import {lineNumberRender} from "../markdown/highlightRender";
|
|
|
|
|
import {scrollEvent} from "../scroll/event";
|
|
|
|
|
import {isMobile} from "../../util/functions";
|
2022-06-30 22:44:50 +08:00
|
|
|
import {Constants} from "../../constants";
|
2022-10-28 12:27:44 +08:00
|
|
|
import {hasClosestByAttribute, hasClosestByClassName} from "../util/hasClosest";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
|
export const initUI = (protyle: IProtyle) => {
|
|
|
|
|
protyle.contentElement = document.createElement("div");
|
|
|
|
|
protyle.contentElement.className = "protyle-content";
|
2022-08-05 13:22:22 +08:00
|
|
|
if (window.siyuan.config.editor.fullWidth) {
|
|
|
|
|
protyle.contentElement.setAttribute("data-fullwidth", "true");
|
|
|
|
|
} else {
|
|
|
|
|
protyle.contentElement.removeAttribute("data-fullwidth");
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
if (protyle.options.render.background) {
|
|
|
|
|
protyle.contentElement.appendChild(protyle.background.element);
|
|
|
|
|
}
|
|
|
|
|
if (protyle.options.render.title) {
|
|
|
|
|
protyle.contentElement.appendChild(protyle.title.element);
|
|
|
|
|
}
|
|
|
|
|
protyle.contentElement.appendChild(protyle.wysiwyg.element);
|
2022-08-31 01:14:45 +08:00
|
|
|
if (!protyle.options.action.includes(Constants.CB_GET_HISTORY)) {
|
|
|
|
|
scrollEvent(protyle, protyle.contentElement);
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
protyle.element.append(protyle.contentElement);
|
|
|
|
|
protyle.element.appendChild(protyle.preview.element);
|
|
|
|
|
if (protyle.upload) {
|
|
|
|
|
protyle.element.appendChild(protyle.upload.element);
|
|
|
|
|
}
|
2022-11-14 17:39:34 +08:00
|
|
|
if (protyle.options.render.scroll) {
|
2022-11-14 17:29:41 +08:00
|
|
|
protyle.element.appendChild(protyle.scroll.element.parentElement);
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
|
if (protyle.gutter) {
|
|
|
|
|
protyle.element.appendChild(protyle.gutter.element);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protyle.element.appendChild(protyle.hint.element);
|
|
|
|
|
|
|
|
|
|
protyle.selectElement = document.createElement("div");
|
|
|
|
|
protyle.selectElement.className = "protyle-select fn__none";
|
|
|
|
|
protyle.element.appendChild(protyle.selectElement);
|
|
|
|
|
|
|
|
|
|
protyle.element.appendChild(protyle.toolbar.element);
|
|
|
|
|
protyle.element.appendChild(protyle.toolbar.subElement);
|
|
|
|
|
|
|
|
|
|
addLoading(protyle);
|
|
|
|
|
|
|
|
|
|
setEditMode(protyle, protyle.options.mode);
|
|
|
|
|
document.execCommand("DefaultParagraphSeparator", false, "p");
|
2022-10-28 12:27:44 +08:00
|
|
|
|
|
|
|
|
// 触摸屏背景和嵌入块按钮显示
|
|
|
|
|
protyle.contentElement.addEventListener("touchstart", (event) => {
|
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/6328
|
|
|
|
|
if (protyle.disabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const target = event.target as HTMLElement;
|
|
|
|
|
if (hasClosestByClassName(target, "protyle-icons") ||
|
|
|
|
|
hasClosestByClassName(target, "item") ||
|
|
|
|
|
target.classList.contains("protyle-background__icon")) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (hasClosestByClassName(target, "protyle-background")) {
|
|
|
|
|
protyle.background.element.classList.toggle("protyle-background--mobileshow");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-30 00:16:48 +08:00
|
|
|
const embedBlockElement = hasClosestByAttribute(target, "data-type", "NodeBlockQueryEmbed");
|
2022-10-28 12:27:44 +08:00
|
|
|
if (embedBlockElement) {
|
|
|
|
|
embedBlockElement.firstElementChild.classList.toggle("protyle-icons--show");
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const addLoading = (protyle: IProtyle) => {
|
2023-05-03 11:44:49 +08:00
|
|
|
protyle.element.removeAttribute("data-loading");
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
if (protyle.element.getAttribute("data-loading") !== "finished") {
|
|
|
|
|
protyle.element.insertAdjacentHTML("beforeend", '<div style="background-color: var(--b3-theme-background)" class="fn__loading wysiwygLoading"><img width="48px" src="/stage/loading-pure.svg"></div>');
|
|
|
|
|
}
|
2023-05-10 20:18:40 +08:00
|
|
|
}, Constants.TIMEOUT_LOAD);
|
2022-08-29 10:32:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const removeLoading = (protyle: IProtyle) => {
|
2023-05-03 11:44:49 +08:00
|
|
|
protyle.element.setAttribute("data-loading", "finished");
|
2022-08-29 10:32:54 +08:00
|
|
|
const loadingElement = protyle.element.querySelector(".wysiwygLoading");
|
|
|
|
|
if (loadingElement) {
|
|
|
|
|
loadingElement.remove();
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const setPadding = (protyle: IProtyle) => {
|
2022-08-31 01:14:45 +08:00
|
|
|
if (protyle.options.action.includes(Constants.CB_GET_HISTORY)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-06-30 22:44:50 +08:00
|
|
|
let min16 = 16;
|
|
|
|
|
let min24 = 24;
|
|
|
|
|
if (!isMobile()) {
|
2022-11-12 23:55:10 +08:00
|
|
|
let padding = (protyle.element.clientWidth - Constants.SIZE_EDITOR_WIDTH) / 2;
|
2022-06-30 22:44:50 +08:00
|
|
|
if (!window.siyuan.config.editor.fullWidth && padding > 96) {
|
2022-11-12 23:55:10 +08:00
|
|
|
if (padding > Constants.SIZE_EDITOR_WIDTH) {
|
|
|
|
|
// 超宽屏调整 https://ld246.com/article/1668266637363
|
2022-11-28 22:33:47 +08:00
|
|
|
padding = protyle.element.clientWidth * .382 / 1.382;
|
2022-11-12 23:55:10 +08:00
|
|
|
}
|
2022-07-01 10:52:17 +08:00
|
|
|
min16 = padding;
|
|
|
|
|
min24 = padding;
|
2022-06-30 22:44:50 +08:00
|
|
|
} else if (protyle.element.clientWidth > Constants.SIZE_EDITOR_WIDTH) {
|
2022-07-01 10:52:17 +08:00
|
|
|
min16 = 96;
|
|
|
|
|
min24 = 96;
|
2022-06-30 22:44:50 +08:00
|
|
|
}
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
if (protyle.options.render.background && protyle.options.render.title) {
|
|
|
|
|
protyle.background.element.lastElementChild.setAttribute("style", `left:${min16}px`);
|
|
|
|
|
protyle.title.element.style.margin = `16px ${min16}px 0 ${min24}px`;
|
|
|
|
|
} else if (protyle.options.render.background && !protyle.options.render.title) {
|
|
|
|
|
protyle.background.element.lastElementChild.setAttribute("style", `left:${min16}px`);
|
|
|
|
|
} else if (!protyle.options.render.background && protyle.options.render.title) {
|
|
|
|
|
protyle.title.element.style.margin = `16px ${min16}px 0 ${min24}px`;
|
|
|
|
|
}
|
|
|
|
|
let bottomHeight = "16px";
|
|
|
|
|
if (protyle.options.typewriterMode) {
|
|
|
|
|
if (isMobile()) {
|
|
|
|
|
bottomHeight = window.innerHeight / 5 + "px";
|
|
|
|
|
} else {
|
|
|
|
|
bottomHeight = protyle.element.clientHeight / 2 + "px";
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-30 10:39:35 +08:00
|
|
|
if (protyle.options.backlinkData) {
|
|
|
|
|
protyle.wysiwyg.element.style.padding = `4px ${min16}px 4px ${min24}px`;
|
|
|
|
|
} else {
|
|
|
|
|
protyle.wysiwyg.element.style.padding = `16px ${min16}px ${bottomHeight} ${min24}px`;
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
if (window.siyuan.config.editor.codeSyntaxHighlightLineNum) {
|
2022-08-14 10:19:16 +08:00
|
|
|
setTimeout(() => { // https://github.com/siyuan-note/siyuan/issues/5612
|
|
|
|
|
protyle.wysiwyg.element.querySelectorAll('.code-block [contenteditable="true"]').forEach((block: HTMLElement) => {
|
|
|
|
|
lineNumberRender(block);
|
|
|
|
|
});
|
|
|
|
|
}, 300);
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
2022-07-03 17:18:48 +08:00
|
|
|
if (window.siyuan.config.editor.displayBookmarkIcon) {
|
2022-07-06 00:35:39 +08:00
|
|
|
const editorAttrElement = document.getElementById("editorAttr");
|
2022-07-03 17:18:48 +08:00
|
|
|
if (editorAttrElement) {
|
|
|
|
|
editorAttrElement.innerHTML = `.protyle-wysiwyg--attr .b3-tooltips:after { max-width: ${protyle.wysiwyg.element.clientWidth - min16 - min24}px; }`;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
};
|