An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:sparkles: Improve empty block conversion menu https://github.com/siyuan-note/siyuan/issues/14744
Daniel6b7204c97ca7
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.tsindex 77e34caec8..cb2191aa80 100644--- a/app/src/protyle/gutter/index.ts+++ b/app/src/protyle/gutter/index.ts@@ -30,6 +30,8 @@ import {turnsIntoOneTransaction,turnsIntoTransaction,turnsOneInto,+ isEmptyParagraph,+ turnEmptyParagraphsIntoTransaction,updateBatchTransaction,updateTransaction} from "../wysiwyg/transaction";@@ -874,6 +876,54 @@ export class Gutter {};}+ private emptyParagraphTurnIntoMenu(protyle: IProtyle, nodeElements: Element[]): IMenu[] {+ if (!nodeElements.every(isEmptyParagraph)) {+ return [];+ }+ const items: {+ id: string,+ icon: string,+ label: string,+ accelerator?: string,+ type: "code" | "table" | "line" | "math",+ }[] = [{+ id: "code",+ icon: "iconCode",+ label: window.siyuan.languages.code,+ accelerator: window.siyuan.config.keymap.editor.insert.code.custom,+ type: "code",+ }, {+ id: "table",+ icon: "iconTable",+ label: window.siyuan.languages.table,+ accelerator: window.siyuan.config.keymap.editor.insert.table.custom,+ type: "table",+ }, {+ id: "line",+ icon: "iconLine",+ label: window.siyuan.languages.line,+ type: "line",+ }, {+ id: "math",+ icon: "iconMath",+ label: window.siyuan.languages.math,+ type: "math",+ }];+ return items.map(item => ({+ id: item.id,+ icon: item.icon,+ label: item.label,+ accelerator: item.accelerator,+ click() {+ turnEmptyParagraphsIntoTransaction({+ protyle,+ nodeElements,+ type: item.type,+ });+ }+ }));+ }+private showMobileAppearance(protyle: IProtyle) {const toolbarElement = document.getElementById("keyboardToolbar");const dynamicElements = toolbarElement.querySelectorAll("#keyboardToolbar .keyboard__dynamic");@@ -1029,6 +1079,7 @@ export class Gutter {type: "Blocks2Hs",isContinue}));+ turnIntoSubmenu.push(...this.emptyParagraphTurnIntoMenu(protyle, selectsElement));window.siyuan.menus.menu.append(new MenuItem({id: "turnInto",icon: "iconTurnInto",@@ -1704,6 +1755,7 @@ export class Gutter {type: "Callout2Blockquote"}));}+ turnIntoSubmenu.push(...this.emptyParagraphTurnIntoMenu(protyle, [nodeElement]));if (turnIntoSubmenu.length > 0 && allowStructuralMutation) {window.siyuan.menus.menu.append(new MenuItem({id: "turnInto",diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.tsindex 26943293a3..ba1d415e5a 100644--- a/app/src/protyle/wysiwyg/transaction.ts+++ b/app/src/protyle/wysiwyg/transaction.ts@@ -2037,6 +2037,115 @@ export const updateTransaction = (protyle: IProtyle, element: Element, oldHTML:transaction(protyle, doOperations, undoOperations);};+type TEmptyParagraphTarget = "code" | "table" | "line" | "math";++const EMPTY_PARAGRAPH_TARGET_NODE_TYPES: Record<TEmptyParagraphTarget, string> = {+ code: "NodeCodeBlock",+ table: "NodeTable",+ line: "NodeThematicBreak",+ math: "NodeMathBlock",+};++const getEmptyParagraphTargetMarkdown = (type: TEmptyParagraphTarget) => {+ if (type === "code") {+ const language = window.siyuan.storage[Constants.LOCAL_CODELANG] || "";+ return "```" + (Constants.SIYUAN_RENDER_CODE_LANGUAGES.includes(language) ? "" : language) ++ Lute.Caret + "\n```";+ }+ if (type === "table") {+ return `| ${Lute.Caret} | | |+| --- | --- | --- |+| | | |+| | | |`;+ }+ return type === "line" ? "---" : "$$";+};++export const isEmptyParagraph = (element: Element) => {+ if (element.getAttribute("data-type") !== "NodeParagraph") {+ return false;+ }+ const editableElement = getContenteditableElement(element);+ if (!editableElement) {+ return false;+ }+ const contentElement = editableElement.cloneNode(true) as Element;+ contentElement.querySelectorAll("br, wbr").forEach(item => item.remove());+ const text = contentElement.textContent.replace(new RegExp(Constants.ZWSP, "g"), "").trim();+ return text === "" && contentElement.childElementCount === 0;+};++export const turnEmptyParagraphsIntoTransaction = (options: {+ protyle: IProtyle,+ nodeElements: Element[],+ type: TEmptyParagraphTarget,+}) => {+ if (options.nodeElements.length === 0 || !options.nodeElements.every(isEmptyParagraph)) {+ return;+ }++ const replacements = options.nodeElements.map((nodeElement) => {+ const oldElement = nodeElement.cloneNode(true) as HTMLElement;+ oldElement.classList.remove("protyle-wysiwyg--select");+ oldElement.removeAttribute("select-start");+ oldElement.removeAttribute("select-end");+ const editableElement = getContenteditableElement(oldElement);+ editableElement.textContent = getEmptyParagraphTargetMarkdown(options.type);++ const template = document.createElement("template");+ template.innerHTML = options.protyle.lute.SpinBlockDOM(oldElement.outerHTML);+ if (template.content.childElementCount !== 1) {+ return;+ }+ const newElement = template.content.firstElementChild as HTMLElement;+ if (newElement.getAttribute("data-node-id") !== oldElement.getAttribute("data-node-id") ||+ newElement.getAttribute("data-type") !== EMPTY_PARAGRAPH_TARGET_NODE_TYPES[options.type]) {+ return;+ }+ if (newElement.getAttribute("data-type") === "NodeTable") {+ newElement.querySelectorAll("colgroup col").forEach((item: HTMLElement) => {+ item.style.minWidth = "60px";+ });+ }+ return {+ nodeElement,+ oldHTML: oldElement.outerHTML,+ newElement,+ };+ });+ if (replacements.some(item => !item)) {+ return;+ }++ const doOperations: IOperation[] = [];+ const undoOperations: IOperation[] = [];+ const newElements: HTMLElement[] = [];+ replacements.forEach((replacement) => {+ const id = replacement.newElement.getAttribute("data-node-id");+ doOperations.push({+ action: "update",+ id,+ data: replacement.newElement.outerHTML,+ });+ undoOperations.push({+ action: "update",+ id,+ data: replacement.oldHTML,+ });+ replacement.nodeElement.insertAdjacentElement("afterend", replacement.newElement);+ replacement.nodeElement.remove();+ replacement.newElement.setAttribute(Constants.ATTRIBUTE_EDITING, "true");+ newElements.push(replacement.newElement);+ });++ transaction(options.protyle, doOperations, undoOperations);+ processRender(options.protyle.wysiwyg.element);+ highlightRender(options.protyle.wysiwyg.element);+ blockRender(options.protyle, options.protyle.wysiwyg.element);+ focusBlock(newElements[0]);+ hideElements(["gutter"], options.protyle);+};+export const updateBatchTransaction = (nodeElements: Element[], protyle: IProtyle, cb: (e: HTMLElement) => void,focusContext?: Record<string, string>) => {const operations: IOperation[] = [];