import {transaction} from "../../wysiwyg/transaction"; import {hasClosestBlock, hasClosestByClassName} from "../../util/hasClosest"; import {openMenuPanel} from "./openMenuPanel"; import {updateAttrViewCellAnimation} from "./action"; import {isNotCtrl} from "../../util/compatibility"; import {objEquals} from "../../../util/functions"; import {fetchPost} from "../../../util/fetch"; import {focusBlock} from "../../util/selection"; import * as dayjs from "dayjs"; export const getCellText = (cellElement: HTMLElement | false) => { if (!cellElement) { return ""; } let cellText = "" const textElement = cellElement.querySelector(".av__celltext"); if (textElement) { if (textElement.querySelector(".av__cellicon")) { cellText = `${textElement.firstChild.textContent} → ${textElement.lastChild.textContent}`; } else { cellText = textElement.textContent; } } else { cellText = cellElement.textContent; } return cellText; }; const genCellValueByElement = (colType: TAVCol, cellElement: HTMLElement) => { let cellValue: IAVCellValue; if (colType === "number") { const value = cellElement.querySelector(".av__celltext").getAttribute("data-content") cellValue = { type: colType, number: { content: parseFloat(value) || 0, isNotEmpty: !!value } }; } else if (["text", "block", "url", "phone", "email", "template"].includes(colType)) { cellValue = { type: colType, [colType]: { content: cellElement.querySelector(".av__celltext").textContent.trim() } }; } else if (colType === "mSelect" || colType === "select") { const mSelect: IAVCellSelectValue[] = [] cellElement.querySelectorAll(".b3-chip").forEach((item: HTMLElement) => { mSelect.push({ content: item.textContent.trim(), color: item.style.color.replace("var(--b3-font-color", "").replace(")", "") }) }) cellValue = { type: colType, mSelect }; } else if (["date", "created", "updated"].includes(colType)) { cellValue = { type: colType, [colType]: JSON.parse(cellElement.querySelector(".av__celltext").getAttribute("data-value")) }; } return cellValue; } export const genCellValue = (colType: TAVCol, value: string | any) => { let cellValue: IAVCellValue; if (typeof value === "string") { if (colType === "number") { if (value) { cellValue = { type: colType, number: { content: parseFloat(value), isNotEmpty: true } }; } else { cellValue = { type: colType, number: { content: 0, isNotEmpty: false } }; } } else if (["text", "block", "url", "phone", "email", "template"].includes(colType)) { cellValue = { type: colType, [colType]: { content: value } }; } else if (colType === "mSelect" || colType === "select") { cellValue = { type: colType, mSelect: [{ content: value, color: value ? "1" : "" }] }; } else if (["date", "created", "updated"].includes(colType) && value === "") { cellValue = { type: colType, [colType]: { content: null, isNotEmpty: false, content2: null, isNotEmpty2: false, hasEndDate: false, isNotTime: true, } }; } } else { if (colType === "mSelect" || colType === "select") { cellValue = { type: colType, mSelect: value as IAVCellSelectValue[] }; } else if (["date", "created", "updated"].includes(colType)) { cellValue = { type: colType, [colType]: value as IAVCellDateValue }; } else if (colType === "mAsset") { cellValue = { type: colType, mAsset: value as IAVCellAssetValue[] }; } else if (colType === "checkbox") { cellValue = { type: colType, checkbox: { checked: value ? true : false } }; } } return cellValue; }; export const cellScrollIntoView = (blockElement: HTMLElement, cellElement: Element, onlyHeight = true) => { const cellRect = cellElement.getBoundingClientRect(); if (!onlyHeight) { const avScrollElement = blockElement.querySelector(".av__scroll"); if (avScrollElement) { const avScrollRect = avScrollElement.getBoundingClientRect(); if (avScrollRect.right < cellRect.right) { avScrollElement.scrollLeft = avScrollElement.scrollLeft + cellRect.right - avScrollRect.right; } else { const rowElement = hasClosestByClassName(cellElement, "av__row"); if (rowElement) { const stickyElement = rowElement.querySelector(".av__colsticky"); if (stickyElement) { const stickyRight = stickyElement.getBoundingClientRect().right; if (stickyRight > cellRect.left) { avScrollElement.scrollLeft = avScrollElement.scrollLeft + cellRect.left - stickyRight; } } else if (avScrollRect.left > cellRect.left) { avScrollElement.scrollLeft = avScrollElement.scrollLeft + cellRect.left - avScrollRect.left; } } } } } if (!blockElement.querySelector(".av__header")) { // 属性面板 return; } const avHeaderRect = blockElement.querySelector(".av__row--header").getBoundingClientRect(); if (avHeaderRect.bottom > cellRect.top) { const contentElement = hasClosestByClassName(blockElement, "protyle-content", true); if (contentElement) { contentElement.scrollTop = contentElement.scrollTop + cellRect.top - avHeaderRect.bottom; } } else { const avFooterRect = blockElement.querySelector(".av__row--footer").getBoundingClientRect(); if (avFooterRect.top < cellRect.bottom) { const contentElement = hasClosestByClassName(blockElement, "protyle-content", true); if (contentElement) { contentElement.scrollTop = contentElement.scrollTop + cellRect.bottom - avFooterRect.top; } } } }; export const getTypeByCellElement = (cellElement: Element) => { const scrollElement = hasClosestByClassName(cellElement, "av__scroll"); if (!scrollElement) { return; } return scrollElement.querySelector(".av__row--header").querySelector(`[data-col-id="${cellElement.getAttribute("data-col-id")}"]`).getAttribute("data-dtype") as TAVCol; }; export const popTextCell = (protyle: IProtyle, cellElements: HTMLElement[], type?: TAVCol) => { if (cellElements.length === 0 || (cellElements.length === 1 && !cellElements[0])) { return; } if (!type) { type = getTypeByCellElement(cellElements[0]); } if (type === "updated" || type === "created" || document.querySelector(".av__mask")) { return; } if (type === "block" && (cellElements.length > 1 || !cellElements[0].getAttribute("data-detached"))) { return; } const blockElement = hasClosestBlock(cellElements[0]); let cellRect = cellElements[0].getBoundingClientRect(); if (blockElement) { /// #if MOBILE const contentElement = hasClosestByClassName(blockElement, "protyle-content", true); if (contentElement) { contentElement.scrollTop = contentElement.scrollTop + cellRect.top - 110; } /// #else cellScrollIntoView(blockElement, cellElements[0], false); /// #endif } cellRect = cellElements[0].getBoundingClientRect(); let html = ""; const style = `style="padding-top: 6.5px;position:absolute;left: ${cellRect.left}px;top: ${cellRect.top}px;width:${Math.max(cellRect.width, 25)}px;height: ${cellRect.height}px"`; if (["text", "url", "email", "phone", "block", "template"].includes(type)) { html = ``; } else if (type === "number") { html = ``; } else if (blockElement) { if (["select", "mSelect"].includes(type)) { openMenuPanel({protyle, blockElement, type: "select", cellElements}); } else if (type === "mAsset") { openMenuPanel({protyle, blockElement, type: "asset", cellElements}); } else if (type === "date") { openMenuPanel({protyle, blockElement, type: "date", cellElements}); } else if (type === "checkbox") { updateCellValueByInput(protyle, type, cellElements); } if (!hasClosestByClassName(cellElements[0], "custom-attr")) { cellElements[0].classList.add("av__cell--select"); } return; } window.siyuan.menus.menu.remove(); document.body.insertAdjacentHTML("beforeend", `