An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: https://github.com/siyuan-note/siyuan/issues/8554
Vanessac7d18b082724
diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.tsindex 06bc0f4b1c..ede9cc7d21 100644--- a/app/src/protyle/toolbar/index.ts+++ b/app/src/protyle/toolbar/index.ts@@ -34,13 +34,13 @@ import {blockRender} from "../render/blockRender";import {openBy} from "../../editor/util";/// #endifimport {fetchPost} from "../../util/fetch";-import {isArrayEqual, isMobile} from "../../util/functions";+import {isMobile} from "../../util/functions";import * as dayjs from "dayjs";import {insertEmptyBlock} from "../../block/util";import {matchHotKey} from "../util/hotKey";import {hideElements} from "../ui/hideElements";import {electronUndo} from "../undo";-import {previewTemplate, toolbarKeyToMenu} from "./util";+import {mergeSameInlineElement, previewTemplate, toolbarKeyToMenu} from "./util";import {showMessage} from "../../dialog/message";import {InlineMath} from "./InlineMath";import {InlineMemo} from "./InlineMemo";@@ -856,36 +856,8 @@ export class Toolbar {}}if (currentNode && currentNode.nodeType !== 3) {- const currentType = (currentNode.getAttribute("data-type") || "").split(" ");- if (currentNode.tagName !== "BR" && !currentNode.classList.contains("img") &&- previousElement && previousElement.nodeType !== 3 &&- currentNode.nodeType !== 3 &&- isArrayEqual(currentType, (previousElement.getAttribute("data-type") || "").split(" ")) &&- hasSameTextStyle(currentNode, previousElement)) {- if (currentType.includes("code") || currentType.includes("tag") || currentType.includes("kbd")) {- if (currentNode.textContent.startsWith(Constants.ZWSP)) {- currentNode.textContent = currentNode.textContent.substring(1);- }- }- if (currentType.includes("inline-math")) {- // 数学公式合并 data-content https://github.com/siyuan-note/siyuan/issues/6028- currentNode.setAttribute("data-content", previousElement.getAttribute("data-content") + currentNode.getAttribute("data-content"));- } else if (currentType.includes("block-ref") && previousElement.getAttribute("data-id") === currentNode.getAttribute("data-id")) {- if (previousElement.dataset.subtype !== "d" || previousElement.dataset.subtype !== "d") {- currentNode.setAttribute("data-subtype", "s");- currentNode.textContent = previousElement.textContent + currentNode.textContent;- }- } else {- // 测试不存在 https://ld246.com/article/1664454663564 情况,故移除引用合并限制- // 搜索结果引用被高亮隔断需进行合并 https://github.com/siyuan-note/siyuan/issues/7588- // textContent:防止赋值后 \n 转换为 br 导致后续 this.range.setStart 报错;innerText:获取 br 的 \n, https://github.com/siyuan-note/siyuan/issues/15968- currentNode.textContent = previousElement.innerText + currentNode.innerText;- // 如果为备注时,合并备注内容- if (currentType.includes("inline-memo")) {- currentNode.setAttribute("data-inline-memo-content", (previousElement.getAttribute("data-inline-memo-content") || "") +- (currentNode.getAttribute("data-inline-memo-content") || ""));- }- }+ if (mergeSameInlineElement(currentNode, previousElement)) {+ const currentType = (currentNode.getAttribute("data-type") || "").split(" ");if (!currentType.includes("inline-math")) {if (i === 0) {startContainer = currentNode;diff --git a/app/src/protyle/toolbar/util.ts b/app/src/protyle/toolbar/util.tsindex 77153bae2f..3bee202f29 100644--- a/app/src/protyle/toolbar/util.ts+++ b/app/src/protyle/toolbar/util.ts@@ -2,6 +2,8 @@ import {fetchPost, fetchSyncPost} from "../../util/fetch";import {Constants} from "../../constants";import {focusByRange, focusByWbr} from "../util/selection";import {writeText} from "../util/compatibility";+import {isArrayEqual} from "../../util/functions";+import {hasSameTextStyle} from "./Font";export const previewTemplate = (pathString: string, element: Element, parentId: string) => {if (!pathString) {@@ -42,6 +44,44 @@ const mergeElement = (a: Element, b: Element, after = true) => {return isMatch;};+export const mergeSameInlineElement = (currentElement: HTMLElement, previousElement: HTMLElement) => {+ if (!currentElement || currentElement.nodeType !== 1 || !previousElement || previousElement.nodeType !== 1) {+ return false;+ }+ const currentType = currentElement.getAttribute("data-type");+ const previousType = previousElement.getAttribute("data-type");+ if (!currentType || !previousType || currentElement.tagName === "BR" || currentElement.classList.contains("img")) {+ return false;+ }+ const types = currentType.split(" ");+ if (!isArrayEqual(types, previousType.split(" ")) || !hasSameTextStyle(currentElement, previousElement)) {+ return false;+ }+ if ((types.includes("code") || types.includes("tag") || types.includes("kbd")) &&+ currentElement.textContent.startsWith(Constants.ZWSP)) {+ currentElement.textContent = currentElement.textContent.substring(1);+ }+ if (types.includes("inline-math")) {+ currentElement.setAttribute("data-content",+ previousElement.getAttribute("data-content") + currentElement.getAttribute("data-content"));+ } else if (types.includes("block-ref") &&+ previousElement.getAttribute("data-id") === currentElement.getAttribute("data-id")) {+ if (previousElement.dataset.subtype !== "d") {+ currentElement.setAttribute("data-subtype", "s");+ currentElement.textContent = previousElement.textContent + currentElement.textContent;+ }+ } else {+ // textContent:防止赋值后 \n 转换为 br;innerText:获取 br 的 \n。+ currentElement.textContent = previousElement.innerText + currentElement.innerText;+ if (types.includes("inline-memo")) {+ currentElement.setAttribute("data-inline-memo-content",+ (previousElement.getAttribute("data-inline-memo-content") || "") ++ (currentElement.getAttribute("data-inline-memo-content") || ""));+ }+ }+ return true;+};+export const removeSearchMark = (element: HTMLElement) => {let previousElement = element.previousSibling as HTMLElement;while (previousElement && previousElement.nodeType !== 3) {diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.tsindex 298f9e4b3e..3f46d9dddb 100644--- a/app/src/protyle/wysiwyg/keydown.ts+++ b/app/src/protyle/wysiwyg/keydown.ts@@ -3,9 +3,7 @@ import {isMac, isNotCtrl, isOnlyMeta, writeText} from "../util/compatibility";import {focusBlock,focusByRange,- focusByOffset,focusByWbr,- getBlockRanges,getEditorRange,getSelectionOffset,getSelectionPosition,@@ -22,7 +20,7 @@ import {hasTopClosestByAttribute,isInEmbedBlock} from "../util/hasClosest";-import {removeBlock, removeImage} from "./remove";+import {removeBlock, removeCrossBlockRange, removeImage} from "./remove";import {getContenteditableElement,getFirstBlock,@@ -31,7 +29,6 @@ import {getParentBlock,getPreviousBlock,getTopAloneElement,- fixAdjacentTags,hasNextSibling,hasPreviousSibling,isEndOfBlock,@@ -794,39 +791,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {if ((!event.altKey && (event.key === "Backspace" || event.key === "Delete")) ||matchHotKey("⌃D", event)) {if (isCrossBlock && selectText !== "") {- const ranges = getBlockRanges(protyle.wysiwyg.element, range);- if (ranges.length > 0) {- const firstRange = ranges[0];- const rangesByBlock = new Map<HTMLElement, typeof ranges>();- ranges.forEach(item => {- const blockRanges = rangesByBlock.get(item.blockElement) || [];- blockRanges.push(item);- rangesByBlock.set(item.blockElement, blockRanges);- });- const blockElements = Array.from(rangesByBlock.keys());- updateBatchTransaction(blockElements, protyle, blockElement => {- rangesByBlock.get(blockElement).forEach(item => {- const boundarySpans = new Set<HTMLElement>([- hasClosestByTag(item.range.startContainer, "SPAN"),- hasClosestByTag(item.range.endContainer, "SPAN"),- ].filter(Boolean) as HTMLElement[]);- item.range.deleteContents();- boundarySpans.forEach(spanElement => {- if (spanElement.isConnected && spanElement.textContent === "" &&- !spanElement.querySelector("img")) {- spanElement.remove();- }- });- fixAdjacentTags(item.editableElement);- });- blockElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));- });- focusByOffset(firstRange.editableElement, firstRange.start, firstRange.start);- blockElements.filter(item => item.getAttribute("data-type") === "NodeCodeBlock").forEach(item => {- item.querySelector(".hljs")?.removeAttribute("data-render");- highlightRender(item);- });- }+ await removeCrossBlockRange(protyle, range, nodeElement, endElement);event.stopPropagation();event.preventDefault();return;diff --git a/app/src/protyle/wysiwyg/remove.ts b/app/src/protyle/wysiwyg/remove.tsindex 601ce739f1..b356ae4e04 100644--- a/app/src/protyle/wysiwyg/remove.ts+++ b/app/src/protyle/wysiwyg/remove.ts@@ -1,5 +1,14 @@-import {focusBlock, focusByRange, focusByWbr, getSelectionOffset, setLastNodeRange} from "../util/selection";import {+ focusBlock,+ focusByOffset,+ focusByRange,+ focusByWbr,+ getBlockRanges,+ getSelectionOffset,+ setLastNodeRange+} from "../util/selection";+import {+ fixAdjacentTags,getContenteditableElement,getEmbedChildOperationContext,getEmbedChildOperationParentID,@@ -25,7 +34,7 @@ import {Constants} from "../../constants";import {scrollCenter} from "../../util/highlightById";import {isMobile} from "../../util/functions";import {mathRender} from "../render/mathRender";-import {hasClosestBlock, hasClosestByClassName, isInEmbedBlock} from "../util/hasClosest";+import {hasClosestBlock, hasClosestByClassName, hasClosestByTag, isInEmbedBlock} from "../util/hasClosest";/// #if !MOBILEimport {getAllModels} from "../../layout/getAll";/// #endif@@ -33,6 +42,175 @@ import {fetchPost, fetchSyncPost} from "../../util/fetch";import {onGet} from "../util/onGet";import {setFold} from "../util/blockFold";import {isEncryptedBox} from "../../util/pathName";+import {highlightRender} from "../render/highlightRender";+import * as dayjs from "dayjs";+import {mergeSameInlineElement} from "../toolbar/util";++export const removeCrossBlockRange = (protyle: IProtyle, selectedRange: Range,+ startElement: HTMLElement, endElement: HTMLElement) => {+ const editorElement = protyle.wysiwyg.element;+ const ranges = getBlockRanges(editorElement, selectedRange);+ const selectedElements: HTMLElement[] = [];+ selectedRange.cloneContents().querySelectorAll<HTMLElement>("[data-node-id]").forEach(item => {+ const element = editorElement.querySelector<HTMLElement>(`[data-node-id="${item.getAttribute("data-node-id")}"]`);+ if (!element || selectedElements.includes(element) || element === startElement || element === endElement ||+ element.contains(startElement) || element.contains(endElement) || isInEmbedBlock(element)) {+ return;+ }+ const elementRange = document.createRange();+ elementRange.selectNode(element);+ if (selectedRange.compareBoundaryPoints(Range.START_TO_START, elementRange) <= 0 &&+ selectedRange.compareBoundaryPoints(Range.END_TO_END, elementRange) >= 0) {+ selectedElements.push(element);+ }+ });+ const selectedSet = new Set(selectedElements);+ const removeElements = selectedElements.filter(item => {+ return !selectedSet.has(item.parentElement.closest<HTMLElement>("[data-node-id]"));+ });+ const rangesByBlock = new Map<HTMLElement, typeof ranges>();+ ranges.forEach(item => {+ if (removeElements.some(removeElement => removeElement.contains(item.blockElement))) {+ return;+ }+ const blockRanges = rangesByBlock.get(item.blockElement) || [];+ blockRanges.push(item);+ rangesByBlock.set(item.blockElement, blockRanges);+ });++ const startEditableElement = rangesByBlock.get(startElement)?.[0]?.editableElement;+ const endEditableElement = rangesByBlock.get(endElement)?.[0]?.editableElement;+ const blockType = startElement.getAttribute("data-type");+ let removeEndElement: Element;+ if (startEditableElement && endEditableElement &&+ ["NodeParagraph", "NodeHeading"].includes(blockType) &&+ blockType === endElement.getAttribute("data-type") &&+ endElement.getAttribute("fold") !== "1") {+ const topElement = getTopAloneElement(endElement);+ const hasChildList = topElement === endElement && endElement.parentElement.classList.contains("li") &&+ !!endElement.parentElement.querySelector(":scope > .list");+ if (!hasChildList && !topElement.contains(startElement)) {+ removeEndElement = topElement;+ removeElements.push(topElement as HTMLElement);+ }+ }++ const updateElements = Array.from(rangesByBlock.keys()).filter(item => {+ return !removeElements.some(removeElement => removeElement.contains(item));+ });+ if (removeElements.length === 0 && updateElements.length === 0) {+ return;+ }++ const undoOperations: IOperation[] = updateElements.map(item => ({+ action: "update",+ id: item.getAttribute("data-node-id"),+ data: item.outerHTML+ }));+ const insertOperations: IOperation[] = removeElements.map(item => {+ let data = item.outerHTML;+ if (item.classList.contains("render-node") || item.querySelector("div.render-node")) {+ data = protyle.lute.SpinBlockDOM(data);+ }+ return {+ action: "insert",+ id: item.getAttribute("data-node-id"),+ data,+ previousID: getPreviousBlockSibling(item)?.getAttribute("data-node-id"),+ parentID: getOperationParentID(item, protyle.block.parentID)+ };+ });+ const orderListElements = Array.from(new Set(removeElements+ .filter(item => item.classList.contains("li") && item.parentElement.getAttribute("data-subtype") === "o")+ .map(item => item.parentElement)));++ rangesByBlock.forEach(blockRanges => {+ blockRanges.forEach(item => {+ const boundarySpans = new Set<HTMLElement>([+ hasClosestByTag(item.range.startContainer, "SPAN"),+ hasClosestByTag(item.range.endContainer, "SPAN"),+ ].filter(Boolean) as HTMLElement[]);+ item.range.deleteContents();+ boundarySpans.forEach(spanElement => {+ if (spanElement.isConnected && spanElement.textContent === "" && !spanElement.querySelector("img")) {+ spanElement.remove();+ }+ });+ fixAdjacentTags(item.editableElement);+ });+ });+ if (removeEndElement) {+ const firstEndNode = endEditableElement.firstChild;+ while (endEditableElement.firstChild) {+ startEditableElement.append(endEditableElement.firstChild);+ }+ if (firstEndNode?.nodeType === 1) {+ const currentElement = firstEndNode as HTMLElement;+ let previousElement = currentElement.previousSibling as HTMLElement;+ while (previousElement?.nodeType === 1 && mergeSameInlineElement(currentElement, previousElement)) {+ previousElement.remove();+ previousElement = currentElement.previousSibling as HTMLElement;+ }+ }+ startEditableElement.normalize();+ fixAdjacentTags(startEditableElement);+ }+ removeElements.forEach(item => item.remove());++ const updated = dayjs().format("YYYYMMDDHHmmss");+ const doOperations: IOperation[] = removeElements.map(item => ({+ action: "delete",+ id: item.getAttribute("data-node-id")+ }));+ orderListElements.forEach(item => {+ const oldListItems = new Map(Array.from(item.querySelectorAll<HTMLElement>(":scope > .li")).map(listItem => {+ return [listItem.getAttribute("data-node-id"), listItem.outerHTML];+ }));+ updateListOrder(item);+ item.querySelectorAll<HTMLElement>(":scope > .li").forEach(listItem => {+ const id = listItem.getAttribute("data-node-id");+ const oldHTML = oldListItems.get(id);+ if (!oldHTML || oldHTML === listItem.outerHTML) {+ return;+ }+ listItem.setAttribute(Constants.ATTRIBUTE_EDITING, "true");+ undoOperations.push({+ action: "update",+ id,+ data: oldHTML+ });+ doOperations.push({+ action: "update",+ id,+ data: listItem.outerHTML+ });+ });+ });+ updateElements.forEach(item => {+ item.classList.remove("protyle-wysiwyg--select");+ item.removeAttribute("select-start");+ item.removeAttribute("select-end");+ item.setAttribute("updated", updated);+ item.setAttribute(Constants.ATTRIBUTE_EDITING, "true");+ doOperations.push({+ action: "update",+ id: item.getAttribute("data-node-id"),+ data: item.outerHTML+ });+ });+ transaction(protyle, doOperations, undoOperations.concat(insertOperations));++ const firstRange = ranges.find(item => item.editableElement.isConnected);+ if (firstRange) {+ focusByOffset(firstRange.editableElement, firstRange.start, firstRange.start);+ }+ updateElements.forEach(item => {+ if (item.getAttribute("data-type") === "NodeCodeBlock") {+ item.querySelector(".hljs")?.removeAttribute("data-render");+ highlightRender(item);+ }+ });+};export const removeBlock = async (protyle: IProtyle, blockElement: Element, range: Range, type: "Delete" | "Backspace" | "remove") => {protyle.observerLoad?.disconnect();@@ -139,9 +317,9 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rangtopElement.firstElementChild.removeAttribute("contenteditable");topElement.remove();} else {- let data = topElement.outerHTML; // 不能 spin ,否则 li 会变为 list+ let data = topElement.outerHTML; // 列表项不可 Spin,否则会转换为列表块。if (topElement.classList.contains("render-node") || topElement.querySelector("div.render-node")) {- data = protyle.lute.SpinBlockDOM(topElement.outerHTML); // 防止图表撤销问题+ data = protyle.lute.SpinBlockDOM(data);}const previousBlockElement = getPreviousBlockSibling(topElement);let previousID = previousBlockElement ? previousBlockElement.getAttribute("data-node-id") : "";