This commit is contained in:
Vanessa 2023-04-27 10:59:20 +08:00
parent 72c7a231c2
commit 751e790c79

View file

@ -1,16 +1,16 @@
import {transaction} from "./transaction";
import {transaction, updateTransaction} from "./transaction";
import {focusByWbr} from "../util/selection";
import * as dayjs from "dayjs";
export const turnIntoTaskList = (protyle: IProtyle, type: string, blockElement: HTMLElement, editElement: HTMLElement) => {
if (type !== "NodeCodeBlock" &&
blockElement.parentElement.getAttribute("data-subtype") !== "t" &&
(
["[ ]", "[x]", "[X]", "【 】", "【x】", "【X】"].includes(editElement.innerHTML.substring(0, 3)) ||
["[]", "【】"].includes(editElement.innerHTML.substring(0, 2))
)
) {
const doOperation: IOperation[] = []
const undoOperation: IOperation[] = []
const contextStartIndex = (editElement.innerHTML.indexOf("]") || editElement.innerHTML.indexOf("]")) + 1;
const contextStartIndex = (editElement.innerHTML.indexOf("]") + 1) || (editElement.innerHTML.indexOf("】") + 1);
const isDone = editElement.innerHTML.substring(1, 2).toLowerCase() === "x";
if (blockElement.parentElement.classList.contains("li") &&
blockElement.parentElement.childElementCount === 3 // https://ld246.com/article/1659315815506
@ -18,9 +18,20 @@ export const turnIntoTaskList = (protyle: IProtyle, type: string, blockElement:
// 仅有一项的列表才可转换
if (!blockElement.parentElement.parentElement.classList.contains("protyle-wysiwyg") && // https://ld246.com/article/1659315815506
blockElement.parentElement.parentElement.childElementCount === 2) {
`<div data-subtype="t" data-node-id="${blockElement.parentElement.parentElement.getAttribute("data-node-id")}" data-type="NodeList" class="list"><div data-marker="*" data-subtype="t" data-node-id="${blockElement.parentElement.getAttribute("data-node-id")}" data-type="NodeListItem" class="li${isDone ? " protyle-task--done" : ""}"><div class="protyle-action protyle-action--task" draggable="true"><svg><use xlink:href="#icon${isDone ? "C" : "Unc"}heck"></use></svg></div><div data-node-id="${id}" data-type="NodeParagraph" class="p"><div contenteditable="true" spellcheck="${window.siyuan.config.editor.spellcheck}"><wbr></div><div class="protyle-attr" contenteditable="false"></div></div><div class="protyle-attr" contenteditable="false"></div></div><div class="protyle-attr" contenteditable="false"></div></div>`;
const liElement = blockElement.parentElement.parentElement;
const oldHTML = liElement.outerHTML;
liElement.setAttribute("data-subtype", "t");
liElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
blockElement.parentElement.setAttribute("data-subtype", "t");
if (isDone) {
blockElement.parentElement.classList.add("protyle-task--done");
}
blockElement.previousElementSibling.outerHTML = `<div class="protyle-action protyle-action--task" draggable="true"><svg><use xlink:href="#icon${isDone ? "C" : "Unc"}heck"></use></svg></div>`
editElement.innerHTML = editElement.innerHTML.substring(contextStartIndex);
updateTransaction(protyle, liElement.getAttribute("data-node-id"), liElement.outerHTML, oldHTML);
return true;
}
return true;
return false;
} else {
const id = blockElement.getAttribute("data-node-id")
const newId = Lute.NewNodeID();