2023-06-08 18:21:10 +08:00
|
|
|
import {fetchPost} from "../../../util/fetch";
|
2023-07-07 11:12:49 +08:00
|
|
|
import {getColIconByType, showColMenu} from "./col";
|
2023-07-01 23:15:21 +08:00
|
|
|
import {Constants} from "../../../constants";
|
2023-06-08 18:21:10 +08:00
|
|
|
|
2023-06-11 23:17:51 +08:00
|
|
|
export const avRender = (element: Element, 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-06-08 18:33:35 +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>';
|
|
|
|
|
let calcHTML = '<div style="width: 24px"></div>'
|
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-07-09 23:54:32 +08:00
|
|
|
tableHTML += `<div class="av__cell" data-col-id="${column.id}" 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">
|
|
|
|
|
<svg><use xlink:href="#${column.icon || getColIconByType(column.type)}"></use></svg>
|
|
|
|
|
<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 00:07:27 +08:00
|
|
|
calcHTML += `<div class="av__calc" data-col-id="${column.id}" data-dtype="${column.type}"
|
|
|
|
|
style="width: ${column.width || "200px"}"><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-06-11 22:01:30 +08:00
|
|
|
<button><svg><use xlink:href="#iconLine"></use></svg></button>
|
|
|
|
|
</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-06-30 16:26:35 +08:00
|
|
|
if (cell.valueType === "text") {
|
2023-07-09 23:54:32 +08:00
|
|
|
text = `<span class="av__celltext">${cell.value?.text.content || ""}</span>`;
|
2023-06-30 16:26:35 +08:00
|
|
|
} else if (cell.valueType === "block") {
|
2023-07-09 23:54:32 +08:00
|
|
|
text = `<span class="av__celltext">${cell.value?.block.content || ""}</span>`;
|
2023-07-01 12:04:23 +08:00
|
|
|
} else if (cell.valueType === "number") {
|
2023-07-09 23:54:32 +08:00
|
|
|
text = `<span class="av__celltext">${cell.value?.number.content || ""}</span>`;
|
2023-07-10 23:22:04 +08:00
|
|
|
} else if (cell.valueType === "mSelect" || cell.valueType === "select") {
|
2023-07-11 00:03:11 +08:00
|
|
|
cell.value?.mSelect?.forEach((item: { content: string, color: string }) => {
|
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-07-09 23:54:32 +08:00
|
|
|
text = `<span class="av__celltext">${cell.value?.date.content || ""}</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-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-07-02 23:58:00 +08:00
|
|
|
${data.columns[index].wrap ? "" : "white-space: nowrap;"}
|
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;
|
|
|
|
|
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-07-11 21:34:22 +08:00
|
|
|
<div contenteditable="true" 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 00:07:27 +08:00
|
|
|
<div class="av__row--footer">${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-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-06-11 23:17:51 +08:00
|
|
|
if (operation.action === "addAttrViewCol") {
|
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");
|
|
|
|
|
avRender(item, () => {
|
2023-07-07 11:12:49 +08:00
|
|
|
showColMenu(protyle, item, item.querySelector(".av__row--header").lastElementChild.previousElementSibling as HTMLElement);
|
2023-06-08 22:55:31 +08:00
|
|
|
});
|
2023-06-07 11:55:45 +08:00
|
|
|
});
|
2023-07-02 23:58:00 +08:00
|
|
|
} else 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");
|
|
|
|
|
avRender(item);
|
|
|
|
|
});
|
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
|
|
|
};
|