Vanessa 2024-05-20 23:00:40 +08:00
parent d0f3ac29b6
commit 212c98d92a

View file

@ -186,9 +186,13 @@ export const getStartEndElement = (selectElements: NodeListOf<Element> | Element
};
export const duplicateBlock = (nodeElements: Element[], protyle: IProtyle) => {
let focusElement;
let focusElement: Element;
const doOperations: IOperation[] = [];
const undoOperations: IOperation[] = [];
let starIndex: number;
if (nodeElements[nodeElements.length - 1].getAttribute("data-subtype") === "o") {
starIndex = parseInt(nodeElements[nodeElements.length - 1].getAttribute("data-marker"), 10);
}
nodeElements.reverse().forEach((item, index) => {
const tempElement = item.cloneNode(true) as HTMLElement;
if (index === 0) {
@ -200,8 +204,13 @@ export const duplicateBlock = (nodeElements: Element[], protyle: IProtyle) => {
childItem.setAttribute("data-node-id", Lute.NewNodeID());
});
item.classList.remove("protyle-wysiwyg--select");
if (tempElement.dataset.type ==="NodeHTMLBlock") {
const phElement = tempElement.querySelector("protyle-html");
if (typeof starIndex === "number") {
const orderIndex = starIndex + (nodeElements.length - index);
tempElement.setAttribute("data-marker", (orderIndex) + ".");
tempElement.querySelector(".protyle-action--order").textContent = (orderIndex) + ".";
}
if (tempElement.dataset.type === "NodeHTMLBlock") {
const phElement = tempElement.querySelector("protyle-html");
const content = phElement.getAttribute("data-content");
phElement.setAttribute("data-content", "");
nodeElements[0].after(tempElement);
@ -220,6 +229,31 @@ export const duplicateBlock = (nodeElements: Element[], protyle: IProtyle) => {
id: newId,
});
});
if (typeof starIndex === "number") {
let nextElement = focusElement.nextElementSibling;
starIndex = starIndex + nodeElements.length;
while (nextElement) {
if (nextElement.getAttribute("data-subtype") === "o") {
starIndex++;
const id = nextElement.getAttribute("data-node-id");
undoOperations.push({
action: "update",
id,
data: nextElement.outerHTML,
});
nextElement.setAttribute("data-marker", starIndex + ".");
nextElement.querySelector(".protyle-action--order").textContent = starIndex + ".";
doOperations.push({
action: "update",
data: nextElement.outerHTML,
id,
});
nextElement = nextElement.nextElementSibling;
} else {
break;
}
}
}
transaction(protyle, doOperations, undoOperations);
focusBlock(focusElement);
scrollCenter(protyle);