2023-07-09 23:54:32 +08:00
|
|
|
import {Menu} from "../../../plugin/Menu";
|
|
|
|
|
import {transaction} from "../../wysiwyg/transaction";
|
|
|
|
|
import {hasClosestByClassName} from "../../util/hasClosest";
|
|
|
|
|
import {confirmDialog} from "../../../dialog/confirmDialog";
|
|
|
|
|
import {setPosition} from "../../../util/setPosition";
|
|
|
|
|
import {upDownHint} from "../../../util/upDownHint";
|
|
|
|
|
|
|
|
|
|
const filterSelectHTML = (key: string, options: { name: string, color: string }[]) => {
|
|
|
|
|
let html = "";
|
|
|
|
|
let hasMatch = false;
|
|
|
|
|
if (options) {
|
|
|
|
|
options.forEach(item => {
|
|
|
|
|
if (!key ||
|
|
|
|
|
(key.toLowerCase().indexOf(item.name.toLowerCase()) > -1 ||
|
|
|
|
|
item.name.toLowerCase().indexOf(key.toLowerCase()) > -1)) {
|
2023-07-10 10:29:26 +08:00
|
|
|
html += `<button data-type="addSelectColAndCell" class="b3-menu__item${html ? "" : " b3-menu__item--current"}" draggable="true" data-name="${item.name}" data-color="${item.color}">
|
2023-07-09 23:54:32 +08:00
|
|
|
<svg class="b3-menu__icon"><use xlink:href="#iconDrag"></use></svg>
|
|
|
|
|
<div class="fn__flex-1">
|
2023-07-10 10:29:26 +08:00
|
|
|
<span class="b3-chip" style="background-color:var(--b3-font-background${item.color});color:var(--b3-font-color${item.color})">
|
2023-07-09 23:54:32 +08:00
|
|
|
<span class="fn__ellipsis">${item.name}</span>
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
2023-07-10 10:29:26 +08:00
|
|
|
<svg class="b3-menu__action" data-type="setSelectCol"><use xlink:href="#iconEdit"></use></svg>
|
2023-07-09 23:54:32 +08:00
|
|
|
</button>`;
|
|
|
|
|
}
|
|
|
|
|
if (key === item.name) {
|
|
|
|
|
hasMatch = true;
|
|
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
|
|
|
|
if (!hasMatch && key) {
|
|
|
|
|
const colorIndex = (options?.length || 0) % 13 + 1;
|
2023-07-10 10:29:26 +08:00
|
|
|
html = `<button data-type="addSelectColAndCell" class="b3-menu__item${html ? "" : " b3-menu__item--current"}" data-name="${key}" data-color="${colorIndex}">
|
2023-07-10 00:17:54 +08:00
|
|
|
<svg class="b3-menu__icon"><use xlink:href="#iconAdd"></use></svg>
|
2023-07-09 23:54:32 +08:00
|
|
|
<div class="fn__flex-1">
|
|
|
|
|
<span class="b3-chip" style="background-color:var(--b3-font-background${colorIndex});color:var(--b3-font-color${colorIndex})">
|
|
|
|
|
<span class="fn__ellipsis">${key}</span>
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<span class="b3-menu__accelerator">Enter</span>
|
2023-07-10 00:17:54 +08:00
|
|
|
</button>${html}`;
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
|
|
|
|
return html;
|
2023-07-09 23:55:29 +08:00
|
|
|
};
|
2023-07-09 23:54:32 +08:00
|
|
|
|
2023-07-10 10:29:26 +08:00
|
|
|
export const removeSelectCell = (protyle: IProtyle, data: IAV, options: {
|
|
|
|
|
cellElement: HTMLElement
|
|
|
|
|
}, target: HTMLElement) => {
|
|
|
|
|
if (!target) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const rowId = options.cellElement.parentElement.dataset.id;
|
|
|
|
|
const colId = options.cellElement.dataset.colId;
|
|
|
|
|
const cellId = options.cellElement.dataset.id;
|
|
|
|
|
let colData: IAVColumn;
|
|
|
|
|
data.columns.find((item: IAVColumn) => {
|
|
|
|
|
if (item.id === colId) {
|
|
|
|
|
colData = item;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (!colData.options) {
|
|
|
|
|
colData.options = [];
|
|
|
|
|
}
|
|
|
|
|
let cellData: IAVCell;
|
|
|
|
|
data.rows.find(row => {
|
|
|
|
|
if (row.id === rowId) {
|
|
|
|
|
row.cells.find(cell => {
|
|
|
|
|
if (cell.id === cellId) {
|
|
|
|
|
cellData = cell;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
let oldValue;
|
|
|
|
|
if (colData.type !== "select") {
|
|
|
|
|
oldValue = Object.assign([], cellData.value.mSelect);
|
|
|
|
|
cellData.value.mSelect?.find((item: { content: string }, index: number) => {
|
|
|
|
|
if (item.content === target.dataset.content) {
|
|
|
|
|
cellData.value.mSelect.splice(index, 1);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
oldValue = Object.assign({}, cellData.value.select);
|
|
|
|
|
cellData.value.select = {
|
|
|
|
|
color: "",
|
|
|
|
|
content: ""
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
target.remove();
|
|
|
|
|
|
|
|
|
|
transaction(protyle, [{
|
|
|
|
|
action: "updateAttrViewCell",
|
|
|
|
|
id: cellId,
|
|
|
|
|
rowID: rowId,
|
|
|
|
|
parentID: data.id,
|
|
|
|
|
data: cellData.value
|
|
|
|
|
}], [{
|
|
|
|
|
action: "updateAttrViewCell",
|
|
|
|
|
id: cellId,
|
|
|
|
|
rowID: rowId,
|
|
|
|
|
parentID: data.id,
|
|
|
|
|
data: {
|
|
|
|
|
[colData.type]: oldValue
|
|
|
|
|
}
|
|
|
|
|
}]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const setSelectCol = (protyle: IProtyle, data: IAV, options: {
|
2023-07-09 23:54:32 +08:00
|
|
|
cellElement: HTMLElement;
|
|
|
|
|
}, target: HTMLElement,) => {
|
|
|
|
|
const name = target.parentElement.dataset.name;
|
2023-07-10 11:18:59 +08:00
|
|
|
const color = target.parentElement.dataset.color;
|
2023-07-09 23:54:32 +08:00
|
|
|
const menu = new Menu("av-select-option", () => {
|
|
|
|
|
transaction(protyle, [{
|
|
|
|
|
action: "updateAttrViewColOption",
|
|
|
|
|
id: colId,
|
|
|
|
|
parentID: data.id,
|
|
|
|
|
data: {
|
2023-07-10 11:18:59 +08:00
|
|
|
newColor: color,
|
2023-07-09 23:54:32 +08:00
|
|
|
oldName: name,
|
|
|
|
|
newName: inputElement.value
|
|
|
|
|
},
|
|
|
|
|
}], [{
|
|
|
|
|
action: "updateAttrViewColOption",
|
|
|
|
|
id: colId,
|
|
|
|
|
parentID: data.id,
|
|
|
|
|
data: {
|
2023-07-10 11:18:59 +08:00
|
|
|
newColor: color,
|
2023-07-09 23:54:32 +08:00
|
|
|
oldName: inputElement.value,
|
|
|
|
|
newName: name
|
|
|
|
|
},
|
|
|
|
|
}]);
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-09 23:54:32 +08:00
|
|
|
if (menu.isOpen) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const menuElement = hasClosestByClassName(target, "b3-menu");
|
|
|
|
|
if (!menuElement) {
|
2023-07-09 23:55:29 +08:00
|
|
|
return;
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
|
|
|
|
const colId = options.cellElement.dataset.colId;
|
|
|
|
|
menu.addItem({
|
|
|
|
|
iconHTML: "",
|
|
|
|
|
label: `<input class="b3-text-field" style="margin: 4px 0" value="${name}">`
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-09 23:54:32 +08:00
|
|
|
menu.addItem({
|
|
|
|
|
label: window.siyuan.languages.delete,
|
|
|
|
|
icon: "iconTrashcan",
|
|
|
|
|
click() {
|
|
|
|
|
confirmDialog(window.siyuan.languages.deleteOpConfirm, window.siyuan.languages.confirmDelete, () => {
|
2023-07-09 23:55:29 +08:00
|
|
|
let colOptions: { name: string, color: string }[] = [];
|
2023-07-09 23:54:32 +08:00
|
|
|
data.columns.find(column => {
|
|
|
|
|
if (column.id === colId) {
|
|
|
|
|
colOptions = column.options;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
|
|
|
|
const newName = target.parentElement.dataset.name;
|
2023-07-09 23:54:32 +08:00
|
|
|
transaction(protyle, [{
|
|
|
|
|
action: "removeAttrViewColOption",
|
|
|
|
|
id: colId,
|
|
|
|
|
parentID: data.id,
|
|
|
|
|
data: newName,
|
|
|
|
|
}], [{
|
|
|
|
|
action: "updateAttrViewColOptions",
|
|
|
|
|
id: colId,
|
|
|
|
|
parentID: data.id,
|
|
|
|
|
data: colOptions
|
|
|
|
|
}]);
|
|
|
|
|
colOptions.find((item, index) => {
|
|
|
|
|
if (item.name === newName) {
|
|
|
|
|
colOptions.splice(index, 1);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-09 23:54:32 +08:00
|
|
|
menuElement.innerHTML = getSelectHTML(data, options);
|
|
|
|
|
const cellRect = options.cellElement.getBoundingClientRect();
|
|
|
|
|
setPosition(menuElement, cellRect.left, cellRect.bottom, cellRect.height);
|
|
|
|
|
bindSelectEvent(protyle, data, menuElement, options);
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
|
|
|
|
menu.addSeparator();
|
2023-07-09 23:54:32 +08:00
|
|
|
Array.from(Array(13).keys()).forEach(index => {
|
|
|
|
|
menu.addItem({
|
2023-07-10 10:40:29 +08:00
|
|
|
accelerator: parseInt(color) === index + 1 ? '<svg class="svg" style="height: 30px; float: left;"><use xlink:href="#iconSelect"></use></svg>' : undefined,
|
2023-07-09 23:54:32 +08:00
|
|
|
iconHTML: "",
|
|
|
|
|
label: `<span class="color__square" style="padding: 5px;margin: 2px;color: var(--b3-font-color${index + 1});background-color: var(--b3-font-background${index + 1});">A</span>`,
|
|
|
|
|
click() {
|
|
|
|
|
transaction(protyle, [{
|
|
|
|
|
action: "updateAttrViewColOption",
|
|
|
|
|
id: colId,
|
|
|
|
|
parentID: data.id,
|
|
|
|
|
data: {
|
2023-07-10 11:18:59 +08:00
|
|
|
oldName: name,
|
|
|
|
|
newName: inputElement.value,
|
2023-07-09 23:54:32 +08:00
|
|
|
oldColor: color,
|
|
|
|
|
newColor: (index + 1).toString()
|
|
|
|
|
},
|
|
|
|
|
}], [{
|
|
|
|
|
action: "updateAttrViewColOption",
|
|
|
|
|
id: colId,
|
|
|
|
|
parentID: data.id,
|
|
|
|
|
data: {
|
2023-07-10 11:18:59 +08:00
|
|
|
oldName: inputElement.value,
|
|
|
|
|
newName: name,
|
2023-07-09 23:54:32 +08:00
|
|
|
oldColor: (index + 1).toString(),
|
|
|
|
|
newColor: color
|
|
|
|
|
},
|
|
|
|
|
}]);
|
|
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
|
|
|
|
});
|
2023-07-09 23:54:32 +08:00
|
|
|
const rect = target.getBoundingClientRect();
|
|
|
|
|
menu.open({
|
|
|
|
|
x: rect.left,
|
|
|
|
|
y: rect.bottom,
|
2023-07-10 11:18:59 +08:00
|
|
|
w: rect.width,
|
2023-07-09 23:54:32 +08:00
|
|
|
h: rect.height,
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-09 23:54:32 +08:00
|
|
|
const inputElement = window.siyuan.menus.menu.element.querySelector("input");
|
|
|
|
|
inputElement.select();
|
2023-07-09 23:55:29 +08:00
|
|
|
};
|
2023-07-09 23:54:32 +08:00
|
|
|
|
|
|
|
|
export const bindSelectEvent = (protyle: IProtyle, data: IAV, menuElement: HTMLElement, options: {
|
|
|
|
|
cellElement: HTMLElement
|
|
|
|
|
}) => {
|
|
|
|
|
const inputElement = menuElement.querySelector("input");
|
|
|
|
|
const colId = options.cellElement.dataset.colId;
|
|
|
|
|
let colData: IAVColumn;
|
|
|
|
|
data.columns.find((item: IAVColumn) => {
|
|
|
|
|
if (item.id === colId) {
|
|
|
|
|
colData = item;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-09 23:54:32 +08:00
|
|
|
if (!colData.options) {
|
|
|
|
|
colData.options = [];
|
|
|
|
|
}
|
|
|
|
|
inputElement.addEventListener("input", (event: InputEvent) => {
|
|
|
|
|
if (event.isComposing) {
|
2023-07-09 23:55:29 +08:00
|
|
|
return;
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
|
|
|
|
menuElement.lastElementChild.innerHTML = filterSelectHTML(inputElement.value, colData.options);
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-09 23:54:32 +08:00
|
|
|
inputElement.addEventListener("compositionend", (event: InputEvent) => {
|
|
|
|
|
if (event.isComposing) {
|
2023-07-09 23:55:29 +08:00
|
|
|
return;
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
|
|
|
|
menuElement.lastElementChild.innerHTML = filterSelectHTML(inputElement.value, colData.options);
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-09 23:54:32 +08:00
|
|
|
inputElement.addEventListener("keydown", (event: KeyboardEvent) => {
|
|
|
|
|
if (event.isComposing) {
|
2023-07-09 23:55:29 +08:00
|
|
|
return;
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
|
|
|
|
let currentElement = upDownHint(menuElement.lastElementChild, event, "b3-menu__item--current");
|
|
|
|
|
if (event.key === "Enter") {
|
|
|
|
|
if (!currentElement) {
|
|
|
|
|
currentElement = menuElement.querySelector(".b3-menu__item--current");
|
|
|
|
|
}
|
2023-07-10 10:29:26 +08:00
|
|
|
addSelectColAndCell(protyle, data, options, currentElement, menuElement);
|
|
|
|
|
} else if (event.key === "Backspace" && inputElement.value === "") {
|
|
|
|
|
removeSelectCell(protyle, data, options, inputElement.previousElementSibling as HTMLElement);
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
|
|
|
|
};
|
2023-07-09 23:54:32 +08:00
|
|
|
|
2023-07-10 10:29:26 +08:00
|
|
|
export const addSelectColAndCell = (protyle: IProtyle, data: IAV, options: {
|
2023-07-10 00:17:54 +08:00
|
|
|
cellElement: HTMLElement
|
2023-07-10 10:29:26 +08:00
|
|
|
}, currentElement: HTMLElement, menuElement: HTMLElement) => {
|
2023-07-10 00:17:54 +08:00
|
|
|
const rowId = options.cellElement.parentElement.dataset.id;
|
|
|
|
|
const colId = options.cellElement.dataset.colId;
|
|
|
|
|
const cellId = options.cellElement.dataset.id;
|
|
|
|
|
let colData: IAVColumn;
|
|
|
|
|
data.columns.find((item: IAVColumn) => {
|
|
|
|
|
if (item.id === colId) {
|
|
|
|
|
colData = item;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (!colData.options) {
|
|
|
|
|
colData.options = [];
|
|
|
|
|
}
|
|
|
|
|
let cellData: IAVCell;
|
|
|
|
|
data.rows.find(row => {
|
|
|
|
|
if (row.id === rowId) {
|
|
|
|
|
row.cells.find(cell => {
|
|
|
|
|
if (cell.id === cellId) {
|
|
|
|
|
cellData = cell;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let oldValue;
|
|
|
|
|
if (colData.type !== "select") {
|
2023-07-10 10:29:26 +08:00
|
|
|
oldValue = Object.assign([], cellData.value.mSelect);
|
|
|
|
|
cellData.value.mSelect?.push({
|
2023-07-10 00:17:54 +08:00
|
|
|
color: currentElement.dataset.color,
|
|
|
|
|
content: currentElement.dataset.name
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2023-07-10 11:04:05 +08:00
|
|
|
if (!cellData.value) {
|
|
|
|
|
cellData.value = {select: {}};
|
|
|
|
|
}
|
2023-07-10 00:17:54 +08:00
|
|
|
oldValue = Object.assign({}, cellData.value.select);
|
|
|
|
|
cellData.value.select = {
|
|
|
|
|
color: currentElement.dataset.color,
|
|
|
|
|
content: currentElement.dataset.name
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (currentElement.querySelector(".b3-menu__accelerator")) {
|
|
|
|
|
colData.options.push({
|
|
|
|
|
color: currentElement.dataset.color,
|
|
|
|
|
name: currentElement.dataset.name
|
|
|
|
|
});
|
|
|
|
|
transaction(protyle, [{
|
|
|
|
|
action: "updateAttrViewColOptions",
|
|
|
|
|
id: colId,
|
|
|
|
|
parentID: data.id,
|
|
|
|
|
data: colData.options
|
|
|
|
|
}, {
|
|
|
|
|
action: "updateAttrViewCell",
|
|
|
|
|
id: cellId,
|
|
|
|
|
rowID: rowId,
|
|
|
|
|
parentID: data.id,
|
|
|
|
|
data: {
|
|
|
|
|
[colData.type]: cellData.value
|
|
|
|
|
}
|
|
|
|
|
}], [{
|
|
|
|
|
action: "removeAttrViewColOption",
|
|
|
|
|
id: colId,
|
|
|
|
|
parentID: data.id,
|
|
|
|
|
data: currentElement.dataset.name,
|
|
|
|
|
}]);
|
|
|
|
|
} else {
|
|
|
|
|
transaction(protyle, [{
|
|
|
|
|
action: "updateAttrViewCell",
|
|
|
|
|
id: cellId,
|
|
|
|
|
rowID: rowId,
|
|
|
|
|
parentID: data.id,
|
|
|
|
|
data: cellData.value
|
|
|
|
|
}], [{
|
|
|
|
|
action: "updateAttrViewCell",
|
|
|
|
|
id: cellId,
|
|
|
|
|
rowID: rowId,
|
|
|
|
|
parentID: data.id,
|
|
|
|
|
data: {
|
|
|
|
|
[colData.type]: oldValue
|
|
|
|
|
}
|
|
|
|
|
}]);
|
|
|
|
|
}
|
|
|
|
|
if (colData.type === "select") {
|
|
|
|
|
menuElement.parentElement.remove();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-09 23:54:32 +08:00
|
|
|
export const getSelectHTML = (data: IAV, options: { cellElement: HTMLElement }) => {
|
2023-07-09 23:55:29 +08:00
|
|
|
const cellId = options.cellElement.dataset.id;
|
|
|
|
|
const colId = options.cellElement.dataset["colId"];
|
2023-07-09 23:54:32 +08:00
|
|
|
const colData = data.columns.find(item => {
|
|
|
|
|
if (item.id === colId) {
|
2023-07-09 23:55:29 +08:00
|
|
|
return item;
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
|
|
|
|
let selectedHTML = "";
|
2023-07-09 23:54:32 +08:00
|
|
|
data.rows.find(row => {
|
|
|
|
|
if (options.cellElement.parentElement.dataset.id === row.id) {
|
|
|
|
|
row.cells.find(cell => {
|
|
|
|
|
if (cell.id === cellId && cell.value) {
|
|
|
|
|
if (colData.type === "mSelect") {
|
2023-07-10 10:29:26 +08:00
|
|
|
cell.value.mSelect?.forEach((item: { content: string, color: string }) => {
|
|
|
|
|
selectedHTML += `<div class="b3-chip" data-type="removeSelectCell" data-content="${item.content}" style="background-color:var(--b3-font-background${item.color});color:var(--b3-font-color${item.color})">${item.content}<svg class="b3-chip__close" data-type="remove-option"><use xlink:href="#iconCloseRound"></use></svg></div>`;
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-10 10:29:26 +08:00
|
|
|
} else if (cell.value.select.content) {
|
|
|
|
|
selectedHTML += `<div class="b3-chip" data-type="removeSelectCell" data-content="${cell.value.select.content}" style="background-color:var(--b3-font-background${cell.value.select.color})";color:var(--b3-font-color${cell.value.select.color})>${cell.value.select.content}<svg class="b3-chip__close" data-type="remove-option"><use xlink:href="#iconCloseRound"></use></svg></div>`;
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
|
|
|
|
return true;
|
2023-07-09 23:54:32 +08:00
|
|
|
}
|
2023-07-09 23:55:29 +08:00
|
|
|
});
|
2023-07-09 23:54:32 +08:00
|
|
|
return `<div class="b3-chips">
|
|
|
|
|
${selectedHTML}
|
2023-07-10 09:44:27 +08:00
|
|
|
<input>
|
2023-07-09 23:54:32 +08:00
|
|
|
</div>
|
2023-07-09 23:55:29 +08:00
|
|
|
<div>${filterSelectHTML("", colData.options)}</div>`;
|
|
|
|
|
};
|