siyuan/app/src/protyle/render/av/action.ts

298 lines
9.7 KiB
TypeScript
Raw Normal View History

import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName} from "../../util/hasClosest";
import {transaction} from "../../wysiwyg/transaction";
import {Menu} from "../../../plugin/API";
import {getIconByType} from "./render";
import {openEditorTab} from "../../../menus/util";
import {copySubMenu} from "../../../menus/commonMenuItem";
import {popTextCell} from "./cell";
const showHeaderCellMenu = (protyle: IProtyle, blockElement: HTMLElement, cellElement: HTMLElement) => {
2023-06-10 17:45:04 +08:00
const type = cellElement.getAttribute("data-dtype") as TAVCol;
const menu = new Menu("av-header-cell");
menu.addItem({
icon: getIconByType(type),
label: `<input style="margin: 4px 0" class="b3-text-field" type="text" value="${cellElement.innerText.trim()}">`,
bind() {
}
});
if (type !== "block") {
menu.addItem({
icon: "iconEdit",
label: window.siyuan.languages.edit,
click() {
}
});
}
menu.addSeparator();
menu.addItem({
icon: "iconUp",
label: window.siyuan.languages.fileNameNatASC,
click() {
}
});
menu.addItem({
icon: "iconDown",
label: window.siyuan.languages.fileNameNatDESC,
click() {
}
});
menu.addItem({
icon: "iconFilter",
label: window.siyuan.languages.filter,
click() {
}
});
menu.addSeparator();
if (type !== "block") {
menu.addItem({
icon: "iconEyeoff",
label: window.siyuan.languages.hide,
click() {
}
});
menu.addItem({
icon: "iconCopy",
label: window.siyuan.languages.duplicate,
click() {
}
});
menu.addItem({
icon: "iconTrashcan",
label: window.siyuan.languages.delete,
click() {
2023-06-10 17:45:04 +08:00
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
}]);
2023-06-10 17:45:04 +08:00
removeCol(cellElement);
}
});
menu.addSeparator();
}
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();
};
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) {
return false
}
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-06-08 22:56:52 +08:00
const menu = new Menu("av-header-add");
menu.addItem({
icon: "iconAlignLeft",
label: window.siyuan.languages.text,
click() {
2023-06-08 22:56:52 +08:00
const id = Lute.NewNodeID();
const type = "text";
transaction(protyle, [{
action: "addAttrViewCol",
name: "Text",
parentID: blockElement.getAttribute("data-av-id"),
type,
id
}], [{
action: "removeAttrViewCol",
id,
parentID: blockElement.getAttribute("data-av-id"),
}]);
2023-06-10 17:45:04 +08:00
addCol(protyle, blockElement, id, type);
}
2023-06-08 22:56:52 +08:00
});
const addRect = addElement.getBoundingClientRect();
menu.open({
x: addRect.left,
y: addRect.bottom,
h: addRect.height
2023-06-08 22:56:52 +08:00
});
event.preventDefault();
event.stopPropagation();
2023-06-08 22:56:52 +08:00
return true;
}
2023-06-10 17:56:45 +08:00
const checkElement = hasClosestByClassName(event.target, "av__firstcol");
if (checkElement) {
event.preventDefault();
event.stopPropagation();
return true;
}
2023-06-08 22:56:52 +08:00
const cellElement = hasClosestByClassName(event.target, "av__cell");
2023-06-10 17:56:45 +08:00
if (cellElement) {
if (cellElement.parentElement.classList.contains("av__row--header")) {
2023-06-10 17:45:04 +08:00
showHeaderCellMenu(protyle, blockElement, cellElement);
event.preventDefault();
event.stopPropagation();
} else {
2023-06-10 17:45:04 +08:00
popTextCell(protyle, cellElement);
}
2023-06-08 22:56:52 +08:00
return true;
}
2023-06-08 22:56:52 +08:00
return false;
};
export const avContextmenu = (protyle: IProtyle, event: MouseEvent & { detail: any }, target: HTMLElement) => {
const rowElement = hasClosestByClassName(target, "av__row");
if (!rowElement) {
return false;
}
const blockElement = hasClosestBlock(rowElement);
if (!blockElement) {
return false;
}
2023-06-10 17:56:45 +08:00
event.preventDefault();
event.stopPropagation();
blockElement.querySelectorAll(".av__row--select").forEach(item => {
item.classList.remove("av__row--select");
});
const rowId = rowElement.getAttribute("data-id");
const menu = new Menu("av-row");
2023-06-10 17:56:45 +08:00
if (menu.isOpen) {
return true
}
rowElement.classList.add("av__row--select");
menu.addItem({
icon: "iconCopy",
label: window.siyuan.languages.duplicate,
click() {
}
});
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],
}]);
rowElement.remove();
}
});
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 17:45:04 +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(),
click() {
}
2023-06-10 17:45:04 +08:00
});
});
menu.addItem({
icon: "iconList",
label: window.siyuan.languages.attr,
type: "submenu",
submenu: editAttrSubmenu
2023-06-10 17:45:04 +08:00
});
menu.open({
x: event.clientX,
y: event.clientY,
});
return true;
2023-06-10 17:45:04 +08:00
};
const addCol = (protyle: IProtyle, blockElement: HTMLElement, id: string, type: TAVCol) => {
2023-06-10 17:45:04 +08:00
let index = "0";
blockElement.querySelectorAll(".av__row--header .av__cell").forEach((item) => {
2023-06-10 17:45:04 +08:00
const dataIndex = item.getAttribute("data-index");
if (dataIndex > index) {
2023-06-10 17:45:04 +08:00
index = dataIndex;
}
2023-06-10 17:45:04 +08:00
});
blockElement.querySelectorAll(".av__row").forEach((item, index) => {
2023-06-10 17:45:04 +08:00
let html = "";
if (index === 0) {
html = `<div class="av__cell" data-index="${index}" data-id="${id}" data-dtype="${type}" data-wrap="false" style="width: 200px;">
<svg><use xlink:href="#iconAlignLeft"></use></svg>
<span>Text</span>
2023-06-10 17:45:04 +08:00
</div>`;
} else {
2023-06-10 17:45:04 +08:00
html = `<div class="av__cell" data-index="${index}" style="width: 200px;"></div>`;
}
2023-06-10 17:45:04 +08:00
item.lastElementChild.insertAdjacentHTML("beforebegin", html);
});
showHeaderCellMenu(protyle, blockElement, blockElement.querySelector(".av__row--header").lastElementChild.previousElementSibling as HTMLElement);
};
const removeCol = (cellElement: HTMLElement) => {
2023-06-10 17:45:04 +08:00
const index = cellElement.getAttribute("data-index");
const blockElement = hasClosestBlock(cellElement);
if (!blockElement) {
return false;
}
blockElement.querySelectorAll(".av__row").forEach((item) => {
item.querySelector(`[data-index="${index}"]`).remove;
2023-06-10 17:45:04 +08:00
});
};
export const addAVRow = (blockElement: HTMLElement, ids: string[], previousID: string) => {
2023-06-10 17:45:04 +08:00
const rowElement = previousID ? blockElement.querySelector(`[data-id=${previousID}]`) : blockElement.querySelector(".av__row--header");
let html = "";
ids.forEach((id) => {
2023-06-10 17:45:04 +08:00
html += "<div class=\"av__row\"><div class=\"av__firstcol\"><svg><use xlink:href=\"#iconUncheck\"></use></svg></div>";
Array.from(rowElement.children).forEach((item: HTMLElement, index) => {
if (index === 0 || index === rowElement.childElementCount - 1) {
2023-06-10 17:45:04 +08:00
return;
}
html += `<div class="av__cell" data-index="${index}" style="width: ${item.style.width};>${id}</div>`;
2023-06-10 17:45:04 +08:00
});
html += "<div></div></div>";
2023-06-10 17:45:04 +08:00
});
rowElement.insertAdjacentHTML("afterend", html);
2023-06-10 17:45:04 +08:00
};