2022-05-26 15:18:53 +08:00
|
|
|
|
import {paste} from "../util/paste";
|
|
|
|
|
|
import {
|
|
|
|
|
|
hasClosestBlock,
|
|
|
|
|
|
hasClosestByAttribute,
|
|
|
|
|
|
hasClosestByClassName,
|
|
|
|
|
|
hasClosestByMatchTag,
|
|
|
|
|
|
hasTopClosestByClassName,
|
|
|
|
|
|
} from "../util/hasClosest";
|
|
|
|
|
|
import {
|
|
|
|
|
|
focusBlock,
|
|
|
|
|
|
focusByRange,
|
|
|
|
|
|
focusByWbr,
|
|
|
|
|
|
focusSideBlock,
|
|
|
|
|
|
getEditorRange,
|
2023-02-09 12:21:02 +08:00
|
|
|
|
getSelectionOffset,
|
|
|
|
|
|
setLastNodeRange,
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} from "../util/selection";
|
|
|
|
|
|
import {Constants} from "../../constants";
|
|
|
|
|
|
import {getSearch, isMobile} from "../../util/functions";
|
|
|
|
|
|
import {isLocalPath, pathPosix} from "../../util/pathName";
|
|
|
|
|
|
import {genEmptyElement} from "../../block/util";
|
|
|
|
|
|
import {previewImage} from "../preview/image";
|
2023-01-15 22:44:05 +08:00
|
|
|
|
import {contentMenu, imgMenu, linkMenu, refMenu, setFold, tagMenu, zoomOut} from "../../menus/protyle";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
import * as dayjs from "dayjs";
|
|
|
|
|
|
import {dropEvent} from "../util/editorCommonEvent";
|
|
|
|
|
|
import {input} from "./input";
|
|
|
|
|
|
import {
|
|
|
|
|
|
getContenteditableElement,
|
2023-02-09 12:21:02 +08:00
|
|
|
|
getLastBlock,
|
|
|
|
|
|
getNextBlock,
|
2022-05-26 15:18:53 +08:00
|
|
|
|
getTopAloneElement,
|
2023-02-09 12:21:02 +08:00
|
|
|
|
hasNextSibling,
|
|
|
|
|
|
hasPreviousSibling,
|
2022-05-26 15:18:53 +08:00
|
|
|
|
isNotEditBlock
|
|
|
|
|
|
} from "./getBlock";
|
|
|
|
|
|
import {transaction, updateTransaction} from "./transaction";
|
|
|
|
|
|
import {hideElements} from "../ui/hideElements";
|
|
|
|
|
|
/// #if !BROWSER
|
|
|
|
|
|
import {shell} from "electron";
|
2022-08-15 17:30:35 +08:00
|
|
|
|
import {getCurrentWindow} from "@electron/remote";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
/// #endif
|
2022-11-24 23:16:56 +08:00
|
|
|
|
import {getEnableHTML, removeEmbed} from "./removeEmbed";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
import {keydown} from "./keydown";
|
|
|
|
|
|
import {openMobileFileById} from "../../mobile/editor";
|
|
|
|
|
|
import {removeBlock} from "./remove";
|
|
|
|
|
|
import {highlightRender} from "../markdown/highlightRender";
|
|
|
|
|
|
import {openAttr} from "../../menus/commonMenuItem";
|
|
|
|
|
|
import {blockRender} from "../markdown/blockRender";
|
2022-06-29 15:36:44 +08:00
|
|
|
|
/// #if !MOBILE
|
2022-06-29 20:25:30 +08:00
|
|
|
|
import {getAllModels} from "../../layout/getAll";
|
|
|
|
|
|
import {pushBack} from "../../util/backForward";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
import {openAsset, openBy, openFileById} from "../../editor/util";
|
2022-06-30 15:56:44 +08:00
|
|
|
|
import {openGlobalSearch} from "../../search/util";
|
2022-06-29 15:36:44 +08:00
|
|
|
|
/// #endif
|
2022-05-26 15:18:53 +08:00
|
|
|
|
import {BlockPanel} from "../../block/Panel";
|
2022-07-27 07:17:45 +08:00
|
|
|
|
import {isCtrl, openByMobile} from "../util/compatibility";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
import {MenuItem} from "../../menus/Menu";
|
|
|
|
|
|
import {fetchPost} from "../../util/fetch";
|
|
|
|
|
|
import {onGet} from "../util/onGet";
|
2022-06-12 22:06:19 +08:00
|
|
|
|
import {setTableAlign} from "../util/table";
|
2022-06-28 10:19:15 +08:00
|
|
|
|
import {countBlockWord, countSelectWord} from "../../layout/status";
|
2022-06-29 10:35:35 +08:00
|
|
|
|
import {showMessage} from "../../dialog/message";
|
2022-09-30 18:05:24 +08:00
|
|
|
|
import {getBacklinkHeadingMore, loadBreadcrumb} from "./renderBacklink";
|
2023-03-07 10:42:20 +08:00
|
|
|
|
import {removeSearchMark} from "../toolbar/util";
|
2023-04-02 12:26:14 +08:00
|
|
|
|
import {activeBlur, hideKeyboardToolbar} from "../../mobile/util/keyboardToolbar";
|
2023-04-12 19:02:35 +08:00
|
|
|
|
import {commonClick} from "./commonClick";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
|
|
|
export class WYSIWYG {
|
|
|
|
|
|
public lastHTMLs: { [key: string]: string } = {};
|
|
|
|
|
|
|
|
|
|
|
|
public element: HTMLDivElement;
|
|
|
|
|
|
public preventKeyup: boolean;
|
|
|
|
|
|
|
|
|
|
|
|
constructor(protyle: IProtyle) {
|
|
|
|
|
|
this.element = document.createElement("div");
|
|
|
|
|
|
this.element.className = "protyle-wysiwyg";
|
|
|
|
|
|
this.element.setAttribute("spellcheck", "false");
|
|
|
|
|
|
if (navigator && navigator.maxTouchPoints > 1 && navigator.platform === "MacIntel") {
|
|
|
|
|
|
// iPhone,iPad 端输入 contenteditable 为 true 时会在块中间插入 span
|
|
|
|
|
|
this.element.setAttribute("contenteditable", "false");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.element.setAttribute("contenteditable", "true");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (window.siyuan.config.editor.displayBookmarkIcon) {
|
|
|
|
|
|
this.element.classList.add("protyle-wysiwyg--attr");
|
|
|
|
|
|
}
|
2022-08-31 22:29:52 +08:00
|
|
|
|
this.bindCommonEvent(protyle);
|
2022-08-31 01:14:45 +08:00
|
|
|
|
if (protyle.options.action.includes(Constants.CB_GET_HISTORY)) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
this.bindEvent(protyle);
|
|
|
|
|
|
keydown(protyle, this.element);
|
|
|
|
|
|
dropEvent(protyle, this.element);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public renderCustom(ial: IObject) {
|
|
|
|
|
|
const ialKeys = Object.keys(ial);
|
|
|
|
|
|
for (let i = 0; i < this.element.attributes.length; i++) {
|
|
|
|
|
|
const oldKey = this.element.attributes[i].nodeName;
|
2022-08-07 00:14:50 +08:00
|
|
|
|
if (!["type", "class", "spellcheck", "contenteditable", "data-doc-type", "style", "scroll"].includes(oldKey) &&
|
2022-05-26 15:18:53 +08:00
|
|
|
|
!ialKeys.includes(oldKey)) {
|
|
|
|
|
|
this.element.removeAttribute(oldKey);
|
|
|
|
|
|
i--;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
ialKeys.forEach((key: string) => {
|
2022-07-28 21:36:18 +08:00
|
|
|
|
if (!["title-img", "title", "updated", "icon", "id", "type", "class", "spellcheck", "contenteditable", "data-doc-type", "style"].includes(key)) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
this.element.setAttribute(key, ial[key]);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-17 17:55:02 +08:00
|
|
|
|
// text block-ref file-annotation-ref a 结尾处打字应为普通文本
|
2022-05-26 15:18:53 +08:00
|
|
|
|
private escapeInline(protyle: IProtyle, range: Range, event: InputEvent) {
|
|
|
|
|
|
if (!event.data) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const inputData = event.data;
|
|
|
|
|
|
protyle.toolbar.range = range;
|
|
|
|
|
|
const inlineElement = range.startContainer.parentElement;
|
2022-09-18 20:20:53 +08:00
|
|
|
|
const currentTypes = protyle.toolbar.getCurrentType();
|
2022-09-22 22:52:05 +08:00
|
|
|
|
|
|
|
|
|
|
let dataLength = inputData.length;
|
|
|
|
|
|
if (inputData === "<" || inputData === ">") {
|
|
|
|
|
|
// 使用 inlineElement.innerHTML 会出现 https://ld246.com/article/1627185027423 中的第2个问题
|
|
|
|
|
|
dataLength = 4;
|
|
|
|
|
|
}
|
2022-09-23 09:10:31 +08:00
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/5924
|
2022-09-22 22:52:05 +08:00
|
|
|
|
if (currentTypes.length > 0 && range.toString() === "" && range.startOffset === inputData.length && inlineElement.tagName === "SPAN" &&
|
2022-09-23 09:10:31 +08:00
|
|
|
|
inlineElement.textContent.replace(Constants.ZWSP, "") !== inputData &&
|
2022-09-30 18:05:24 +08:00
|
|
|
|
inlineElement.textContent.replace(Constants.ZWSP, "").length >= inputData.length &&
|
2022-09-22 22:52:05 +08:00
|
|
|
|
!hasPreviousSibling(range.startContainer) && !hasPreviousSibling(inlineElement)) {
|
|
|
|
|
|
const html = inlineElement.innerHTML.replace(Constants.ZWSP, "");
|
|
|
|
|
|
inlineElement.innerHTML = html.substr(dataLength);
|
|
|
|
|
|
const textNode = document.createTextNode(inputData);
|
|
|
|
|
|
inlineElement.before(textNode);
|
|
|
|
|
|
range.selectNodeContents(textNode);
|
|
|
|
|
|
range.collapse(false);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (// 表格行内公式之前无法插入文字 https://github.com/siyuan-note/siyuan/issues/3908
|
2022-09-18 20:20:53 +08:00
|
|
|
|
inlineElement.tagName === "SPAN" &&
|
2022-09-23 09:10:31 +08:00
|
|
|
|
inlineElement.textContent !== inputData &&
|
2023-03-07 11:17:00 +08:00
|
|
|
|
!currentTypes.includes("search-mark") && // https://github.com/siyuan-note/siyuan/issues/7586
|
2022-09-17 17:55:02 +08:00
|
|
|
|
range.toString() === "" && range.startContainer.nodeType === 3 &&
|
2022-09-18 20:20:53 +08:00
|
|
|
|
(currentTypes.includes("inline-memo") || currentTypes.includes("text") || currentTypes.includes("block-ref") || currentTypes.includes("file-annotation-ref") || currentTypes.includes("a")) &&
|
2022-09-17 17:55:02 +08:00
|
|
|
|
!hasNextSibling(range.startContainer) && range.startContainer.textContent.length === range.startOffset &&
|
2022-09-23 09:10:31 +08:00
|
|
|
|
inlineElement.textContent.length > inputData.length
|
2022-09-17 17:55:02 +08:00
|
|
|
|
) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
const position = getSelectionOffset(inlineElement, protyle.wysiwyg.element, range);
|
2022-09-23 09:10:31 +08:00
|
|
|
|
const html = inlineElement.innerHTML;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (position.start === inlineElement.textContent.length) {
|
|
|
|
|
|
// 使用 inlineElement.textContent **$a$b** 中数学公式消失
|
|
|
|
|
|
inlineElement.innerHTML = html.substr(0, html.length - dataLength);
|
|
|
|
|
|
const textNode = document.createTextNode(inputData);
|
2022-09-17 17:55:02 +08:00
|
|
|
|
inlineElement.after(textNode);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
range.selectNodeContents(textNode);
|
|
|
|
|
|
range.collapse(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private setEmptyOutline(protyle: IProtyle, element: HTMLElement) {
|
|
|
|
|
|
// 图片移除选择状态应放在前面,否则 https://github.com/siyuan-note/siyuan/issues/4173
|
|
|
|
|
|
const selectImgElement = protyle.wysiwyg.element.querySelector(".img--select");
|
|
|
|
|
|
if (selectImgElement) {
|
|
|
|
|
|
selectImgElement.classList.remove("img--select");
|
|
|
|
|
|
}
|
|
|
|
|
|
let nodeElement = element;
|
|
|
|
|
|
if (!element.getAttribute("data-node-id")) {
|
|
|
|
|
|
const tempElement = hasClosestBlock(element);
|
|
|
|
|
|
if (!tempElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
nodeElement = tempElement;
|
|
|
|
|
|
}
|
2022-06-29 20:25:30 +08:00
|
|
|
|
/// #if !MOBILE
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (protyle.model) {
|
|
|
|
|
|
getAllModels().outline.forEach(item => {
|
|
|
|
|
|
if (item.blockId === protyle.block.rootID) {
|
2023-03-03 10:39:58 +08:00
|
|
|
|
item.setCurrent(nodeElement);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2022-06-29 20:25:30 +08:00
|
|
|
|
/// #endif
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-15 20:19:56 +08:00
|
|
|
|
private bindCommonEvent(protyle: IProtyle) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
this.element.addEventListener("copy", (event: ClipboardEvent & { target: HTMLElement }) => {
|
2022-10-27 23:10:29 +08:00
|
|
|
|
window.siyuan.ctrlIsPressed = false; // https://github.com/siyuan-note/siyuan/issues/6373
|
2022-05-26 15:18:53 +08:00
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/4600
|
|
|
|
|
|
if (event.target.tagName === "PROTYLE-HTML") {
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
const range = getEditorRange(protyle.wysiwyg.element);
|
|
|
|
|
|
const nodeElement = hasClosestBlock(range.startContainer);
|
|
|
|
|
|
if (!nodeElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-05-29 10:06:02 +08:00
|
|
|
|
const selectImgElement = nodeElement.querySelector(".img--select");
|
2022-05-26 15:18:53 +08:00
|
|
|
|
let selectElements = Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select"));
|
|
|
|
|
|
if (selectElements.length === 0 && range.toString() === "" && !range.cloneContents().querySelector("img") &&
|
|
|
|
|
|
!selectImgElement) {
|
|
|
|
|
|
nodeElement.classList.add("protyle-wysiwyg--select");
|
2022-06-28 10:20:55 +08:00
|
|
|
|
countBlockWord([nodeElement.getAttribute("data-node-id")]);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
selectElements = [nodeElement];
|
|
|
|
|
|
}
|
|
|
|
|
|
let html = "";
|
|
|
|
|
|
let textPlain = "";
|
|
|
|
|
|
if (selectElements.length > 0) {
|
2023-02-06 21:54:26 +08:00
|
|
|
|
const isRefText = selectElements[0].getAttribute("data-reftext") === "true";
|
2022-11-12 00:36:53 +08:00
|
|
|
|
if (selectElements[0].getAttribute("data-type") === "NodeListItem" &&
|
|
|
|
|
|
selectElements[0].parentElement.classList.contains("list") && // 反链复制列表项 https://github.com/siyuan-note/siyuan/issues/6555
|
|
|
|
|
|
selectElements[0].parentElement.childElementCount - 1 === selectElements.length) {
|
2023-02-04 15:40:54 +08:00
|
|
|
|
if (isRefText) {
|
2023-02-06 21:54:26 +08:00
|
|
|
|
const cloneElement = selectElements[0].parentElement.cloneNode(true) as HTMLElement;
|
|
|
|
|
|
const cloneEditElement = getContenteditableElement(cloneElement);
|
2023-02-04 15:40:54 +08:00
|
|
|
|
if (cloneEditElement) {
|
2023-02-06 21:54:26 +08:00
|
|
|
|
cloneEditElement.insertAdjacentHTML("beforeend", ` <span data-type="block-ref" data-subtype="s" data-id="${cloneElement.getAttribute("data-node-id")}">*</span>`);
|
2023-02-04 15:40:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
html = cloneElement.outerHTML;
|
2023-02-06 21:54:26 +08:00
|
|
|
|
selectElements[0].removeAttribute("data-reftext");
|
2023-02-04 15:40:54 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
html = selectElements[0].parentElement.outerHTML;
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} else {
|
2023-02-04 15:40:54 +08:00
|
|
|
|
selectElements.forEach((item, index) => {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
const topElement = getTopAloneElement(item);
|
2023-02-04 15:40:54 +08:00
|
|
|
|
if (isRefText && index === 0) {
|
2023-02-06 21:54:26 +08:00
|
|
|
|
const cloneElement = topElement.cloneNode(true) as HTMLElement;
|
|
|
|
|
|
const cloneEditElement = getContenteditableElement(cloneElement);
|
2023-02-04 15:40:54 +08:00
|
|
|
|
if (cloneEditElement) {
|
2023-02-06 21:54:26 +08:00
|
|
|
|
cloneEditElement.insertAdjacentHTML("beforeend", ` <span data-type="block-ref" data-subtype="s" data-id="${topElement.getAttribute("data-node-id")}">*</span>`);
|
2023-02-04 15:40:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
html += removeEmbed(cloneElement);
|
2023-02-06 21:54:26 +08:00
|
|
|
|
selectElements[0].removeAttribute("data-reftext");
|
2023-02-04 15:40:54 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
html += removeEmbed(topElement);
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const tempElement = document.createElement("div");
|
2022-07-30 16:29:11 +08:00
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/5540
|
2022-07-31 10:18:05 +08:00
|
|
|
|
const selectTypes = protyle.toolbar.getCurrentType(range);
|
2022-09-16 11:01:05 +08:00
|
|
|
|
if ((selectTypes.length > 0 || range.startContainer.parentElement.parentElement.getAttribute("data-type") === "NodeHeading") &&
|
2022-07-30 16:29:11 +08:00
|
|
|
|
(
|
|
|
|
|
|
(range.startContainer.nodeType === 3 && range.startContainer.parentElement.textContent === range.toString()) ||
|
|
|
|
|
|
(range.startContainer.nodeType !== 3 && range.startContainer.textContent === range.toString())
|
|
|
|
|
|
)) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (range.startContainer.parentElement.parentElement.getAttribute("data-type") === "NodeHeading") {
|
|
|
|
|
|
// 复制标题 https://github.com/siyuan-note/insider/issues/297
|
|
|
|
|
|
tempElement.append(range.startContainer.parentElement.parentElement.cloneNode(true));
|
2022-09-19 16:47:38 +08:00
|
|
|
|
} else if (!["DIV", "TD", "TH", "TR"].includes(range.startContainer.parentElement.tagName)) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
// 复制行内元素 https://github.com/siyuan-note/insider/issues/191
|
|
|
|
|
|
tempElement.append(range.startContainer.parentElement.cloneNode(true));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 直接复制块 https://github.com/siyuan-note/insider/issues/318
|
|
|
|
|
|
tempElement.append(range.cloneContents());
|
|
|
|
|
|
}
|
|
|
|
|
|
html = tempElement.innerHTML;
|
|
|
|
|
|
} else if (selectImgElement) {
|
|
|
|
|
|
html = selectImgElement.outerHTML;
|
2022-09-16 11:54:27 +08:00
|
|
|
|
} else if (selectTypes.length > 0 && range.startContainer.nodeType === 3 && range.startContainer.parentElement.tagName === "SPAN" &&
|
2022-09-16 21:16:57 +08:00
|
|
|
|
range.startContainer.parentElement.isSameNode(range.endContainer.parentElement)) {
|
2022-09-16 11:54:27 +08:00
|
|
|
|
// 复制粗体等字体中的一部分
|
2022-09-18 17:51:19 +08:00
|
|
|
|
const attributes = range.startContainer.parentElement.attributes;
|
2022-09-16 11:54:27 +08:00
|
|
|
|
const spanElement = document.createElement("span");
|
|
|
|
|
|
for (let i = 0; i < attributes.length; i++) {
|
|
|
|
|
|
spanElement.setAttribute(attributes[i].name, attributes[i].value);
|
|
|
|
|
|
}
|
2022-09-30 22:26:02 +08:00
|
|
|
|
if (spanElement.getAttribute("data-type").indexOf("block-ref") > -1 &&
|
|
|
|
|
|
spanElement.getAttribute("data-subtype") === "d") {
|
|
|
|
|
|
// 需变为静态锚文本
|
|
|
|
|
|
spanElement.setAttribute("data-subtype", "s");
|
|
|
|
|
|
}
|
2022-09-18 17:51:19 +08:00
|
|
|
|
spanElement.textContent = range.toString();
|
2022-09-16 11:54:27 +08:00
|
|
|
|
html = spanElement.outerHTML;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
tempElement.append(range.cloneContents());
|
|
|
|
|
|
const inlineMathElement = hasClosestByAttribute(range.commonAncestorContainer, "data-type", "inline-math");
|
|
|
|
|
|
if (inlineMathElement) {
|
|
|
|
|
|
// 表格内复制数学公式 https://ld246.com/article/1631708573504
|
|
|
|
|
|
html = inlineMathElement.outerHTML;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
html = tempElement.innerHTML;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 不能使用 commonAncestorContainer https://ld246.com/article/1643282894693
|
|
|
|
|
|
if (hasClosestByAttribute(range.startContainer, "data-type", "NodeCodeBlock") ||
|
|
|
|
|
|
hasClosestByMatchTag(range.startContainer, "CODE")) {
|
|
|
|
|
|
textPlain = tempElement.textContent.replace(Constants.ZWSP, "");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-11-24 23:16:56 +08:00
|
|
|
|
if (protyle.disabled) {
|
2022-11-25 23:09:16 +08:00
|
|
|
|
html = getEnableHTML(html);
|
2022-11-24 23:16:56 +08:00
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
event.clipboardData.setData("text/plain", textPlain || protyle.lute.BlockDOM2StdMd(html).trimEnd());
|
2023-02-02 21:51:30 +08:00
|
|
|
|
event.clipboardData.setData("text/html", protyle.lute.BlockDOM2HTML(html));
|
|
|
|
|
|
event.clipboardData.setData("text/siyuan", html);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
});
|
2022-08-31 22:29:52 +08:00
|
|
|
|
this.element.addEventListener("mousedown", (event: MouseEvent) => {
|
|
|
|
|
|
if (event.button === 2 || window.siyuan.ctrlIsPressed) {
|
|
|
|
|
|
// 右键
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-08-31 22:29:52 +08:00
|
|
|
|
if (!window.siyuan.shiftIsPressed) {
|
|
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/3026
|
|
|
|
|
|
hideElements(["select"], protyle);
|
2022-08-31 01:14:45 +08:00
|
|
|
|
}
|
2022-08-31 22:29:52 +08:00
|
|
|
|
const target = event.target as HTMLElement;
|
|
|
|
|
|
if (hasClosestByClassName(target, "protyle-action")) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-08-31 22:29:52 +08:00
|
|
|
|
const documentSelf = document;
|
|
|
|
|
|
const rect = protyle.element.getBoundingClientRect();
|
|
|
|
|
|
const mostLeft = rect.left + parseInt(protyle.wysiwyg.element.style.paddingLeft) + 1;
|
2022-11-19 11:07:47 +08:00
|
|
|
|
// 不能用 firstElement,否则 https://ld246.com/article/1668758661338
|
|
|
|
|
|
const mostRight = mostLeft + (protyle.wysiwyg.element.clientWidth - parseInt(protyle.wysiwyg.element.style.paddingLeft) - parseInt(protyle.wysiwyg.element.style.paddingRight)) - 1;
|
2022-08-31 22:29:52 +08:00
|
|
|
|
const mostBottom = rect.bottom;
|
|
|
|
|
|
const y = event.clientY;
|
|
|
|
|
|
// 图片、iframe、video 缩放
|
|
|
|
|
|
if (!protyle.disabled && target.classList.contains("protyle-action__drag")) {
|
|
|
|
|
|
const nodeElement = hasClosestBlock(target);
|
|
|
|
|
|
if (!nodeElement) {
|
|
|
|
|
|
return;
|
2022-07-21 23:25:39 +08:00
|
|
|
|
}
|
2022-08-31 22:29:52 +08:00
|
|
|
|
let isCenter = true;
|
|
|
|
|
|
if (["NodeIFrame", "NodeWidget", "NodeVideo"].includes(nodeElement.getAttribute("data-type"))) {
|
|
|
|
|
|
nodeElement.classList.add("iframe--drag");
|
|
|
|
|
|
if (nodeElement.style.textAlign === "left" || nodeElement.style.textAlign === "right") {
|
|
|
|
|
|
isCenter = false;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2022-08-31 22:29:52 +08:00
|
|
|
|
} else if (target.parentElement.parentElement.getAttribute("data-type") === "img") {
|
|
|
|
|
|
target.parentElement.parentElement.classList.add("img--drag");
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2022-08-31 22:29:52 +08:00
|
|
|
|
|
|
|
|
|
|
const id = nodeElement.getAttribute("data-node-id");
|
|
|
|
|
|
const html = nodeElement.outerHTML;
|
|
|
|
|
|
const x = event.clientX;
|
|
|
|
|
|
const dragElement = target.previousElementSibling as HTMLElement;
|
|
|
|
|
|
const dragWidth = dragElement.clientWidth;
|
|
|
|
|
|
const dragHeight = dragElement.clientHeight;
|
|
|
|
|
|
documentSelf.onmousemove = (moveEvent: MouseEvent) => {
|
|
|
|
|
|
if (dragElement.tagName === "IMG") {
|
|
|
|
|
|
dragElement.parentElement.parentElement.style.width = "";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2022-08-31 22:29:52 +08:00
|
|
|
|
if (moveEvent.clientX > x - dragWidth + 8 && moveEvent.clientX < mostRight) {
|
|
|
|
|
|
if ((dragElement.tagName === "IMG" && dragElement.parentElement.parentElement.style.display !== "block") || !isCenter) {
|
|
|
|
|
|
dragElement.style.width = Math.max(17, dragWidth + (moveEvent.clientX - x)) + "px";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} else {
|
2022-08-31 22:29:52 +08:00
|
|
|
|
dragElement.style.width = Math.max(17, dragWidth + (moveEvent.clientX - x) * 2) + "px";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-08-31 22:29:52 +08:00
|
|
|
|
if (dragElement.tagName !== "IMG") {
|
|
|
|
|
|
if (moveEvent.clientY > y - dragHeight + 8 && moveEvent.clientY < mostBottom) {
|
|
|
|
|
|
dragElement.style.height = (dragHeight + (moveEvent.clientY - y)) + "px";
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
dragElement.parentElement.parentElement.style.maxWidth = (parseInt(dragElement.style.width) + 10) + "px";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2022-08-31 22:29:52 +08:00
|
|
|
|
};
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
2022-08-31 22:29:52 +08:00
|
|
|
|
documentSelf.onmouseup = () => {
|
|
|
|
|
|
documentSelf.onmousemove = null;
|
|
|
|
|
|
documentSelf.onmouseup = null;
|
|
|
|
|
|
documentSelf.ondragstart = null;
|
|
|
|
|
|
documentSelf.onselectstart = null;
|
|
|
|
|
|
documentSelf.onselect = null;
|
|
|
|
|
|
if (target.classList.contains("protyle-action__drag") && nodeElement) {
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
|
|
}
|
|
|
|
|
|
nodeElement.classList.remove("iframe--drag");
|
|
|
|
|
|
target.parentElement.parentElement.classList.remove("img--drag");
|
|
|
|
|
|
};
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-08-31 22:29:52 +08:00
|
|
|
|
// table cell select
|
|
|
|
|
|
let tableBlockElement: HTMLElement | false;
|
|
|
|
|
|
if (target.tagName === "TH" || target.tagName === "TD" || target.firstElementChild?.tagName === "TABLE" || target.classList.contains("table__resize") || target.classList.contains("table__select")) {
|
|
|
|
|
|
tableBlockElement = hasClosestBlock(target);
|
|
|
|
|
|
if (tableBlockElement) {
|
|
|
|
|
|
tableBlockElement.querySelector("table").classList.remove("select");
|
|
|
|
|
|
tableBlockElement.querySelector(".table__select").removeAttribute("style");
|
|
|
|
|
|
window.siyuan.menus.menu.remove();
|
|
|
|
|
|
event.stopPropagation();
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
// 后续拖拽操作写在多选节点中
|
|
|
|
|
|
}
|
|
|
|
|
|
// table col resize
|
2022-08-31 22:29:52 +08:00
|
|
|
|
if (!protyle.disabled && target.classList.contains("table__resize")) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
const nodeElement = hasClosestBlock(target);
|
|
|
|
|
|
if (!nodeElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const html = nodeElement.outerHTML;
|
|
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/4455
|
|
|
|
|
|
if (getSelection().rangeCount > 0) {
|
|
|
|
|
|
getSelection().getRangeAt(0).collapse(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
nodeElement.firstElementChild.style.webkitUserModify = "read-only";
|
|
|
|
|
|
nodeElement.style.cursor = "col-resize";
|
|
|
|
|
|
target.removeAttribute("style");
|
|
|
|
|
|
const id = nodeElement.getAttribute("data-node-id");
|
|
|
|
|
|
const x = event.clientX;
|
2023-03-28 10:22:32 +08:00
|
|
|
|
const colIndex = parseInt(target.getAttribute("data-col-index"));
|
2023-03-28 10:12:30 +08:00
|
|
|
|
const colElement = nodeElement.querySelectorAll("table col")[colIndex] as HTMLElement;
|
2022-12-29 15:13:37 +08:00
|
|
|
|
// 清空初始化 table 时的最小宽度
|
|
|
|
|
|
if (colElement.style.minWidth) {
|
2023-03-28 10:12:30 +08:00
|
|
|
|
colElement.style.width = (nodeElement.querySelectorAll("table td, table th")[colIndex] as HTMLElement).offsetWidth + "px";
|
2022-12-29 15:13:37 +08:00
|
|
|
|
colElement.style.minWidth = "";
|
|
|
|
|
|
}
|
2023-03-28 10:12:30 +08:00
|
|
|
|
// 移除 cell 上的宽度限制 https://github.com/siyuan-note/siyuan/issues/7795
|
|
|
|
|
|
nodeElement.querySelectorAll("tr").forEach((trItem: HTMLTableRowElement) => {
|
|
|
|
|
|
trItem.cells[colIndex].style.width = "";
|
|
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
|
const oldWidth = colElement.clientWidth;
|
|
|
|
|
|
const hasScroll = nodeElement.firstElementChild.clientWidth < nodeElement.firstElementChild.scrollWidth;
|
|
|
|
|
|
documentSelf.onmousemove = (moveEvent: MouseEvent) => {
|
|
|
|
|
|
if (nodeElement.style.textAlign === "center" && !hasScroll) {
|
|
|
|
|
|
colElement.style.width = (oldWidth + (moveEvent.clientX - x) * 2) + "px";
|
|
|
|
|
|
} else {
|
|
|
|
|
|
colElement.style.width = (oldWidth + (moveEvent.clientX - x)) + "px";
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
documentSelf.onmouseup = () => {
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
nodeElement.firstElementChild.style.webkitUserModify = "";
|
|
|
|
|
|
nodeElement.style.cursor = "";
|
|
|
|
|
|
documentSelf.onmousemove = null;
|
|
|
|
|
|
documentSelf.onmouseup = null;
|
|
|
|
|
|
documentSelf.ondragstart = null;
|
|
|
|
|
|
documentSelf.onselectstart = null;
|
|
|
|
|
|
documentSelf.onselect = null;
|
|
|
|
|
|
if (nodeElement) {
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, html);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 多选节点
|
|
|
|
|
|
let x = event.clientX;
|
|
|
|
|
|
if (event.clientX > mostRight) {
|
|
|
|
|
|
x = mostRight;
|
|
|
|
|
|
} else if (event.clientX < mostLeft) {
|
|
|
|
|
|
x = mostLeft;
|
|
|
|
|
|
}
|
2022-09-15 20:19:56 +08:00
|
|
|
|
const mostTop = rect.top + (protyle.options.render.breadcrumb ? protyle.breadcrumb.element.parentElement.clientHeight : 0);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
|
|
|
let mouseElement: Element;
|
|
|
|
|
|
let moveCellElement: HTMLElement;
|
2023-02-03 15:18:54 +08:00
|
|
|
|
let startFirstElement: Element;
|
|
|
|
|
|
let endLastElement: Element;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
documentSelf.onmousemove = (moveEvent: MouseEvent) => {
|
|
|
|
|
|
const moveTarget = moveEvent.target as HTMLElement;
|
|
|
|
|
|
// table cell select
|
2022-08-31 22:29:52 +08:00
|
|
|
|
if (!protyle.disabled && tableBlockElement && tableBlockElement.contains(moveTarget) && !hasClosestByClassName(tableBlockElement, "protyle-wysiwyg__embed")) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if ((moveTarget.tagName === "TH" || moveTarget.tagName === "TD") && !moveTarget.isSameNode(target) && (!moveCellElement || !moveCellElement.isSameNode(moveTarget))) {
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
tableBlockElement.firstElementChild.style.webkitUserModify = "read-only";
|
|
|
|
|
|
let width = target.offsetLeft + target.clientWidth - moveTarget.offsetLeft;
|
|
|
|
|
|
let left = moveTarget.offsetLeft;
|
|
|
|
|
|
if (target.offsetLeft === moveTarget.offsetLeft) {
|
|
|
|
|
|
width = Math.max(target.clientWidth, moveTarget.clientWidth);
|
|
|
|
|
|
} else if (target.offsetLeft < moveTarget.offsetLeft) {
|
|
|
|
|
|
width = moveTarget.offsetLeft + moveTarget.clientWidth - target.offsetLeft;
|
|
|
|
|
|
left = target.offsetLeft;
|
|
|
|
|
|
}
|
|
|
|
|
|
let height = target.offsetTop + target.clientHeight - moveTarget.offsetTop;
|
|
|
|
|
|
let top = moveTarget.offsetTop;
|
|
|
|
|
|
if (target.offsetTop === moveTarget.offsetTop) {
|
|
|
|
|
|
height = Math.max(target.clientHeight, moveTarget.clientHeight);
|
|
|
|
|
|
} else if (target.offsetTop < moveTarget.offsetTop) {
|
|
|
|
|
|
height = moveTarget.offsetTop + moveTarget.clientHeight - target.offsetTop;
|
|
|
|
|
|
top = target.offsetTop;
|
|
|
|
|
|
}
|
|
|
|
|
|
// https://github.com/siyuan-note/insider/issues/1015
|
|
|
|
|
|
Array.from(tableBlockElement.querySelectorAll("th, td")).find((item: HTMLElement) => {
|
|
|
|
|
|
const updateWidth = item.offsetLeft < left + width && item.offsetLeft + item.clientWidth > left + width;
|
|
|
|
|
|
const updateWidth2 = item.offsetLeft < left && item.offsetLeft + item.clientWidth > left;
|
|
|
|
|
|
if (item.offsetTop < top && item.offsetTop + item.clientHeight > top) {
|
|
|
|
|
|
if ((item.offsetLeft + 6 > left && item.offsetLeft + item.clientWidth - 6 < left + width) || updateWidth || updateWidth2) {
|
|
|
|
|
|
height = top + height - item.offsetTop;
|
|
|
|
|
|
top = item.offsetTop;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (updateWidth) {
|
|
|
|
|
|
width = item.offsetLeft + item.clientWidth - left;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (updateWidth2) {
|
|
|
|
|
|
width = left + width - item.offsetLeft;
|
|
|
|
|
|
left = item.offsetLeft;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (item.offsetTop < top + height && item.offsetTop + item.clientHeight > top + height) {
|
|
|
|
|
|
if ((item.offsetLeft + 6 > left && item.offsetLeft + item.clientWidth - 6 < left + width) || updateWidth || updateWidth2) {
|
|
|
|
|
|
height = item.clientHeight + item.offsetTop - top;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (updateWidth) {
|
|
|
|
|
|
width = item.offsetLeft + item.clientWidth - left;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (updateWidth2) {
|
|
|
|
|
|
width = left + width - item.offsetLeft;
|
|
|
|
|
|
left = item.offsetLeft;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (updateWidth2 && item.offsetTop + 6 > top && item.offsetTop + item.clientHeight - 6 < top + height) {
|
|
|
|
|
|
width = left + width - item.offsetLeft;
|
|
|
|
|
|
left = item.offsetLeft;
|
|
|
|
|
|
} else if (updateWidth && item.offsetTop + 6 > top && item.offsetTop + item.clientHeight - 6 < top + height) {
|
|
|
|
|
|
width = item.offsetLeft + item.clientWidth - left;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
tableBlockElement.querySelector("table").classList.add("select");
|
|
|
|
|
|
tableBlockElement.querySelector(".table__select").setAttribute("style", `left:${left - tableBlockElement.firstElementChild.scrollLeft}px;top:${top}px;height:${height}px;width:${width + 1}px;`);
|
|
|
|
|
|
moveCellElement = moveTarget;
|
|
|
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
protyle.selectElement.classList.remove("fn__none");
|
|
|
|
|
|
// 向左选择,遇到 gutter 就不会弹出 toolbar
|
|
|
|
|
|
hideElements(["gutter"], protyle);
|
|
|
|
|
|
let newTop = 0;
|
|
|
|
|
|
let newLeft = 0;
|
|
|
|
|
|
let newWidth = 0;
|
|
|
|
|
|
let newHeight = 0;
|
|
|
|
|
|
if (moveEvent.clientX < x) {
|
|
|
|
|
|
if (moveEvent.clientX < mostLeft) {
|
|
|
|
|
|
// 向左越界
|
|
|
|
|
|
newLeft = mostLeft;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 向左
|
|
|
|
|
|
newLeft = moveEvent.clientX;
|
|
|
|
|
|
}
|
|
|
|
|
|
newWidth = x - newLeft;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (moveEvent.clientX > mostRight) {
|
|
|
|
|
|
// 向右越界
|
|
|
|
|
|
newLeft = x;
|
|
|
|
|
|
newWidth = mostRight - newLeft;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 向右
|
|
|
|
|
|
newLeft = x;
|
|
|
|
|
|
newWidth = moveEvent.clientX - x;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (moveEvent.clientY > y) {
|
|
|
|
|
|
if (moveEvent.clientY > mostBottom) {
|
|
|
|
|
|
// 向下越界
|
|
|
|
|
|
newTop = y;
|
|
|
|
|
|
newHeight = mostBottom - y;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 向下
|
|
|
|
|
|
newTop = y;
|
|
|
|
|
|
newHeight = moveEvent.clientY - y;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (moveEvent.clientY < mostTop) {
|
|
|
|
|
|
// 向上越界
|
|
|
|
|
|
newTop = mostTop;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 向上
|
|
|
|
|
|
newTop = moveEvent.clientY;
|
|
|
|
|
|
}
|
|
|
|
|
|
newHeight = y - newTop;
|
|
|
|
|
|
}
|
2023-02-09 12:21:02 +08:00
|
|
|
|
if (newHeight < 4) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
protyle.selectElement.setAttribute("style", `background-color: ${protyle.selectElement.style.backgroundColor};top:${newTop}px;height:${newHeight}px;left:${newLeft + 2}px;width:${newWidth - 2}px;`);
|
|
|
|
|
|
const newMouseElement = document.elementFromPoint(moveEvent.clientX, moveEvent.clientY);
|
|
|
|
|
|
if (mouseElement && mouseElement.isSameNode(newMouseElement) && !mouseElement.classList.contains("protyle-wysiwyg") &&
|
|
|
|
|
|
!mouseElement.classList.contains("list") && !mouseElement.classList.contains("bq") && !mouseElement.classList.contains("sb")) {
|
|
|
|
|
|
// 性能优化,同一个p元素不进行选中计算
|
|
|
|
|
|
return;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
mouseElement = newMouseElement;
|
|
|
|
|
|
}
|
|
|
|
|
|
hideElements(["select"], protyle);
|
2023-02-04 09:51:24 +08:00
|
|
|
|
let firstElement;
|
2023-02-03 15:18:54 +08:00
|
|
|
|
if (moveEvent.clientY > y) {
|
|
|
|
|
|
firstElement = startFirstElement || document.elementFromPoint(newLeft - 1, newTop);
|
2023-02-04 09:51:24 +08:00
|
|
|
|
endLastElement = undefined;
|
2023-02-03 15:18:54 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
firstElement = document.elementFromPoint(newLeft - 1, newTop);
|
|
|
|
|
|
startFirstElement = undefined;
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (!firstElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (firstElement.classList.contains("protyle-wysiwyg") || firstElement.classList.contains("list") || firstElement.classList.contains("sb") || firstElement.classList.contains("bq")) {
|
|
|
|
|
|
firstElement = document.elementFromPoint(newLeft - 1, newTop + 16);
|
|
|
|
|
|
}
|
2023-02-03 17:32:46 +08:00
|
|
|
|
if (!firstElement) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-02-03 17:32:46 +08:00
|
|
|
|
let firstBlockElement = hasClosestBlock(firstElement);
|
|
|
|
|
|
if (moveEvent.clientY > y) {
|
|
|
|
|
|
if (!startFirstElement) {
|
|
|
|
|
|
startFirstElement = firstElement;
|
|
|
|
|
|
}
|
2023-03-07 18:42:13 +08:00
|
|
|
|
} else if (!firstBlockElement &&
|
|
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/7580
|
|
|
|
|
|
moveEvent.clientY < protyle.wysiwyg.element.lastElementChild.getBoundingClientRect().bottom) {
|
2023-02-03 17:32:46 +08:00
|
|
|
|
firstBlockElement = protyle.wysiwyg.element.firstElementChild as HTMLElement;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
let selectElements: Element[] = [];
|
|
|
|
|
|
let currentElement: Element | boolean = firstBlockElement;
|
|
|
|
|
|
let hasJump = false;
|
2023-02-04 09:51:24 +08:00
|
|
|
|
const selectBottom = endLastElement ? endLastElement.getBoundingClientRect().bottom : (newTop + newHeight);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
while (currentElement) {
|
|
|
|
|
|
if (currentElement && !currentElement.classList.contains("protyle-attr")) {
|
|
|
|
|
|
const currentRect = currentElement.getBoundingClientRect();
|
2023-02-03 15:18:54 +08:00
|
|
|
|
if (currentRect.height > 0 && currentRect.top < selectBottom && currentRect.left < newLeft + newWidth) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (hasJump) {
|
|
|
|
|
|
// 父节点的下个节点在选中范围内才可使用父节点作为选中节点
|
|
|
|
|
|
if (currentElement.nextElementSibling && !currentElement.nextElementSibling.classList.contains("protyle-attr")) {
|
|
|
|
|
|
const nextRect = currentElement.nextElementSibling.getBoundingClientRect();
|
2023-02-03 15:18:54 +08:00
|
|
|
|
if (nextRect.top < selectBottom && nextRect.left < newLeft + newWidth) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
selectElements = [currentElement];
|
|
|
|
|
|
currentElement = currentElement.nextElementSibling;
|
|
|
|
|
|
hasJump = false;
|
|
|
|
|
|
} else if (currentElement.parentElement.classList.contains("sb")) {
|
|
|
|
|
|
currentElement = hasClosestBlock(currentElement.parentElement);
|
|
|
|
|
|
hasJump = true;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
currentElement = hasClosestBlock(currentElement.parentElement);
|
|
|
|
|
|
hasJump = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
selectElements.push(currentElement);
|
|
|
|
|
|
currentElement = currentElement.nextElementSibling;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (currentElement.parentElement.classList.contains("sb")) {
|
|
|
|
|
|
// 跳出超级块横向排版中的未选中元素
|
|
|
|
|
|
currentElement = hasClosestBlock(currentElement.parentElement);
|
|
|
|
|
|
hasJump = true;
|
|
|
|
|
|
} else if (currentRect.height === 0 && currentRect.width === 0 && currentElement.parentElement.getAttribute("fold") === "1") {
|
|
|
|
|
|
currentElement = currentElement.parentElement;
|
|
|
|
|
|
selectElements = [];
|
|
|
|
|
|
} else {
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
currentElement = hasClosestBlock(currentElement.parentElement);
|
|
|
|
|
|
hasJump = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-02-03 15:27:27 +08:00
|
|
|
|
if (moveEvent.clientY <= y && !endLastElement) {
|
2023-02-04 09:51:24 +08:00
|
|
|
|
endLastElement = selectElements[selectElements.length - 1];
|
2023-02-03 15:27:27 +08:00
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (selectElements.length === 1 && !selectElements[0].classList.contains("list") && !selectElements[0].classList.contains("bq") && !selectElements[0].classList.contains("sb")) {
|
|
|
|
|
|
// 只有一个 p 时不选中
|
|
|
|
|
|
protyle.selectElement.style.backgroundColor = "transparent";
|
|
|
|
|
|
} else {
|
|
|
|
|
|
selectElements.forEach(item => {
|
|
|
|
|
|
if (!hasClosestByClassName(item, "protyle-wysiwyg__embed")) {
|
|
|
|
|
|
item.classList.add("protyle-wysiwyg--select");
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
protyle.selectElement.style.backgroundColor = "";
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
documentSelf.onmouseup = (mouseUpEvent) => {
|
|
|
|
|
|
documentSelf.onmousemove = null;
|
|
|
|
|
|
documentSelf.onmouseup = null;
|
|
|
|
|
|
documentSelf.ondragstart = null;
|
|
|
|
|
|
documentSelf.onselectstart = null;
|
|
|
|
|
|
documentSelf.onselect = null;
|
2023-02-03 15:18:54 +08:00
|
|
|
|
startFirstElement = undefined;
|
|
|
|
|
|
endLastElement = undefined;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
protyle.selectElement.classList.add("fn__none");
|
|
|
|
|
|
protyle.selectElement.removeAttribute("style");
|
2022-08-31 22:29:52 +08:00
|
|
|
|
if (!protyle.disabled && tableBlockElement) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
tableBlockElement.firstElementChild.style.webkitUserModify = "";
|
|
|
|
|
|
const tableSelectElement = tableBlockElement.querySelector(".table__select") as HTMLElement;
|
|
|
|
|
|
if (tableSelectElement.getAttribute("style")) {
|
|
|
|
|
|
if (getSelection().rangeCount > 0) {
|
|
|
|
|
|
getSelection().getRangeAt(0).collapse(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
window.siyuan.menus.menu.remove();
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
label: window.siyuan.languages.mergeCell,
|
|
|
|
|
|
click: () => {
|
|
|
|
|
|
if (tableBlockElement) {
|
|
|
|
|
|
const selectCellElements: HTMLTableCellElement[] = [];
|
|
|
|
|
|
const colIndexList: number[] = [];
|
|
|
|
|
|
const colCount = tableBlockElement.querySelectorAll("th").length;
|
|
|
|
|
|
let fnNoneMax = 0;
|
|
|
|
|
|
const scrollLeft = tableBlockElement.firstElementChild.scrollLeft;
|
|
|
|
|
|
let isTHead = false;
|
|
|
|
|
|
let isTBody = false;
|
|
|
|
|
|
tableBlockElement.querySelectorAll("th, td").forEach((item: HTMLTableCellElement, index: number) => {
|
|
|
|
|
|
if (item.classList.contains("fn__none")) {
|
|
|
|
|
|
// 合并的元素中间有 fn__none 的元素
|
|
|
|
|
|
if (item.previousElementSibling && item.previousElementSibling.isSameNode(selectCellElements[selectCellElements.length - 1])) {
|
|
|
|
|
|
selectCellElements.push(item);
|
|
|
|
|
|
if (!isTHead && item.parentElement.parentElement.tagName === "THEAD") {
|
|
|
|
|
|
isTHead = true;
|
|
|
|
|
|
} else if (!isTBody && item.parentElement.parentElement.tagName === "TBODY") {
|
|
|
|
|
|
isTBody = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (index < fnNoneMax && colIndexList.includes((index + 1) % colCount)) {
|
|
|
|
|
|
selectCellElements.push(item);
|
|
|
|
|
|
if (!isTHead && item.parentElement.parentElement.tagName === "THEAD") {
|
|
|
|
|
|
isTHead = true;
|
|
|
|
|
|
} else if (!isTBody && item.parentElement.parentElement.tagName === "TBODY") {
|
|
|
|
|
|
isTBody = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (item.offsetLeft + 6 > tableSelectElement.offsetLeft + scrollLeft && item.offsetLeft + item.clientWidth - 6 < tableSelectElement.offsetLeft + scrollLeft + tableSelectElement.clientWidth &&
|
|
|
|
|
|
item.offsetTop + 6 > tableSelectElement.offsetTop && item.offsetTop + item.clientHeight - 6 < tableSelectElement.offsetTop + tableSelectElement.clientHeight) {
|
|
|
|
|
|
selectCellElements.push(item);
|
|
|
|
|
|
if (!isTHead && item.parentElement.parentElement.tagName === "THEAD") {
|
|
|
|
|
|
isTHead = true;
|
|
|
|
|
|
} else if (!isTBody && item.parentElement.parentElement.tagName === "TBODY") {
|
|
|
|
|
|
isTBody = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
colIndexList.push((index + 1) % colCount);
|
|
|
|
|
|
// https://github.com/siyuan-note/insider/issues/1014
|
|
|
|
|
|
fnNoneMax = Math.max((item.rowSpan - 1) * colCount + index + 1, fnNoneMax);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
tableBlockElement.querySelector("table").classList.remove("select");
|
|
|
|
|
|
tableSelectElement.removeAttribute("style");
|
|
|
|
|
|
const oldHTML = tableBlockElement.outerHTML;
|
|
|
|
|
|
let cellElement = selectCellElements[0];
|
|
|
|
|
|
let colSpan = cellElement.colSpan;
|
|
|
|
|
|
let index = 1;
|
|
|
|
|
|
while (cellElement.nextElementSibling && cellElement.nextElementSibling.isSameNode(selectCellElements[index])) {
|
|
|
|
|
|
cellElement = cellElement.nextElementSibling as HTMLTableCellElement;
|
|
|
|
|
|
if (!cellElement.classList.contains("fn__none")) { // https://github.com/siyuan-note/insider/issues/1007#issuecomment-1046195608
|
|
|
|
|
|
colSpan += cellElement.colSpan;
|
|
|
|
|
|
}
|
|
|
|
|
|
index++;
|
|
|
|
|
|
}
|
|
|
|
|
|
let html = "";
|
|
|
|
|
|
let rowElement: Element = selectCellElements[0].parentElement;
|
|
|
|
|
|
let rowSpan = selectCellElements[0].rowSpan;
|
|
|
|
|
|
selectCellElements.forEach((item, index) => {
|
|
|
|
|
|
let cellHTML = item.innerHTML.trim();
|
|
|
|
|
|
if (cellHTML.endsWith("<br>")) {
|
|
|
|
|
|
cellHTML = cellHTML.substr(0, cellHTML.length - 4);
|
|
|
|
|
|
}
|
|
|
|
|
|
html += cellHTML + ((!cellHTML || index === selectCellElements.length - 1) ? "" : "<br>");
|
|
|
|
|
|
if (index !== 0) {
|
|
|
|
|
|
if (!rowElement.isSameNode(item.parentElement)) {
|
|
|
|
|
|
if (!item.classList.contains("fn__none")) { // https://github.com/siyuan-note/insider/issues/1011
|
|
|
|
|
|
rowSpan += item.rowSpan;
|
|
|
|
|
|
}
|
|
|
|
|
|
rowElement = item.parentElement;
|
|
|
|
|
|
if (selectCellElements[0].parentElement.parentElement.tagName === "THEAD" && item.parentElement.parentElement.tagName !== "THEAD") {
|
|
|
|
|
|
selectCellElements[0].parentElement.parentElement.insertAdjacentElement("beforeend", item.parentElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
item.classList.add("fn__none");
|
|
|
|
|
|
item.innerHTML = "";
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// https://github.com/siyuan-note/insider/issues/1017
|
|
|
|
|
|
if (isTHead && isTBody) {
|
|
|
|
|
|
rowElement = rowElement.parentElement.nextElementSibling.firstElementChild;
|
|
|
|
|
|
while (rowElement && rowElement.parentElement.tagName !== "THEAD") {
|
|
|
|
|
|
let colSpanCount = 0;
|
|
|
|
|
|
let noneCount = 0;
|
|
|
|
|
|
Array.from(rowElement.children).forEach((item: HTMLTableCellElement) => {
|
|
|
|
|
|
colSpanCount += item.colSpan - 1;
|
|
|
|
|
|
if (item.classList.contains("fn__none")) {
|
|
|
|
|
|
noneCount++;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
if (colSpanCount !== noneCount) {
|
|
|
|
|
|
selectCellElements[0].parentElement.parentElement.insertAdjacentElement("beforeend", rowElement);
|
|
|
|
|
|
rowElement = rowElement.parentElement.nextElementSibling.firstElementChild;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 合并背景色不会修改,需要等计算完毕
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
if (tableBlockElement) {
|
2022-10-21 11:35:12 +08:00
|
|
|
|
selectCellElements[0].innerHTML = html + "<wbr>";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
selectCellElements[0].colSpan = colSpan;
|
|
|
|
|
|
selectCellElements[0].rowSpan = rowSpan;
|
2022-10-21 11:35:12 +08:00
|
|
|
|
focusByWbr(selectCellElements[0], document.createRange());
|
2022-05-26 15:18:53 +08:00
|
|
|
|
updateTransaction(protyle, tableBlockElement.getAttribute("data-node-id"), tableBlockElement.outerHTML, oldHTML);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
2022-07-30 10:00:43 +08:00
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({type: "separator"}).element);
|
2022-06-12 22:06:19 +08:00
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
icon: "iconAlignLeft",
|
|
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.general.alignLeft.custom,
|
|
|
|
|
|
label: window.siyuan.languages.alignLeft,
|
|
|
|
|
|
click: () => {
|
|
|
|
|
|
if (tableBlockElement) {
|
|
|
|
|
|
const selectCellElements: HTMLTableCellElement[] = [];
|
|
|
|
|
|
const scrollLeft = tableBlockElement.firstElementChild.scrollLeft;
|
|
|
|
|
|
tableBlockElement.querySelectorAll("th, td").forEach((item: HTMLTableCellElement) => {
|
|
|
|
|
|
if (!item.classList.contains("fn__none") &&
|
|
|
|
|
|
item.offsetLeft + 6 > tableSelectElement.offsetLeft + scrollLeft && item.offsetLeft + item.clientWidth - 6 < tableSelectElement.offsetLeft + scrollLeft + tableSelectElement.clientWidth &&
|
|
|
|
|
|
item.offsetTop + 6 > tableSelectElement.offsetTop && item.offsetTop + item.clientHeight - 6 < tableSelectElement.offsetTop + tableSelectElement.clientHeight &&
|
|
|
|
|
|
(selectCellElements.length === 0 || (selectCellElements.length > 0 && item.offsetTop === selectCellElements[0].offsetTop))) {
|
|
|
|
|
|
selectCellElements.push(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
tableBlockElement.querySelector("table").classList.remove("select");
|
|
|
|
|
|
tableSelectElement.removeAttribute("style");
|
|
|
|
|
|
setTableAlign(protyle, selectCellElements, tableBlockElement, "left", getEditorRange(tableBlockElement));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
icon: "iconAlignCenter",
|
|
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.general.alignCenter.custom,
|
|
|
|
|
|
label: window.siyuan.languages.alignCenter,
|
|
|
|
|
|
click: () => {
|
|
|
|
|
|
if (tableBlockElement) {
|
|
|
|
|
|
const selectCellElements: HTMLTableCellElement[] = [];
|
|
|
|
|
|
const scrollLeft = tableBlockElement.firstElementChild.scrollLeft;
|
|
|
|
|
|
tableBlockElement.querySelectorAll("th, td").forEach((item: HTMLTableCellElement) => {
|
|
|
|
|
|
if (!item.classList.contains("fn__none") &&
|
|
|
|
|
|
item.offsetLeft + 6 > tableSelectElement.offsetLeft + scrollLeft && item.offsetLeft + item.clientWidth - 6 < tableSelectElement.offsetLeft + scrollLeft + tableSelectElement.clientWidth &&
|
|
|
|
|
|
item.offsetTop + 6 > tableSelectElement.offsetTop && item.offsetTop + item.clientHeight - 6 < tableSelectElement.offsetTop + tableSelectElement.clientHeight &&
|
|
|
|
|
|
(selectCellElements.length === 0 || (selectCellElements.length > 0 && item.offsetTop === selectCellElements[0].offsetTop))) {
|
|
|
|
|
|
selectCellElements.push(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
tableBlockElement.querySelector("table").classList.remove("select");
|
|
|
|
|
|
tableSelectElement.removeAttribute("style");
|
|
|
|
|
|
setTableAlign(protyle, selectCellElements, tableBlockElement, "center", getEditorRange(tableBlockElement));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
|
|
|
|
|
icon: "iconAlignRight",
|
|
|
|
|
|
accelerator: window.siyuan.config.keymap.editor.general.alignRight.custom,
|
|
|
|
|
|
label: window.siyuan.languages.alignRight,
|
|
|
|
|
|
click: () => {
|
|
|
|
|
|
if (tableBlockElement) {
|
|
|
|
|
|
const selectCellElements: HTMLTableCellElement[] = [];
|
|
|
|
|
|
const scrollLeft = tableBlockElement.firstElementChild.scrollLeft;
|
|
|
|
|
|
tableBlockElement.querySelectorAll("th, td").forEach((item: HTMLTableCellElement) => {
|
|
|
|
|
|
if (!item.classList.contains("fn__none") &&
|
|
|
|
|
|
item.offsetLeft + 6 > tableSelectElement.offsetLeft + scrollLeft && item.offsetLeft + item.clientWidth - 6 < tableSelectElement.offsetLeft + scrollLeft + tableSelectElement.clientWidth &&
|
|
|
|
|
|
item.offsetTop + 6 > tableSelectElement.offsetTop && item.offsetTop + item.clientHeight - 6 < tableSelectElement.offsetTop + tableSelectElement.clientHeight &&
|
|
|
|
|
|
(selectCellElements.length === 0 || (selectCellElements.length > 0 && item.offsetTop === selectCellElements[0].offsetTop))) {
|
|
|
|
|
|
selectCellElements.push(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
tableBlockElement.querySelector("table").classList.remove("select");
|
|
|
|
|
|
tableSelectElement.removeAttribute("style");
|
|
|
|
|
|
setTableAlign(protyle, selectCellElements, tableBlockElement, "right", getEditorRange(tableBlockElement));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
2022-07-30 10:00:43 +08:00
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({type: "separator"}).element);
|
|
|
|
|
|
window.siyuan.menus.menu.append(new MenuItem({
|
2022-12-10 11:45:53 +08:00
|
|
|
|
label: window.siyuan.languages.clear,
|
2022-07-30 10:00:43 +08:00
|
|
|
|
icon: "iconTrashcan",
|
|
|
|
|
|
click() {
|
|
|
|
|
|
if (tableBlockElement) {
|
|
|
|
|
|
const selectCellElements: HTMLTableCellElement[] = [];
|
|
|
|
|
|
const scrollLeft = tableBlockElement.firstElementChild.scrollLeft;
|
|
|
|
|
|
tableBlockElement.querySelectorAll("th, td").forEach((item: HTMLTableCellElement) => {
|
|
|
|
|
|
if (!item.classList.contains("fn__none") &&
|
|
|
|
|
|
item.offsetLeft + 6 > tableSelectElement.offsetLeft + scrollLeft && item.offsetLeft + item.clientWidth - 6 < tableSelectElement.offsetLeft + scrollLeft + tableSelectElement.clientWidth &&
|
|
|
|
|
|
item.offsetTop + 6 > tableSelectElement.offsetTop && item.offsetTop + item.clientHeight - 6 < tableSelectElement.offsetTop + tableSelectElement.clientHeight) {
|
|
|
|
|
|
selectCellElements.push(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
tableBlockElement.querySelector("table").classList.remove("select");
|
|
|
|
|
|
tableSelectElement.removeAttribute("style");
|
|
|
|
|
|
const oldHTML = tableBlockElement.outerHTML;
|
|
|
|
|
|
selectCellElements.forEach(item => {
|
|
|
|
|
|
item.innerHTML = "";
|
|
|
|
|
|
});
|
|
|
|
|
|
updateTransaction(protyle, tableBlockElement.getAttribute("data-node-id"), tableBlockElement.outerHTML, oldHTML);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}).element);
|
2022-08-02 11:15:18 +08:00
|
|
|
|
window.siyuan.menus.menu.popup({x: mouseUpEvent.clientX - 8, y: mouseUpEvent.clientY - 16});
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-06-28 18:39:24 +08:00
|
|
|
|
|
|
|
|
|
|
const ids: string[] = [];
|
2022-06-29 09:10:03 +08:00
|
|
|
|
const selectElement = protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select");
|
2022-06-28 18:39:24 +08:00
|
|
|
|
selectElement.forEach(item => {
|
|
|
|
|
|
ids.push(item.getAttribute("data-node-id"));
|
|
|
|
|
|
});
|
|
|
|
|
|
countBlockWord(ids);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
// 划选后不能存在跨块的 range https://github.com/siyuan-note/siyuan/issues/4473
|
|
|
|
|
|
if (getSelection().rangeCount > 0) {
|
|
|
|
|
|
const range = getSelection().getRangeAt(0);
|
|
|
|
|
|
if (range.toString() === "" ||
|
|
|
|
|
|
window.siyuan.shiftIsPressed // https://ld246.com/article/1650096678723
|
|
|
|
|
|
) {
|
2022-09-19 22:48:47 +08:00
|
|
|
|
if (event.detail === 3) {
|
|
|
|
|
|
// table 前或最后一个 cell 三击状态不对
|
|
|
|
|
|
let cursorElement = hasClosestBlock(range.startContainer) as Element;
|
|
|
|
|
|
if (cursorElement) {
|
|
|
|
|
|
if (cursorElement.nextElementSibling?.classList.contains("table")) {
|
2022-09-19 23:56:47 +08:00
|
|
|
|
setLastNodeRange(getContenteditableElement(cursorElement), range, false);
|
2022-09-19 22:48:47 +08:00
|
|
|
|
} else if (cursorElement.classList.contains("table")) {
|
|
|
|
|
|
const cellElements = cursorElement.querySelectorAll("th, td");
|
|
|
|
|
|
cursorElement = cellElements[cellElements.length - 1];
|
|
|
|
|
|
if (cursorElement.contains(range.startContainer)) {
|
2022-09-19 23:56:47 +08:00
|
|
|
|
setLastNodeRange(cursorElement, range, false);
|
2022-09-19 22:48:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-06-28 18:39:24 +08:00
|
|
|
|
if (selectElement.length > 0) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
range.collapse(true);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const startBlockElement = hasClosestBlock(range.startContainer);
|
|
|
|
|
|
let endBlockElement: false | HTMLElement;
|
|
|
|
|
|
if (mouseUpEvent.detail === 3 && range.endContainer.nodeType !== 3 && (range.endContainer as HTMLElement).tagName === "DIV" && range.endOffset === 0) {
|
|
|
|
|
|
// 三击选中段落块时,rangeEnd 会在下一个块
|
|
|
|
|
|
if ((range.endContainer as HTMLElement).classList.contains("protyle-attr")) {
|
|
|
|
|
|
// 三击在悬浮层中会选择到 attr https://github.com/siyuan-note/siyuan/issues/4636
|
|
|
|
|
|
range.setEndAfter(range.endContainer.previousSibling.lastChild);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
endBlockElement = hasClosestBlock(range.endContainer);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (startBlockElement && endBlockElement && !endBlockElement.isSameNode(startBlockElement)) {
|
|
|
|
|
|
range.collapse(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
});
|
2022-08-31 22:29:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private bindEvent(protyle: IProtyle) {
|
|
|
|
|
|
this.element.addEventListener("focusout", () => {
|
|
|
|
|
|
if (getSelection().rangeCount > 0 && !this.element.contains(getSelection().getRangeAt(0).startContainer)) {
|
|
|
|
|
|
// 对 ctrl+tab 切换后 range 已经在新页面中才会触发的 focusout 进行忽略,因为在切换之前已经 dispatchEvent 了。
|
|
|
|
|
|
if (!protyle.toolbar.range) {
|
|
|
|
|
|
protyle.toolbar.range = this.element.ownerDocument.createRange();
|
|
|
|
|
|
protyle.toolbar.range.setStart(getContenteditableElement(this.element) || this.element, 0);
|
|
|
|
|
|
protyle.toolbar.range.collapse(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
protyle.toolbar.range = getEditorRange(this.element);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
this.element.addEventListener("cut", (event: ClipboardEvent & { target: HTMLElement }) => {
|
2022-10-27 23:10:29 +08:00
|
|
|
|
window.siyuan.ctrlIsPressed = false; // https://github.com/siyuan-note/siyuan/issues/6373
|
2022-11-11 23:35:03 +08:00
|
|
|
|
if (protyle.disabled) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-08-31 22:29:52 +08:00
|
|
|
|
if (event.target.tagName === "PROTYLE-HTML") {
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
|
|
|
|
if (protyle.options.render.breadcrumb) {
|
|
|
|
|
|
protyle.breadcrumb.hide();
|
|
|
|
|
|
}
|
|
|
|
|
|
const range = getEditorRange(protyle.wysiwyg.element);
|
|
|
|
|
|
let nodeElement = hasClosestBlock(range.startContainer);
|
|
|
|
|
|
if (!nodeElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const selectImgElement = nodeElement.querySelector(".img--select");
|
|
|
|
|
|
let selectElements = Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select"));
|
|
|
|
|
|
if (selectElements.length === 0 && range.toString() === "" && !range.cloneContents().querySelector("img") &&
|
|
|
|
|
|
!selectImgElement) {
|
|
|
|
|
|
nodeElement.classList.add("protyle-wysiwyg--select");
|
|
|
|
|
|
selectElements = [nodeElement];
|
|
|
|
|
|
}
|
|
|
|
|
|
let html = "";
|
|
|
|
|
|
if (selectElements.length > 0) {
|
2022-11-12 00:36:53 +08:00
|
|
|
|
if (selectElements[0].getAttribute("data-type") === "NodeListItem" &&
|
|
|
|
|
|
selectElements[0].parentElement.classList.contains("list") && // 反链复制列表项 https://github.com/siyuan-note/siyuan/issues/6555
|
|
|
|
|
|
selectElements[0].parentElement.childElementCount - 1 === selectElements.length) {
|
2022-08-31 22:29:52 +08:00
|
|
|
|
html = selectElements[0].parentElement.outerHTML;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
selectElements.forEach(item => {
|
|
|
|
|
|
const topElement = getTopAloneElement(item);
|
|
|
|
|
|
if (item.getAttribute("data-type") === "NodeHeading" && item.getAttribute("fold") === "1") {
|
|
|
|
|
|
html += removeEmbed(topElement).replace('fold="1"', "");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
html += removeEmbed(topElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
if (selectElements[0].getAttribute("data-type") === "NodeListItem") {
|
|
|
|
|
|
html = `<div data-subtype="${selectElements[0].getAttribute("data-subtype")}" data-node-id="${Lute.NewNodeID()}" data-type="NodeList" class="list">${html}<div class="protyle-attr" contenteditable="false">${Constants.ZWSP}</div></div>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
const nextElement = getNextBlock(selectElements[selectElements.length - 1]);
|
|
|
|
|
|
removeBlock(protyle, nodeElement, range);
|
|
|
|
|
|
if (nextElement) {
|
|
|
|
|
|
// Ctrl+X 剪切后光标应跳到下一行行首 https://github.com/siyuan-note/siyuan/issues/5485
|
|
|
|
|
|
focusBlock(nextElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const id = nodeElement.getAttribute("data-node-id");
|
|
|
|
|
|
const oldHTML = nodeElement.outerHTML;
|
|
|
|
|
|
const tempElement = document.createElement("div");
|
|
|
|
|
|
// 首次选中标题时,range.startContainer 会为空
|
|
|
|
|
|
let startContainer = range.startContainer;
|
|
|
|
|
|
if (startContainer.nodeType === 3 && startContainer.textContent === "") {
|
|
|
|
|
|
const nextSibling = hasNextSibling(range.startContainer);
|
|
|
|
|
|
if (nextSibling) {
|
|
|
|
|
|
startContainer = nextSibling;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// 选中整个标题 https://github.com/siyuan-note/siyuan/issues/4329
|
|
|
|
|
|
const headElement = hasClosestByAttribute(startContainer, "data-type", "NodeHeading");
|
|
|
|
|
|
let isFoldHeading = false;
|
|
|
|
|
|
if (headElement && range.toString() === headElement.firstElementChild.textContent) {
|
|
|
|
|
|
const doOperations: IOperation[] = [{
|
|
|
|
|
|
action: "delete",
|
|
|
|
|
|
id: headElement.getAttribute("data-node-id")
|
|
|
|
|
|
}];
|
|
|
|
|
|
const undoOperations: IOperation[] = [{
|
|
|
|
|
|
action: "insert",
|
|
|
|
|
|
id: headElement.getAttribute("data-node-id"),
|
|
|
|
|
|
data: headElement.outerHTML,
|
|
|
|
|
|
previousID: headElement.previousElementSibling?.getAttribute("data-node-id"),
|
|
|
|
|
|
parentID: headElement.parentElement?.getAttribute("data-node-id") || protyle.block.parentID
|
|
|
|
|
|
}];
|
|
|
|
|
|
if (headElement.getAttribute("fold") === "1") {
|
|
|
|
|
|
isFoldHeading = true;
|
|
|
|
|
|
const headCloneElement = headElement.cloneNode(true) as HTMLElement;
|
|
|
|
|
|
headCloneElement.removeAttribute("fold");
|
|
|
|
|
|
tempElement.append(headCloneElement);
|
|
|
|
|
|
undoOperations[0].data = headCloneElement.outerHTML;
|
|
|
|
|
|
setFold(protyle, headElement, undefined, true);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if ((headElement.parentElement.childElementCount === 3 && headElement.parentElement.classList.contains("li")) ||
|
|
|
|
|
|
(headElement.parentElement.childElementCount === 2 && (headElement.parentElement.classList.contains("bq") || headElement.parentElement.classList.contains("sb"))) ||
|
|
|
|
|
|
(headElement.parentElement.childElementCount === 1 && headElement.parentElement.classList.contains("protyle-wysiwyg")) // 全选剪切标题
|
|
|
|
|
|
) {
|
|
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/4040
|
|
|
|
|
|
const emptyId = Lute.NewNodeID();
|
|
|
|
|
|
const emptyElement = genEmptyElement(false, false, emptyId);
|
|
|
|
|
|
doOperations.push({
|
|
|
|
|
|
id: emptyId,
|
|
|
|
|
|
data: emptyElement.outerHTML,
|
|
|
|
|
|
action: "insert",
|
|
|
|
|
|
parentID: headElement.parentElement.getAttribute("data-node-id") || protyle.block.parentID
|
|
|
|
|
|
});
|
|
|
|
|
|
undoOperations.push({
|
|
|
|
|
|
id: emptyId,
|
|
|
|
|
|
action: "delete",
|
|
|
|
|
|
});
|
|
|
|
|
|
headElement.before(emptyElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
focusSideBlock(headElement);
|
|
|
|
|
|
tempElement.append(headElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
transaction(protyle, doOperations, undoOperations);
|
|
|
|
|
|
} else if (range.toString() !== "" && startContainer.isSameNode(range.endContainer) && range.startContainer.nodeType === 3
|
|
|
|
|
|
&& range.endOffset === range.endContainer.textContent.length && range.startOffset === 0 &&
|
2022-09-19 16:47:38 +08:00
|
|
|
|
!["DIV", "TD", "TH", "TR"].includes(range.startContainer.parentElement.tagName)) {
|
2022-08-31 22:29:52 +08:00
|
|
|
|
// 选中整个内联元素
|
|
|
|
|
|
tempElement.append(range.startContainer.parentElement);
|
|
|
|
|
|
} else if (selectImgElement) {
|
|
|
|
|
|
tempElement.append(selectImgElement);
|
2022-09-30 22:25:35 +08:00
|
|
|
|
} else if (range.startContainer.nodeType === 3 && range.startContainer.parentElement.tagName === "SPAN" &&
|
2022-10-31 15:45:45 +08:00
|
|
|
|
range.startContainer.parentElement.getAttribute("data-type") &&
|
2022-09-30 22:25:35 +08:00
|
|
|
|
range.startContainer.parentElement.isSameNode(range.endContainer.parentElement)) {
|
|
|
|
|
|
// 剪切粗体等字体中的一部分
|
2022-10-03 16:22:18 +08:00
|
|
|
|
const spanElement = range.startContainer.parentElement;
|
2022-09-30 22:25:35 +08:00
|
|
|
|
const attributes = spanElement.attributes;
|
|
|
|
|
|
const newSpanElement = document.createElement("span");
|
|
|
|
|
|
for (let i = 0; i < attributes.length; i++) {
|
|
|
|
|
|
newSpanElement.setAttribute(attributes[i].name, attributes[i].value);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (spanElement.getAttribute("data-type").indexOf("block-ref") > -1 &&
|
|
|
|
|
|
spanElement.getAttribute("data-subtype") === "d") {
|
|
|
|
|
|
// 引用被剪切后需变为静态锚文本
|
|
|
|
|
|
newSpanElement.setAttribute("data-subtype", "s");
|
|
|
|
|
|
spanElement.setAttribute("data-subtype", "s");
|
|
|
|
|
|
}
|
|
|
|
|
|
newSpanElement.textContent = range.toString();
|
|
|
|
|
|
range.deleteContents();
|
|
|
|
|
|
tempElement.append(newSpanElement);
|
2022-08-31 22:29:52 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
if (range.cloneContents().querySelectorAll("td, th").length > 0) {
|
|
|
|
|
|
// 表格内多格子 cut https://github.com/siyuan-note/insider/issues/564
|
|
|
|
|
|
const wbrElement = document.createElement("wbr");
|
|
|
|
|
|
range.insertNode(wbrElement);
|
|
|
|
|
|
range.setStartAfter(wbrElement);
|
|
|
|
|
|
tempElement.append(range.extractContents());
|
|
|
|
|
|
nodeElement.outerHTML = protyle.lute.SpinBlockDOM(nodeElement.outerHTML);
|
|
|
|
|
|
nodeElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`) as HTMLElement;
|
|
|
|
|
|
focusByWbr(nodeElement, range);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const inlineMathElement = hasClosestByAttribute(range.commonAncestorContainer, "data-type", "inline-math");
|
|
|
|
|
|
if (inlineMathElement) {
|
|
|
|
|
|
// 表格内剪切数学公式 https://ld246.com/article/1631708573504
|
|
|
|
|
|
tempElement.append(inlineMathElement);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
tempElement.append(range.extractContents());
|
|
|
|
|
|
let parentElement: Element | false;
|
|
|
|
|
|
// https://ld246.com/article/1647689760545
|
|
|
|
|
|
if (nodeElement.classList.contains("table")) {
|
|
|
|
|
|
parentElement = hasClosestByMatchTag(range.startContainer, "TD") || hasClosestByMatchTag(range.startContainer, "TH");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
parentElement = getContenteditableElement(nodeElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (parentElement) {
|
|
|
|
|
|
// 引用文本剪切 https://ld246.com/article/1647689760545
|
|
|
|
|
|
// 表格多行剪切 https://ld246.com/article/1652603836350
|
2022-11-20 00:21:51 +08:00
|
|
|
|
// 自定义表情的段落剪切后表情丢失 https://ld246.com/article/1668781478724
|
2022-08-31 22:29:52 +08:00
|
|
|
|
Array.from(parentElement.children).forEach(item => {
|
2022-11-20 00:21:51 +08:00
|
|
|
|
if (item.textContent === "" && (item.nodeType === 1 && !["BR", "IMG"].includes(item.tagName))) {
|
2022-08-31 22:29:52 +08:00
|
|
|
|
item.remove();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
html = tempElement.innerHTML;
|
|
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/4321
|
|
|
|
|
|
if (!nodeElement.classList.contains("table")) {
|
|
|
|
|
|
const editableElement = getContenteditableElement(nodeElement);
|
|
|
|
|
|
if (editableElement && editableElement.textContent === "") {
|
|
|
|
|
|
editableElement.innerHTML = "";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
|
|
|
|
|
if (nodeElement.getAttribute("data-type") === "NodeCodeBlock") {
|
|
|
|
|
|
range.insertNode(document.createElement("wbr"));
|
|
|
|
|
|
getContenteditableElement(nodeElement).removeAttribute("data-render");
|
|
|
|
|
|
highlightRender(nodeElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (nodeElement.parentElement.parentElement && !isFoldHeading) {
|
|
|
|
|
|
// 选中 heading 时,使用删除的 transaction
|
|
|
|
|
|
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
protyle.hint.render(protyle);
|
2022-10-16 17:48:52 +08:00
|
|
|
|
event.clipboardData.setData("text/plain", protyle.lute.BlockDOM2StdMd(html).trimEnd()); // 需要 trimEnd,否则 \n 会导致 https://github.com/siyuan-note/siyuan/issues/6218
|
2023-02-03 15:18:54 +08:00
|
|
|
|
event.clipboardData.setData("text/html", protyle.lute.BlockDOM2HTML(html));
|
|
|
|
|
|
event.clipboardData.setData("text/siyuan", html);
|
2022-08-31 22:29:52 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
let beforeContextmenuRange: Range;
|
2022-10-03 18:01:03 +08:00
|
|
|
|
this.element.addEventListener("contextmenu", (event: MouseEvent & { detail: any }) => {
|
2022-08-31 22:29:52 +08:00
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
event.preventDefault();
|
2022-10-05 21:29:56 +08:00
|
|
|
|
const x = event.clientX || event.detail.x;
|
|
|
|
|
|
const y = event.clientY || event.detail.y;
|
2022-08-31 22:29:52 +08:00
|
|
|
|
const selectElements = protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select");
|
|
|
|
|
|
if (selectElements.length > 1) {
|
|
|
|
|
|
// 多选块
|
|
|
|
|
|
hideElements(["util"], protyle);
|
|
|
|
|
|
protyle.gutter.renderMenu(protyle, selectElements[0]);
|
2022-10-03 18:01:03 +08:00
|
|
|
|
window.siyuan.menus.menu.popup({x, y});
|
2022-08-31 22:29:52 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-10-05 21:29:56 +08:00
|
|
|
|
const target = event.detail.target || event.target as HTMLElement;
|
2022-08-31 22:29:52 +08:00
|
|
|
|
const embedElement = hasClosestByAttribute(target, "data-type", "NodeBlockQueryEmbed");
|
|
|
|
|
|
if (embedElement) {
|
|
|
|
|
|
if (getSelection().rangeCount === 0) {
|
|
|
|
|
|
focusSideBlock(embedElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
protyle.gutter.renderMenu(protyle, embedElement);
|
2023-04-10 18:00:36 +08:00
|
|
|
|
/// #if MOBILE
|
|
|
|
|
|
window.siyuan.menus.menu.fullscreen();
|
|
|
|
|
|
/// #else
|
2022-10-03 18:01:03 +08:00
|
|
|
|
window.siyuan.menus.menu.popup({x, y});
|
2023-04-10 18:00:36 +08:00
|
|
|
|
/// #endif
|
2022-08-31 22:29:52 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
protyle.toolbar.range = getEditorRange(protyle.element);
|
2022-11-24 23:57:22 +08:00
|
|
|
|
if (target.tagName === "SPAN" && !protyle.disabled) { // https://ld246.com/article/1665141518103
|
2022-10-07 23:01:03 +08:00
|
|
|
|
const types = protyle.toolbar.getCurrentType(protyle.toolbar.range);
|
2023-03-07 10:42:20 +08:00
|
|
|
|
if (types.length > 0) {
|
|
|
|
|
|
removeSearchMark(target);
|
|
|
|
|
|
}
|
2022-10-07 23:01:03 +08:00
|
|
|
|
if (types.includes("block-ref")) {
|
|
|
|
|
|
refMenu(protyle, target);
|
2022-08-31 22:29:52 +08:00
|
|
|
|
// 阻止 popover
|
|
|
|
|
|
target.setAttribute("prevent-popover", "true");
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
target.removeAttribute("prevent-popover");
|
|
|
|
|
|
}, 620);
|
2022-10-07 23:01:03 +08:00
|
|
|
|
return false;
|
2023-03-07 10:42:20 +08:00
|
|
|
|
} else if (types.includes("file-annotation-ref")) {
|
2022-10-07 23:01:03 +08:00
|
|
|
|
protyle.toolbar.showFileAnnotationRef(protyle, target);
|
|
|
|
|
|
return false;
|
2023-03-07 10:42:20 +08:00
|
|
|
|
} else if (types.includes("tag")) {
|
2023-01-15 22:44:05 +08:00
|
|
|
|
tagMenu(protyle, target);
|
|
|
|
|
|
return false;
|
2023-03-07 10:42:20 +08:00
|
|
|
|
} else if (types.includes("inline-memo")) {
|
2022-10-07 23:01:03 +08:00
|
|
|
|
protyle.toolbar.showRender(protyle, target);
|
|
|
|
|
|
return false;
|
2023-03-07 10:42:20 +08:00
|
|
|
|
} else if (types.includes("a")) {
|
2022-10-07 23:01:03 +08:00
|
|
|
|
linkMenu(protyle, target);
|
2023-01-15 22:44:05 +08:00
|
|
|
|
if (window.siyuan.config.editor.floatWindowMode === 0 &&
|
|
|
|
|
|
target.getAttribute("data-href")?.startsWith("siyuan://blocks")) {
|
2022-10-07 23:01:03 +08:00
|
|
|
|
// 阻止 popover
|
|
|
|
|
|
target.setAttribute("prevent-popover", "true");
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
target.removeAttribute("prevent-popover");
|
|
|
|
|
|
}, 620);
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
2022-08-31 22:29:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-11-24 23:57:22 +08:00
|
|
|
|
if (!protyle.disabled && target.tagName === "IMG" && hasClosestByClassName(target, "img")) {
|
2022-08-31 22:29:52 +08:00
|
|
|
|
imgMenu(protyle, protyle.toolbar.range, target.parentElement.parentElement, {
|
2022-10-03 18:01:03 +08:00
|
|
|
|
clientX: x + 4,
|
|
|
|
|
|
clientY: y
|
2022-08-31 22:29:52 +08:00
|
|
|
|
});
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
const nodeElement = hasClosestBlock(target);
|
|
|
|
|
|
if (!nodeElement) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2022-11-28 18:30:01 +08:00
|
|
|
|
if (!isNotEditBlock(nodeElement) && !nodeElement.classList.contains("protyle-wysiwyg--select") &&
|
2022-10-03 18:24:22 +08:00
|
|
|
|
(isMobile() || event.detail.target || (beforeContextmenuRange && nodeElement.contains(beforeContextmenuRange.startContainer)))
|
|
|
|
|
|
) {
|
2022-08-31 22:29:52 +08:00
|
|
|
|
if (!isMobile() || protyle.toolbar?.element.classList.contains("fn__none")) {
|
|
|
|
|
|
contentMenu(protyle, nodeElement);
|
2022-10-03 18:01:03 +08:00
|
|
|
|
window.siyuan.menus.menu.popup({x, y: y + 13, h: 26});
|
2022-08-31 22:29:52 +08:00
|
|
|
|
protyle.toolbar?.element.classList.add("fn__none");
|
|
|
|
|
|
if (nodeElement.classList.contains("table")) {
|
|
|
|
|
|
nodeElement.querySelector("table").classList.remove("select");
|
|
|
|
|
|
nodeElement.querySelector(".table__select").removeAttribute("style");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (protyle.toolbar.range.toString() === "") {
|
|
|
|
|
|
hideElements(["util"], protyle);
|
2022-09-30 00:12:35 +08:00
|
|
|
|
if (protyle.gutter) {
|
|
|
|
|
|
protyle.gutter.renderMenu(protyle, nodeElement);
|
|
|
|
|
|
}
|
2023-04-10 18:00:36 +08:00
|
|
|
|
/// #if MOBILE
|
|
|
|
|
|
window.siyuan.menus.menu.fullscreen();
|
|
|
|
|
|
/// #else
|
2022-10-03 18:01:03 +08:00
|
|
|
|
window.siyuan.menus.menu.popup({x, y});
|
2023-04-10 18:00:36 +08:00
|
|
|
|
/// #endif
|
2022-08-31 22:29:52 +08:00
|
|
|
|
protyle.toolbar?.element.classList.add("fn__none");
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
this.element.addEventListener("pointerdown", () => {
|
|
|
|
|
|
if (getSelection().rangeCount > 0) {
|
|
|
|
|
|
beforeContextmenuRange = getSelection().getRangeAt(0);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
beforeContextmenuRange = undefined;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2022-09-17 00:53:44 +08:00
|
|
|
|
let preventGetTopHTML = false;
|
2022-08-31 22:29:52 +08:00
|
|
|
|
this.element.addEventListener("mousewheel", (event: WheelEvent) => {
|
|
|
|
|
|
// https://ld246.com/article/1648865235549
|
2022-09-17 00:53:44 +08:00
|
|
|
|
// 不能使用上一版本的 timeStamp,否则一直滚动将导致间隔不够 https://ld246.com/article/1662852664926
|
|
|
|
|
|
if (!preventGetTopHTML &&
|
2022-08-31 22:29:52 +08:00
|
|
|
|
event.deltaY < 0 && !protyle.scroll.element.classList.contains("fn__none") &&
|
|
|
|
|
|
protyle.contentElement.clientHeight === protyle.contentElement.scrollHeight &&
|
|
|
|
|
|
protyle.wysiwyg.element.firstElementChild.getAttribute("data-eof") !== "true") {
|
|
|
|
|
|
fetchPost("/api/filetree/getDoc", {
|
|
|
|
|
|
id: protyle.wysiwyg.element.firstElementChild.getAttribute("data-node-id"),
|
|
|
|
|
|
mode: 1,
|
|
|
|
|
|
k: protyle.options.key || "",
|
2022-10-30 23:13:41 +08:00
|
|
|
|
size: window.siyuan.config.editor.dynamicLoadBlocks,
|
2022-08-31 22:29:52 +08:00
|
|
|
|
}, getResponse => {
|
2022-09-17 00:53:44 +08:00
|
|
|
|
preventGetTopHTML = false;
|
2022-08-31 22:29:52 +08:00
|
|
|
|
onGet(getResponse, protyle, [Constants.CB_GET_BEFORE, Constants.CB_GET_UNCHANGEID]);
|
|
|
|
|
|
});
|
2022-09-17 00:53:44 +08:00
|
|
|
|
preventGetTopHTML = true;
|
2022-08-31 22:29:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (event.deltaX === 0) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/4099
|
|
|
|
|
|
const tableElement = hasClosestByClassName(event.target as HTMLElement, "table");
|
|
|
|
|
|
if (tableElement) {
|
|
|
|
|
|
const tableSelectElement = tableElement.querySelector(".table__select") as HTMLElement;
|
|
|
|
|
|
if (tableSelectElement?.style.width) {
|
|
|
|
|
|
tableSelectElement.removeAttribute("style");
|
|
|
|
|
|
window.siyuan.menus.menu.remove();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}, {passive: true});
|
|
|
|
|
|
|
|
|
|
|
|
let overAttr = false;
|
|
|
|
|
|
this.element.addEventListener("mouseover", (event: MouseEvent & { target: Element }) => {
|
|
|
|
|
|
const attrElement = hasClosestByClassName(event.target, "protyle-attr");
|
|
|
|
|
|
if (attrElement) {
|
|
|
|
|
|
overAttr = true;
|
|
|
|
|
|
attrElement.parentElement.classList.add("protyle-wysiwyg--hl");
|
|
|
|
|
|
return;
|
|
|
|
|
|
} else if (overAttr) {
|
|
|
|
|
|
const hlElement = protyle.wysiwyg.element.querySelector(".protyle-wysiwyg--hl");
|
|
|
|
|
|
if (hlElement) {
|
|
|
|
|
|
hlElement.classList.remove("protyle-wysiwyg--hl");
|
|
|
|
|
|
}
|
|
|
|
|
|
overAttr = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (hasClosestByClassName(event.target, "protyle-action") || !protyle.options.render.gutter) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
const nodeElement = hasClosestBlock(event.target);
|
|
|
|
|
|
if (nodeElement && (nodeElement.classList.contains("list") || nodeElement.classList.contains("li"))) {
|
|
|
|
|
|
// 光标在列表下部应显示右侧的元素,而不是列表本身。放在 globalShortcut 中的 mousemove 下处理
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (nodeElement) {
|
|
|
|
|
|
const embedElement = hasClosestByAttribute(nodeElement, "data-type", "NodeBlockQueryEmbed");
|
|
|
|
|
|
if (embedElement) {
|
2022-10-25 00:23:42 +08:00
|
|
|
|
protyle.gutter.render(protyle, embedElement, this.element);
|
2022-08-31 22:29:52 +08:00
|
|
|
|
} else {
|
2022-10-25 00:23:42 +08:00
|
|
|
|
protyle.gutter.render(protyle, nodeElement, this.element);
|
2022-08-31 22:29:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
|
|
|
this.element.addEventListener("paste", (event: ClipboardEvent & { target: HTMLElement }) => {
|
2022-10-31 15:52:01 +08:00
|
|
|
|
if (protyle.disabled) {
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-10-27 23:10:29 +08:00
|
|
|
|
window.siyuan.ctrlIsPressed = false; // https://github.com/siyuan-note/siyuan/issues/6373
|
2022-05-26 15:18:53 +08:00
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/4600
|
|
|
|
|
|
if (event.target.tagName === "PROTYLE-HTML") {
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
paste(protyle, event);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 输入法测试点 https://github.com/siyuan-note/siyuan/issues/3027
|
|
|
|
|
|
let isComposition = false; // for iPhone
|
|
|
|
|
|
this.element.addEventListener("compositionstart", (event) => {
|
|
|
|
|
|
// 搜狗输入法划选输入后无 data https://github.com/siyuan-note/siyuan/issues/4672
|
|
|
|
|
|
const range = getEditorRange(protyle.wysiwyg.element);
|
|
|
|
|
|
const nodeElement = hasClosestBlock(range.startContainer);
|
|
|
|
|
|
if (nodeElement && typeof protyle.wysiwyg.lastHTMLs[nodeElement.getAttribute("data-node-id")] === "undefined") {
|
|
|
|
|
|
range.insertNode(document.createElement("wbr"));
|
|
|
|
|
|
protyle.wysiwyg.lastHTMLs[nodeElement.getAttribute("data-node-id")] = nodeElement.outerHTML;
|
|
|
|
|
|
nodeElement.querySelector("wbr").remove();
|
|
|
|
|
|
}
|
|
|
|
|
|
isComposition = true;
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
this.element.addEventListener("compositionend", (event: InputEvent) => {
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
isComposition = false;
|
|
|
|
|
|
const range = getEditorRange(this.element);
|
|
|
|
|
|
const blockElement = hasClosestBlock(range.startContainer);
|
|
|
|
|
|
if (blockElement && blockElement.getAttribute("data-type") === "NodeHTMLBlock") {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!blockElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if ("" !== event.data) {
|
|
|
|
|
|
this.escapeInline(protyle, range, event);
|
|
|
|
|
|
// 小鹤音形 ;k 不能使用 setTimeout;
|
|
|
|
|
|
// wysiwyg.element contenteditable 为 false 时,连拼 needRender 必须为 false
|
|
|
|
|
|
// hr 渲染;任务列表、粗体、数学公示结尾 needRender 必须为 true
|
|
|
|
|
|
input(protyle, blockElement, range, true);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const id = blockElement.getAttribute("data-node-id");
|
|
|
|
|
|
if (protyle.wysiwyg.lastHTMLs[id]) {
|
|
|
|
|
|
updateTransaction(protyle, id, blockElement.outerHTML, protyle.wysiwyg.lastHTMLs[id]);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
this.element.addEventListener("input", (event: InputEvent) => {
|
|
|
|
|
|
const target = event.target as HTMLElement;
|
2022-08-15 17:36:14 +08:00
|
|
|
|
if (target.tagName === "VIDEO" || target.tagName === "AUDIO" || event.inputType === "historyRedo") {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-08-15 17:30:35 +08:00
|
|
|
|
if (event.inputType === "historyUndo") {
|
2022-11-09 00:19:47 +08:00
|
|
|
|
/// #if !BROWSER
|
2022-08-15 17:30:35 +08:00
|
|
|
|
getCurrentWindow().webContents.redo();
|
2022-11-09 00:19:47 +08:00
|
|
|
|
/// #endif
|
2022-08-15 17:30:35 +08:00
|
|
|
|
window.siyuan.menus.menu.remove();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
const range = getEditorRange(this.element);
|
|
|
|
|
|
const blockElement = hasClosestBlock(range.startContainer);
|
|
|
|
|
|
if (!blockElement) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (blockElement && blockElement.getAttribute("data-type") === "NodeHTMLBlock") {
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-02-09 12:37:04 +08:00
|
|
|
|
if ([":", "(", "【", "(", "[", "{", "「", "#", "/", "、"].includes(event.data)) {
|
|
|
|
|
|
protyle.hint.enableExtend = true;
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (event.isComposing || isComposition ||
|
|
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/337 编辑器内容拖拽问题
|
|
|
|
|
|
event.inputType === "deleteByDrag" || event.inputType === "insertFromDrop"
|
|
|
|
|
|
) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
this.escapeInline(protyle, range, event);
|
|
|
|
|
|
if (/^\d{1}$/.test(event.data) || event.data === "‘" || event.data === "“") {
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
input(protyle, blockElement, range, true); // 搜狗拼音数字后面句号变为点;Mac 反向双引号无法输入
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
input(protyle, blockElement, range, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
this.element.addEventListener("keyup", (event) => {
|
|
|
|
|
|
const range = getEditorRange(this.element).cloneRange();
|
|
|
|
|
|
if (event.key !== "PageUp" && event.key !== "PageDown" && event.key !== "Home" && event.key !== "End" && event.key.indexOf("Arrow") === -1 &&
|
|
|
|
|
|
event.key !== "Alt" && event.key !== "Shift" && event.key !== "CapsLock" && event.key !== "Escape" && event.key !== "Meta" && !/^F\d{1,2}$/.test(event.key) &&
|
|
|
|
|
|
(!event.isComposing || (event.isComposing && range.toString() !== "")) // https://github.com/siyuan-note/siyuan/issues/4341
|
|
|
|
|
|
) {
|
|
|
|
|
|
// 搜狗输入法不走 keydown,需重新记录历史状态
|
|
|
|
|
|
const nodeElement = hasClosestBlock(range.startContainer);
|
|
|
|
|
|
if (nodeElement && typeof protyle.wysiwyg.lastHTMLs[nodeElement.getAttribute("data-node-id")] === "undefined") {
|
|
|
|
|
|
range.insertNode(document.createElement("wbr"));
|
|
|
|
|
|
protyle.wysiwyg.lastHTMLs[nodeElement.getAttribute("data-node-id")] = nodeElement.outerHTML;
|
|
|
|
|
|
nodeElement.querySelector("wbr").remove();
|
|
|
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 需放在 lastHTMLs 后,否则 https://github.com/siyuan-note/siyuan/issues/4388
|
|
|
|
|
|
if (this.preventKeyup) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ((event.shiftKey || isCtrl(event)) && !event.isComposing && range.toString() !== "") {
|
|
|
|
|
|
// 工具栏
|
|
|
|
|
|
protyle.toolbar.render(protyle, range, event);
|
2022-06-27 23:48:24 +08:00
|
|
|
|
countSelectWord(range);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (event.eventPhase !== 3 && !event.shiftKey && (event.key.indexOf("Arrow") > -1 || event.key === "Home" || event.key === "End" || event.key === "PageUp" || event.key === "PageDown") && !event.isComposing) {
|
|
|
|
|
|
const nodeElement = hasClosestBlock(range.startContainer);
|
|
|
|
|
|
if (nodeElement) {
|
|
|
|
|
|
this.setEmptyOutline(protyle, nodeElement);
|
2022-10-07 20:56:19 +08:00
|
|
|
|
if (range.toString() === "") {
|
|
|
|
|
|
countSelectWord(range, protyle.block.rootID);
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
this.element.addEventListener("dblclick", (event: MouseEvent & { target: HTMLElement }) => {
|
2022-11-20 22:26:55 +08:00
|
|
|
|
if (event.target.tagName === "IMG" && !event.target.classList.contains("emoji")) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
previewImage((event.target as HTMLImageElement).src, protyle.block.rootID);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
let shiftStartElement: HTMLElement;
|
|
|
|
|
|
this.element.addEventListener("click", (event: MouseEvent & { target: HTMLElement }) => {
|
|
|
|
|
|
hideElements(["hint", "util"], protyle);
|
2023-02-11 21:34:42 +08:00
|
|
|
|
const ctrlIsPressed = event.metaKey || event.ctrlKey;
|
2022-10-28 12:27:44 +08:00
|
|
|
|
/// #if !MOBILE
|
2022-09-30 18:05:24 +08:00
|
|
|
|
const backlinkBreadcrumbItemElement = hasClosestByClassName(event.target, "protyle-breadcrumb__item");
|
2022-09-30 00:12:35 +08:00
|
|
|
|
if (backlinkBreadcrumbItemElement) {
|
2022-10-30 00:16:48 +08:00
|
|
|
|
const breadcrumbId = backlinkBreadcrumbItemElement.getAttribute("data-id");
|
2022-10-28 11:46:44 +08:00
|
|
|
|
if (breadcrumbId) {
|
2023-02-09 11:16:34 +08:00
|
|
|
|
if (ctrlIsPressed) {
|
2023-04-12 15:06:57 +08:00
|
|
|
|
fetchPost("/api/block/checkBlockFold", {id: breadcrumbId}, (foldResponse) => {
|
|
|
|
|
|
openFileById({
|
|
|
|
|
|
id: breadcrumbId,
|
|
|
|
|
|
action: foldResponse.data ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT],
|
|
|
|
|
|
zoomIn: foldResponse.data
|
|
|
|
|
|
});
|
2023-02-02 15:33:34 +08:00
|
|
|
|
});
|
2022-10-28 11:46:44 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
loadBreadcrumb(protyle, backlinkBreadcrumbItemElement);
|
|
|
|
|
|
}
|
2022-09-30 18:05:24 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// 引用标题时的更多加载
|
2022-10-03 16:22:18 +08:00
|
|
|
|
getBacklinkHeadingMore(backlinkBreadcrumbItemElement);
|
2022-09-30 18:05:24 +08:00
|
|
|
|
}
|
2022-09-30 00:13:07 +08:00
|
|
|
|
event.stopPropagation();
|
2022-09-30 00:12:35 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-10-28 12:27:44 +08:00
|
|
|
|
/// #endif
|
2023-02-09 11:16:34 +08:00
|
|
|
|
if (!event.shiftKey) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
shiftStartElement = undefined;
|
|
|
|
|
|
}
|
|
|
|
|
|
this.setEmptyOutline(protyle, event.target);
|
|
|
|
|
|
const tableElement = hasClosestByClassName(event.target, "table");
|
|
|
|
|
|
this.element.querySelectorAll(".table").forEach(item => {
|
|
|
|
|
|
if (!tableElement || !item.isSameNode(tableElement)) {
|
|
|
|
|
|
item.querySelector("table").classList.remove("select");
|
|
|
|
|
|
item.querySelector(".table__select").removeAttribute("style");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (tableElement && tableElement.isSameNode(item) && item.querySelector(".table__select").getAttribute("style")) {
|
|
|
|
|
|
// 防止合并单元格的菜单消失
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2023-02-23 14:06:25 +08:00
|
|
|
|
// 面包屑定位,需至于前,否则 return 的元素就无法进行面包屑定位
|
|
|
|
|
|
if (protyle.options.render.breadcrumb) {
|
|
|
|
|
|
protyle.breadcrumb.render(protyle);
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
const range = getEditorRange(this.element);
|
|
|
|
|
|
// 需放在嵌入块之前,否则嵌入块内的引用、链接、pdf 双链无法点击打开 https://ld246.com/article/1630479789513
|
|
|
|
|
|
const blockRefElement = hasClosestByAttribute(event.target, "data-type", "block-ref");
|
|
|
|
|
|
const aElement = hasClosestByAttribute(event.target, "data-type", "a");
|
|
|
|
|
|
if (blockRefElement ||
|
|
|
|
|
|
(aElement && aElement.getAttribute("data-href").startsWith("siyuan://blocks/"))
|
|
|
|
|
|
) {
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
hideElements(["dialog", "toolbar"], protyle);
|
2023-02-09 11:16:34 +08:00
|
|
|
|
if (range.toString() !== "" && !event.shiftKey) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
// 选择不打开引用
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
let refBlockId: string;
|
|
|
|
|
|
if (blockRefElement) {
|
|
|
|
|
|
refBlockId = blockRefElement.getAttribute("data-id");
|
|
|
|
|
|
} else if (aElement) {
|
|
|
|
|
|
refBlockId = aElement.getAttribute("data-href").substring(16, 38);
|
|
|
|
|
|
}
|
2022-06-01 10:12:14 +08:00
|
|
|
|
|
|
|
|
|
|
fetchPost("/api/block/checkBlockFold", {id: refBlockId}, (foldResponse) => {
|
2022-06-29 15:36:44 +08:00
|
|
|
|
/// #if MOBILE
|
2022-08-07 09:26:08 +08:00
|
|
|
|
openMobileFileById(refBlockId, foldResponse.data ? [Constants.CB_GET_ALL, Constants.CB_GET_HL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT]);
|
2023-04-02 12:26:14 +08:00
|
|
|
|
activeBlur();
|
|
|
|
|
|
hideKeyboardToolbar();
|
2022-06-29 15:36:44 +08:00
|
|
|
|
/// #else
|
2023-01-09 22:26:16 +08:00
|
|
|
|
if (aElement) {
|
2023-01-10 22:54:25 +08:00
|
|
|
|
window.open(aElement.getAttribute("data-href"));
|
2023-01-09 22:26:16 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-02-09 11:16:34 +08:00
|
|
|
|
if (event.shiftKey) {
|
2022-06-29 15:36:44 +08:00
|
|
|
|
openFileById({
|
|
|
|
|
|
id: refBlockId,
|
|
|
|
|
|
position: "bottom",
|
2022-08-07 09:26:08 +08:00
|
|
|
|
action: foldResponse.data ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT],
|
2022-06-29 15:36:44 +08:00
|
|
|
|
zoomIn: foldResponse.data
|
|
|
|
|
|
});
|
2023-02-09 11:16:34 +08:00
|
|
|
|
} else if (event.altKey) {
|
2022-06-29 15:36:44 +08:00
|
|
|
|
openFileById({
|
|
|
|
|
|
id: refBlockId,
|
|
|
|
|
|
position: "right",
|
2022-08-07 09:26:08 +08:00
|
|
|
|
action: foldResponse.data ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT],
|
2022-06-29 15:36:44 +08:00
|
|
|
|
zoomIn: foldResponse.data
|
|
|
|
|
|
});
|
2023-02-09 11:16:34 +08:00
|
|
|
|
} else if (ctrlIsPressed) {
|
2022-06-29 15:36:44 +08:00
|
|
|
|
openFileById({
|
|
|
|
|
|
id: refBlockId,
|
2022-08-07 09:26:08 +08:00
|
|
|
|
action: foldResponse.data ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT],
|
2022-06-29 15:36:44 +08:00
|
|
|
|
keepCursor: true,
|
|
|
|
|
|
zoomIn: foldResponse.data
|
|
|
|
|
|
});
|
2022-06-01 10:12:14 +08:00
|
|
|
|
} else {
|
2022-06-29 15:36:44 +08:00
|
|
|
|
openFileById({
|
|
|
|
|
|
id: refBlockId,
|
2022-08-07 09:26:08 +08:00
|
|
|
|
action: foldResponse.data ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT],
|
2022-06-29 15:36:44 +08:00
|
|
|
|
zoomIn: foldResponse.data
|
|
|
|
|
|
});
|
2022-06-01 10:12:14 +08:00
|
|
|
|
}
|
2022-06-29 15:36:44 +08:00
|
|
|
|
/// #endif
|
2022-06-01 10:12:14 +08:00
|
|
|
|
});
|
2022-06-29 20:25:30 +08:00
|
|
|
|
/// #if !MOBILE
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (protyle.model) {
|
|
|
|
|
|
// 打开双链需记录到后退中 https://github.com/siyuan-note/insider/issues/801
|
|
|
|
|
|
let blockElement: HTMLElement | false;
|
|
|
|
|
|
if (blockRefElement) {
|
|
|
|
|
|
blockElement = hasClosestBlock(blockRefElement);
|
|
|
|
|
|
} else if (aElement) {
|
|
|
|
|
|
blockElement = hasClosestBlock(aElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (blockElement) {
|
|
|
|
|
|
pushBack(protyle, getEditorRange(this.element), blockElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-06-29 20:25:30 +08:00
|
|
|
|
/// #endif
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const fileElement = hasClosestByAttribute(event.target, "data-type", "file-annotation-ref");
|
|
|
|
|
|
if (fileElement && range.toString() === "") {
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
const fileIds = fileElement.getAttribute("data-id").split("/");
|
|
|
|
|
|
const linkAddress = `assets/${fileIds[1]}`;
|
2022-06-29 15:36:44 +08:00
|
|
|
|
/// #if MOBILE
|
2022-07-27 07:20:27 +08:00
|
|
|
|
openByMobile(linkAddress);
|
2022-06-29 15:36:44 +08:00
|
|
|
|
/// #else
|
2023-02-09 11:16:34 +08:00
|
|
|
|
if (ctrlIsPressed) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
openBy(linkAddress, "folder");
|
2023-02-09 11:16:34 +08:00
|
|
|
|
} else if (event.shiftKey) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
openBy(linkAddress, "app");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
openAsset(linkAddress, fileIds[2], "right");
|
|
|
|
|
|
}
|
2022-06-29 15:36:44 +08:00
|
|
|
|
/// #endif
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-09 11:16:34 +08:00
|
|
|
|
if (aElement && !event.altKey) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
const linkAddress = aElement.getAttribute("data-href");
|
2022-06-29 15:36:44 +08:00
|
|
|
|
/// #if MOBILE
|
2022-07-27 07:17:45 +08:00
|
|
|
|
openByMobile(linkAddress);
|
2022-06-29 15:36:44 +08:00
|
|
|
|
/// #else
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (isLocalPath(linkAddress)) {
|
|
|
|
|
|
const linkPathname = linkAddress.split("?page")[0];
|
|
|
|
|
|
if (Constants.SIYUAN_ASSETS_EXTS.includes(pathPosix().extname(linkPathname)) &&
|
|
|
|
|
|
(!linkPathname.endsWith(".pdf") ||
|
|
|
|
|
|
(linkPathname.endsWith(".pdf") && !linkAddress.startsWith("file://")))
|
|
|
|
|
|
) {
|
2023-02-09 11:16:34 +08:00
|
|
|
|
if (ctrlIsPressed) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
openBy(linkAddress, "folder");
|
2023-02-09 11:16:34 +08:00
|
|
|
|
} else if (event.shiftKey) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
openBy(linkAddress, "app");
|
|
|
|
|
|
} else {
|
2023-01-17 00:23:21 +08:00
|
|
|
|
openAsset(linkPathname, parseInt(getSearch("page", linkAddress)), "right");
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
/// #if !BROWSER
|
2023-02-09 11:16:34 +08:00
|
|
|
|
if (ctrlIsPressed) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
openBy(linkAddress, "folder");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
openBy(linkAddress, "app");
|
|
|
|
|
|
}
|
|
|
|
|
|
/// #else
|
2022-07-27 07:20:27 +08:00
|
|
|
|
openByMobile(linkAddress);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
/// #endif
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
/// #if !BROWSER
|
|
|
|
|
|
shell.openExternal(linkAddress).catch((e) => {
|
2022-06-29 10:35:35 +08:00
|
|
|
|
showMessage(e);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
});
|
|
|
|
|
|
/// #else
|
2022-07-27 07:20:27 +08:00
|
|
|
|
openByMobile(linkAddress);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
/// #endif
|
|
|
|
|
|
}
|
2022-06-29 15:36:44 +08:00
|
|
|
|
/// #endif
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-06-30 15:56:44 +08:00
|
|
|
|
/// #if !MOBILE
|
2022-05-26 15:18:53 +08:00
|
|
|
|
const tagElement = hasClosestByAttribute(event.target, "data-type", "tag");
|
2023-02-09 11:16:34 +08:00
|
|
|
|
if (tagElement && !event.altKey && protyle.model) {
|
|
|
|
|
|
openGlobalSearch(`#${tagElement.textContent}#`, !ctrlIsPressed);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
hideElements(["dialog"]);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-06-30 15:56:44 +08:00
|
|
|
|
/// #endif
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
|
|
|
const embedItemElement = hasClosestByClassName(event.target, "protyle-wysiwyg__embed");
|
|
|
|
|
|
if (embedItemElement) {
|
2022-06-23 01:19:04 +08:00
|
|
|
|
const embedId = embedItemElement.getAttribute("data-id");
|
2022-06-29 15:36:44 +08:00
|
|
|
|
/// #if MOBILE
|
2022-08-06 01:02:55 +08:00
|
|
|
|
openMobileFileById(embedId, [Constants.CB_GET_ALL]);
|
2023-04-02 12:26:14 +08:00
|
|
|
|
activeBlur();
|
|
|
|
|
|
hideKeyboardToolbar();
|
2022-06-29 15:36:44 +08:00
|
|
|
|
/// #else
|
2023-02-09 11:16:34 +08:00
|
|
|
|
if (event.shiftKey) {
|
2022-06-19 00:34:15 +08:00
|
|
|
|
openFileById({
|
|
|
|
|
|
id: embedId,
|
|
|
|
|
|
position: "bottom",
|
|
|
|
|
|
action: [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL],
|
|
|
|
|
|
zoomIn: true
|
|
|
|
|
|
});
|
2023-02-09 11:16:34 +08:00
|
|
|
|
} else if (event.altKey) {
|
2022-06-19 00:34:15 +08:00
|
|
|
|
openFileById({
|
|
|
|
|
|
id: embedId,
|
|
|
|
|
|
position: "right",
|
|
|
|
|
|
action: [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL],
|
|
|
|
|
|
zoomIn: true
|
|
|
|
|
|
});
|
2023-02-09 11:16:34 +08:00
|
|
|
|
} else if (ctrlIsPressed) {
|
2022-06-19 00:34:15 +08:00
|
|
|
|
openFileById({
|
|
|
|
|
|
id: embedId,
|
|
|
|
|
|
action: [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL],
|
|
|
|
|
|
keepCursor: true,
|
|
|
|
|
|
zoomIn: true
|
|
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} else if (!protyle.disabled) {
|
|
|
|
|
|
window.siyuan.blockPanels.push(new BlockPanel({
|
|
|
|
|
|
targetElement: embedItemElement,
|
2022-06-19 00:34:15 +08:00
|
|
|
|
nodeIds: [embedId],
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}));
|
|
|
|
|
|
}
|
2022-06-29 15:36:44 +08:00
|
|
|
|
/// #endif
|
2022-05-26 15:18:53 +08:00
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-12 19:02:35 +08:00
|
|
|
|
if (commonClick(event, protyle)) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (hasTopClosestByClassName(event.target, "protyle-action__copy")) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const editElement = hasClosestByClassName(event.target, "protyle-action__edit");
|
|
|
|
|
|
if (editElement && !protyle.disabled) {
|
|
|
|
|
|
protyle.toolbar.showRender(protyle, editElement.parentElement.parentElement);
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const menuElement = hasClosestByClassName(event.target, "protyle-action__menu");
|
2022-10-17 09:49:59 +08:00
|
|
|
|
if (menuElement) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
protyle.gutter.renderMenu(protyle, menuElement.parentElement.parentElement);
|
2023-04-10 18:00:36 +08:00
|
|
|
|
/// #if MOBILE
|
|
|
|
|
|
window.siyuan.menus.menu.fullscreen();
|
|
|
|
|
|
/// #else
|
2022-05-26 15:18:53 +08:00
|
|
|
|
const rect = menuElement.getBoundingClientRect();
|
2022-08-02 11:13:11 +08:00
|
|
|
|
window.siyuan.menus.menu.popup({
|
|
|
|
|
|
x: rect.left,
|
|
|
|
|
|
y: rect.top
|
2022-08-02 11:15:18 +08:00
|
|
|
|
}, true);
|
2023-04-10 18:00:36 +08:00
|
|
|
|
/// #endif
|
2022-05-26 15:18:53 +08:00
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const reloadElement = hasClosestByClassName(event.target, "protyle-action__reload");
|
|
|
|
|
|
if (reloadElement) {
|
|
|
|
|
|
const embedReloadElement = hasClosestByAttribute(reloadElement, "data-type", "NodeBlockQueryEmbed");
|
|
|
|
|
|
if (embedReloadElement) {
|
|
|
|
|
|
embedReloadElement.removeAttribute("data-render");
|
|
|
|
|
|
blockRender(protyle, embedReloadElement);
|
|
|
|
|
|
}
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const languageElement = hasClosestByClassName(event.target, "protyle-action__language");
|
|
|
|
|
|
if (languageElement && !protyle.disabled) {
|
|
|
|
|
|
protyle.toolbar.showCodeLanguage(protyle, languageElement);
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 需放在属性后,否则数学公式无法点击属性;需放在 action 后,否则嵌入块的的 action 无法打开;需放在嵌入块后,否则嵌入块中的数学公式会被打开
|
|
|
|
|
|
const mathElement = hasClosestByAttribute(event.target, "data-subtype", "math");
|
2023-02-09 11:16:34 +08:00
|
|
|
|
if (!event.shiftKey && !ctrlIsPressed && mathElement && !protyle.disabled) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
protyle.toolbar.showRender(protyle, mathElement);
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const actionElement = hasClosestByClassName(event.target, "protyle-action");
|
|
|
|
|
|
if (actionElement) {
|
|
|
|
|
|
const type = actionElement.parentElement.parentElement.getAttribute("data-type");
|
|
|
|
|
|
if (type === "img" && !protyle.disabled) {
|
|
|
|
|
|
imgMenu(protyle, range, actionElement.parentElement.parentElement, {
|
2022-06-09 11:28:35 +08:00
|
|
|
|
clientX: event.clientX + 4,
|
2022-05-26 15:18:53 +08:00
|
|
|
|
clientY: event.clientY
|
|
|
|
|
|
});
|
|
|
|
|
|
} else if (!protyle.disabled && actionElement.parentElement.classList.contains("li")) {
|
2023-02-09 11:16:34 +08:00
|
|
|
|
if (event.altKey) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
// 展开/折叠当前层级的所有列表项
|
|
|
|
|
|
if (actionElement.parentElement.parentElement.classList.contains("protyle-wysiwyg")) {
|
|
|
|
|
|
// 缩放列表项 https://ld246.com/article/1653123034794
|
|
|
|
|
|
setFold(protyle, actionElement.parentElement);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
let hasFold = true;
|
|
|
|
|
|
const oldHTML = actionElement.parentElement.parentElement.outerHTML;
|
|
|
|
|
|
Array.from(actionElement.parentElement.parentElement.children).find((listItemElement) => {
|
|
|
|
|
|
if (listItemElement.classList.contains("li")) {
|
|
|
|
|
|
if (listItemElement.getAttribute("fold") !== "1" && listItemElement.childElementCount > 3) {
|
|
|
|
|
|
hasFold = false;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
Array.from(actionElement.parentElement.parentElement.children).find((listItemElement) => {
|
|
|
|
|
|
if (listItemElement.classList.contains("li")) {
|
|
|
|
|
|
if (hasFold) {
|
|
|
|
|
|
listItemElement.removeAttribute("fold");
|
|
|
|
|
|
} else if (listItemElement.childElementCount > 3) {
|
|
|
|
|
|
listItemElement.setAttribute("fold", "1");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
updateTransaction(protyle, actionElement.parentElement.parentElement.getAttribute("data-node-id"), actionElement.parentElement.parentElement.outerHTML, oldHTML);
|
|
|
|
|
|
}
|
|
|
|
|
|
hideElements(["gutter"], protyle);
|
2023-02-09 11:16:34 +08:00
|
|
|
|
} else if (event.shiftKey) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
openAttr(actionElement.parentElement, protyle);
|
2023-02-09 11:16:34 +08:00
|
|
|
|
} else if (ctrlIsPressed) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
zoomOut(protyle, actionElement.parentElement.getAttribute("data-node-id"));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (actionElement.classList.contains("protyle-action--task")) {
|
|
|
|
|
|
const html = actionElement.parentElement.outerHTML;
|
|
|
|
|
|
if (actionElement.parentElement.classList.contains("protyle-task--done")) {
|
|
|
|
|
|
actionElement.querySelector("use").setAttribute("xlink:href", "#iconUncheck");
|
|
|
|
|
|
actionElement.parentElement.classList.remove("protyle-task--done");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
actionElement.querySelector("use").setAttribute("xlink:href", "#iconCheck");
|
|
|
|
|
|
actionElement.parentElement.classList.add("protyle-task--done");
|
|
|
|
|
|
}
|
|
|
|
|
|
actionElement.parentElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
|
|
|
|
|
|
updateTransaction(protyle, actionElement.parentElement.getAttribute("data-node-id"), actionElement.parentElement.outerHTML, html);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
protyle.gutter.renderMenu(protyle, actionElement.parentElement);
|
2023-04-10 18:00:36 +08:00
|
|
|
|
/// #if MOBILE
|
|
|
|
|
|
window.siyuan.menus.menu.fullscreen();
|
|
|
|
|
|
/// #else
|
2022-08-02 11:13:11 +08:00
|
|
|
|
window.siyuan.menus.menu.popup({
|
|
|
|
|
|
x: event.clientX - 16,
|
|
|
|
|
|
y: event.clientY - 16
|
2022-08-02 11:15:18 +08:00
|
|
|
|
}, true);
|
2023-04-10 18:00:36 +08:00
|
|
|
|
/// #endif
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const selectElement = hasClosestByClassName(event.target, "hr") ||
|
|
|
|
|
|
hasClosestByClassName(event.target, "iframe");
|
2023-02-09 11:16:34 +08:00
|
|
|
|
if (!event.shiftKey && !ctrlIsPressed && selectElement) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
selectElement.classList.add("protyle-wysiwyg--select");
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const imgElement = hasTopClosestByClassName(event.target, "img");
|
2023-02-09 11:16:34 +08:00
|
|
|
|
if (!event.shiftKey && !ctrlIsPressed && imgElement) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
imgElement.classList.add("img--select");
|
|
|
|
|
|
range.setStartAfter(imgElement);
|
|
|
|
|
|
range.collapse(true);
|
|
|
|
|
|
focusByRange(range);
|
2023-02-23 14:06:25 +08:00
|
|
|
|
// 需等待 range 更新再次进行渲染
|
|
|
|
|
|
if (protyle.options.render.breadcrumb) {
|
|
|
|
|
|
protyle.breadcrumb.render(protyle);
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 点击空白
|
|
|
|
|
|
if (event.target.contains(this.element) && this.element.lastElementChild && !protyle.disabled) {
|
|
|
|
|
|
const lastRect = this.element.lastElementChild.getBoundingClientRect();
|
|
|
|
|
|
if (event.y > lastRect.bottom) {
|
|
|
|
|
|
const lastEditElement = getContenteditableElement(getLastBlock(this.element.lastElementChild));
|
|
|
|
|
|
if (!lastEditElement ||
|
|
|
|
|
|
(this.element.lastElementChild.getAttribute("data-type") !== "NodeParagraph" && protyle.wysiwyg.element.getAttribute("data-doc-type") !== "NodeListItem") ||
|
2023-02-23 13:37:19 +08:00
|
|
|
|
(this.element.lastElementChild.getAttribute("data-type") === "NodeParagraph" && getContenteditableElement(lastEditElement).innerHTML !== "")) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
const emptyElement = genEmptyElement(false, false);
|
|
|
|
|
|
this.element.insertAdjacentElement("beforeend", emptyElement);
|
|
|
|
|
|
transaction(protyle, [{
|
|
|
|
|
|
action: "insert",
|
|
|
|
|
|
data: emptyElement.outerHTML,
|
|
|
|
|
|
id: emptyElement.getAttribute("data-node-id"),
|
|
|
|
|
|
previousID: emptyElement.previousElementSibling.getAttribute("data-node-id"),
|
|
|
|
|
|
parentID: protyle.block.parentID
|
|
|
|
|
|
}], [{
|
|
|
|
|
|
action: "delete",
|
|
|
|
|
|
id: emptyElement.getAttribute("data-node-id")
|
|
|
|
|
|
}]);
|
|
|
|
|
|
const emptyEditElement = getContenteditableElement(emptyElement) as HTMLInputElement;
|
|
|
|
|
|
range.selectNodeContents(emptyEditElement);
|
|
|
|
|
|
range.collapse(true);
|
2022-09-18 17:51:19 +08:00
|
|
|
|
focusByRange(range);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} else if (lastEditElement) {
|
|
|
|
|
|
range.selectNodeContents(lastEditElement);
|
|
|
|
|
|
range.collapse(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
// 选中后,在选中的文字上点击需等待 range 更新
|
|
|
|
|
|
const newRange = getEditorRange(this.element);
|
2023-03-01 18:58:39 +08:00
|
|
|
|
/// #if !MOBILE
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (newRange.toString().replace(Constants.ZWSP, "") !== "") {
|
|
|
|
|
|
protyle.toolbar.render(protyle, newRange);
|
2023-03-01 18:58:39 +08:00
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
hideElements(["toolbar"], protyle);
|
|
|
|
|
|
}
|
2023-03-01 18:58:39 +08:00
|
|
|
|
/// #endif
|
2022-06-28 17:51:51 +08:00
|
|
|
|
if (!protyle.wysiwyg.element.querySelector(".protyle-wysiwyg--select")) {
|
2022-10-07 20:56:19 +08:00
|
|
|
|
countSelectWord(newRange, protyle.block.rootID);
|
2022-06-28 17:51:51 +08:00
|
|
|
|
}
|
2022-09-17 22:04:07 +08:00
|
|
|
|
if (getSelection().rangeCount === 0) {
|
|
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/5901
|
2022-09-18 17:51:19 +08:00
|
|
|
|
focusByRange(newRange);
|
2022-09-17 22:04:07 +08:00
|
|
|
|
}
|
2022-06-29 20:25:30 +08:00
|
|
|
|
/// #if !MOBILE
|
2022-05-26 15:18:53 +08:00
|
|
|
|
pushBack(protyle, newRange);
|
2022-06-29 20:25:30 +08:00
|
|
|
|
/// #endif
|
2022-11-30 23:55:03 +08:00
|
|
|
|
}, (isMobile() || window.webkit?.messageHandlers) ? 520 : 0); // Android/iPad 双击慢了出不来
|
2023-02-08 17:33:49 +08:00
|
|
|
|
protyle.hint.enableExtend = false;
|
2023-02-09 11:16:34 +08:00
|
|
|
|
if (event.shiftKey) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
// shift 多选
|
|
|
|
|
|
let startElement = hasClosestBlock(range.startContainer);
|
|
|
|
|
|
let endElement = hasClosestBlock(event.target);
|
|
|
|
|
|
if (startElement && endElement && startElement.isSameNode(endElement)) {
|
|
|
|
|
|
startElement = hasClosestBlock(range.endContainer);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (startElement && endElement && (!startElement.isSameNode(endElement) || (shiftStartElement && shiftStartElement.isSameNode(startElement)))) {
|
|
|
|
|
|
let toDown = true;
|
|
|
|
|
|
range.collapse(true);
|
|
|
|
|
|
if (shiftStartElement) {
|
|
|
|
|
|
startElement = shiftStartElement;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
shiftStartElement = startElement;
|
|
|
|
|
|
}
|
2022-09-16 21:16:57 +08:00
|
|
|
|
const startRect = startElement.getBoundingClientRect();
|
|
|
|
|
|
const endRect = endElement.getBoundingClientRect();
|
|
|
|
|
|
let startTop = startRect.top;
|
|
|
|
|
|
let endTop = endRect.top;
|
|
|
|
|
|
if (startTop === endTop) {
|
|
|
|
|
|
// 横排 https://ld246.com/article/1663036247544
|
|
|
|
|
|
startTop = startRect.right;
|
|
|
|
|
|
endTop = endRect.right;
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (startTop > endTop) {
|
|
|
|
|
|
const tempElement = endElement;
|
|
|
|
|
|
endElement = startElement;
|
|
|
|
|
|
startElement = tempElement;
|
|
|
|
|
|
const tempTop = endTop;
|
|
|
|
|
|
endTop = startTop;
|
|
|
|
|
|
startTop = tempTop;
|
|
|
|
|
|
toDown = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
let selectElements: Element[] = [];
|
|
|
|
|
|
let currentElement: HTMLElement = startElement;
|
|
|
|
|
|
let hasJump = false;
|
|
|
|
|
|
while (currentElement) {
|
|
|
|
|
|
if (currentElement && !currentElement.classList.contains("protyle-attr")) {
|
2022-09-18 17:51:19 +08:00
|
|
|
|
const currentRect = currentElement.getBoundingClientRect();
|
2022-09-16 21:16:57 +08:00
|
|
|
|
if (startRect.top === endRect.top ? (currentRect.right <= endTop) : (currentRect.top <= endTop)) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (hasJump) {
|
|
|
|
|
|
// 父节点的下个节点在选中范围内才可使用父节点作为选中节点
|
|
|
|
|
|
if (currentElement.nextElementSibling && !currentElement.nextElementSibling.classList.contains("protyle-attr")) {
|
2022-09-18 17:51:19 +08:00
|
|
|
|
const currentNextRect = currentElement.nextElementSibling.getBoundingClientRect();
|
2022-09-16 21:16:57 +08:00
|
|
|
|
if (startRect.top === endRect.top ? (currentNextRect.right <= endTop) : (currentNextRect.top <= endTop)) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
selectElements = [currentElement];
|
|
|
|
|
|
currentElement = currentElement.nextElementSibling as HTMLElement;
|
|
|
|
|
|
hasJump = false;
|
|
|
|
|
|
} else if (currentElement.parentElement.classList.contains("sb")) {
|
|
|
|
|
|
currentElement = hasClosestBlock(currentElement.parentElement) as HTMLElement;
|
|
|
|
|
|
hasJump = true;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
currentElement = hasClosestBlock(currentElement.parentElement) as HTMLElement;
|
|
|
|
|
|
hasJump = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
selectElements.push(currentElement);
|
|
|
|
|
|
currentElement = currentElement.nextElementSibling as HTMLElement;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (currentElement.parentElement.classList.contains("sb")) {
|
|
|
|
|
|
// 跳出超级块横向排版中的未选中元素
|
|
|
|
|
|
currentElement = hasClosestBlock(currentElement.parentElement) as HTMLElement;
|
|
|
|
|
|
hasJump = true;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
currentElement = hasClosestBlock(currentElement.parentElement) as HTMLElement;
|
|
|
|
|
|
hasJump = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (selectElements.length === 1 && !selectElements[0].classList.contains("list") && !selectElements[0].classList.contains("bq") && !selectElements[0].classList.contains("sb")) {
|
|
|
|
|
|
// 单个 p 不选中
|
|
|
|
|
|
shiftStartElement = undefined;
|
|
|
|
|
|
} else {
|
2022-06-28 10:20:55 +08:00
|
|
|
|
const ids: string[] = [];
|
2022-07-07 23:10:48 +08:00
|
|
|
|
if (!protyle.wysiwyg.element.querySelector(".protyle-wysiwyg--select") && protyle.scroll && !protyle.scroll.element.classList.contains("fn__none") && !protyle.scroll.keepLazyLoad &&
|
|
|
|
|
|
(startElement.getBoundingClientRect().top < -protyle.contentElement.clientHeight * 2 || endElement.getBoundingClientRect().bottom > protyle.contentElement.clientHeight * 2)) {
|
|
|
|
|
|
showMessage(window.siyuan.languages.crossKeepLazyLoad);
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
selectElements.forEach(item => {
|
|
|
|
|
|
item.classList.add("protyle-wysiwyg--select");
|
2022-06-28 10:20:55 +08:00
|
|
|
|
ids.push(item.getAttribute("data-node-id"));
|
2022-11-07 22:59:25 +08:00
|
|
|
|
// 清除选中的子块 https://ld246.com/article/1667826582251
|
|
|
|
|
|
item.querySelectorAll(".protyle-wysiwyg--select").forEach(subItem => {
|
|
|
|
|
|
subItem.classList.remove("protyle-wysiwyg--select");
|
2022-11-08 19:25:07 +08:00
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
|
});
|
2022-06-28 10:19:15 +08:00
|
|
|
|
countBlockWord(ids);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (toDown) {
|
|
|
|
|
|
focusBlock(selectElements[selectElements.length - 1], protyle.wysiwyg.element, false);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
focusBlock(selectElements[0], protyle.wysiwyg.element, false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (this.element.querySelector(".protyle-wysiwyg--select") && range.toString() !== "") {
|
|
|
|
|
|
// 选中块后,文字不能被选中。需在 shift click 之后,防止shift点击单个块出现文字选中
|
|
|
|
|
|
range.collapse(false);
|
|
|
|
|
|
focusByRange(range);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-21 11:48:44 +08:00
|
|
|
|
if (ctrlIsPressed && range.toString() === "") {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
let ctrlElement = hasClosestBlock(event.target);
|
|
|
|
|
|
if (ctrlElement) {
|
|
|
|
|
|
ctrlElement = getTopAloneElement(ctrlElement) as HTMLElement;
|
|
|
|
|
|
if (ctrlElement.classList.contains("protyle-wysiwyg--select")) {
|
|
|
|
|
|
ctrlElement.classList.remove("protyle-wysiwyg--select");
|
2022-10-22 11:31:41 +08:00
|
|
|
|
ctrlElement.removeAttribute("select-start");
|
|
|
|
|
|
ctrlElement.removeAttribute("select-end");
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
ctrlElement.classList.add("protyle-wysiwyg--select");
|
|
|
|
|
|
}
|
|
|
|
|
|
ctrlElement.querySelectorAll(".protyle-wysiwyg--select").forEach(item => {
|
|
|
|
|
|
item.classList.remove("protyle-wysiwyg--select");
|
2022-10-22 11:31:41 +08:00
|
|
|
|
item.removeAttribute("select-start");
|
|
|
|
|
|
item.removeAttribute("select-end");
|
2022-05-26 15:18:53 +08:00
|
|
|
|
});
|
2022-11-09 14:47:53 +08:00
|
|
|
|
const ctrlParentElement = hasClosestByClassName(ctrlElement.parentElement, "protyle-wysiwyg--select");
|
|
|
|
|
|
if (ctrlParentElement) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
ctrlParentElement.classList.remove("protyle-wysiwyg--select");
|
2022-10-22 11:31:41 +08:00
|
|
|
|
ctrlParentElement.removeAttribute("select-start");
|
|
|
|
|
|
ctrlParentElement.removeAttribute("select-end");
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2022-06-28 10:20:55 +08:00
|
|
|
|
const ids: string[] = [];
|
2022-06-28 10:19:15 +08:00
|
|
|
|
protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select").forEach(item => {
|
2022-06-28 10:20:55 +08:00
|
|
|
|
ids.push(item.getAttribute("data-node-id"));
|
2022-06-28 10:19:15 +08:00
|
|
|
|
});
|
|
|
|
|
|
countBlockWord(ids);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|