import {fetchPost} from "../../../util/fetch";
import {getColIconByType, showColMenu} from "./col";
import {Constants} from "../../../constants";
export const avRender = (element: Element, 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;
}
fetchPost("/api/av/renderAttributeView", {id: e.getAttribute("data-av-id")}, (response) => {
const data = response.data.av as IAV;
// header
let tableHTML = '
`;
// body
data.rows.forEach((row: IAVRow) => {
tableHTML += `
`;
row.cells.forEach((cell, index) => {
if (data.columns[index].hidden) {
return;
}
let text: string;
if (cell.valueType === "text") {
text = `
${cell.value?.text.content || ""}`;
} else if (cell.valueType === "block") {
text = `
${cell.value?.block.content || ""}`;
} else if (cell.valueType === "number") {
text = `
${cell.value?.number.content || ""}`;
} else if (cell.valueType === "select") {
if (cell.value?.select.content) {
text = `
${cell.value.select.content}`;
} else {
text = `
`;
}
} else if (cell.valueType === "mSelect") {
text = `
${cell.value?.mSelect.content || ""}`;
} else if (cell.valueType === "date") {
text = `
${cell.value?.date.content || ""}`;
}
tableHTML += `
${text}
`;
});
tableHTML += "
";
});
const paddingLeft = e.parentElement.style.paddingLeft;
const paddingRight = e.parentElement.style.paddingRight;
e.style.width = e.parentElement.clientWidth + "px";
e.style.alignSelf = "center";
e.firstElementChild.outerHTML = ``;
e.setAttribute("data-render", "true");
if (cb) {
cb();
}
});
});
}
};
let lastParentID: string;
let lastElement: HTMLElement;
export const refreshAV = (protyle: IProtyle, operation: IOperation) => {
if (lastParentID === operation.parentID && protyle.contentElement.isSameNode(lastElement)) {
return;
}
lastElement = protyle.contentElement;
lastParentID = operation.parentID;
const avId = operation.action === "setAttrView" ? operation.id : operation.parentID;
if (operation.action === "addAttrViewCol") {
Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-av-id="${avId}"]`)).forEach((item: HTMLElement) => {
item.removeAttribute("data-render");
avRender(item, () => {
showColMenu(protyle, item, item.querySelector(".av__row--header").lastElementChild.previousElementSibling as HTMLElement);
});
});
} else 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 if (operation.action === "setAttrView" && typeof operation.data.name === "string") {
Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-av-id="${avId}"]`)).forEach((item: HTMLElement) => {
const titleElement = item.querySelector(".av__title") as HTMLElement;
if (!titleElement || titleElement.textContent.trim() === operation.data.name) {
return;
}
titleElement.textContent = operation.data.name;
titleElement.dataset.title = operation.data.name;
});
} else {
Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-av-id="${avId}"]`)).forEach((item: HTMLElement) => {
item.removeAttribute("data-render");
avRender(item);
});
}
setTimeout(() => {
lastParentID = null;
}, Constants.TIMEOUT_TRANSITION);
};