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: Stabilize semantic inline marker editing https://github.com/siyuan-note/siyuan/issues/11248
Danielf524adbdc7a0
diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.tsindex af69eafec7..cb6d7d1c2c 100644--- a/app/src/protyle/toolbar/index.ts+++ b/app/src/protyle/toolbar/index.ts@@ -78,6 +78,7 @@ import {emitToPlugins, forEachPluginSubscriber, hasPluginSubscriber} from "../..import {getSelectionElementAvailableHeight, getSelectionElementY} from "./selectionElementPosition";import {getInlinePlaceholder,+ getSemanticInlineFirstTextNode,getSemanticInlineVisibleText,getSemanticInternalMarkerPrefixLength,getSemanticMarkerPrefixLengthForNode,@@ -1208,7 +1209,15 @@ export class Toolbar {(newNodes[0].nodeType !== Node.TEXT_NODE && hasSemanticInlineType((newNodes[0] as HTMLElement).getAttribute("data-type")) &&getSemanticInternalMarkerPrefixLength(newNodes[0].textContent) === newNodes[0].textContent.length))) {- this.range.setStart(newNodes[0], 1);+ const semanticTextNode = newNodes[0].nodeType === Node.ELEMENT_NODE &&+ hasSemanticInlineType((newNodes[0] as HTMLElement).getAttribute("data-type")) ?+ getSemanticInlineFirstTextNode(newNodes[0] as HTMLElement) : undefined;+ if (semanticTextNode) {+ this.range.setStart(semanticTextNode,+ getSemanticInternalMarkerPrefixLength(semanticTextNode.data));+ } else {+ this.range.setStart(newNodes[0], 1);+ }this.range.collapse(true);if (newNodes[0].nodeType !== 3) {// 不选中后,ctrl+g 光标重置@@ -1304,9 +1313,6 @@ export class Toolbar {} else {const currentType = currentNode.nodeType === 3 ? [] : (currentNode.getAttribute("data-type") || "").split(" ");if (currentType.includes("code") || currentType.includes("tag") || currentType.includes("kbd")) {- if (!currentNode.textContent.startsWith(Constants.ZWSP)) {- currentNode.insertAdjacentText("afterbegin", Constants.ZWSP);- }if (!previousElement || (previousElement.nodeType === 3 && previousElement.textContent.endsWith("\n"))) {currentNode.insertAdjacentText("beforebegin", Constants.ZWSP);}diff --git a/app/src/protyle/util/inlineElementMarker.ts b/app/src/protyle/util/inlineElementMarker.tsindex bed676d5c8..961577dc77 100644--- a/app/src/protyle/util/inlineElementMarker.ts+++ b/app/src/protyle/util/inlineElementMarker.ts@@ -83,12 +83,18 @@ export const isSemanticBackwardDeleteBoundary = (text: string, offset: number) =getSemanticInternalMarkerPrefixLength(text) > 0 && offset <= getSemanticInternalMarkerPrefixLength(text);export const setSemanticInlineElementMarker = (element: HTMLElement, mode: TSemanticInlineMarkerMode) => {+ // 合并浏览器编辑产生的相邻文本节点,确保语义元素只保留一个内部标记。+ element.normalize();let textNode = getFirstTextNode(element);if (!textNode) {textNode = element.ownerDocument.createTextNode("");element.prepend(textNode);}- textNode.data = normalizeSemanticInternalMarkerPrefix(textNode.data, mode);+ const normalizedText = normalizeSemanticInternalMarkerPrefix(textNode.data, mode);+ // 对文本节点重复赋值会重置 Chromium 中位于该节点内的光标偏移。+ if (textNode.data !== normalizedText) {+ textNode.data = normalizedText;+ }};const ensureLeftExternalBoundary = (element: HTMLElement) => {@@ -332,6 +338,45 @@ const setRangeAfterRightBoundary = (range: Range, element: HTMLElement) => {return true;};+export const removeEmptySemanticInlineElement = (range: Range, element: HTMLElement) => {+ if (stripSemanticInternalMarkerPrefix(element.textContent || "") !== "") {+ return false;+ }+ const parentElement = element.parentElement;+ if (!parentElement) {+ return false;+ }+ removeSemanticInlineExternalBoundaries(element);+ const caretElement = element.ownerDocument.createElement("wbr");+ element.before(caretElement);+ element.remove();+ normalizeSemanticInlineElements(parentElement);+ range.setStartBefore(caretElement);+ range.collapse(true);+ caretElement.remove();+ return true;+};++export const getEmptySemanticInlineForDelete = (range: Range, forward: boolean) => {+ if (!range.collapsed || range.startContainer.nodeType !== Node.TEXT_NODE) {+ return;+ }+ const textNode = range.startContainer as Text;+ const semanticElement = textNode.parentElement?.closest("span[data-type]") as HTMLElement | null;+ if (isSemanticInlineElement(semanticElement) &&+ stripSemanticInternalMarkerPrefix(semanticElement.textContent || "") === "") {+ return semanticElement;+ }+ const adjacentSemantic = getAdjacentSemanticInline(textNode, !forward);+ const atAdjacentBoundary = forward ?+ range.startOffset >= textNode.data.length - (textNode.data.match(/\u200b+$/u)?.[0].length || 0) :+ range.startOffset <= (textNode.data.match(/^\u200b+/u)?.[0].length || 0);+ if (atAdjacentBoundary && adjacentSemantic &&+ stripSemanticInternalMarkerPrefix(adjacentSemantic.textContent || "") === "") {+ return adjacentSemantic;+ }+};+export const moveCaretAcrossSemanticMarker = (range: Range, direction: "left" | "right") => {if (!range.collapsed || range.startContainer.nodeType !== Node.TEXT_NODE) {return false;@@ -399,12 +444,27 @@ export const moveCaretForSemanticDelete = (range: Range, forward: boolean) => {if (nextSemantic && range.startOffset >= textNode.data.length - boundaryLength) {return setRangeAtSemanticStart(range, nextSemantic);}+ const previousSemantic = getAdjacentSemanticInline(textNode, true);+ const leadingBoundaryLength = textNode.data.match(/^\u200b+/u)?.[0].length || 0;+ if (previousSemantic && leadingBoundaryLength > 0 && range.startOffset < leadingBoundaryLength) {+ range.setStart(textNode, leadingBoundaryLength);+ range.collapse(true);+ return true;+ }} else {const previousSemantic = getAdjacentSemanticInline(textNode, true);const boundaryLength = textNode.data.match(/^\u200b+/u)?.[0].length || 0;if (previousSemantic && range.startOffset <= boundaryLength) {return setRangeAtSemanticEnd(range, previousSemantic);}+ const nextSemantic = getAdjacentSemanticInline(textNode, false);+ const trailingBoundaryLength = textNode.data.match(/\u200b+$/u)?.[0].length || 0;+ if (nextSemantic && trailingBoundaryLength > 0 &&+ range.startOffset > textNode.data.length - trailingBoundaryLength) {+ range.setStart(textNode, textNode.data.length - trailingBoundaryLength);+ range.collapse(true);+ return true;+ }}return false;};diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.tsindex 9a445a3cf4..46796ced6b 100644--- a/app/src/protyle/wysiwyg/keydown.ts+++ b/app/src/protyle/wysiwyg/keydown.ts@@ -19,11 +19,13 @@ import {} from "../util/selection";import {selectTextToEditorBoundary} from "../util/selectionBoundary";import {+ getEmptySemanticInlineForDelete,getSemanticInlineVisibleText,getSemanticMarkerPrefixLengthForNode,hasSemanticInlineType,moveCaretAcrossSemanticMarker,- moveCaretForSemanticDelete+ moveCaretForSemanticDelete,+ removeEmptySemanticInlineElement} from "../util/inlineElementMarker";import {hasUnloadedDocumentBlocks} from "../util/documentRange";import {@@ -1243,7 +1245,20 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {return;}if (selectText === "" && range.collapsed) {- if (moveCaretForSemanticDelete(range, event.key === "Delete" || matchHotKey("⌃D", event))) {+ const forward = event.key === "Delete" || matchHotKey("⌃D", event);+ const emptySemanticElement = getEmptySemanticInlineForDelete(range, forward);+ if (emptySemanticElement) {+ const oldHTML = nodeElement.outerHTML;+ if (removeEmptySemanticInlineElement(range, emptySemanticElement)) {+ nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));+ updateTransaction(protyle, nodeElement, oldHTML);+ focusByRange(range);+ event.stopPropagation();+ event.preventDefault();+ return;+ }+ }+ if (moveCaretForSemanticDelete(range, forward)) {focusByRange(range);event.stopPropagation();event.preventDefault();diff --git a/app/src/protyle/wysiwyg/remove.ts b/app/src/protyle/wysiwyg/remove.tsindex 0163ce6e6d..94d31d1141 100644--- a/app/src/protyle/wysiwyg/remove.ts+++ b/app/src/protyle/wysiwyg/remove.ts@@ -81,7 +81,13 @@ import {setBlockSelectionModeElement} from "./blockSelection";import {countBlockWord} from "../../layout/status";-import {getTextWithoutSemanticMarkers} from "../util/inlineElementMarker";+import {+ getSemanticInlineVisibleText,+ getTextWithoutSemanticMarkers,+ hasSemanticInlineType,+ normalizeSemanticInlineElements,+ removeSemanticInlineExternalBoundaries+} from "../util/inlineElementMarker";export interface IBlockRefCheckTargets {elements: HTMLElement[];@@ -390,12 +396,22 @@ const deleteCrossBlockRangeContents = (rangesByBlock: ReturnType<typeof getCross}});boundarySpans.forEach(spanElement => {- if (spanElement.isConnected && spanElement.textContent === "" && !spanElement.querySelector("img")) {+ const isSemanticInline = hasSemanticInlineType(spanElement.getAttribute("data-type"));+ const isEmpty = isSemanticInline ?+ getSemanticInlineVisibleText(spanElement) === "" : spanElement.textContent === "";+ if (spanElement.isConnected && isEmpty && !spanElement.querySelector("img")) {+ if (isSemanticInline) {+ removeSemanticInlineExternalBoundaries(spanElement);+ }spanElement.remove();}});- fixAdjacentTags(item.editableElement);});+ const editableElement = blockRanges[0]?.editableElement;+ if (editableElement?.isConnected) {+ fixAdjacentTags(editableElement);+ normalizeSemanticInlineElements(editableElement);+ }});};@@ -811,8 +827,14 @@ export const removeCrossBlockRange = async (protyle: IProtyle, selectedRange: Ra}startEditableElement.normalize();fixAdjacentTags(startEditableElement);+ normalizeSemanticInlineElements(startEditableElement);}removeElements.forEach(item => item.remove());+ updateElements.forEach(item => {+ if (item.isConnected) {+ normalizeSemanticInlineElements(item);+ }+ });const firstRange = ranges.find(item => item.editableElement.isConnected);let replacementRange: Range;