import {Menu} from "../../../plugin/Menu";
import {hasClosestByClassName, hasTopClosestByClassName} from "../../util/hasClosest";
import {upDownHint} from "../../../util/upDownHint";
import {fetchPost} from "../../../util/fetch";
import {escapeGreat, escapeHtml} from "../../../util/escape";
import {transaction} from "../../wysiwyg/transaction";
import {updateCellsValue} from "./cell";
import {updateAttrViewCellAnimation} from "./action";
import {focusBlock} from "../../util/selection";
import {setPosition} from "../../../util/setPosition";
import * as dayjs from "dayjs";
const genSearchList = (element: Element, keyword: string, avId?: string, excludes = true, cb?: () => void) => {
fetchPost("/api/av/searchAttributeView", {
keyword,
excludes: (excludes && avId) ? [avId] : undefined
}, (response) => {
let html = "";
response.data.results.forEach((item: {
avID: string
avName: string
blockID: string
hPath: string
}, index: number) => {
html += `
`;
} else {
return "";
}
};
export const setRelationCell = (protyle: IProtyle, nodeElement: HTMLElement, target: HTMLElement, cellElements: HTMLElement[]) => {
const menuElement = hasClosestByClassName(target, "b3-menu");
if (!menuElement) {
return;
}
if (menuElement.querySelector(".dragover__bottom, .dragover__top")) {
return;
}
const rowElement = hasClosestByClassName(cellElements[0], "av__row");
if (!rowElement) {
return;
}
if (!nodeElement.contains(cellElements[0])) {
cellElements[0] = (nodeElement.querySelector(`.av__row[data-id="${rowElement.dataset.id}"] .av__cell[data-col-id="${cellElements[0].dataset.colId}"]`) ||
nodeElement.querySelector(`.fn__flex-1[data-col-id="${cellElements[0].dataset.colId}"]`)) as HTMLElement;
}
const newValue: IAVCellRelationValue = {blockIDs: [], contents: []};
menuElement.querySelectorAll('[draggable="true"]').forEach(item => {
const id = item.getAttribute("data-id");
newValue.blockIDs.push(id);
newValue.contents.push({
type: "block",
block: {
id,
content: item.querySelector(".b3-menu__label").textContent
},
isDetached: !item.querySelector(".popover__block")
});
});
if (target.classList.contains("b3-menu__item")) {
const targetId = target.getAttribute("data-id");
const separatorElement = menuElement.querySelector(".b3-menu__separator");
const searchValue = menuElement.querySelector("input").value;
if (target.getAttribute("draggable")) {
if (!separatorElement.nextElementSibling.getAttribute("data-id") && !searchValue) {
separatorElement.nextElementSibling.remove();
}
const removeIndex = newValue.blockIDs.indexOf(targetId);
newValue.blockIDs.splice(removeIndex, 1);
newValue.contents.splice(removeIndex, 1);
separatorElement.after(target);
target.outerHTML = genSelectItemHTML("unselect", targetId, !target.querySelector(".popover__block"), Lute.EscapeHTMLStr(target.querySelector(".b3-menu__label").textContent));
} else if (targetId) {
newValue.blockIDs.push(targetId);
newValue.contents.push({
type: "block",
block: {
id: targetId,
content: target.firstElementChild.textContent
},
isDetached: !target.firstElementChild.getAttribute("style")
});
separatorElement.before(target);
target.outerHTML = ``;
if (!separatorElement.nextElementSibling) {
separatorElement.insertAdjacentHTML("afterend", genSelectItemHTML("empty"));
}
} else {
const blockID = target.querySelector(".popover__block").getAttribute("data-id");
const content = target.querySelector("b").textContent;
const rowId = Lute.NewNodeID();
transaction(protyle, [{
action: "insertAttrViewBlock",
ignoreFillFilter: true,
avID: menuElement.firstElementChild.getAttribute("data-av-id"),
srcs: [{
id: rowId,
isDetached: true,
content
}],
blockID,
}, {
action: "doUpdateUpdated",
id: blockID,
data: dayjs().format("YYYYMMDDHHmmss"),
}]);
newValue.blockIDs.push(rowId);
newValue.contents.push({
type: "block",
block: {
id: rowId,
content
},
isDetached: true
});
separatorElement.insertAdjacentHTML("beforebegin", ``);
}
menuElement.querySelector(".b3-menu__item--current")?.classList.remove("b3-menu__item--current");
menuElement.querySelector(".b3-menu__items .b3-menu__item:not(.fn__none)").classList.add("b3-menu__item--current");
}
updateCellsValue(protyle, nodeElement, newValue, cellElements);
};