An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Warn before cutting referenced content across blocks https://github.com/siyuan-note/siyuan/issues/13396
Daniel9bfd9818acce
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.tsindex f19f2d67ff..36fd3efe6d 100644--- a/app/src/protyle/wysiwyg/index.ts+++ b/app/src/protyle/wysiwyg/index.ts@@ -58,7 +58,7 @@ import {ipcRenderer} from "electron";import {getEnableHTML, removeEmbed} from "./removeEmbed";import {keydown} from "./keydown";import {openMobileFileById} from "../../mobile/editor";-import {removeBlock} from "./remove";+import {getCrossBlockRefCheckElements, removeBlock} from "./remove";import {highlightRender} from "../render/highlightRender";import {openAttr} from "../../menus/commonMenuItem";import {blockRender} from "../render/blockRender";@@ -2519,6 +2519,24 @@ export class WYSIWYG {autoSelectedBlock = true;}const selectedStateElements = [...selectElements];+ if (selectedStateElements.length === 0 && range.toString() !== "") {+ const endElement = hasClosestBlock(range.endContainer);+ if (endElement && nodeElement !== endElement) {+ const checkElements = getCrossBlockRefCheckElements(+ protyle.wysiwyg.element, range, nodeElement, endElement);+ const checkIDs = checkElements.map(item => item.getAttribute("data-node-id")).filter(Boolean);+ if (checkIDs.length > 0 && !await confirmBlockRef({+ scope: "blocks",+ ids: checkIDs,+ notebook: protyle.notebookId,+ }, protyle)) {+ return;+ }+ if (checkElements.some(item => !item.isConnected)) {+ return;+ }+ }+ }let html = "";let textPlain = "";let isInCodeBlock = false;diff --git a/app/src/protyle/wysiwyg/remove.ts b/app/src/protyle/wysiwyg/remove.tsindex edcb52cf4e..2207cc90d7 100644--- a/app/src/protyle/wysiwyg/remove.ts+++ b/app/src/protyle/wysiwyg/remove.ts@@ -46,12 +46,11 @@ import {isEncryptedBox} from "../../util/pathName";import {highlightRender} from "../render/highlightRender";import * as dayjs from "dayjs";import {mergeSameInlineElement} from "../toolbar/util";-import {getCrossBlockMergeRemoveElement} from "./removeRange";+import {getCrossBlockMergeRemoveElement, isEntireBlockContentSelected} from "./removeRange";import {confirmBlockRef} from "../../util/checkBlockRef";-export const removeCrossBlockRange = async (protyle: IProtyle, selectedRange: Range,- startElement: HTMLElement, endElement: HTMLElement) => {- const editorElement = protyle.wysiwyg.element;+const getCrossBlockRemovalContext = (editorElement: HTMLElement, selectedRange: Range,+ startElement: HTMLElement, endElement: HTMLElement) => {const ranges = getBlockRanges(editorElement, selectedRange);const selectedElements: HTMLElement[] = [];selectedRange.cloneContents().querySelectorAll<HTMLElement>("[data-node-id]").forEach(item => {@@ -104,18 +103,73 @@ export const removeCrossBlockRange = async (protyle: IProtyle, selectedRange: Raconst updateElements = Array.from(rangesByBlock.keys()).filter(item => {return !removeElements.some(removeElement => removeElement.contains(item));});+ return {+ ranges,+ removeElements,+ rangesByBlock,+ startEditableElement,+ endEditableElement,+ removeEndElement,+ updateElements,+ };+};++const getCrossBlockRefCheckElementsFromContext = (context: ReturnType<typeof getCrossBlockRemovalContext>) => {+ const elementsByID = new Map<string, HTMLElement>();+ context.removeElements.forEach(item => {+ const id = item.getAttribute("data-node-id");+ if (id) {+ elementsByID.set(id, item);+ }+ });+ context.ranges.forEach(item => {+ const contentRange = document.createRange();+ contentRange.selectNodeContents(item.editableElement);+ const contentPosition = getSelectionOffset(item.editableElement, undefined, contentRange);+ if (!isEntireBlockContentSelected(item.start, item.end, contentPosition.end)) {+ return;+ }+ const topElement = getTopAloneElement(item.blockElement) as HTMLElement;+ const id = topElement.getAttribute("data-node-id");+ if (id) {+ elementsByID.set(id, topElement);+ }+ });+ return Array.from(elementsByID.values());+};++export const getCrossBlockRefCheckElements = (editorElement: HTMLElement, selectedRange: Range,+ startElement: HTMLElement, endElement: HTMLElement) => {+ return getCrossBlockRefCheckElementsFromContext(+ getCrossBlockRemovalContext(editorElement, selectedRange, startElement, endElement));+};++export const removeCrossBlockRange = async (protyle: IProtyle, selectedRange: Range,+ startElement: HTMLElement, endElement: HTMLElement) => {+ const editorElement = protyle.wysiwyg.element;+ const context = getCrossBlockRemovalContext(editorElement, selectedRange, startElement, endElement);+ const {+ ranges,+ removeElements,+ rangesByBlock,+ startEditableElement,+ endEditableElement,+ removeEndElement,+ updateElements,+ } = context;if (removeElements.length === 0 && updateElements.length === 0) {return;}- const removeIDs = removeElements.map(item => item.getAttribute("data-node-id")).filter(Boolean);- if (removeIDs.length > 0 && !await confirmBlockRef({+ const checkElements = getCrossBlockRefCheckElementsFromContext(context);+ const checkIDs = checkElements.map(item => item.getAttribute("data-node-id")).filter(Boolean);+ if (checkIDs.length > 0 && !await confirmBlockRef({scope: "blocks",- ids: removeIDs,+ ids: checkIDs,notebook: protyle.notebookId,}, protyle)) {return;}- if (removeElements.some(item => !item.isConnected || item.getAttribute("data-node-id") === null)) {+ if (checkElements.some(item => !item.isConnected || item.getAttribute("data-node-id") === null)) {return;}diff --git a/app/src/protyle/wysiwyg/removeRange.test.ts b/app/src/protyle/wysiwyg/removeRange.test.tsindex 164cf7b233..1c19cc7624 100644--- a/app/src/protyle/wysiwyg/removeRange.test.ts+++ b/app/src/protyle/wysiwyg/removeRange.test.ts@@ -1,6 +1,6 @@import {describe, it} from "node:test";import * as assert from "node:assert/strict";-import {getCrossBlockMergeRemoveElement} from "./removeRange";+import {getCrossBlockMergeRemoveElement, isEntireBlockContentSelected} from "./removeRange";class TestElement {parentElement: TestElement | null = null;@@ -55,6 +55,15 @@ const block = (name: string, type: string, ...children: TestElement[]) =>const attr = (name: string) => new TestElement(name);const asHTMLElement = (element: TestElement) => element as unknown as HTMLElement;+describe("isEntireBlockContentSelected", () => {+ it("requires the selection to cover the complete editable content", () => {+ assert.equal(isEntireBlockContentSelected(0, 3, 3), true);+ assert.equal(isEntireBlockContentSelected(0, 4, 3), true);+ assert.equal(isEntireBlockContentSelected(1, 3, 3), false);+ assert.equal(isEntireBlockContentSelected(0, 2, 3), false);+ });+});+describe("getCrossBlockMergeRemoveElement", () => {it("删除多层列表中起点下方的完整分支", () => {const start = block("start", "NodeParagraph");diff --git a/app/src/protyle/wysiwyg/removeRange.ts b/app/src/protyle/wysiwyg/removeRange.tsindex fd35dcebdd..119989d3b5 100644--- a/app/src/protyle/wysiwyg/removeRange.ts+++ b/app/src/protyle/wysiwyg/removeRange.ts@@ -16,6 +16,10 @@ const getListItemElement = (element: HTMLElement, editorElement: HTMLElement) =>}};+export const isEntireBlockContentSelected = (start: number, end: number, contentEnd: number) => {+ return start === 0 && end >= contentEnd;+};+export const getCrossBlockMergeRemoveElement = (editorElement: HTMLElement, startElement: HTMLElement,endElement: HTMLElement) => {let topElement = endElement;