2023-06-08 18:21:10 +08:00
|
|
|
import {fetchPost} from "../../../util/fetch";
|
2023-09-21 10:54:41 +08:00
|
|
|
import {getColIconByType} from "./col";
|
2023-07-01 23:15:21 +08:00
|
|
|
import {Constants} from "../../../constants";
|
2023-07-15 23:18:35 +08:00
|
|
|
import {getCalcValue} from "./cell";
|
2023-07-22 22:56:15 +08:00
|
|
|
import * as dayjs from "dayjs";
|
2023-10-05 11:16:44 +08:00
|
|
|
import {unicode2Emoji} from "../../../emoji";
|
2023-06-08 18:21:10 +08:00
|
|
|
|
2023-09-09 23:21:46 +08:00
|
|
|
export const avRender = (element: Element, protyle: IProtyle, cb?: () => void) => {
|
2023-06-07 11:55:45 +08:00
|
|
|
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) {
|
2023-06-08 22:55:31 +08:00
|
|
|
avElements.forEach((e: HTMLElement) => {
|
2023-06-07 11:55:45 +08:00
|
|
|
if (e.getAttribute("data-render") === "true") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-08-01 21:29:15 +08:00
|
|
|
const left = e.querySelector(".av__scroll")?.scrollLeft || 0;
|
2023-10-12 11:54:15 +08:00
|
|
|
const headerTransform = (e.querySelector(".av__row--header") as HTMLElement)?.style.transform;
|
|
|
|
|
const footerTransform = (e.querySelector(".av__row--footer") as HTMLElement)?.style.transform;
|
2023-07-31 23:58:45 +08:00
|
|
|
fetchPost("/api/av/renderAttributeView", {
|
|
|
|
|
id: e.getAttribute("data-av-id"),
|
|
|
|
|
}, (response) => {
|
2023-07-11 21:34:22 +08:00
|
|
|
const data = response.data.view as IAVTable;
|
2023-06-08 19:21:33 +08:00
|
|
|
// header
|
2023-07-15 00:07:27 +08:00
|
|
|
let tableHTML = '<div class="av__row av__row--header"><div class="av__firstcol"><svg style="height: 32px"><use xlink:href="#iconUncheck"></use></svg></div>';
|
2023-07-15 23:19:40 +08:00
|
|
|
let calcHTML = "";
|
2023-06-08 18:25:33 +08:00
|
|
|
data.columns.forEach((column: IAVColumn) => {
|
2023-06-08 22:55:31 +08:00
|
|
|
if (column.hidden) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-10-05 11:16:44 +08:00
|
|
|
tableHTML += `<div class="av__cell" data-col-id="${column.id}" data-icon="${column.icon}" data-dtype="${column.type}"
|
2023-07-02 23:58:00 +08:00
|
|
|
style="width: ${column.width || "200px"};
|
2023-07-02 22:37:17 +08:00
|
|
|
${column.wrap ? "" : "white-space: nowrap;"}">
|
2023-07-02 23:58:00 +08:00
|
|
|
<div draggable="true" class="av__cellheader">
|
2023-10-05 12:03:38 +08:00
|
|
|
${column.icon ? unicode2Emoji(column.icon, "av__cellicon", true) : `<svg class="av__cellicon"><use xlink:href="#${getColIconByType(column.type)}"></use></svg>`}
|
2023-07-02 23:58:00 +08:00
|
|
|
<span class="av__celltext">${column.name}</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="av__widthdrag"></div>
|
2023-06-08 22:56:52 +08:00
|
|
|
</div>`;
|
2023-07-15 23:18:35 +08:00
|
|
|
calcHTML += `<div class="av__calc${calcHTML ? "" : " av__calc--show"}${column.calc && column.calc.operator !== "" ? " av__calc--ashow" : ""}" data-col-id="${column.id}" data-dtype="${column.type}" data-operator="${column.calc?.operator || ""}"
|
|
|
|
|
style="width: ${column.width || "200px"}">${getCalcValue(column) || '<svg><use xlink:href="#iconDown"></use></svg>' + window.siyuan.languages.calc}</div>`;
|
2023-06-08 18:21:10 +08:00
|
|
|
});
|
2023-07-15 00:07:27 +08:00
|
|
|
tableHTML += `<div class="block__icons" style="min-height: auto">
|
2023-06-08 19:21:33 +08:00
|
|
|
<div class="block__icon block__icon--show" data-type="av-header-add"><svg><use xlink:href="#iconAdd"></use></svg></div>
|
2023-06-08 12:03:04 +08:00
|
|
|
<div class="fn__space"></div>
|
2023-06-08 19:21:33 +08:00
|
|
|
<div class="block__icon block__icon--show" data-type="av-header-more"><svg><use xlink:href="#iconMore"></use></svg></div>
|
2023-06-08 12:03:04 +08:00
|
|
|
</div>
|
|
|
|
|
</div>`;
|
2023-06-08 19:21:33 +08:00
|
|
|
// body
|
2023-06-08 18:25:33 +08:00
|
|
|
data.rows.forEach((row: IAVRow) => {
|
2023-06-11 22:01:30 +08:00
|
|
|
tableHTML += `<div class="av__row" data-id="${row.id}">
|
2023-07-12 11:46:58 +08:00
|
|
|
<div class="av__gutters ariaLabel" draggable="true" data-position="right" aria-label="${window.siyuan.languages.rowTip}">
|
2023-10-05 22:11:29 +08:00
|
|
|
<button><svg><use xlink:href="#iconDrag"></use></svg></button>
|
2023-06-11 22:01:30 +08:00
|
|
|
</div>
|
|
|
|
|
<div class="av__firstcol"><svg><use xlink:href="#iconUncheck"></use></svg></div>`;
|
2023-06-08 18:21:10 +08:00
|
|
|
row.cells.forEach((cell, index) => {
|
2023-07-02 20:52:16 +08:00
|
|
|
if (data.columns[index].hidden) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-12 00:08:00 +08:00
|
|
|
let text = "";
|
2023-10-01 17:41:00 +08:00
|
|
|
if (["text", "template"].includes(cell.valueType)) {
|
2023-10-01 17:58:48 +08:00
|
|
|
text = `<span class="av__celltext">${cell.value ? (cell.value[cell.valueType as "text"].content || "") : ""}</span>`;
|
2023-08-09 22:55:34 +08:00
|
|
|
} else if (["url", "email", "phone"].includes(cell.valueType)) {
|
2023-10-01 11:01:57 +08:00
|
|
|
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 = `<span class="av__celltext av__celltext--url" data-type="${cell.valueType}"${urlAttr}>${urlContent}</span>`;
|
2023-08-03 15:08:07 +08:00
|
|
|
if (cell.value && cell.value[cell.valueType as "url"].content) {
|
|
|
|
|
text += `<span data-type="copy" class="b3-tooltips b3-tooltips__n block__icon" aria-label="${window.siyuan.languages.copy}"><svg><use xlink:href="#iconCopy"></use></svg></span>`;
|
2023-07-30 23:09:37 +08:00
|
|
|
}
|
2023-06-30 16:26:35 +08:00
|
|
|
} else if (cell.valueType === "block") {
|
2023-09-27 23:58:48 +08:00
|
|
|
text = `<span class="av__celltext">${cell.value.block.content || ""}</span>`;
|
2023-09-27 23:34:07 +08:00
|
|
|
if (cell.value?.isDetached) {
|
2023-09-28 11:29:58 +08:00
|
|
|
text += `<span class="b3-chip b3-chip--info b3-chip--small" data-type="block-more" >${window.siyuan.languages.more}</span>`;
|
2023-09-27 23:34:07 +08:00
|
|
|
} else {
|
2023-07-28 15:20:34 +08:00
|
|
|
text += `<span class="b3-chip b3-chip--info b3-chip--small" data-type="block-ref" data-id="${cell.value.block.id}" data-subtype="s">${window.siyuan.languages.openBy}</span>`;
|
2023-07-17 20:23:27 +08:00
|
|
|
}
|
2023-07-01 12:04:23 +08:00
|
|
|
} else if (cell.valueType === "number") {
|
2023-10-06 10:56:30 +08:00
|
|
|
text = `<span class="av__celltext" data-content="${cell.value?.number.isNotEmpty ? cell.value?.number.content : ""}">${cell.value?.number.formattedContent || ""}</span>`;
|
2023-07-10 23:22:04 +08:00
|
|
|
} else if (cell.valueType === "mSelect" || cell.valueType === "select") {
|
2023-09-21 17:27:48 +08:00
|
|
|
cell.value?.mSelect?.forEach((item) => {
|
2023-07-13 23:16:07 +08:00
|
|
|
text += `<span class="b3-chip b3-chip--middle" style="background-color:var(--b3-font-background${item.color});color:var(--b3-font-color${item.color})">${item.content}</span>`;
|
2023-07-10 00:17:54 +08:00
|
|
|
});
|
2023-07-10 23:22:04 +08:00
|
|
|
if (!text) {
|
2023-07-12 00:08:00 +08:00
|
|
|
text = '<span class="av__celltext"></span>';
|
2023-07-13 23:16:07 +08:00
|
|
|
} else {
|
|
|
|
|
text = `<span class="av__celltext">${text}</span>`;
|
2023-07-10 23:22:04 +08:00
|
|
|
}
|
2023-07-01 12:04:23 +08:00
|
|
|
} else if (cell.valueType === "date") {
|
2023-08-03 23:23:28 +08:00
|
|
|
text = '<span class="av__celltext av__celltext--date">';
|
2023-10-09 11:33:29 +08:00
|
|
|
const dataValue = cell.value ? cell.value.date : null;
|
|
|
|
|
if (dataValue && dataValue.isNotEmpty) {
|
|
|
|
|
text += dayjs(dataValue.content).format("YYYY-MM-DD HH:mm");
|
2023-07-22 22:56:15 +08:00
|
|
|
}
|
2023-10-09 11:33:29 +08:00
|
|
|
if (dataValue && dataValue.hasEndDate && dataValue.isNotEmpty && dataValue.isNotEmpty2) {
|
|
|
|
|
text += `<svg><use xlink:href="#iconForward"></use></svg>${dayjs(dataValue.content2).format("YYYY-MM-DD HH:mm")}`;
|
|
|
|
|
}
|
|
|
|
|
text += "</span>";
|
|
|
|
|
} else if (["created", "updated"].includes(cell.valueType)) {
|
|
|
|
|
text = '<span class="av__celltext av__celltext--date">';
|
|
|
|
|
const dataValue = cell.value ? cell.value[cell.valueType as "date"] : null;
|
2023-10-10 20:05:48 +08:00
|
|
|
if (dataValue && dataValue.isNotEmpty) {
|
2023-10-09 11:33:29 +08:00
|
|
|
text += dayjs(dataValue.content).format("YYYY-MM-DD HH:mm");
|
2023-07-22 22:56:15 +08:00
|
|
|
}
|
2023-07-25 20:40:28 +08:00
|
|
|
text += "</span>";
|
2023-09-21 17:27:48 +08:00
|
|
|
} else if (cell.valueType === "mAsset") {
|
|
|
|
|
cell.value?.mAsset?.forEach((item) => {
|
|
|
|
|
if (item.type === "image") {
|
2023-09-22 12:11:56 +08:00
|
|
|
text += `<img class="av__cellassetimg" src="${item.content}">`;
|
2023-09-21 17:27:48 +08:00
|
|
|
} else {
|
2023-09-22 20:40:28 +08:00
|
|
|
text += `<span class="b3-chip b3-chip--middle av__celltext--url" data-url="${item.content}">${item.name}</span>`;
|
2023-09-21 17:27:48 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (!text) {
|
|
|
|
|
text = '<span class="av__celltext"></span>';
|
|
|
|
|
} else {
|
|
|
|
|
text = `<span class="av__celltext">${text}</span>`;
|
|
|
|
|
}
|
2023-06-30 16:26:35 +08:00
|
|
|
}
|
2023-07-09 23:54:32 +08:00
|
|
|
tableHTML += `<div class="av__cell" data-id="${cell.id}" data-col-id="${data.columns[index].id}"
|
2023-07-02 22:37:17 +08:00
|
|
|
${cell.valueType === "block" ? 'data-block-id="' + (cell.value.block.id || "") + '"' : ""}
|
2023-09-27 23:58:48 +08:00
|
|
|
${cell.value?.isDetached ? ' data-detached="true"' : ""}
|
2023-07-02 23:58:00 +08:00
|
|
|
style="width: ${data.columns[index].width || "200px"};
|
2023-07-02 22:37:17 +08:00
|
|
|
${cell.bgColor ? `background-color:${cell.bgColor};` : ""}
|
2023-09-28 17:25:18 +08:00
|
|
|
white-space: ${data.columns[index].wrap ? "pre-wrap" : "nowrap"};
|
2023-08-03 23:23:28 +08:00
|
|
|
${cell.valueType !== "number" ? "" : "flex-direction: row-reverse;"}
|
2023-07-09 23:54:32 +08:00
|
|
|
${cell.color ? `color:${cell.color};` : ""}">${text}</div>`;
|
2023-06-08 18:21:10 +08:00
|
|
|
});
|
2023-06-08 22:56:52 +08:00
|
|
|
tableHTML += "<div></div></div>";
|
2023-06-08 10:07:26 +08:00
|
|
|
});
|
2023-07-12 00:08:00 +08:00
|
|
|
let tabHTML = "";
|
2023-07-11 23:13:19 +08:00
|
|
|
response.data.views.forEach((item: IAVView) => {
|
|
|
|
|
tabHTML += `<div data-id="${response.data.viewID}" class="item${item.id === response.data.viewID ? " item--focus" : ""}">
|
2023-07-11 21:34:22 +08:00
|
|
|
<svg class="item__graphic"><use xlink:href="#iconTable"></use></svg>
|
|
|
|
|
<span class="item__text">${item.name}</span>
|
2023-07-12 00:08:00 +08:00
|
|
|
</div>`;
|
|
|
|
|
});
|
2023-06-11 22:01:30 +08:00
|
|
|
const paddingLeft = e.parentElement.style.paddingLeft;
|
|
|
|
|
const paddingRight = e.parentElement.style.paddingRight;
|
2023-09-28 12:26:40 +08:00
|
|
|
if (e.parentElement.clientWidth > 0) {
|
|
|
|
|
e.style.width = e.parentElement.clientWidth + "px";
|
|
|
|
|
}
|
2023-06-08 18:21:10 +08:00
|
|
|
e.style.alignSelf = "center";
|
|
|
|
|
e.firstElementChild.outerHTML = `<div>
|
2023-06-30 23:57:11 +08:00
|
|
|
<div class="av__header" style="padding-left: ${paddingLeft};padding-right: ${paddingRight};">
|
|
|
|
|
<div class="layout-tab-bar fn__flex">
|
2023-07-11 21:34:22 +08:00
|
|
|
${tabHTML}
|
2023-07-02 20:52:16 +08:00
|
|
|
<div class="fn__flex-1"></div>
|
2023-07-03 20:54:57 +08:00
|
|
|
<span data-type="av-filter" class="block__icon block__icon--show b3-tooltips b3-tooltips__w${data.filters.length > 0 ? " block__icon--active" : ""}" aria-label="${window.siyuan.languages.filter}">
|
2023-07-02 20:52:16 +08:00
|
|
|
<svg><use xlink:href="#iconFilter"></use></svg>
|
|
|
|
|
</span>
|
|
|
|
|
<div class="fn__space"></div>
|
2023-07-03 20:54:57 +08:00
|
|
|
<span data-type="av-sort" class="block__icon block__icon--show b3-tooltips b3-tooltips__w${data.sorts.length > 0 ? " block__icon--active" : ""}" aria-label="${window.siyuan.languages.sort}">
|
2023-07-02 20:52:16 +08:00
|
|
|
<svg><use xlink:href="#iconSort"></use></svg>
|
|
|
|
|
</span>
|
|
|
|
|
<div class="fn__space"></div>
|
|
|
|
|
<span data-type="av-more" class="block__icon block__icon--show b3-tooltips b3-tooltips__w" aria-label="${window.siyuan.languages.more}">
|
|
|
|
|
<svg><use xlink:href="#iconMore"></use></svg>
|
|
|
|
|
</span>
|
|
|
|
|
<div class="fn__space"></div>
|
2023-06-08 10:07:26 +08:00
|
|
|
</div>
|
2023-09-09 23:21:46 +08:00
|
|
|
<div contenteditable="${protyle.disabled ? "false" : "true"}" spellcheck="${window.siyuan.config.editor.spellcheck.toString()}" class="av__title" data-title="${data.name || ""}" data-tip="${window.siyuan.languages.title}">${response.data.name || ""}</div>
|
2023-06-30 23:57:11 +08:00
|
|
|
<div class="av__counter fn__none"></div>
|
2023-06-08 10:07:26 +08:00
|
|
|
</div>
|
|
|
|
|
<div class="av__scroll">
|
2023-06-11 22:01:30 +08:00
|
|
|
<div style="padding-left: ${paddingLeft};padding-right: ${paddingRight};float: left;">
|
2023-06-08 10:07:26 +08:00
|
|
|
${tableHTML}
|
2023-07-15 00:07:27 +08:00
|
|
|
<div class="av__row--add">
|
|
|
|
|
<svg><use xlink:href="#iconAdd"></use></svg>
|
2023-06-08 12:03:04 +08:00
|
|
|
${window.siyuan.languages.addAttr}
|
|
|
|
|
</div>
|
2023-07-15 22:24:54 +08:00
|
|
|
<div class="av__row--footer"><div style="width: 24px"></div>${calcHTML}</div>
|
2023-06-08 10:07:26 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>`;
|
2023-06-08 18:21:10 +08:00
|
|
|
e.setAttribute("data-render", "true");
|
2023-10-12 11:54:15 +08:00
|
|
|
if (left) {
|
|
|
|
|
e.querySelector(".av__scroll").scrollLeft = left;
|
|
|
|
|
}
|
|
|
|
|
if (headerTransform) {
|
|
|
|
|
(e.querySelector(".av__row--header") as HTMLElement).style.transform = headerTransform;
|
|
|
|
|
}
|
|
|
|
|
if (footerTransform) {
|
|
|
|
|
(e.querySelector(".av__row--footer") as HTMLElement).style.transform = footerTransform;
|
|
|
|
|
}
|
2023-06-11 23:17:51 +08:00
|
|
|
if (cb) {
|
|
|
|
|
cb();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-02 23:59:01 +08:00
|
|
|
let lastParentID: string;
|
|
|
|
|
let lastElement: HTMLElement;
|
2023-06-11 23:17:51 +08:00
|
|
|
export const refreshAV = (protyle: IProtyle, operation: IOperation) => {
|
2023-07-01 23:15:21 +08:00
|
|
|
if (lastParentID === operation.parentID && protyle.contentElement.isSameNode(lastElement)) {
|
2023-07-02 23:59:01 +08:00
|
|
|
return;
|
2023-07-01 20:48:44 +08:00
|
|
|
}
|
2023-07-01 23:15:21 +08:00
|
|
|
lastElement = protyle.contentElement;
|
|
|
|
|
lastParentID = operation.parentID;
|
2023-07-11 23:13:19 +08:00
|
|
|
const avId = operation.avID;
|
2023-09-21 10:54:17 +08:00
|
|
|
if (operation.action === "setAttrViewColWidth") {
|
2023-07-03 20:54:57 +08:00
|
|
|
Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-av-id="${avId}"]`)).forEach((item: HTMLElement) => {
|
2023-07-09 23:54:32 +08:00
|
|
|
const cellElement = item.querySelector(`.av__cell[data-col-id="${operation.id}"]`) as HTMLElement;
|
2023-07-02 23:58:00 +08:00
|
|
|
if (!cellElement || cellElement.style.width === operation.data) {
|
2023-07-02 23:59:01 +08:00
|
|
|
return;
|
2023-07-02 23:58:00 +08:00
|
|
|
}
|
|
|
|
|
item.querySelectorAll(".av__row").forEach(rowItem => {
|
2023-07-09 23:54:32 +08:00
|
|
|
(rowItem.querySelector(`[data-col-id="${operation.id}"]`) as HTMLElement).style.width = operation.data;
|
2023-07-02 23:59:01 +08:00
|
|
|
});
|
2023-07-02 23:58:00 +08:00
|
|
|
});
|
2023-07-11 22:48:48 +08:00
|
|
|
} else if (operation.action === "setAttrViewName") {
|
2023-07-03 23:02:19 +08:00
|
|
|
Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-av-id="${avId}"]`)).forEach((item: HTMLElement) => {
|
|
|
|
|
const titleElement = item.querySelector(".av__title") as HTMLElement;
|
2023-07-11 22:48:48 +08:00
|
|
|
if (!titleElement || titleElement.textContent.trim() === operation.data) {
|
2023-07-03 23:02:19 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2023-07-11 22:48:48 +08:00
|
|
|
titleElement.textContent = operation.data;
|
|
|
|
|
titleElement.dataset.title = operation.data;
|
2023-07-03 23:02:19 +08:00
|
|
|
});
|
2023-07-01 12:15:31 +08:00
|
|
|
} else {
|
2023-07-03 20:54:57 +08:00
|
|
|
Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-av-id="${avId}"]`)).forEach((item: HTMLElement) => {
|
2023-06-11 23:17:51 +08:00
|
|
|
item.removeAttribute("data-render");
|
2023-09-09 23:21:46 +08:00
|
|
|
avRender(item, protyle);
|
2023-06-11 23:17:51 +08:00
|
|
|
});
|
2023-06-07 11:55:45 +08:00
|
|
|
}
|
2023-07-01 23:15:21 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
|
lastParentID = null;
|
|
|
|
|
}, Constants.TIMEOUT_TRANSITION);
|
2023-06-07 11:56:34 +08:00
|
|
|
};
|