Vanessa 2022-11-21 23:36:49 +08:00
parent a6f5b9a36e
commit 6f577f87ab
2 changed files with 90 additions and 56 deletions

View file

@ -242,7 +242,7 @@ const dragSb = (protyle: IProtyle, sourceElements: Element[], targetElement: Ele
focusBlock(sourceElements[0]);
};
const dragSame = (protyle: IProtyle, sourceElements: Element[], targetElement: Element, isBottom: boolean) => {
const dragSame = async (protyle: IProtyle, sourceElements: Element[], targetElement: Element, isBottom: boolean) => {
const isSameDoc = protyle.element.contains(sourceElements[0]);
const doOperations: IOperation[] = [];
const undoOperations: IOperation[] = [];
@ -258,6 +258,7 @@ const dragSame = (protyle: IProtyle, sourceElements: Element[], targetElement: E
}
let topSourceElement: Element;
let oldSourceParentElement = sourceElements[0].parentElement;
const targetId = targetElement.getAttribute("data-node-id")
if (isBottom) {
if (newSourceElement) {
targetElement.insertAdjacentElement("afterend", newSourceElement);
@ -265,7 +266,7 @@ const dragSame = (protyle: IProtyle, sourceElements: Element[], targetElement: E
action: "insert",
data: newSourceElement.outerHTML,
id: newSourceElement.getAttribute("data-node-id"),
previousID: targetElement.getAttribute("data-node-id"),
previousID: targetId,
});
sourceElements.reverse().forEach((item, index) => {
if (index === sourceElements.length - 1) {
@ -313,8 +314,13 @@ const dragSame = (protyle: IProtyle, sourceElements: Element[], targetElement: E
id: newSourceElement.getAttribute("data-node-id"),
});
} else {
sourceElements.reverse().forEach((item, index) => {
if (index === sourceElements.length - 1) {
const foldHeadingIds: string[] = []
for (let i = sourceElements.length - 1; i >= 0; i--) {
const item = sourceElements[i];
const id = item.getAttribute("data-node-id");
const previousID = item.previousElementSibling?.getAttribute("data-node-id");
const parentID = item.parentElement.getAttribute("data-node-id") || protyle.block.rootID
if (i === 0) {
topSourceElement = getTopAloneElement(item);
if (topSourceElement.isSameNode(item)) {
topSourceElement = undefined;
@ -323,15 +329,32 @@ const dragSame = (protyle: IProtyle, sourceElements: Element[], targetElement: E
topSourceElement = targetElement;
}
}
if (item.getAttribute("data-type") === "NodeHeading" && item.getAttribute("fold") === "1") {
foldHeadingIds.push(id);
const headingIds = await fetchSyncPost("/api/block/getHeadingChildrenIDs", {id})
headingIds.data.reverse().forEach((headingId: string) => {
undoOperations.push({
action: "move",
id: item.getAttribute("data-node-id"),
previousID: item.previousElementSibling?.getAttribute("data-node-id"),
parentID: item.parentElement.getAttribute("data-node-id") || protyle.block.rootID,
id,
previousID,
parentID,
});
doOperations.push({
action: "move",
id:headingId,
previousID: targetId,
});
})
}
undoOperations.push({
action: "move",
id,
previousID,
parentID,
});
if (!isSameDoc) {
// 打开两个相同的文档
const sameElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${item.getAttribute("data-node-id")}"]`);
const sameElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`);
if (sameElement) {
sameElement.remove();
}
@ -339,11 +362,22 @@ const dragSame = (protyle: IProtyle, sourceElements: Element[], targetElement: E
targetElement.insertAdjacentElement("afterend", item);
doOperations.push({
action: "move",
id: item.getAttribute("data-node-id"),
previousID: targetElement.getAttribute("data-node-id"),
});
id,
previousID: targetId,
});
}
undoOperations.reverse();
foldHeadingIds.forEach(id => {
undoOperations.push({
action: "foldHeading",
id,
data: "remove"
});
doOperations.push({
action: "unfoldHeading",
id,
});
})
}
} else {
if (newSourceElement) {
@ -352,7 +386,7 @@ const dragSame = (protyle: IProtyle, sourceElements: Element[], targetElement: E
action: "insert",
data: newSourceElement.outerHTML,
id: newSourceElement.getAttribute("data-node-id"),
nextID: targetElement.getAttribute("data-node-id"),
nextID: targetId,
});
sourceElements.reverse().forEach((item, index) => {
if (index === sourceElements.length - 1) {

View file

@ -73,49 +73,6 @@ const promiseTransaction = () => {
return;
}
countBlockWord([], protyle.block.rootID, true);
if (doOperations.length === 1 && (doOperations[0].action === "unfoldHeading" || doOperations[0].action === "foldHeading")) {
const gutterFoldElement = protyle.gutter.element.querySelector('[data-type="fold"]');
if (gutterFoldElement) {
gutterFoldElement.removeAttribute("disabled");
}
if (doOperations[0].action === "unfoldHeading") {
const scrollTop = protyle.contentElement.scrollTop;
protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${doOperations[0].id}"]`).forEach(item => {
if (!item.lastElementChild.classList.contains("protyle-attr")) {
item.lastElementChild.remove();
}
removeUnfoldRepeatBlock(response.data[0].doOperations[0].retData, protyle);
item.insertAdjacentHTML("afterend", response.data[0].doOperations[0].retData);
if (doOperations[0].data === "remove") {
// https://github.com/siyuan-note/siyuan/issues/2188
const selection = getSelection();
if (selection.rangeCount > 0 && item.contains(selection.getRangeAt(0).startContainer)) {
focusBlock(item.nextElementSibling, undefined, true);
}
item.remove();
}
});
processRender(protyle.wysiwyg.element);
highlightRender(protyle.wysiwyg.element);
blockRender(protyle, protyle.wysiwyg.element);
protyle.contentElement.scrollTop = scrollTop;
protyle.scroll.lastScrollTop = scrollTop;
} else if (doOperations[0].action === "foldHeading") {
// 折叠标题后未触发动态加载 https://github.com/siyuan-note/siyuan/issues/4168
if (protyle.wysiwyg.element.lastElementChild.getAttribute("data-eof") !== "true" &&
!protyle.scroll.element.classList.contains("fn__none")) {
fetchPost("/api/filetree/getDoc", {
id: protyle.wysiwyg.element.lastElementChild.getAttribute("data-node-id"),
mode: 2,
k: protyle.options.key || "",
size: window.siyuan.config.editor.dynamicLoadBlocks,
}, getResponse => {
onGet(getResponse, protyle, [Constants.CB_GET_APPEND, Constants.CB_GET_UNCHANGEID]);
});
}
}
return;
}
if (doOperations[0].action === "setAttrs") {
const gutterFoldElement = protyle.gutter.element.querySelector('[data-type="fold"]');
if (gutterFoldElement) {
@ -136,6 +93,49 @@ const promiseTransaction = () => {
range = getSelection().getRangeAt(0);
}
doOperations.forEach(operation => {
if (operation.action === "unfoldHeading" || operation.action === "foldHeading") {
const gutterFoldElement = protyle.gutter.element.querySelector('[data-type="fold"]');
if (gutterFoldElement) {
gutterFoldElement.removeAttribute("disabled");
}
if (operation.action === "unfoldHeading") {
const scrollTop = protyle.contentElement.scrollTop;
protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${operation.id}"]`).forEach(item => {
if (!item.lastElementChild.classList.contains("protyle-attr")) {
item.lastElementChild.remove();
}
removeUnfoldRepeatBlock(operation.retData, protyle);
item.insertAdjacentHTML("afterend", operation.retData);
if (operation.data === "remove") {
// https://github.com/siyuan-note/siyuan/issues/2188
const selection = getSelection();
if (selection.rangeCount > 0 && item.contains(selection.getRangeAt(0).startContainer)) {
focusBlock(item.nextElementSibling, undefined, true);
}
item.remove();
}
});
processRender(protyle.wysiwyg.element);
highlightRender(protyle.wysiwyg.element);
blockRender(protyle, protyle.wysiwyg.element);
protyle.contentElement.scrollTop = scrollTop;
protyle.scroll.lastScrollTop = scrollTop;
return;
}
// 折叠标题后未触发动态加载 https://github.com/siyuan-note/siyuan/issues/4168
if (protyle.wysiwyg.element.lastElementChild.getAttribute("data-eof") !== "true" &&
!protyle.scroll.element.classList.contains("fn__none")) {
fetchPost("/api/filetree/getDoc", {
id: protyle.wysiwyg.element.lastElementChild.getAttribute("data-node-id"),
mode: 2,
k: protyle.options.key || "",
size: window.siyuan.config.editor.dynamicLoadBlocks,
}, getResponse => {
onGet(getResponse, protyle, [Constants.CB_GET_APPEND, Constants.CB_GET_UNCHANGEID]);
});
}
return;
}
if (operation.action === "update") {
if (protyle.options.backlinkData) {
// 反链中有多个相同块的情况