2023-06-08 19:21:33 +08:00
|
|
|
import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName} from "../../util/hasClosest";
|
|
|
|
|
import {transaction} from "../../wysiwyg/transaction";
|
|
|
|
|
import {Menu} from "../../../plugin/API";
|
2023-06-08 22:55:31 +08:00
|
|
|
import {getIconByType} from "./render";
|
2023-06-10 11:59:12 +08:00
|
|
|
import {openEditorTab} from "../../../menus/util";
|
|
|
|
|
import {copySubMenu} from "../../../menus/commonMenuItem";
|
2023-06-10 12:22:10 +08:00
|
|
|
import {popTextCell} from "./cell";
|
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);
|
|
|
|
|
const addElement = hasClosestByAttribute(event.target, "data-type", "av-header-add");
|
2023-06-08 19:21:33 +08:00
|
|
|
if (addElement && blockElement) {
|
2023-06-08 22:56:52 +08:00
|
|
|
const menu = new Menu("av-header-add");
|
2023-06-08 19:21:33 +08:00
|
|
|
menu.addItem({
|
|
|
|
|
icon: "iconAlignLeft",
|
|
|
|
|
label: window.siyuan.languages.text,
|
|
|
|
|
click() {
|
2023-06-08 22:56:52 +08:00
|
|
|
const id = Lute.NewNodeID();
|
2023-06-08 19:21:33 +08:00
|
|
|
transaction(protyle, [{
|
|
|
|
|
action: "addAttrViewCol",
|
|
|
|
|
name: "Text",
|
|
|
|
|
parentID: blockElement.getAttribute("data-av-id"),
|
|
|
|
|
type: "text",
|
|
|
|
|
id
|
|
|
|
|
}], [{
|
|
|
|
|
action: "removeAttrViewCol",
|
|
|
|
|
id,
|
2023-06-10 11:26:56 +08:00
|
|
|
parentID: blockElement.getAttribute("data-av-id"),
|
2023-06-08 19:21:33 +08:00
|
|
|
}]);
|
|
|
|
|
}
|
2023-06-08 22:56:52 +08:00
|
|
|
});
|
|
|
|
|
const addRect = addElement.getBoundingClientRect();
|
2023-06-08 19:21:33 +08:00
|
|
|
menu.open({
|
|
|
|
|
x: addRect.left,
|
2023-06-08 22:55:31 +08:00
|
|
|
y: addRect.bottom,
|
|
|
|
|
h: addRect.height
|
2023-06-08 22:56:52 +08:00
|
|
|
});
|
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-08 22:56:52 +08:00
|
|
|
const cellElement = hasClosestByClassName(event.target, "av__cell");
|
2023-06-08 19:21:33 +08:00
|
|
|
if (cellElement && blockElement) {
|
2023-06-10 12:22:10 +08:00
|
|
|
if (cellElement.parentElement.classList.contains("av__row--header")) {
|
2023-06-10 16:27:52 +08:00
|
|
|
const type = cellElement.getAttribute("data-dtype") as TAVCol;
|
2023-06-10 12:22:10 +08:00
|
|
|
const menu = new Menu("av-header-cell");
|
2023-06-08 22:55:31 +08:00
|
|
|
menu.addItem({
|
2023-06-10 12:22:10 +08:00
|
|
|
icon: getIconByType(type),
|
|
|
|
|
label: `<input style="margin: 4px 0" class="b3-text-field" type="text" value="${cellElement.innerText.trim()}">`,
|
|
|
|
|
bind() {
|
2023-06-08 22:55:31 +08:00
|
|
|
|
|
|
|
|
}
|
2023-06-08 22:56:52 +08:00
|
|
|
});
|
2023-06-10 12:22:10 +08:00
|
|
|
if (type !== "block") {
|
|
|
|
|
menu.addItem({
|
|
|
|
|
icon: "iconEdit",
|
|
|
|
|
label: window.siyuan.languages.edit,
|
|
|
|
|
click() {
|
2023-06-08 22:55:31 +08:00
|
|
|
|
2023-06-10 12:22:10 +08:00
|
|
|
}
|
|
|
|
|
});
|
2023-06-08 22:55:31 +08:00
|
|
|
}
|
2023-06-10 12:22:10 +08:00
|
|
|
menu.addSeparator();
|
2023-06-08 22:55:31 +08:00
|
|
|
menu.addItem({
|
2023-06-10 12:22:10 +08:00
|
|
|
icon: "iconUp",
|
|
|
|
|
label: window.siyuan.languages.fileNameNatASC,
|
2023-06-08 22:55:31 +08:00
|
|
|
click() {
|
|
|
|
|
|
|
|
|
|
}
|
2023-06-08 22:56:52 +08:00
|
|
|
});
|
2023-06-08 22:55:31 +08:00
|
|
|
menu.addItem({
|
2023-06-10 12:22:10 +08:00
|
|
|
icon: "iconDown",
|
|
|
|
|
label: window.siyuan.languages.fileNameNatDESC,
|
2023-06-08 22:55:31 +08:00
|
|
|
click() {
|
|
|
|
|
|
|
|
|
|
}
|
2023-06-08 22:56:52 +08:00
|
|
|
});
|
2023-06-08 22:55:31 +08:00
|
|
|
menu.addItem({
|
2023-06-10 12:22:10 +08:00
|
|
|
icon: "iconFilter",
|
|
|
|
|
label: window.siyuan.languages.filter,
|
2023-06-08 22:55:31 +08:00
|
|
|
click() {
|
2023-06-10 12:22:10 +08:00
|
|
|
|
2023-06-08 22:55:31 +08:00
|
|
|
}
|
2023-06-08 22:56:52 +08:00
|
|
|
});
|
|
|
|
|
menu.addSeparator();
|
2023-06-10 12:22:10 +08:00
|
|
|
if (type !== "block") {
|
|
|
|
|
menu.addItem({
|
|
|
|
|
icon: "iconEyeoff",
|
|
|
|
|
label: window.siyuan.languages.hide,
|
|
|
|
|
click() {
|
2023-06-08 22:55:31 +08:00
|
|
|
|
2023-06-10 12:22:10 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
menu.addItem({
|
|
|
|
|
icon: "iconCopy",
|
|
|
|
|
label: window.siyuan.languages.duplicate,
|
|
|
|
|
click() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
menu.addItem({
|
|
|
|
|
icon: "iconTrashcan",
|
|
|
|
|
label: window.siyuan.languages.delete,
|
|
|
|
|
click() {
|
|
|
|
|
const id = cellElement.getAttribute("data-id")
|
|
|
|
|
transaction(protyle, [{
|
|
|
|
|
action: "removeAttrViewCol",
|
|
|
|
|
id,
|
|
|
|
|
parentID: blockElement.getAttribute("data-av-id"),
|
|
|
|
|
}], [{
|
|
|
|
|
action: "addAttrViewCol",
|
|
|
|
|
name: cellElement.textContent.trim(),
|
|
|
|
|
parentID: blockElement.getAttribute("data-av-id"),
|
|
|
|
|
type: type,
|
|
|
|
|
id
|
|
|
|
|
}]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
menu.addSeparator();
|
2023-06-08 22:55:31 +08:00
|
|
|
}
|
2023-06-10 12:22:10 +08:00
|
|
|
menu.addItem({
|
|
|
|
|
label: `<div class="fn__flex" style="margin-bottom: 4px"><span>${window.siyuan.languages.wrap}</span><span class="fn__space fn__flex-1"></span>
|
|
|
|
|
<input type="checkbox" class="b3-switch fn__flex-center"${cellElement.getAttribute("data-wrap") === "true" ? " checked" : ""}></div>`,
|
|
|
|
|
click() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
const cellRect = cellElement.getBoundingClientRect();
|
|
|
|
|
menu.open({
|
|
|
|
|
x: cellRect.left,
|
|
|
|
|
y: cellRect.bottom,
|
|
|
|
|
h: cellRect.height
|
|
|
|
|
});
|
|
|
|
|
(window.siyuan.menus.menu.element.querySelector(".b3-text-field") as HTMLInputElement)?.select();
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
} else {
|
2023-06-10 16:27:52 +08:00
|
|
|
popTextCell(protyle, cellElement)
|
2023-06-10 12:22:10 +08:00
|
|
|
}
|
2023-06-08 22:56:52 +08:00
|
|
|
return true;
|
2023-06-08 19:21:33 +08:00
|
|
|
}
|
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;
|
|
|
|
|
}
|
|
|
|
|
const blockElement = hasClosestBlock(rowElement);
|
|
|
|
|
if (!blockElement) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
const rowId = rowElement.getAttribute("data-id");
|
|
|
|
|
const menu = new Menu("av-row");
|
|
|
|
|
menu.addItem({
|
|
|
|
|
icon: "iconCopy",
|
|
|
|
|
label: window.siyuan.languages.duplicate,
|
|
|
|
|
click() {
|
2023-06-10 11:26:56 +08:00
|
|
|
|
2023-06-10 11:59:12 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
menu.addItem({
|
|
|
|
|
icon: "iconTrashcan",
|
|
|
|
|
label: window.siyuan.languages.delete,
|
|
|
|
|
click() {
|
|
|
|
|
transaction(protyle, [{
|
|
|
|
|
action: "removeAttrViewBlock",
|
|
|
|
|
id: blockElement.getAttribute("data-node-id"),
|
|
|
|
|
parentID: blockElement.getAttribute("data-av-id"),
|
|
|
|
|
}], [{
|
|
|
|
|
action: "insertAttrViewBlock",
|
|
|
|
|
id: blockElement.getAttribute("data-node-id"),
|
|
|
|
|
parentID: blockElement.getAttribute("data-av-id"),
|
|
|
|
|
previousID: rowElement.previousElementSibling?.getAttribute("data-id") || "",
|
|
|
|
|
srcIDs: [rowId],
|
|
|
|
|
}]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
menu.addSeparator();
|
|
|
|
|
openEditorTab(protyle.app, rowId);
|
|
|
|
|
menu.addItem({
|
|
|
|
|
label: window.siyuan.languages.copy,
|
|
|
|
|
icon: "iconCopy",
|
|
|
|
|
type: "submenu",
|
|
|
|
|
submenu: copySubMenu(rowId)
|
|
|
|
|
});
|
|
|
|
|
menu.addSeparator();
|
|
|
|
|
menu.addItem({
|
|
|
|
|
icon: "iconEdit",
|
|
|
|
|
label: window.siyuan.languages.edit,
|
|
|
|
|
click() {
|
2023-06-10 11:26:56 +08:00
|
|
|
|
2023-06-10 11:59:12 +08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
const editAttrSubmenu: IMenu[] = []
|
|
|
|
|
rowElement.parentElement.querySelectorAll(".av__row--header .av__cell").forEach((cellElement) => {
|
|
|
|
|
editAttrSubmenu.push({
|
|
|
|
|
icon: getIconByType(cellElement.getAttribute("data-dtype") as TAVCol),
|
|
|
|
|
label: cellElement.textContent.trim(),
|
2023-06-10 11:26:56 +08:00
|
|
|
click() {
|
|
|
|
|
}
|
2023-06-10 11:59:12 +08:00
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
menu.addItem({
|
|
|
|
|
icon: "iconList",
|
|
|
|
|
label: window.siyuan.languages.attr,
|
|
|
|
|
type: "submenu",
|
|
|
|
|
submenu: editAttrSubmenu
|
|
|
|
|
})
|
|
|
|
|
menu.open({
|
|
|
|
|
x: event.clientX,
|
|
|
|
|
y: event.clientY,
|
|
|
|
|
});
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
return true;
|
2023-06-10 11:26:56 +08:00
|
|
|
}
|
2023-06-10 16:27:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
const addRow = () => {
|
|
|
|
|
|
|
|
|
|
}
|