Skip to content

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

:bug: Improve blockquote marker conversion https://github.com/siyuan-note/siyuan/issues/14124

Danielc51dc3b434b2
diff --git a/app/src/protyle/wysiwyg/blockquote.test.ts b/app/src/protyle/wysiwyg/blockquote.test.ts
index dcf7a86fd7..489d400331 100644
--- a/app/src/protyle/wysiwyg/blockquote.test.ts
+++ b/app/src/protyle/wysiwyg/blockquote.test.ts
@@ -103,6 +103,16 @@ describe("getBlockquoteContext", () => {
assert.equal(shouldCancelBlockquote(context!), true);
});
+
+ it("does not cancel a blockquote when the current paragraph has a sibling block", () => {
+ const sibling = new TestElement("NodeParagraph", "sibling");
+ const paragraph = new TestElement("NodeParagraph", "paragraph");
+ const blockquote = new TestElement("NodeBlockquote", "blockquote").append(sibling, paragraph, new TestElement());
+ const editor = new TestElement().append(blockquote);
+ const context = getBlockquoteContext(asHTMLElement(paragraph), asHTMLElement(editor));
+
+ assert.equal(shouldCancelBlockquote(context!), false);
+ });
});
describe("isBlockquoteMarker", () => {
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 48e72747fc..b700a63774 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -44,7 +44,7 @@ import {
} from "../../menus/protyle";
import * as dayjs from "dayjs";
import {dropEvent} from "../util/editorCommonEvent";
-import {input} from "./input";
+import {beforeBlockquoteInput, input} from "./input";
import {
getContenteditableElement,
getFirstBlock,
@@ -3549,6 +3549,12 @@ export class WYSIWYG {
}
});
+ this.element.addEventListener("beforeinput", (event: InputEvent) => {
+ if (!isComposition) {
+ beforeBlockquoteInput(protyle, event);
+ }
+ });
+
this.element.addEventListener("input", (event: InputEvent) => {
if (getAVTemplateInteractiveElement(event.target)) {
event.stopPropagation();
diff --git a/app/src/protyle/wysiwyg/input.ts b/app/src/protyle/wysiwyg/input.ts
index 61de99e451..ea1fa3f874 100644
--- a/app/src/protyle/wysiwyg/input.ts
+++ b/app/src/protyle/wysiwyg/input.ts
@@ -1,7 +1,7 @@
-import {focusBlock, focusByOffset, focusByWbr, getSelectionOffset} from "../util/selection";
+import {focusBlock, focusByOffset, focusByWbr, getEditorRange, getSelectionOffset} from "../util/selection";
import {Constants} from "../../constants";
import * as dayjs from "dayjs";
-import {insertEmptyBlockquote, transaction, turnsOneInto, updateTransaction} from "./transaction";
+import {transaction, turnsOneInto, updateTransaction, wrapBlockInBlockquote} from "./transaction";
import {mathRender} from "../render/mathRender";
import {highlightRender} from "../render/highlightRender";
import {
@@ -17,7 +17,7 @@ import {
import {genEmptyBlock} from "../../block/util";
import {blockRender} from "../render/blockRender";
import {hideElements} from "../ui/hideElements";
-import {hasClosestByAttribute, hasClosestByClassName} from "../util/hasClosest";
+import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName} from "../util/hasClosest";
import {fetchPost, fetchSyncPost} from "../../util/fetch";
import {headingTurnIntoList, turnIntoTaskList} from "./turnIntoList";
import {updateAVName} from "../render/av/action";
@@ -31,6 +31,69 @@ interface IInputOperations {
undoContext?: Record<string, string>;
}
+export const beforeBlockquoteInput = (protyle: IProtyle, event: InputEvent) => {
+ if (event.isComposing || !event.cancelable || (event.data !== ">" && event.data !== "》")) {
+ return false;
+ }
+ const range = getEditorRange(protyle.wysiwyg.element);
+ if (!range.collapsed) {
+ return false;
+ }
+ const blockElement = hasClosestBlock(range.startContainer) as HTMLElement;
+ if (!blockElement || blockElement.getAttribute("data-type") !== "NodeParagraph") {
+ return false;
+ }
+ const editElement = getContenteditableElement(blockElement) as HTMLElement;
+ const blockquoteContext = getBlockquoteContext(blockElement, protyle.wysiwyg.element);
+ if (!editElement || !blockquoteContext || !editElement.contains(range.startContainer)) {
+ return false;
+ }
+ const markerPrefixRange = range.cloneRange();
+ markerPrefixRange.setStart(editElement, 0);
+ const markerPrefix = nbsp2space(markerPrefixRange.toString()).split(Constants.ZWSP).join("");
+ const marker = markerPrefix.substring(markerPrefix.lastIndexOf("\n") + 1) + event.data;
+ if (!isBlockquoteMarker(marker)) {
+ return false;
+ }
+ const markerStart = getSelectionOffset(editElement, undefined, markerPrefixRange, true).end - marker.length + 1;
+ const markerRange = marker.length > 1 ?
+ focusByOffset(editElement, markerStart, markerStart + marker.length - 1, false, true) : range.cloneRange();
+ if (!markerRange) {
+ return false;
+ }
+
+ event.preventDefault();
+ event.stopPropagation();
+ const id = blockElement.dataset.nodeId;
+ const oldChildHTML = blockElement.outerHTML;
+ const hasLeadingSpaces = marker.length > 1;
+ if (hasLeadingSpaces) {
+ markerRange.deleteContents();
+ blockElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
+ }
+ if (shouldCancelBlockquote(blockquoteContext)) {
+ markerRange.collapse(true);
+ markerRange.insertNode(document.createElement("wbr"));
+ turnsOneInto({
+ protyle,
+ nodeElement: blockquoteContext.blockquoteElement,
+ id: blockquoteContext.blockquoteElement.dataset.nodeId,
+ type: "CancelBlockquote",
+ undoElement: hasLeadingSpaces ? {
+ id,
+ html: oldChildHTML,
+ } : undefined,
+ });
+ } else {
+ markerRange.collapse(true);
+ markerRange.insertNode(document.createElement("wbr"));
+ wrapBlockInBlockquote(protyle, blockElement, hasLeadingSpaces ? {
+ oldHTML: oldChildHTML,
+ } : undefined);
+ }
+ return true;
+};
+
export const input = async (protyle: IProtyle, blockElement: HTMLElement, range: Range, needRender = true,
event?: InputEvent, inputOperations?: IInputOperations) => {
if (!blockElement.parentElement) {
@@ -177,9 +240,7 @@ export const input = async (protyle: IProtyle, blockElement: HTMLElement, range:
additionalOperations: inputOperations,
});
} else {
- currentWbrElement.remove();
- insertEmptyBlockquote(protyle, blockquoteContext.childElement, {
- updateElement: blockElement,
+ await wrapBlockInBlockquote(protyle, blockElement, {
oldHTML: oldChildHTML,
undoContext: inputOperations?.undoContext,
additionalOperations: inputOperations,
diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts
index 89240f7727..d1cfffa3c4 100644
--- a/app/src/protyle/wysiwyg/transaction.ts
+++ b/app/src/protyle/wysiwyg/transaction.ts
@@ -1453,6 +1453,50 @@ export const insertEmptyBlockquote = (protyle: IProtyle, previousElement: HTMLEl
hideElements(["gutter"], protyle);
};
+export const wrapBlockInBlockquote = async (protyle: IProtyle, blockElement: HTMLElement, options?: {
+ oldHTML: string,
+ undoContext?: Record<string, string>,
+ additionalOperations?: {
+ doOperations: IOperation[],
+ undoOperations: IOperation[],
+ },
+}) => {
+ const range = getEditorRange(protyle.wysiwyg.element);
+ const undoFocusContext = getUndoFocusContext(protyle.wysiwyg.element, range, true);
+ const blockID = blockElement.getAttribute("data-node-id");
+ const operations = await turnsIntoOneTransaction({
+ protyle,
+ selectsElement: [blockElement],
+ type: "Blocks2Blockquote",
+ getOperations: true,
+ });
+ operations.undoOperations[operations.undoOperations.length - 1].context =
+ options?.undoContext || undoFocusContext;
+ if (options) {
+ operations.doOperations.unshift({
+ action: "update",
+ id: blockElement.getAttribute("data-node-id"),
+ data: blockElement.outerHTML,
+ });
+ operations.undoOperations.push({
+ action: "update",
+ id: blockElement.getAttribute("data-node-id"),
+ data: options.oldHTML,
+ context: options.undoContext,
+ });
+ if (options.additionalOperations) {
+ operations.doOperations.unshift(...options.additionalOperations.doOperations);
+ operations.undoOperations.push(...options.additionalOperations.undoOperations);
+ }
+ }
+ transaction(protyle, operations.doOperations, operations.undoOperations);
+ const currentBlockElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${blockID}"]`);
+ if (currentBlockElement && !focusByWbr(currentBlockElement, document.createRange())) {
+ focusBlock(currentBlockElement);
+ }
+ hideElements(["gutter"], protyle);
+};
+
export const turnsIntoGroupsTransaction = async (options: {
protyle: IProtyle,
selectsElementGroups: Element[][],