2023-06-10 16:27:52 +08:00
|
|
|
import {transaction} from "../../wysiwyg/transaction";
|
2023-06-30 11:11:45 +08:00
|
|
|
import {hasClosestBlock, hasClosestByClassName} from "../../util/hasClosest";
|
2023-06-10 12:22:10 +08:00
|
|
|
|
2023-06-10 16:27:52 +08:00
|
|
|
export const popTextCell = (protyle: IProtyle, cellElement: HTMLElement) => {
|
2023-06-10 16:40:44 +08:00
|
|
|
const type = cellElement.parentElement.parentElement.firstElementChild.children[parseInt(cellElement.getAttribute("data-index")) + 1].getAttribute("data-dtype") as TAVCol;
|
2023-06-10 17:45:04 +08:00
|
|
|
const cellRect = cellElement.getBoundingClientRect();
|
|
|
|
|
let html = "";
|
2023-07-07 23:15:36 +08:00
|
|
|
const style = `style="position:absolute;left: ${cellRect.left}px;top: ${cellRect.top}px;width:${Math.max(cellRect.width, 200)}px;height: ${cellRect.height}px"`
|
2023-06-11 09:17:54 +08:00
|
|
|
if (type === "block" || type === "text") {
|
2023-07-07 23:15:36 +08:00
|
|
|
html = `<textarea ${style} class="b3-text-field">${cellElement.textContent}</textarea>`;
|
|
|
|
|
} else if (type === "number") {
|
|
|
|
|
html = `<input type="number" value="${cellElement.textContent}" ${style} class="b3-text-field">`;
|
2023-06-10 16:27:52 +08:00
|
|
|
}
|
|
|
|
|
document.body.insertAdjacentHTML("beforeend", `<div class="av__mask">
|
|
|
|
|
${html}
|
|
|
|
|
</div>`);
|
|
|
|
|
const avMaskElement = document.querySelector(".av__mask");
|
|
|
|
|
const inputElement = avMaskElement.querySelector(".b3-text-field") as HTMLInputElement;
|
|
|
|
|
if (inputElement) {
|
|
|
|
|
inputElement.select();
|
2023-06-30 17:05:54 +08:00
|
|
|
inputElement.focus();
|
2023-06-10 16:27:52 +08:00
|
|
|
inputElement.addEventListener("blur", () => {
|
2023-06-10 17:45:04 +08:00
|
|
|
updateCellValue(protyle, cellElement, type);
|
|
|
|
|
});
|
2023-06-10 16:27:52 +08:00
|
|
|
inputElement.addEventListener("keydown", (event) => {
|
|
|
|
|
if (event.isComposing) {
|
2023-06-10 17:45:04 +08:00
|
|
|
return;
|
2023-06-10 16:27:52 +08:00
|
|
|
}
|
|
|
|
|
if (event.key === "Escape" || event.key === "Enter") {
|
2023-06-10 17:45:04 +08:00
|
|
|
updateCellValue(protyle, cellElement, type);
|
2023-06-10 16:27:52 +08:00
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
}
|
2023-06-10 17:45:04 +08:00
|
|
|
});
|
2023-06-10 16:27:52 +08:00
|
|
|
}
|
|
|
|
|
avMaskElement.addEventListener("click", (event) => {
|
|
|
|
|
if ((event.target as HTMLElement).classList.contains("av__mask")) {
|
|
|
|
|
avMaskElement?.remove();
|
|
|
|
|
}
|
2023-06-10 17:45:04 +08:00
|
|
|
});
|
2023-06-10 16:27:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updateCellValue = (protyle: IProtyle, cellElement: HTMLElement, type: TAVCol) => {
|
2023-06-30 11:11:45 +08:00
|
|
|
const rowElement = hasClosestByClassName(cellElement, "av__row");
|
|
|
|
|
if (!rowElement) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const blockElement = hasClosestBlock(rowElement);
|
2023-06-10 16:27:52 +08:00
|
|
|
if (!blockElement) {
|
2023-06-10 17:45:04 +08:00
|
|
|
return;
|
2023-06-10 16:27:52 +08:00
|
|
|
}
|
2023-06-30 11:11:45 +08:00
|
|
|
const avMaskElement = document.querySelector(".av__mask");
|
2023-07-01 17:06:28 +08:00
|
|
|
const cellId = cellElement.getAttribute("data-id");
|
|
|
|
|
const avId = blockElement.getAttribute("data-av-id");
|
|
|
|
|
const rowId = rowElement.getAttribute("data-id");
|
2023-07-07 23:15:36 +08:00
|
|
|
let inputValue: string | number = (avMaskElement.querySelector(".b3-text-field") as HTMLInputElement).value
|
|
|
|
|
let oldValue: string | number = cellElement.textContent.trim()
|
|
|
|
|
if (type === "number") {
|
|
|
|
|
inputValue = parseFloat(inputValue);
|
|
|
|
|
oldValue = parseFloat(oldValue);
|
|
|
|
|
}
|
2023-06-10 16:27:52 +08:00
|
|
|
transaction(protyle, [{
|
|
|
|
|
action: "updateAttrViewCell",
|
2023-07-01 12:04:23 +08:00
|
|
|
id: cellId,
|
|
|
|
|
rowID: rowId,
|
|
|
|
|
parentID: avId,
|
|
|
|
|
data: {
|
2023-07-07 23:15:36 +08:00
|
|
|
[type]: {content: inputValue}
|
2023-07-01 12:04:23 +08:00
|
|
|
}
|
2023-06-10 16:27:52 +08:00
|
|
|
}], [{
|
|
|
|
|
action: "updateAttrViewCell",
|
2023-07-01 12:04:23 +08:00
|
|
|
id: cellId,
|
|
|
|
|
rowID: rowId,
|
|
|
|
|
parentID: avId,
|
|
|
|
|
data: {
|
2023-07-07 23:15:36 +08:00
|
|
|
[type]: {content: oldValue}
|
2023-07-01 12:04:23 +08:00
|
|
|
}
|
2023-06-10 16:27:52 +08:00
|
|
|
}]);
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
avMaskElement.remove();
|
2023-06-10 17:45:04 +08:00
|
|
|
});
|
|
|
|
|
};
|