Vanessa 2024-10-29 23:00:39 +08:00
parent 505cf51efe
commit 6fe03226c5
3 changed files with 143 additions and 12 deletions

View file

@ -7,7 +7,7 @@ import {
focusBlock,
focusByWbr,
getEditorRange,
getSelectionOffset,
getSelectionOffset, setLastNodeRange,
} from "./selection";
import {Constants} from "../../constants";
import {highlightRender} from "../render/highlightRender";
@ -199,6 +199,33 @@ const processAV = (range: Range, html: string, protyle: IProtyle, blockElement:
});
};
const processTable = (range: Range, html: string, protyle: IProtyle, blockElement: HTMLElement) => {
const tempElement = document.createElement("template");
tempElement.innerHTML = html;
const cellElements = tempElement.content.querySelectorAll("th, td");
if (cellElements.length === 0) {
return false;
}
const scrollLeft = blockElement.firstElementChild.scrollLeft;
const tableSelectElement = blockElement.querySelector(".table__select") as HTMLElement;
let index = 0;
const oldHTML = blockElement.outerHTML;
blockElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
blockElement.querySelectorAll("th, td").forEach((item: HTMLTableCellElement) => {
if (!item.classList.contains("fn__none") &&
item.offsetLeft + 6 > tableSelectElement.offsetLeft + scrollLeft && item.offsetLeft + item.clientWidth - 6 < tableSelectElement.offsetLeft + scrollLeft + tableSelectElement.clientWidth &&
item.offsetTop + 6 > tableSelectElement.offsetTop && item.offsetTop + item.clientHeight - 6 < tableSelectElement.offsetTop + tableSelectElement.clientHeight &&
cellElements.length > index) {
item.innerHTML = cellElements[index].innerHTML;
index++;
setLastNodeRange(item, range, false);
}
});
range.collapse(false);
updateTransaction(protyle, blockElement.getAttribute("data-node-id"), blockElement.outerHTML, oldHTML);
return true;
}
export const insertHTML = (html: string, protyle: IProtyle, isBlock = false,
// 移动端插入嵌入块时,获取到的 range 为旧值
useProtyleRange = false,
@ -217,13 +244,13 @@ export const insertHTML = (html: string, protyle: IProtyle, isBlock = false,
isBlock = true;
}
}
let blockElement = hasClosestBlock(range.startContainer) as Element;
let blockElement = hasClosestBlock(range.startContainer) as HTMLElement;
if (!blockElement) {
// 使用鼠标点击选则模版提示列表后 range 丢失
if (protyle.toolbar.range) {
blockElement = hasClosestBlock(protyle.toolbar.range.startContainer) as Element;
blockElement = hasClosestBlock(protyle.toolbar.range.startContainer) as HTMLElement;
} else {
blockElement = protyle.wysiwyg.element.firstElementChild as Element;
blockElement = protyle.wysiwyg.element.firstElementChild as HTMLElement;
}
}
if (!blockElement) {
@ -234,6 +261,10 @@ export const insertHTML = (html: string, protyle: IProtyle, isBlock = false,
processAV(range, html, protyle, blockElement as HTMLElement);
return;
}
if (blockElement.classList.contains("table") && blockElement.querySelector(".table__select").clientWidth > 0 &&
processTable(range, html, protyle, blockElement)) {
return;
}
let id = blockElement.getAttribute("data-node-id");
range.insertNode(document.createElement("wbr"));
let oldHTML = blockElement.outerHTML;