import {fetchPost} from "../../../util/fetch"; import {getColIconByType} from "./col"; import {Constants} from "../../../constants"; import {getCalcValue} from "./cell"; import * as dayjs from "dayjs"; import {unicode2Emoji} from "../../../emoji"; export const avRender = (element: Element, protyle: IProtyle, cb?: () => void) => { let avElements: Element[] = []; if (element.getAttribute("data-type") === "NodeAttributeView") { // 编辑器内代码块编辑渲染 avElements = [element]; } else { avElements = Array.from(element.querySelectorAll('[data-type="NodeAttributeView"]')); } if (avElements.length === 0) { return; } if (avElements.length > 0) { avElements.forEach((e: HTMLElement) => { if (e.getAttribute("data-render") === "true") { return; } const left = e.querySelector(".av__scroll")?.scrollLeft || 0; fetchPost("/api/av/renderAttributeView", { id: e.getAttribute("data-av-id"), }, (response) => { const data = response.data.view as IAVTable; // header let tableHTML = '
'; let calcHTML = ""; data.columns.forEach((column: IAVColumn) => { if (column.hidden) { return; } tableHTML += `
${column.icon ? unicode2Emoji(column.icon, "av__cellicon", true) : ``} ${column.name}
`; calcHTML += `
${getCalcValue(column) || '' + window.siyuan.languages.calc}
`; }); tableHTML += `
`; // body data.rows.forEach((row: IAVRow) => { tableHTML += `
`; row.cells.forEach((cell, index) => { if (data.columns[index].hidden) { return; } let text = ""; if (["text", "template"].includes(cell.valueType)) { text = `${cell.value ? (cell.value[cell.valueType as "text"].content || "") : ""}`; } else if (["url", "email", "phone"].includes(cell.valueType)) { const urlContent = cell.value ? cell.value[cell.valueType as "url"].content : ""; // https://github.com/siyuan-note/siyuan/issues/9291 let urlAttr = ""; if (cell.valueType === "url") { urlAttr = ` data-href="${urlContent}"`; } text = `${urlContent}`; if (cell.value && cell.value[cell.valueType as "url"].content) { text += ``; } } else if (cell.valueType === "block") { text = `${cell.value.block.content || ""}`; if (cell.value?.isDetached) { text += `${window.siyuan.languages.more}`; } else { text += `${window.siyuan.languages.openBy}`; } } else if (cell.valueType === "number") { text = `${cell.value?.number.formattedContent || ""}`; } else if (cell.valueType === "mSelect" || cell.valueType === "select") { cell.value?.mSelect?.forEach((item) => { text += `${item.content}`; }); if (!text) { text = ''; } else { text = `${text}`; } } else if (cell.valueType === "date") { text = ''; if (cell.value?.date.isNotEmpty) { text += dayjs(cell.value.date.content).format("YYYY-MM-DD HH:mm"); } if (cell.value?.date.hasEndDate && cell.value?.date.isNotEmpty && cell.value?.date.isNotEmpty2) { text += `${dayjs(cell.value.date.content2).format("YYYY-MM-DD HH:mm")}`; } text += ""; } else if (cell.valueType === "mAsset") { cell.value?.mAsset?.forEach((item) => { if (item.type === "image") { text += ``; } else { text += `${item.name}`; } }); if (!text) { text = ''; } else { text = `${text}`; } } tableHTML += `
${text}
`; }); tableHTML += "
"; }); let tabHTML = ""; response.data.views.forEach((item: IAVView) => { tabHTML += `
${item.name}
`; }); const paddingLeft = e.parentElement.style.paddingLeft; const paddingRight = e.parentElement.style.paddingRight; if (e.parentElement.clientWidth > 0) { e.style.width = e.parentElement.clientWidth + "px"; } e.style.alignSelf = "center"; e.firstElementChild.outerHTML = `
${tabHTML}
${response.data.name || ""}
${tableHTML}
${window.siyuan.languages.addAttr}
`; e.setAttribute("data-render", "true"); e.querySelector(".av__scroll").scrollLeft = left; if (cb) { cb(); } }); }); } }; let lastParentID: string; let lastElement: HTMLElement; export const refreshAV = (protyle: IProtyle, operation: IOperation) => { if (lastParentID === operation.parentID && protyle.contentElement.isSameNode(lastElement)) { return; } lastElement = protyle.contentElement; lastParentID = operation.parentID; const avId = operation.avID; if (operation.action === "setAttrViewColWidth") { Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-av-id="${avId}"]`)).forEach((item: HTMLElement) => { const cellElement = item.querySelector(`.av__cell[data-col-id="${operation.id}"]`) as HTMLElement; if (!cellElement || cellElement.style.width === operation.data) { return; } item.querySelectorAll(".av__row").forEach(rowItem => { (rowItem.querySelector(`[data-col-id="${operation.id}"]`) as HTMLElement).style.width = operation.data; }); }); } else if (operation.action === "setAttrViewName") { Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-av-id="${avId}"]`)).forEach((item: HTMLElement) => { const titleElement = item.querySelector(".av__title") as HTMLElement; if (!titleElement || titleElement.textContent.trim() === operation.data) { return; } titleElement.textContent = operation.data; titleElement.dataset.title = operation.data; }); } else { Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-av-id="${avId}"]`)).forEach((item: HTMLElement) => { item.removeAttribute("data-render"); avRender(item, protyle); }); } setTimeout(() => { lastParentID = null; }, Constants.TIMEOUT_TRANSITION); };