2023-06-08 18:21:10 +08:00
|
|
|
import {fetchPost} from "../../../util/fetch";
|
2023-06-11 23:17:51 +08:00
|
|
|
import {getColIconByType} from "./col";
|
|
|
|
|
import {showHeaderCellMenu} from "./cell";
|
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-06-08 18:25:33 +08:00
|
|
|
const data = response.data.av;
|
2023-06-08 19:21:33 +08:00
|
|
|
// header
|
2023-06-10 12:22:10 +08:00
|
|
|
let tableHTML = '<div class="av__row av__row--header"><div class="av__firstcol"><svg style="height: 42px"><use xlink:href="#iconUncheck"></use></svg></div>';
|
2023-06-10 17:45:04 +08:00
|
|
|
let index = 0;
|
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-02 23:58:00 +08:00
|
|
|
tableHTML += `<div class="av__cell" data-index="${index}" data-id="${column.id}" data-dtype="${column.type}"
|
|
|
|
|
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-06-10 16:40:44 +08:00
|
|
|
index++;
|
2023-06-08 18:21:10 +08:00
|
|
|
});
|
|
|
|
|
tableHTML += `<div class="block__icons">
|
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-01 20:41:35 +08:00
|
|
|
<div class="av__gutters" 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-01 17:06:28 +08:00
|
|
|
let text: string;
|
2023-06-30 16:26:35 +08:00
|
|
|
if (cell.valueType === "text") {
|
2023-07-01 17:06:28 +08:00
|
|
|
text = cell.value?.text.content || "";
|
2023-06-30 16:26:35 +08:00
|
|
|
} else if (cell.valueType === "block") {
|
2023-07-01 17:06:28 +08:00
|
|
|
text = cell.value?.block.content || "";
|
2023-07-01 12:04:23 +08:00
|
|
|
} else if (cell.valueType === "number") {
|
2023-07-01 17:06:28 +08:00
|
|
|
text = cell.value?.number.content || "";
|
2023-07-01 12:04:23 +08:00
|
|
|
} else if (cell.valueType === "select") {
|
2023-07-01 17:06:28 +08:00
|
|
|
text = cell.value?.select.content || "";
|
2023-07-01 12:04:23 +08:00
|
|
|
} else if (cell.valueType === "mSelect") {
|
2023-07-01 17:06:28 +08:00
|
|
|
text = cell.value?.mSelect.content || "";
|
2023-07-01 12:04:23 +08:00
|
|
|
} else if (cell.valueType === "date") {
|
2023-07-01 17:06:28 +08:00
|
|
|
text = cell.value?.date.content || "";
|
2023-06-30 16:26:35 +08:00
|
|
|
}
|
2023-07-02 22:37:17 +08:00
|
|
|
tableHTML += `<div class="av__cell" data-id="${cell.id}" data-index="${index}"
|
|
|
|
|
${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;"}
|
|
|
|
|
${cell.color ? `color:${cell.color};` : ""}"><span class="av__celltext">${text}</span></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-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-01 23:54:24 +08:00
|
|
|
<div class="item item--focus">
|
|
|
|
|
<svg class="item__graphic"><use xlink:href="#iconTable"></use></svg>
|
|
|
|
|
<span class="item__text">Table</span>
|
|
|
|
|
</div>
|
2023-07-02 20:52:16 +08:00
|
|
|
<div class="fn__flex-1"></div>
|
|
|
|
|
<span data-type="av-filter" class="block__icon block__icon--show b3-tooltips b3-tooltips__w" aria-label="${window.siyuan.languages.filter}">
|
|
|
|
|
<svg><use xlink:href="#iconFilter"></use></svg>
|
|
|
|
|
</span>
|
|
|
|
|
<div class="fn__space"></div>
|
|
|
|
|
<span data-type="av-sort" class="block__icon block__icon--show b3-tooltips b3-tooltips__w" aria-label="${window.siyuan.languages.sort}">
|
|
|
|
|
<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-02 20:52:16 +08:00
|
|
|
<div contenteditable="true" class="av__title" data-tip="${window.siyuan.languages.title}">${data.title || ""}</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-06-08 12:03:04 +08:00
|
|
|
<div class="block__icon block__icon--show">
|
|
|
|
|
<div class="fn__space"></div>
|
|
|
|
|
<svg><use xlink:href="#iconAdd"></use></svg><span class="fn__space"></span>
|
|
|
|
|
${window.siyuan.languages.addAttr}
|
|
|
|
|
</div>
|
2023-06-08 22:55:31 +08:00
|
|
|
<div class="av__row--footer">Calculate</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-06-11 23:17:51 +08:00
|
|
|
if (operation.action === "addAttrViewCol") {
|
2023-06-11 23:24:26 +08:00
|
|
|
Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-av-id="${operation.parentID}"]`)).forEach((item: HTMLElement) => {
|
2023-06-11 23:17:51 +08:00
|
|
|
item.removeAttribute("data-render");
|
|
|
|
|
avRender(item, () => {
|
|
|
|
|
showHeaderCellMenu(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") {
|
|
|
|
|
Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-av-id="${operation.parentID}"]`)).forEach((item: HTMLElement) => {
|
|
|
|
|
const cellElement = item.querySelector(`.av__cell[data-id="${operation.id}"]`) as HTMLElement;
|
|
|
|
|
if (!cellElement || cellElement.style.width === operation.data) {
|
2023-07-02 23:59:01 +08:00
|
|
|
return;
|
2023-07-02 23:58:00 +08:00
|
|
|
}
|
|
|
|
|
const index = cellElement.dataset.index;
|
|
|
|
|
item.querySelectorAll(".av__row").forEach(rowItem => {
|
|
|
|
|
(rowItem.querySelector(`[data-index="${index}"]`) as HTMLElement).style.width = operation.data;
|
2023-07-02 23:59:01 +08:00
|
|
|
});
|
2023-07-02 23:58:00 +08:00
|
|
|
});
|
2023-07-01 12:15:31 +08:00
|
|
|
} else {
|
2023-06-30 11:11:45 +08:00
|
|
|
Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-av-id="${operation.parentID}"]`)).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
|
|
|
};
|