Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: https://github.com/siyuan-note/siyuan/issues/5735 划选

Vanessa78d69af54e1f
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index a91882cafb..5477bc5eb2 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -78,7 +78,7 @@ export class WYSIWYG {
if (window.siyuan.config.editor.displayBookmarkIcon) {
this.element.classList.add("protyle-wysiwyg--attr");
}
- this.bindCopyEvent(protyle);
+ this.bindCommonEvent(protyle);
if (protyle.options.action.includes(Constants.CB_GET_HISTORY)) {
return;
}
@@ -181,7 +181,7 @@ export class WYSIWYG {
/// #endif
}
- private bindCopyEvent (protyle: IProtyle) {
+ private bindCommonEvent (protyle: IProtyle) {
this.element.addEventListener("copy", (event: ClipboardEvent & { target: HTMLElement }) => {
// https://github.com/siyuan-note/siyuan/issues/4600
if (event.target.tagName === "PROTYLE-HTML") {
@@ -255,435 +255,95 @@ export class WYSIWYG {
event.clipboardData.setData("text/plain", textPlain || protyle.lute.BlockDOM2StdMd(html).trimEnd());
event.clipboardData.setData("text/html", html + Constants.ZWSP);
});
- }
-
- 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 }) => {
- if (event.target.tagName === "PROTYLE-HTML") {
- event.stopPropagation();
+ this.element.addEventListener("mousedown", (event: MouseEvent) => {
+ if (event.button === 2 || window.siyuan.ctrlIsPressed) {
+ // 右键
return;
}
- event.stopPropagation();
- event.preventDefault();
-
- if (protyle.options.render.breadcrumb) {
- protyle.breadcrumb.hide();
+ if (!window.siyuan.shiftIsPressed) {
+ // https://github.com/siyuan-note/siyuan/issues/3026
+ hideElements(["select"], protyle);
}
- const range = getEditorRange(protyle.wysiwyg.element);
- let nodeElement = hasClosestBlock(range.startContainer);
- if (!nodeElement) {
+ const target = event.target as HTMLElement;
+ if (hasClosestByClassName(target, "protyle-action")) {
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) {
- if (selectElements[0].getAttribute("data-type") === "NodeListItem" && selectElements[0].parentElement.childElementCount - 1 === selectElements.length) {
- 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);
+ const documentSelf = document;
+ const rect = protyle.element.getBoundingClientRect();
+ const mostLeft = rect.left + parseInt(protyle.wysiwyg.element.style.paddingLeft) + 1;
+ const mostRight = mostLeft + protyle.wysiwyg.element.firstElementChild.clientWidth - 1;
+ 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;
}
- } 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;
+ 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;
}
+ } else if (target.parentElement.parentElement.getAttribute("data-type") === "img") {
+ target.parentElement.parentElement.classList.add("img--drag");
}
- // 选中整个标题 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);
+
+ 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 = "";
}
- 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 &&
- !["DIV", "TD", "TH"].includes(range.startContainer.parentElement.tagName)) {
- // 选中整个内联元素
- tempElement.append(range.startContainer.parentElement);
- } else if (selectImgElement) {
- tempElement.append(selectImgElement);
- } 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);
+ 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";
} 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
- Array.from(parentElement.children).forEach(item => {
- if (item.textContent === "" && (item.nodeType === 1 && item.tagName !== "BR")) {
- item.remove();
- }
- });
- }
+ dragElement.style.width = Math.max(17, dragWidth + (moveEvent.clientX - x) * 2) + "px";
}
}
- }
- 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 = "";
+ 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";
}
- }
- 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);
- event.clipboardData.setData("text/plain", protyle.lute.BlockDOM2StdMd(html));
- event.clipboardData.setData("text/html", Constants.ZWSP + html);
- });
+ };
- let beforeContextmenuRange: Range;
- this.element.addEventListener("contextmenu", (event) => {
- event.stopPropagation();
- event.preventDefault();
- const selectElements = protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select");
- if (selectElements.length > 1) {
- // 多选块
- hideElements(["util"], protyle);
- protyle.gutter.renderMenu(protyle, selectElements[0]);
- window.siyuan.menus.menu.popup({x: event.clientX, y: event.clientY});
+ 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");
+ };
return;
}
- const target = event.target as HTMLElement;
- const embedElement = hasClosestByAttribute(target, "data-type", "NodeBlockQueryEmbed");
- if (embedElement) {
- if (getSelection().rangeCount === 0) {
- focusSideBlock(embedElement);
- }
- protyle.gutter.renderMenu(protyle, embedElement);
- window.siyuan.menus.menu.popup({x: event.clientX, y: event.clientY});
- return false;
- }
- if (protyle.disabled) {
- return false;
- }
- protyle.toolbar.range = getEditorRange(protyle.element);
- const type = target.getAttribute("data-type");
- if (type === "block-ref") {
- refMenu(protyle, target);
- // 阻止 popover
- target.setAttribute("prevent-popover", "true");
- setTimeout(() => {
- target.removeAttribute("prevent-popover");
- }, 620);
- return false;
- }
- if (type === "file-annotation-ref") {
- protyle.toolbar.showFileAnnotationRef(protyle, target);
- return false;
- }
- if (type === "a") {
- linkMenu(protyle, target);
- if (target.getAttribute("data-href")?.startsWith("siyuan://blocks")) {
- // 阻止 popover
- target.setAttribute("prevent-popover", "true");
- setTimeout(() => {
- target.removeAttribute("prevent-popover");
- }, 620);
- }
- return false;
- }
- if (target.tagName === "IMG" && hasClosestByClassName(target, "img")) {
- imgMenu(protyle, protyle.toolbar.range, target.parentElement.parentElement, {
- clientX: event.clientX + 4,
- clientY: event.clientY
- });
- return false;
- }
- const nodeElement = hasClosestBlock(target);
- if (!nodeElement) {
- return false;
- }
- if (!isNotEditBlock(nodeElement) &&
- (isMobile() || beforeContextmenuRange && nodeElement.contains(beforeContextmenuRange.startContainer))) {
- if (!isMobile() || protyle.toolbar?.element.classList.contains("fn__none")) {
- contentMenu(protyle, nodeElement);
- window.siyuan.menus.menu.popup({x: event.clientX, y: event.clientY + 13, h: 26});
- 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);
- protyle.gutter.renderMenu(protyle, nodeElement);
- window.siyuan.menus.menu.popup({x: event.clientX, y: event.clientY});
- protyle.toolbar?.element.classList.add("fn__none");
- }
- });
-
- this.element.addEventListener("pointerdown", () => {
- if (getSelection().rangeCount > 0) {
- beforeContextmenuRange = getSelection().getRangeAt(0);
- } else {
- beforeContextmenuRange = undefined;
- }
- });
-
- let mousewheelTime = 0;
- this.element.addEventListener("mousewheel", (event: WheelEvent) => {
- // https://ld246.com/article/1648865235549
- if (event.timeStamp - mousewheelTime > 6000 &&
- 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 || "",
- size: Constants.SIZE_GET,
- }, getResponse => {
- onGet(getResponse, protyle, [Constants.CB_GET_BEFORE, Constants.CB_GET_UNCHANGEID]);
- });
- mousewheelTime = 0;
- }
- mousewheelTime = event.timeStamp;
- 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) {
- protyle.gutter.render(embedElement, this.element);
- } else {
- protyle.gutter.render(nodeElement, this.element);
- }
- }
- });
-
- this.element.addEventListener("mousedown", (event: MouseEvent) => {
- if (event.button === 2 || window.siyuan.ctrlIsPressed) {
- // 右键
- return;
- }
- if (!window.siyuan.shiftIsPressed) {
- // https://github.com/siyuan-note/siyuan/issues/3026
- hideElements(["select"], protyle);
- }
- const target = event.target as HTMLElement;
- if (hasClosestByClassName(target, "protyle-action")) {
- return;
- }
- const documentSelf = document;
- const rect = protyle.element.getBoundingClientRect();
- const mostLeft = rect.left + parseInt(protyle.wysiwyg.element.style.paddingLeft) + 1;
- const mostRight = mostLeft + protyle.wysiwyg.element.firstElementChild.clientWidth - 1;
- const mostBottom = rect.bottom;
- const y = event.clientY;
- // 图片、iframe、video 缩放
- if (target.classList.contains("protyle-action__drag")) {
- const nodeElement = hasClosestBlock(target);
- if (!nodeElement) {
- return;
- }
- 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;
- }
- } else if (target.parentElement.parentElement.getAttribute("data-type") === "img") {
- target.parentElement.parentElement.classList.add("img--drag");
- }
-
- 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 = "";
- }
- 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";
- } else {
- dragElement.style.width = Math.max(17, dragWidth + (moveEvent.clientX - x) * 2) + "px";
- }
- }
- 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";
- }
- };
-
- 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");
- };
- return;
- }
- // 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();
+ // 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();
}
// 后续拖拽操作写在多选节点中
}
// table col resize
- if (target.classList.contains("table__resize")) {
+ if (!protyle.disabled && target.classList.contains("table__resize")) {
const nodeElement = hasClosestBlock(target);
if (!nodeElement) {
return;
@@ -742,7 +402,7 @@ export class WYSIWYG {
documentSelf.onmousemove = (moveEvent: MouseEvent) => {
const moveTarget = moveEvent.target as HTMLElement;
// table cell select
- if (tableBlockElement && tableBlockElement.contains(moveTarget) && !hasClosestByClassName(tableBlockElement, "protyle-wysiwyg__embed")) {
+ if (!protyle.disabled && tableBlockElement && tableBlockElement.contains(moveTarget) && !hasClosestByClassName(tableBlockElement, "protyle-wysiwyg__embed")) {
if ((moveTarget.tagName === "TH" || moveTarget.tagName === "TD") && !moveTarget.isSameNode(target) && (!moveCellElement || !moveCellElement.isSameNode(moveTarget))) {
// @ts-ignore
tableBlockElement.firstElementChild.style.webkitUserModify = "read-only";
@@ -939,7 +599,7 @@ export class WYSIWYG {
documentSelf.onselect = null;
protyle.selectElement.classList.add("fn__none");
protyle.selectElement.removeAttribute("style");
- if (tableBlockElement) {
+ if (!protyle.disabled && tableBlockElement) {
// @ts-ignore
tableBlockElement.firstElementChild.style.webkitUserModify = "";
const tableSelectElement = tableBlockElement.querySelector(".table__select") as HTMLElement;
@@ -1195,6 +855,345 @@ export class WYSIWYG {
}
};
});
+ }
+
+ 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 }) => {
+ 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) {
+ if (selectElements[0].getAttribute("data-type") === "NodeListItem" && selectElements[0].parentElement.childElementCount - 1 === selectElements.length) {
+ 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 &&
+ !["DIV", "TD", "TH"].includes(range.startContainer.parentElement.tagName)) {
+ // 选中整个内联元素
+ tempElement.append(range.startContainer.parentElement);
+ } else if (selectImgElement) {
+ tempElement.append(selectImgElement);
+ } 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
+ Array.from(parentElement.children).forEach(item => {
+ if (item.textContent === "" && (item.nodeType === 1 && item.tagName !== "BR")) {
+ 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);
+ event.clipboardData.setData("text/plain", protyle.lute.BlockDOM2StdMd(html));
+ event.clipboardData.setData("text/html", Constants.ZWSP + html);
+ });
+
+ let beforeContextmenuRange: Range;
+ this.element.addEventListener("contextmenu", (event) => {
+ event.stopPropagation();
+ event.preventDefault();
+ const selectElements = protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select");
+ if (selectElements.length > 1) {
+ // 多选块
+ hideElements(["util"], protyle);
+ protyle.gutter.renderMenu(protyle, selectElements[0]);
+ window.siyuan.menus.menu.popup({x: event.clientX, y: event.clientY});
+ return;
+ }
+ const target = event.target as HTMLElement;
+ const embedElement = hasClosestByAttribute(target, "data-type", "NodeBlockQueryEmbed");
+ if (embedElement) {
+ if (getSelection().rangeCount === 0) {
+ focusSideBlock(embedElement);
+ }
+ protyle.gutter.renderMenu(protyle, embedElement);
+ window.siyuan.menus.menu.popup({x: event.clientX, y: event.clientY});
+ return false;
+ }
+ if (protyle.disabled) {
+ return false;
+ }
+ protyle.toolbar.range = getEditorRange(protyle.element);
+ const type = target.getAttribute("data-type");
+ if (type === "block-ref") {
+ refMenu(protyle, target);
+ // 阻止 popover
+ target.setAttribute("prevent-popover", "true");
+ setTimeout(() => {
+ target.removeAttribute("prevent-popover");
+ }, 620);
+ return false;
+ }
+ if (type === "file-annotation-ref") {
+ protyle.toolbar.showFileAnnotationRef(protyle, target);
+ return false;
+ }
+ if (type === "a") {
+ linkMenu(protyle, target);
+ if (target.getAttribute("data-href")?.startsWith("siyuan://blocks")) {
+ // 阻止 popover
+ target.setAttribute("prevent-popover", "true");
+ setTimeout(() => {
+ target.removeAttribute("prevent-popover");
+ }, 620);
+ }
+ return false;
+ }
+ if (target.tagName === "IMG" && hasClosestByClassName(target, "img")) {
+ imgMenu(protyle, protyle.toolbar.range, target.parentElement.parentElement, {
+ clientX: event.clientX + 4,
+ clientY: event.clientY
+ });
+ return false;
+ }
+ const nodeElement = hasClosestBlock(target);
+ if (!nodeElement) {
+ return false;
+ }
+ if (!isNotEditBlock(nodeElement) &&
+ (isMobile() || beforeContextmenuRange && nodeElement.contains(beforeContextmenuRange.startContainer))) {
+ if (!isMobile() || protyle.toolbar?.element.classList.contains("fn__none")) {
+ contentMenu(protyle, nodeElement);
+ window.siyuan.menus.menu.popup({x: event.clientX, y: event.clientY + 13, h: 26});
+ 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);
+ protyle.gutter.renderMenu(protyle, nodeElement);
+ window.siyuan.menus.menu.popup({x: event.clientX, y: event.clientY});
+ protyle.toolbar?.element.classList.add("fn__none");
+ }
+ });
+
+ this.element.addEventListener("pointerdown", () => {
+ if (getSelection().rangeCount > 0) {
+ beforeContextmenuRange = getSelection().getRangeAt(0);
+ } else {
+ beforeContextmenuRange = undefined;
+ }
+ });
+
+ let mousewheelTime = 0;
+ this.element.addEventListener("mousewheel", (event: WheelEvent) => {
+ // https://ld246.com/article/1648865235549
+ if (event.timeStamp - mousewheelTime > 6000 &&
+ 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 || "",
+ size: Constants.SIZE_GET,
+ }, getResponse => {
+ onGet(getResponse, protyle, [Constants.CB_GET_BEFORE, Constants.CB_GET_UNCHANGEID]);
+ });
+ mousewheelTime = 0;
+ }
+ mousewheelTime = event.timeStamp;
+ 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) {
+ protyle.gutter.render(embedElement, this.element);
+ } else {
+ protyle.gutter.render(nodeElement, this.element);
+ }
+ }
+ });
this.element.addEventListener("paste", (event: ClipboardEvent & { target: HTMLElement }) => {
// https://github.com/siyuan-note/siyuan/issues/4600