diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.ts index 0b4d2f41d..28712dfd4 100644 --- a/app/src/protyle/util/editorCommonEvent.ts +++ b/app/src/protyle/util/editorCommonEvent.ts @@ -15,11 +15,9 @@ import {updateListOrder} from "../wysiwyg/list"; import {fetchPost, fetchSyncPost} from "../../util/fetch"; import {onGet} from "./onGet"; /// #if !MOBILE -import {getInstanceById} from "../../layout/util"; -import {Tab} from "../../layout/Tab"; +import {getAllEditor} from "../../layout/getAll"; import {updatePanelByEditor} from "../../editor/util"; /// #endif -import {Editor} from "../../editor"; import {blockRender} from "../render/blockRender"; import {uploadLocalFiles} from "../upload"; import {insertHTML} from "./insertHTML"; @@ -512,32 +510,52 @@ const dragSb = async (protyle: IProtyle, sourceElements: Element[], targetElemen } if (!isCopy && oldSourceParentElement && oldSourceParentElement.classList.contains("sb") && oldSourceParentElement.childElementCount === 2) { // 拖拽后,sb 只剩下一个元素 - const sbData = cancelSB(protyle, oldSourceParentElement); - doOperations.push(sbData.doOperations[0], sbData.doOperations[1]); - undoOperations.splice(0, 0, sbData.undoOperations[0], sbData.undoOperations[1]); + if (isSameDoc) { + const sbData = cancelSB(protyle, oldSourceParentElement); + doOperations.push(sbData.doOperations[0], sbData.doOperations[1]); + undoOperations.splice(0, 0, sbData.undoOperations[0], sbData.undoOperations[1]); + } else { + /// #if !MOBILE + const otherProtyleElement = hasClosestByClassName(oldSourceParentElement, "protyle", true); + if (otherProtyleElement) { + getAllEditor().find(item => { + if (item.protyle.element.isSameNode(otherProtyleElement)) { + const otherSbData = cancelSB(item.protyle, oldSourceParentElement); + doOperations.push(otherSbData.doOperations[0], otherSbData.doOperations[1]); + undoOperations.splice(0, 0, otherSbData.undoOperations[0], otherSbData.undoOperations[1]); + // 需清空操作栈,否则撤销到移动出去的块的操作会抛异常 + item.protyle.undo.clear(); + return true; + } + }) + } + /// #endif + } } else if (!isCopy && oldSourceParentElement && oldSourceParentElement.classList.contains("protyle-wysiwyg") && oldSourceParentElement.innerHTML === "") { /// #if !MOBILE // 拖拽后,根文档原内容为空,且不为悬浮窗 const protyleElement = hasClosestByClassName(oldSourceParentElement, "protyle", true); - if (protyleElement && !protyleElement.classList.contains("block__edit")) { - const editor = getInstanceById(protyleElement.getAttribute("data-id")) as Tab; - if (editor && editor.model instanceof Editor) { - if (editor.model.editor.protyle.block.id === editor.model.editor.protyle.block.rootID) { - const newId = Lute.NewNodeID(); - doOperations.splice(0, 0, { - action: "insert", - id: newId, - data: genEmptyElement(false, false, newId).outerHTML, - parentID: editor.model.editor.protyle.block.parentID - }); - undoOperations.splice(0, 0, { - action: "delete", - id: newId, - }); - } else { - zoomOut({protyle: editor.model.editor.protyle, id: editor.model.editor.protyle.block.rootID}); + if (protyleElement) { + getAllEditor().find(item => { + if (item.protyle.element.isSameNode(protyleElement)) { + if (item.protyle.block.id === item.protyle.block.rootID) { + const newId = Lute.NewNodeID(); + doOperations.splice(0, 0, { + action: "insert", + id: newId, + data: genEmptyElement(false, false, newId).outerHTML, + parentID: item.protyle.block.parentID + }); + undoOperations.splice(0, 0, { + action: "delete", + id: newId, + }); + } else { + zoomOut({protyle: item.protyle, id: item.protyle.block.rootID}); + } + return true; } - } + }); } /// #endif } @@ -676,33 +694,52 @@ const dragSame = async (protyle: IProtyle, sourceElements: Element[], targetElem } if (!isCopy && oldSourceParentElement && oldSourceParentElement.classList.contains("sb") && oldSourceParentElement.childElementCount === 2) { // 拖拽后,sb 只剩下一个元素 - const sbData = cancelSB(protyle, oldSourceParentElement); - doOperations.push(sbData.doOperations[0], sbData.doOperations[1]); - undoOperations.splice(0, 0, sbData.undoOperations[0], sbData.undoOperations[1]); + if (isSameDoc) { + const sbData = cancelSB(protyle, oldSourceParentElement); + doOperations.push(sbData.doOperations[0], sbData.doOperations[1]); + undoOperations.splice(0, 0, sbData.undoOperations[0], sbData.undoOperations[1]); + } else { + /// #if !MOBILE + const otherProtyleElement = hasClosestByClassName(oldSourceParentElement, "protyle", true); + if (otherProtyleElement) { + getAllEditor().find(item => { + if (item.protyle.element.isSameNode(otherProtyleElement)) { + const otherSbData = cancelSB(item.protyle, oldSourceParentElement); + doOperations.push(otherSbData.doOperations[0], otherSbData.doOperations[1]); + undoOperations.splice(0, 0, otherSbData.undoOperations[0], otherSbData.undoOperations[1]); + // 需清空操作栈,否则撤销到移动出去的块的操作会抛异常 + item.protyle.undo.clear(); + return true; + } + }) + } + /// #endif + } } else if (!isCopy && oldSourceParentElement && oldSourceParentElement.classList.contains("protyle-wysiwyg") && oldSourceParentElement.childElementCount === 0) { /// #if !MOBILE - // 拖拽后,根文档原内容为空,且不为悬浮窗 + // 拖拽后,根文档原内容为空 const protyleElement = hasClosestByClassName(oldSourceParentElement, "protyle", true); - if (protyleElement && !protyleElement.classList.contains("block__edit")) { - const editor = getInstanceById(protyleElement.getAttribute("data-id")) as Tab; - if (editor && editor.model instanceof Editor) { - if (editor.model.editor.protyle.block.id === editor.model.editor.protyle.block.rootID) { - const newId = Lute.NewNodeID(); - doOperations.splice(0, 0, { - action: "insert", - id: newId, - data: genEmptyElement(false, false, newId).outerHTML, - parentID: editor.model.editor.protyle.block.parentID - }); - undoOperations.splice(0, 0, { - action: "delete", - id: newId, - }); - } else { - zoomOut({protyle: editor.model.editor.protyle, id: editor.model.editor.protyle.block.rootID}); + if (protyleElement) { + getAllEditor().find(item => { + if (item.protyle.element.isSameNode(protyleElement)) { + if (item.protyle.block.id === item.protyle.block.rootID) { + const newId = Lute.NewNodeID(); + doOperations.splice(0, 0, { + action: "insert", + id: newId, + data: genEmptyElement(false, false, newId).outerHTML, + parentID: item.protyle.block.parentID + }); + undoOperations.splice(0, 0, { + action: "delete", + id: newId, + }); + } else { + zoomOut({protyle: item.protyle, id: item.protyle.block.rootID}); + } + return true; } - - } + }); } /// #endif }