import {fetchPost} from "../../../util/fetch"; import {getColIconByType} from "./col"; import {Constants} from "../../../constants"; import {getCalcValue, popTextCell} from "./cell"; import * as dayjs from "dayjs"; import {unicode2Emoji} from "../../../emoji"; import {focusBlock} from "../../util/selection"; import {isMac} from "../../util/compatibility"; import {hasClosestByClassName} from "../../util/hasClosest"; import {stickyRow} from "./row"; 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; } let time: number; if (e.firstElementChild.innerHTML === "") { e.style.alignSelf = ""; time = new Date().getTime(); let html = ""; [1, 2, 3].forEach(() => { html += `
`; }); e.firstElementChild.innerHTML = html; } const left = e.querySelector(".av__scroll")?.scrollLeft || 0; const headerTransform = (e.querySelector(".av__row--header") as HTMLElement)?.style.transform; const footerTransform = (e.querySelector(".av__row--footer") as HTMLElement)?.style.transform; let selectCellId = ""; const selectCellElement = e.querySelector(".av__cell--select") as HTMLElement; if (selectCellElement) { selectCellId = (hasClosestByClassName(selectCellElement, "av__row") as HTMLElement).dataset.id + Constants.ZWSP + selectCellElement.getAttribute("data-col-id"); } fetchPost("/api/av/renderAttributeView", { id: e.getAttribute("data-av-id"), }, (response) => { const data = response.data.view as IAVTable; // header let tableHTML = '
'; let calcHTML = '
'; let pinIndex = -1; let pinMaxIndex = -1; let indexWidth = 0; const eWidth = e.clientWidth; data.columns.forEach((item, index) => { if (!item.hidden) { if (item.pin) { pinIndex = index; } if (indexWidth < eWidth - 200) { indexWidth += parseInt(item.width) || 200; pinMaxIndex = index; } } }); pinIndex = Math.min(pinIndex, pinMaxIndex); if (pinIndex > -1) { tableHTML = '
'; calcHTML = '
'; } data.columns.forEach((column: IAVColumn, index: number) => { if (column.hidden) { return; } tableHTML += `
${column.icon ? unicode2Emoji(column.icon, "av__cellicon", true) : ``} ${column.name} ${column.pin ? '
' : ""}
`; if (pinIndex === index) { tableHTML += "
"; } calcHTML += `
${getCalcValue(column) || '' + window.siyuan.languages.calc}
`; if (pinIndex === index) { calcHTML += "
"; } }); tableHTML += `
`; // body data.rows.forEach((row: IAVRow) => { tableHTML += `
`; if (pinIndex > -1) { tableHTML += '
'; } else { 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}`; } 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}`; }); } else if (cell.valueType === "date") { text = ''; const dataValue = cell.value ? cell.value.date : null; if (dataValue && dataValue.isNotEmpty) { text += dayjs(dataValue.content).format(dataValue.isNotTime ? "YYYY-MM-DD" : "YYYY-MM-DD HH:mm"); } if (dataValue && dataValue.hasEndDate && dataValue.isNotEmpty && dataValue.isNotEmpty2) { text += `${dayjs(dataValue.content2).format(dataValue.isNotTime ? "YYYY-MM-DD" : "YYYY-MM-DD HH:mm")}`; } text += ""; } else if (["created", "updated"].includes(cell.valueType)) { text = ''; const dataValue = cell.value ? cell.value[cell.valueType as "date"] : null; if (dataValue && dataValue.isNotEmpty) { text += dayjs(dataValue.content).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", "template", "url", "email", "phone", "number", "date", "created", "updated"].includes(cell.valueType) && cell.value && cell.value[cell.valueType as "url"].content) { text += ``; } tableHTML += `
${text}
`; if (pinIndex === index) { tableHTML += "
"; } }); tableHTML += "
"; }); let tabHTML = ""; response.data.views.forEach((item: IAVView) => { tabHTML += `
${item.name}
`; }); setTimeout(() => { e.firstElementChild.outerHTML = `
${tabHTML}
${response.data.isMirror ? `
` : ""}
${response.data.name || ""}
${tableHTML}
${window.siyuan.languages.addAttr}
`; e.setAttribute("data-render", "true"); // 历史兼容 e.style.margin = ""; if (left) { e.querySelector(".av__scroll").scrollLeft = left; } const editRect = protyle.contentElement.getBoundingClientRect(); if (headerTransform) { (e.querySelector(".av__row--header") as HTMLElement).style.transform = headerTransform; } else { stickyRow(e, editRect, "top"); } if (footerTransform) { (e.querySelector(".av__row--footer") as HTMLElement).style.transform = footerTransform; } else { stickyRow(e, editRect, "bottom"); } if (selectCellId) { const newCellElement = e.querySelector(`.av__row[data-id="${selectCellId.split(Constants.ZWSP)[0]}"] .av__cell[data-col-id="${selectCellId.split(Constants.ZWSP)[1]}"]`); if (newCellElement) { newCellElement.classList.add("av__cell--select"); } if (!document.querySelector(".av__panel")) { focusBlock(e); } } if (cb) { cb(); } }, time ? 256 - (new Date().getTime() - time) : 0); // 为了让动画更好看,需延时到 256ms }); }); } }; let lastParentID: string; let lastElement: HTMLElement; export const refreshAV = (protyle: IProtyle, operation: IOperation, isUndo: boolean) => { if (operation.action === "setAttrViewName") { Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-av-id="${operation.id}"]`)).forEach((item: HTMLElement) => { const titleElement = item.querySelector(".av__title") as HTMLElement; if (!titleElement) { return; } titleElement.textContent = operation.data; titleElement.dataset.title = operation.data; item.querySelector(".layout-tab-bar .item__text").textContent = operation.data; }); } 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 { Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-av-id="${avId}"]`)).forEach((item: HTMLElement) => { item.removeAttribute("data-render"); avRender(item, protyle, () => { // https://github.com/siyuan-note/siyuan/issues/9599 if (!isUndo && operation.action === "insertAttrViewBlock" && operation.isDetached) { popTextCell(protyle, [item.querySelector(`.av__row[data-id="${operation.srcIDs[0]}"] .av__cell[data-detached="true"]`)], "block"); } }); }); } setTimeout(() => { lastParentID = null; }, Constants.TIMEOUT_TRANSITION); };