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: Support interactions with rendered HTML elements in database template fields https://github.com/siyuan-note/siyuan/issues/12012
Daniel8fcb468b3b40
diff --git a/app/src/protyle/render/av/action.ts b/app/src/protyle/render/av/action.tsindex 6c6ab1823b..e55d0ea2ce 100644--- a/app/src/protyle/render/av/action.ts+++ b/app/src/protyle/render/av/action.ts@@ -50,6 +50,7 @@ import {resetCardCoverPosition,startCardCoverPosition} from "./coverPosition";+import {getAVTemplateInteractiveElement, isAVTemplateLink} from "./attributeValue";const isDetachedDatabaseCell = (cellElement: HTMLElement) => {return cellElement.dataset.detached === "true" || !cellElement.querySelector(".av__celltext--ref");@@ -95,6 +96,14 @@ const setGroupFolded = (foldElement: HTMLElement, folded: boolean) => {let foldTimeout: number;export const avClick = (protyle: IProtyle, event: MouseEvent & { target: HTMLElement }) => {+ const templateInteractiveElement = getAVTemplateInteractiveElement(event.target);+ if (templateInteractiveElement) {+ if (isAVTemplateLink(templateInteractiveElement)) {+ event.preventDefault();+ }+ event.stopPropagation();+ return true;+ }if (isOnlyMeta(event)) {return false;}diff --git a/app/src/protyle/render/av/attributeValue.ts b/app/src/protyle/render/av/attributeValue.tsindex f51b19ac69..04bd828c50 100644--- a/app/src/protyle/render/av/attributeValue.ts+++ b/app/src/protyle/render/av/attributeValue.ts@@ -33,6 +33,44 @@ export const getAVTemplateHTML = (content: string) => {return window.DOMPurify.sanitize(content);};+const AV_TEMPLATE_INTERACTIVE_SELECTOR = [+ "a[href]",+ "button",+ "input",+ "select",+ "textarea",+ "label",+ "summary",+ "audio[controls]",+ "video[controls]",+ '[contenteditable=""]',+ '[contenteditable="true"]',+ '[contenteditable="plaintext-only"]',+ "[onclick]",+ '[role="button"]',+ '[role="link"]',+ "[data-av-interactive]",+ '[data-type~="a"][data-href]',+].join(",");++export const getAVTemplateInteractiveElement = (target: EventTarget | null) => {+ if (!(target instanceof Element)) {+ return;+ }+ const templateElement = target.closest(".av__celltext--template");+ if (!templateElement) {+ return;+ }+ const interactiveElement = target.closest<HTMLElement>(AV_TEMPLATE_INTERACTIVE_SELECTOR);+ if (interactiveElement && templateElement.contains(interactiveElement)) {+ return interactiveElement;+ }+};++export const isAVTemplateLink = (element: Element) => {+ return element.matches('a[href], [data-type~="a"][data-href]');+};+const genAVRollupHTML = (value: IAVCellValue) => {let html = "";const dataValue: IAVCellDateValue = value[value.type as "date"];@@ -140,7 +178,7 @@ export const genAVValueHTML = (value: IAVCellValue) => {html = `<svg class="av__checkbox"><use xlink:href="#icon${value.checkbox.checked ? "Check" : "Uncheck"}"></use></svg>`;break;case "template":- html = `<div class="fn__flex-1" placeholder="${window.siyuan.languages.empty}">${getAVTemplateHTML(value.template.content)}</div>`;+ html = `<div class="fn__flex-1 av__celltext--template" placeholder="${window.siyuan.languages.empty}">${getAVTemplateHTML(value.template.content)}</div>`;break;case "email":html = `<input value="${escapeAttr(value.email.content)}" class="b3-text-field b3-text-field--text fn__flex-1" placeholder="${window.siyuan.languages.empty}">diff --git a/app/src/protyle/render/av/blockAttr.ts b/app/src/protyle/render/av/blockAttr.tsindex 2309e21fa1..1e260916e8 100644--- a/app/src/protyle/render/av/blockAttr.ts+++ b/app/src/protyle/render/av/blockAttr.ts@@ -18,7 +18,12 @@ import {Constants} from "../../../constants";import {removeCompressURL} from "../../../util/image";import {openDatabaseRowByData} from "./openDatabaseRow";import {confirmDialog} from "../../../dialog/confirmDialog";-import {createEmptyAVValue, genAVAttributeRowHTML} from "./attributeValue";+import {+ createEmptyAVValue,+ genAVAttributeRowHTML,+ getAVTemplateInteractiveElement,+ isAVTemplateLink+} from "./attributeValue";import {isLastPointerMouse} from "../../../util/touchDragBridge";interface IAVAttributeTableData {@@ -45,6 +50,23 @@ const attributeTableData = new WeakMap<HTMLElement, IAVAttributeTableData>();let attributeViewRenderID = 0;+const handleTemplateInteraction = (protyle: IProtyle, event: MouseEvent) => {+ const templateInteractiveElement = getAVTemplateInteractiveElement(event.target);+ if (!templateInteractiveElement) {+ return false;+ }+ if (isAVTemplateLink(templateInteractiveElement)) {+ const link = templateInteractiveElement.getAttribute("data-href") ||+ templateInteractiveElement.getAttribute("href");+ if (link) {+ openLink(protyle.app, link, event, event.ctrlKey || event.metaKey);+ event.preventDefault();+ }+ }+ event.stopPropagation();+ return true;+};+export const renderAVAttribute = (element: HTMLElement, id: string, protyle: IProtyle, cb?: (element: HTMLElement) => void,row?: { avID: string, itemID: string, valueID: string }) => {const renderID = (++attributeViewRenderID).toString();@@ -264,6 +286,9 @@ export const renderAVAttribute = (element: HTMLElement, id: string, protyle: IPr}});element.addEventListener("click", (event) => {+ if (handleTemplateInteraction(protyle, event)) {+ return;+ }const databaseElement = hasClosestByClassName(event.target as HTMLElement, "popover__block");if (databaseElement && isTouchDevice() && !isLastPointerMouse() &&hasClosestByClassName(databaseElement, "custom-attr__avheader")) {diff --git a/app/src/protyle/render/av/cell.ts b/app/src/protyle/render/av/cell.tsindex 47d3a64032..bf48f41df5 100644--- a/app/src/protyle/render/av/cell.ts+++ b/app/src/protyle/render/av/cell.ts@@ -1035,7 +1035,7 @@ export const renderCell = (cellValue: IAVCellValue, rowIndex = 0, showIcon = truselectOptions?: IAVColumn["options"]) => {let text = "";if ("template" === cellValue.type) {- text = `<span class="av__celltext">${cellValue ? getAVTemplateHTML(cellValue.template.content || "") : ""}</span>`;+ text = `<span class="av__celltext av__celltext--template">${cellValue ? getAVTemplateHTML(cellValue.template.content || "") : ""}</span>`;} else if ("text" === cellValue.type) {text = `<span class="av__celltext">${cellValue ? Lute.EscapeHTMLStr(cellValue.text.content || "") : ""}</span>`;} else if (["email", "phone"].includes(cellValue.type)) {diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.tsindex 209b838cde..81824506d6 100644--- a/app/src/protyle/wysiwyg/index.ts+++ b/app/src/protyle/wysiwyg/index.ts@@ -114,6 +114,7 @@ import {isEncryptedBox, parseSiYuanUriInfo} from "../../util/pathName";import {processSiYuanUri} from "../../util/uri";import {enhanceRichClipboard} from "../util/richClipboard";import {addSpellcheckMenuItems, requestSpellcheckContext} from "../../menus/spellcheck";+import {getAVTemplateInteractiveElement, isAVTemplateLink} from "../render/av/attributeValue";export class WYSIWYG {public lastHTMLs: { [key: string]: string } = {};@@ -312,6 +313,10 @@ export class WYSIWYG {private bindCommonEvent(protyle: IProtyle) {this.element.addEventListener("copy", async (event: ClipboardEvent & { target: HTMLElement }) => {window.siyuan.ctrlIsPressed = false; // https://github.com/siyuan-note/siyuan/issues/6373+ if (getAVTemplateInteractiveElement(event.target)) {+ event.stopPropagation();+ return;+ }// https://github.com/siyuan-note/siyuan/issues/4600if (event.target.tagName === "PROTYLE-HTML" || event.target.localName === "input") {event.stopPropagation();@@ -631,6 +636,10 @@ export class WYSIWYG {// 右键return;}+ if (getAVTemplateInteractiveElement(event.target)) {+ event.stopPropagation();+ return;+ }const documentSelf = document;documentSelf.onmouseup = null;let target = event.target as HTMLElement;@@ -2175,7 +2184,11 @@ export class WYSIWYG {});});- this.element.addEventListener("focusout", () => {+ this.element.addEventListener("focusout", (event) => {+ if (getAVTemplateInteractiveElement(event.target)) {+ event.stopPropagation();+ return;+ }if (getSelection().rangeCount === 0) {return;}@@ -2187,6 +2200,10 @@ export class WYSIWYG {this.element.addEventListener("cut", async (event: ClipboardEvent & { target: HTMLElement }) => {window.siyuan.ctrlIsPressed = false; // https://github.com/siyuan-note/siyuan/issues/6373+ if (getAVTemplateInteractiveElement(event.target)) {+ event.stopPropagation();+ return;+ }if (protyle.disabled) {return;}@@ -2854,6 +2871,10 @@ export class WYSIWYG {}, {passive: true});this.element.addEventListener("paste", (event: ClipboardEvent & { target: HTMLElement }) => {+ if (getAVTemplateInteractiveElement(event.target)) {+ event.stopPropagation();+ return;+ }// https://github.com/siyuan-note/siyuan/issues/11241if (hasClosestByAttribute(event.target, "data-type", "av-search")) {return;@@ -2892,6 +2913,10 @@ export class WYSIWYG {// 记录组合开始时的光标位置,用于取消组合后恢复光标(输入法删空候选词触发 compositionend 时浏览器会把光标移出可编辑单元格)let compositionRange: { cell: HTMLElement; offset: number };this.element.addEventListener("compositionstart", (event) => {+ if (getAVTemplateInteractiveElement(event.target)) {+ event.stopPropagation();+ return;+ }isComposition = true;// 微软双拼由于 focusByRange 导致无法输入文字,因此不再 keydown 中记录了,但 keyup 会记录拼音字符,因此使用 isComposition 阻止 keyup 记录。// 但搜狗输入法选中后继续输入不走 keydown,isComposition 阻止了 keyup 记录,因此需在此记录。@@ -2919,6 +2944,9 @@ export class WYSIWYG {this.element.addEventListener("compositionend", (event: InputEvent) => {event.stopPropagation();+ if (getAVTemplateInteractiveElement(event.target)) {+ return;+ }isComposition = false;const range = getEditorRange(this.element);const blockElement = hasClosestBlock(range.startContainer);@@ -2955,6 +2983,10 @@ export class WYSIWYG {});this.element.addEventListener("input", (event: InputEvent) => {+ if (getAVTemplateInteractiveElement(event.target)) {+ event.stopPropagation();+ return;+ }if (this.preventInput) {event.stopPropagation();return;@@ -3008,6 +3040,10 @@ export class WYSIWYG {});this.element.addEventListener("keyup", (event) => {+ if (getAVTemplateInteractiveElement(event.target)) {+ event.stopPropagation();+ return;+ }const range = getEditorRange(this.element).cloneRange();const nodeElement = hasClosestBlock(range.startContainer);@@ -3090,6 +3126,10 @@ export class WYSIWYG {event.stopPropagation();return;}+ if (getAVTemplateInteractiveElement(event.target)) {+ event.stopPropagation();+ return;+ }const target = event.target as HTMLElement;// 双击超级块拖拽手柄,均分所有列宽if (target.classList.contains("sb__resize")) {@@ -3147,6 +3187,11 @@ export class WYSIWYG {event});});+ const templateInteractiveElement = getAVTemplateInteractiveElement(event.target);+ if (templateInteractiveElement && !isAVTemplateLink(templateInteractiveElement)) {+ event.stopPropagation();+ return;+ }const ctrlIsPressed = isOnlyMeta(event);const backlinkBreadcrumbItemElement = hasClosestByClassName(event.target, "protyle-breadcrumb__item");if (backlinkBreadcrumbItemElement) {@@ -3210,9 +3255,13 @@ export class WYSIWYG {setFirstNodeRange(range.startContainer.parentElement.querySelector(".hljs").lastElementChild, range);}// 需放在嵌入块之前,否则嵌入块内的引用、链接、pdf 双链无法点击打开 https://ld246.com/article/1630479789513+ const templateLinkElement = templateInteractiveElement &&+ isAVTemplateLink(templateInteractiveElement) ? templateInteractiveElement : undefined;const aElement = hasClosestByAttribute(event.target, "data-type", "a") ||- hasClosestByClassName(event.target, "av__celltext--url"); // 数据库中资源文件、链接、电话、邮箱单元格- let aLink = aElement ? (aElement.getAttribute("data-href") || "") : "";+ hasClosestByClassName(event.target, "av__celltext--url") || // 数据库中资源文件、链接、电话、邮箱单元格+ templateLinkElement;+ let aLink = aElement ? (aElement.getAttribute("data-href") ||+ (templateLinkElement ? aElement.getAttribute("href") : "") || "") : "";if (aElement && !aLink && aElement.classList.contains("av__celltext--url")) {aLink = aElement.textContent.trim();if (aElement.dataset.type === "phone") {diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.tsindex 3f46d9dddb..74da12366d 100644--- a/app/src/protyle/wysiwyg/keydown.ts+++ b/app/src/protyle/wysiwyg/keydown.ts@@ -78,6 +78,7 @@ import {AIChat} from "../../ai/chat";import {updateCalloutType} from "./callout";import {tabCodeBlock} from "./codeBlock";import {getTopBarHeight} from "../../layout/getTopBarHeight";+import {getAVTemplateInteractiveElement} from "../render/av/attributeValue";export const getContentByInlineHTML = (range: Range, cb: (content: string) => void) => {let html = "";@@ -99,6 +100,10 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {event.stopPropagation();return;}+ if (getAVTemplateInteractiveElement(event.target)) {+ event.stopPropagation();+ return;+ }if (hasClosestByAttribute(event.target, "data-type", "av-search")) {if (matchHotKey("⌘A", event)) {event.preventDefault();