2023-06-12 12:42:22 +08:00
|
|
|
import {Menu} from "../../../plugin/Menu";
|
2023-06-08 19:21:33 +08:00
|
|
|
import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName} from "../../util/hasClosest";
|
|
|
|
|
import {transaction} from "../../wysiwyg/transaction";
|
2023-06-10 11:59:12 +08:00
|
|
|
import {openEditorTab} from "../../../menus/util";
|
|
|
|
|
import {copySubMenu} from "../../../menus/commonMenuItem";
|
2023-07-15 22:24:54 +08:00
|
|
|
import {openCalcMenu, popTextCell} from "./cell";
|
2023-07-07 11:12:49 +08:00
|
|
|
import {getColIconByType, showColMenu, updateHeader} from "./col";
|
2023-06-29 18:56:46 +08:00
|
|
|
import {emitOpenMenu} from "../../../plugin/EventBus";
|
2023-07-02 20:52:16 +08:00
|
|
|
import {addCol} from "./addCol";
|
|
|
|
|
import {openMenuPanel} from "./openMenuPanel";
|
2023-06-10 17:44:19 +08:00
|
|
|
|
2023-06-08 19:21:33 +08:00
|
|
|
export const avClick = (protyle: IProtyle, event: MouseEvent & { target: HTMLElement }) => {
|
2023-06-08 22:56:52 +08:00
|
|
|
const blockElement = hasClosestBlock(event.target);
|
2023-06-10 17:56:45 +08:00
|
|
|
if (!blockElement) {
|
2023-06-11 22:58:48 +08:00
|
|
|
return false;
|
2023-06-10 17:56:45 +08:00
|
|
|
}
|
2023-06-08 22:56:52 +08:00
|
|
|
const addElement = hasClosestByAttribute(event.target, "data-type", "av-header-add");
|
2023-06-10 17:56:45 +08:00
|
|
|
if (addElement) {
|
2023-07-02 21:53:02 +08:00
|
|
|
const addMenu = addCol(protyle, blockElement);
|
2023-07-02 23:59:01 +08:00
|
|
|
const addRect = addElement.getBoundingClientRect();
|
2023-07-02 21:53:02 +08:00
|
|
|
addMenu.open({
|
|
|
|
|
x: addRect.left,
|
|
|
|
|
y: addRect.bottom,
|
|
|
|
|
h: addRect.height
|
|
|
|
|
});
|
2023-06-08 19:21:33 +08:00
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
2023-06-08 22:56:52 +08:00
|
|
|
return true;
|
2023-06-08 19:21:33 +08:00
|
|
|
}
|
2023-06-10 11:11:49 +08:00
|
|
|
|
2023-06-30 10:26:48 +08:00
|
|
|
const gutterElement = hasClosestByClassName(event.target, "av__gutters");
|
|
|
|
|
if (gutterElement) {
|
|
|
|
|
avContextmenu(protyle, event, gutterElement);
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 17:56:45 +08:00
|
|
|
const checkElement = hasClosestByClassName(event.target, "av__firstcol");
|
|
|
|
|
if (checkElement) {
|
2023-06-30 23:57:11 +08:00
|
|
|
window.siyuan.menus.menu.remove();
|
|
|
|
|
const rowElement = checkElement.parentElement;
|
2023-07-01 17:06:28 +08:00
|
|
|
const useElement = checkElement.querySelector("use");
|
2023-06-30 23:57:11 +08:00
|
|
|
if (rowElement.classList.contains("av__row--header")) {
|
|
|
|
|
if ("#iconCheck" === useElement.getAttribute("xlink:href")) {
|
|
|
|
|
rowElement.parentElement.querySelectorAll(".av__firstcol").forEach(item => {
|
|
|
|
|
item.querySelector("use").setAttribute("xlink:href", "#iconUncheck");
|
|
|
|
|
item.parentElement.classList.remove("av__row--select");
|
2023-07-01 17:06:28 +08:00
|
|
|
});
|
2023-06-30 23:57:11 +08:00
|
|
|
} else {
|
|
|
|
|
rowElement.parentElement.querySelectorAll(".av__firstcol").forEach(item => {
|
|
|
|
|
item.querySelector("use").setAttribute("xlink:href", "#iconCheck");
|
|
|
|
|
item.parentElement.classList.add("av__row--select");
|
2023-07-01 17:06:28 +08:00
|
|
|
});
|
2023-06-30 23:57:11 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (useElement.getAttribute("xlink:href") === "#iconUncheck") {
|
|
|
|
|
checkElement.parentElement.classList.add("av__row--select");
|
|
|
|
|
useElement.setAttribute("xlink:href", "#iconCheck");
|
|
|
|
|
} else {
|
|
|
|
|
checkElement.parentElement.classList.remove("av__row--select");
|
|
|
|
|
useElement.setAttribute("xlink:href", "#iconUncheck");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
updateHeader(rowElement);
|
2023-06-10 17:56:45 +08:00
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-02 23:58:00 +08:00
|
|
|
const headerMoreElement = hasClosestByAttribute(event.target, "data-type", "av-header-more");
|
2023-07-02 20:52:16 +08:00
|
|
|
if (headerMoreElement) {
|
|
|
|
|
openMenuPanel(protyle, blockElement, "properties");
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-02 23:58:00 +08:00
|
|
|
const moreElement = hasClosestByAttribute(event.target, "data-type", "av-more");
|
2023-07-02 20:52:16 +08:00
|
|
|
if (moreElement) {
|
|
|
|
|
openMenuPanel(protyle, blockElement, "config");
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-07-03 12:25:02 +08:00
|
|
|
|
|
|
|
|
const sortsElement = hasClosestByAttribute(event.target, "data-type", "av-sort");
|
|
|
|
|
if (sortsElement) {
|
|
|
|
|
openMenuPanel(protyle, blockElement, "sorts");
|
|
|
|
|
event.preventDefault();
|
2023-07-06 18:56:16 +08:00
|
|
|
event.stopPropagation();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const filtersElement = hasClosestByAttribute(event.target, "data-type", "av-filter");
|
|
|
|
|
if (filtersElement) {
|
|
|
|
|
openMenuPanel(protyle, blockElement, "filters");
|
|
|
|
|
event.preventDefault();
|
2023-07-03 12:25:02 +08:00
|
|
|
event.stopPropagation();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-02 23:58:00 +08:00
|
|
|
const cellHeaderElement = hasClosestByClassName(event.target, "av__cellheader");
|
|
|
|
|
if (cellHeaderElement) {
|
2023-07-07 11:12:49 +08:00
|
|
|
showColMenu(protyle, blockElement, cellHeaderElement.parentElement);
|
2023-07-02 23:58:00 +08:00
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-07-03 12:25:02 +08:00
|
|
|
|
2023-06-08 22:56:52 +08:00
|
|
|
const cellElement = hasClosestByClassName(event.target, "av__cell");
|
2023-07-02 23:58:00 +08:00
|
|
|
if (cellElement && !cellElement.parentElement.classList.contains("av__row--header")) {
|
|
|
|
|
popTextCell(protyle, cellElement);
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
2023-06-08 22:56:52 +08:00
|
|
|
return true;
|
2023-06-08 19:21:33 +08:00
|
|
|
}
|
2023-07-15 22:24:54 +08:00
|
|
|
|
|
|
|
|
const calcElement = hasClosestByClassName(event.target, "av__calc");
|
|
|
|
|
if (calcElement) {
|
|
|
|
|
openCalcMenu(protyle, calcElement);
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-06-08 22:56:52 +08:00
|
|
|
return false;
|
|
|
|
|
};
|
2023-06-10 11:26:56 +08:00
|
|
|
|
|
|
|
|
export const avContextmenu = (protyle: IProtyle, event: MouseEvent & { detail: any }, target: HTMLElement) => {
|
|
|
|
|
const rowElement = hasClosestByClassName(target, "av__row");
|
2023-06-10 11:59:12 +08:00
|
|
|
if (!rowElement) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-06-30 10:26:48 +08:00
|
|
|
if (rowElement.classList.contains("av__row--header")) {
|
2023-07-01 17:06:28 +08:00
|
|
|
return false;
|
2023-06-30 10:26:48 +08:00
|
|
|
}
|
2023-06-10 11:59:12 +08:00
|
|
|
const blockElement = hasClosestBlock(rowElement);
|
|
|
|
|
if (!blockElement) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-06-10 17:56:45 +08:00
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
2023-06-30 23:57:11 +08:00
|
|
|
if (!rowElement.classList.contains("av__row--select")) {
|
|
|
|
|
blockElement.querySelectorAll(".av__row--select").forEach(item => {
|
|
|
|
|
item.classList.remove("av__row--select");
|
|
|
|
|
});
|
|
|
|
|
blockElement.querySelectorAll(".av__firstcol use").forEach(item => {
|
|
|
|
|
item.setAttribute("xlink:href", "#iconUncheck");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const menu = new Menu("av-row-menu");
|
2023-06-10 17:56:45 +08:00
|
|
|
if (menu.isOpen) {
|
2023-06-11 22:58:48 +08:00
|
|
|
return true;
|
2023-06-10 17:56:45 +08:00
|
|
|
}
|
|
|
|
|
rowElement.classList.add("av__row--select");
|
2023-06-30 23:57:11 +08:00
|
|
|
rowElement.querySelector(".av__firstcol use").setAttribute("xlink:href", "#iconCheck");
|
|
|
|
|
const rowIds: string[] = [];
|
|
|
|
|
const blockIds: string[] = [];
|
|
|
|
|
blockElement.querySelectorAll(".av__row--select:not(.av__row--header )").forEach(item => {
|
|
|
|
|
rowIds.push(item.getAttribute("data-id"));
|
|
|
|
|
blockIds.push(item.querySelector(".av__cell").getAttribute("data-block-id"));
|
2023-06-10 11:59:12 +08:00
|
|
|
});
|
2023-06-30 23:57:11 +08:00
|
|
|
updateHeader(rowElement);
|
2023-06-10 11:59:12 +08:00
|
|
|
menu.addItem({
|
|
|
|
|
icon: "iconTrashcan",
|
|
|
|
|
label: window.siyuan.languages.delete,
|
|
|
|
|
click() {
|
|
|
|
|
transaction(protyle, [{
|
|
|
|
|
action: "removeAttrViewBlock",
|
2023-06-30 23:57:11 +08:00
|
|
|
srcIDs: blockIds,
|
2023-07-12 00:07:16 +08:00
|
|
|
avID: blockElement.getAttribute("data-av-id"),
|
2023-06-10 11:59:12 +08:00
|
|
|
}], [{
|
|
|
|
|
action: "insertAttrViewBlock",
|
2023-07-12 00:07:16 +08:00
|
|
|
avID: blockElement.getAttribute("data-av-id"),
|
2023-06-10 11:59:12 +08:00
|
|
|
previousID: rowElement.previousElementSibling?.getAttribute("data-id") || "",
|
2023-06-30 23:57:11 +08:00
|
|
|
srcIDs: rowIds,
|
2023-06-10 11:59:12 +08:00
|
|
|
}]);
|
2023-06-10 17:44:19 +08:00
|
|
|
rowElement.remove();
|
2023-06-10 11:59:12 +08:00
|
|
|
}
|
|
|
|
|
});
|
2023-06-30 23:57:11 +08:00
|
|
|
if (rowIds.length === 1) {
|
|
|
|
|
menu.addSeparator();
|
|
|
|
|
openEditorTab(protyle.app, rowIds[0]);
|
|
|
|
|
menu.addItem({
|
|
|
|
|
label: window.siyuan.languages.copy,
|
|
|
|
|
icon: "iconCopy",
|
|
|
|
|
type: "submenu",
|
|
|
|
|
submenu: copySubMenu(rowIds[0])
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
menu.addSeparator();
|
|
|
|
|
if (rowIds.length === 1) {
|
|
|
|
|
menu.addItem({
|
|
|
|
|
icon: "iconEdit",
|
|
|
|
|
label: window.siyuan.languages.edit,
|
|
|
|
|
click() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-06-10 17:45:04 +08:00
|
|
|
const editAttrSubmenu: IMenu[] = [];
|
2023-06-10 11:59:12 +08:00
|
|
|
rowElement.parentElement.querySelectorAll(".av__row--header .av__cell").forEach((cellElement) => {
|
|
|
|
|
editAttrSubmenu.push({
|
2023-06-11 23:17:51 +08:00
|
|
|
icon: getColIconByType(cellElement.getAttribute("data-dtype") as TAVCol),
|
2023-06-10 11:59:12 +08:00
|
|
|
label: cellElement.textContent.trim(),
|
2023-06-10 11:26:56 +08:00
|
|
|
click() {
|
|
|
|
|
}
|
2023-06-10 17:45:04 +08:00
|
|
|
});
|
2023-06-10 11:59:12 +08:00
|
|
|
});
|
|
|
|
|
menu.addItem({
|
|
|
|
|
icon: "iconList",
|
|
|
|
|
label: window.siyuan.languages.attr,
|
|
|
|
|
type: "submenu",
|
|
|
|
|
submenu: editAttrSubmenu
|
2023-06-10 17:45:04 +08:00
|
|
|
});
|
2023-06-29 18:56:46 +08:00
|
|
|
if (protyle?.app?.plugins) {
|
|
|
|
|
emitOpenMenu({
|
|
|
|
|
plugins: protyle.app.plugins,
|
|
|
|
|
type: "open-menu-av",
|
|
|
|
|
detail: {
|
|
|
|
|
protyle,
|
|
|
|
|
element: hasClosestByClassName(target, "av__cell"),
|
|
|
|
|
},
|
|
|
|
|
separatorPosition: "top",
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-06-10 11:59:12 +08:00
|
|
|
menu.open({
|
|
|
|
|
x: event.clientX,
|
|
|
|
|
y: event.clientY,
|
|
|
|
|
});
|
|
|
|
|
return true;
|
2023-06-10 17:45:04 +08:00
|
|
|
};
|
2023-07-03 23:02:19 +08:00
|
|
|
|
|
|
|
|
export const updateAVName = (protyle: IProtyle, blockElement: Element) => {
|
|
|
|
|
const avId = blockElement.getAttribute("data-av-id");
|
|
|
|
|
const nameElement = blockElement.querySelector(".av__title") as HTMLElement;
|
|
|
|
|
if (nameElement.textContent.trim() === nameElement.dataset.title.trim()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
transaction(protyle, [{
|
2023-07-11 22:48:48 +08:00
|
|
|
action: "setAttrViewName",
|
2023-07-03 23:02:19 +08:00
|
|
|
id: avId,
|
2023-07-11 22:48:48 +08:00
|
|
|
data: nameElement.textContent.trim(),
|
2023-07-03 23:02:19 +08:00
|
|
|
}], [{
|
2023-07-11 22:48:48 +08:00
|
|
|
action: "setAttrViewName",
|
2023-07-03 23:02:19 +08:00
|
|
|
id: avId,
|
2023-07-11 22:48:48 +08:00
|
|
|
name: nameElement.dataset.title,
|
2023-07-04 09:20:55 +08:00
|
|
|
}]);
|
2023-07-03 23:02:19 +08:00
|
|
|
nameElement.dataset.title = nameElement.textContent.trim();
|
2023-07-04 09:20:55 +08:00
|
|
|
};
|