Skip to content

Commit e3b9e70

Browse files
committed
🎨 #14496
1 parent f8babd3 commit e3b9e70

File tree

5 files changed

+38
-26
lines changed

5 files changed

+38
-26
lines changed

app/src/block/util.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import {scrollCenter} from "../util/highlightById";
77
import {Constants} from "../constants";
88
import {hideElements} from "../protyle/ui/hideElements";
99
import {blockRender} from "../protyle/render/blockRender";
10-
import {fetchPost} from "../util/fetch";
10+
import {fetchPost, fetchSyncPost} from "../util/fetch";
1111
import {openFileById} from "../editor/util";
1212
import {openMobileFileById} from "../mobile/editor";
1313

14-
export const cancelSB = (protyle: IProtyle, nodeElement: Element) => {
14+
export const cancelSB = async (protyle: IProtyle, nodeElement: Element) => {
1515
const doOperations: IOperation[] = [];
1616
const undoOperations: IOperation[] = [];
1717
let previousId = nodeElement.previousElementSibling ? nodeElement.previousElementSibling.getAttribute("data-node-id") : undefined;
@@ -21,12 +21,23 @@ export const cancelSB = (protyle: IProtyle, nodeElement: Element) => {
2121
const id = nodeElement.getAttribute("data-node-id");
2222
const sbElement = nodeElement.cloneNode() as HTMLElement;
2323
sbElement.innerHTML = nodeElement.lastElementChild.outerHTML;
24+
let parentID = nodeElement.parentElement.getAttribute("data-node-id");
25+
// 缩放和反链需要接口获取
26+
if (!previousId && !parentID) {
27+
if (protyle.block.showAll || protyle.options.backlinkData) {
28+
const idData = await fetchSyncPost("/api/block/getBlockSiblingID", {id});
29+
previousId = idData.data.previous;
30+
parentID = idData.data.parent;
31+
} else {
32+
parentID = protyle.block.rootID;
33+
}
34+
}
2435
undoOperations.push({
2536
action: "insert",
2637
id,
2738
data: sbElement.outerHTML,
2839
previousID: nodeElement.previousElementSibling ? nodeElement.previousElementSibling.getAttribute("data-node-id") : undefined,
29-
parentID: nodeElement.parentElement.getAttribute("data-node-id") || protyle.block.parentID
40+
parentID,
3041
});
3142
Array.from(nodeElement.children).forEach((item, index) => {
3243
if (index === nodeElement.childElementCount - 1) {
@@ -46,7 +57,7 @@ export const cancelSB = (protyle: IProtyle, nodeElement: Element) => {
4657
action: "move",
4758
id: item.getAttribute("data-node-id"),
4859
previousID: previousId,
49-
parentID: nodeElement.parentElement.getAttribute("data-node-id") || protyle.block.parentID
60+
parentID,
5061
});
5162
undoOperations.push({
5263
action: "move",

app/src/protyle/gutter/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,8 +1365,8 @@ export class Gutter {
13651365
id: "cancelSuperBlock",
13661366
label: window.siyuan.languages.cancel + " " + window.siyuan.languages.superBlock,
13671367
accelerator: window.siyuan.config.keymap.editor.general[isCol ? "hLayout" : "vLayout"].custom,
1368-
click() {
1369-
const sbData = cancelSB(protyle, nodeElement);
1368+
async click() {
1369+
const sbData = await cancelSB(protyle, nodeElement);
13701370
transaction(protyle, sbData.doOperations, sbData.undoOperations);
13711371
focusBlock(protyle.wysiwyg.element.querySelector(`[data-node-id="${sbData.previousId}"]`));
13721372
hideElements(["gutter"], protyle);

app/src/protyle/util/editorCommonEvent.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -528,23 +528,24 @@ const dragSb = async (protyle: IProtyle, sourceElements: Element[], targetElemen
528528
if (!isCopy && oldSourceParentElement && oldSourceParentElement.classList.contains("sb") && oldSourceParentElement.childElementCount === 2) {
529529
// 拖拽后,sb 只剩下一个元素
530530
if (isSameDoc) {
531-
const sbData = cancelSB(protyle, oldSourceParentElement);
531+
const sbData = await cancelSB(protyle, oldSourceParentElement);
532532
doOperations.push(sbData.doOperations[0], sbData.doOperations[1]);
533533
undoOperations.splice(0, 0, sbData.undoOperations[0], sbData.undoOperations[1]);
534534
} else {
535535
/// #if !MOBILE
536536
const otherProtyleElement = hasClosestByClassName(oldSourceParentElement, "protyle", true);
537537
if (otherProtyleElement) {
538-
getAllEditor().find(item => {
539-
if (item.protyle.element.isSameNode(otherProtyleElement)) {
540-
const otherSbData = cancelSB(item.protyle, oldSourceParentElement);
538+
const allEditor = getAllEditor()
539+
for (let i = 0; i < allEditor.length; i++) {
540+
if (allEditor[i].protyle.element.isSameNode(otherProtyleElement)) {
541+
const otherSbData = await cancelSB(allEditor[i].protyle, oldSourceParentElement);
541542
doOperations.push(otherSbData.doOperations[0], otherSbData.doOperations[1]);
542543
undoOperations.splice(0, 0, otherSbData.undoOperations[0], otherSbData.undoOperations[1]);
543544
// 需清空操作栈,否则撤销到移动出去的块的操作会抛异常
544-
item.protyle.undo.clear();
545+
allEditor[i].protyle.undo.clear();
545546
return true;
546547
}
547-
});
548+
}
548549
}
549550
/// #endif
550551
}
@@ -730,23 +731,23 @@ const dragSame = async (protyle: IProtyle, sourceElements: Element[], targetElem
730731
if (!isCopy && oldSourceParentElement && oldSourceParentElement.classList.contains("sb") && oldSourceParentElement.childElementCount === 2) {
731732
// 拖拽后,sb 只剩下一个元素
732733
if (isSameDoc) {
733-
const sbData = cancelSB(protyle, oldSourceParentElement);
734+
const sbData = await cancelSB(protyle, oldSourceParentElement);
734735
doOperations.push(sbData.doOperations[0], sbData.doOperations[1]);
735736
undoOperations.splice(0, 0, sbData.undoOperations[0], sbData.undoOperations[1]);
736737
} else {
737738
/// #if !MOBILE
738739
const otherProtyleElement = hasClosestByClassName(oldSourceParentElement, "protyle", true);
739740
if (otherProtyleElement) {
740-
getAllEditor().find(item => {
741-
if (item.protyle.element.isSameNode(otherProtyleElement)) {
742-
const otherSbData = cancelSB(item.protyle, oldSourceParentElement);
741+
const allEditor = getAllEditor()
742+
for (let i = 0; i < allEditor.length; i++) {
743+
if (allEditor[i].protyle.element.isSameNode(otherProtyleElement)) {
744+
const otherSbData = await cancelSB(allEditor[i].protyle, oldSourceParentElement);
743745
doOperations.push(otherSbData.doOperations[0], otherSbData.doOperations[1]);
744746
undoOperations.splice(0, 0, otherSbData.undoOperations[0], otherSbData.undoOperations[1]);
745747
// 需清空操作栈,否则撤销到移动出去的块的操作会抛异常
746-
item.protyle.undo.clear();
747-
return true;
748+
allEditor[i].protyle.undo.clear();
748749
}
749-
});
750+
}
750751
}
751752
/// #endif
752753
}

app/src/protyle/wysiwyg/keydown.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export const getContentByInlineHTML = (range: Range, cb: (content: string) => vo
8686
};
8787

8888
export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
89-
editorElement.addEventListener("keydown", (event: KeyboardEvent & { target: HTMLElement }) => {
89+
editorElement.addEventListener("keydown", async (event: KeyboardEvent & { target: HTMLElement }) => {
9090
if (event.target.localName === "protyle-html" || event.target.localName === "input") {
9191
event.stopPropagation();
9292
return;
@@ -1665,7 +1665,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
16651665
updateTransaction(protyle, selectsElement[0].getAttribute("data-node-id"), selectsElement[0].outerHTML, oldHTML);
16661666
} else {
16671667
range.insertNode(document.createElement("wbr"));
1668-
const sbData = cancelSB(protyle, selectsElement[0]);
1668+
const sbData = await cancelSB(protyle, selectsElement[0]);
16691669
transaction(protyle, sbData.doOperations, sbData.undoOperations);
16701670
focusByWbr(protyle.wysiwyg.element, range);
16711671
}
@@ -1694,7 +1694,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
16941694
updateTransaction(protyle, selectsElement[0].getAttribute("data-node-id"), selectsElement[0].outerHTML, oldHTML);
16951695
} else {
16961696
range.insertNode(document.createElement("wbr"));
1697-
const sbData = cancelSB(protyle, selectsElement[0]);
1697+
const sbData = await cancelSB(protyle, selectsElement[0]);
16981698
transaction(protyle, sbData.doOperations, sbData.undoOperations);
16991699
focusByWbr(protyle.wysiwyg.element, range);
17001700
}

app/src/protyle/wysiwyg/remove.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {scrollCenter} from "../../util/highlightById";
2020
import {isMobile} from "../../util/functions";
2121
import {mathRender} from "../render/mathRender";
2222

23-
export const removeBlock = (protyle: IProtyle, blockElement: Element, range: Range, type: "Delete" | "Backspace" | "remove") => {
23+
export const removeBlock = async (protyle: IProtyle, blockElement: Element, range: Range, type: "Delete" | "Backspace" | "remove") => {
2424
// 删除后,防止滚动条滚动后调用 get 请求,因为返回的请求已查找不到内容块了
2525
preventScroll(protyle);
2626
const selectElements = Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select"));
@@ -181,7 +181,7 @@ export const removeBlock = (protyle: IProtyle, blockElement: Element, range: Ran
181181
}
182182
if (deletes.length > 0) {
183183
if (topParentElement && topParentElement.getAttribute("data-type") === "NodeSuperBlock" && topParentElement.childElementCount === 2) {
184-
const sbData = cancelSB(protyle, topParentElement);
184+
const sbData = await cancelSB(protyle, topParentElement);
185185
transaction(protyle, deletes.concat(sbData.doOperations), sbData.undoOperations.concat(inserts.reverse()));
186186
} else {
187187
transaction(protyle, deletes, inserts.reverse());
@@ -351,7 +351,7 @@ export const removeBlock = (protyle: IProtyle, blockElement: Element, range: Ran
351351
blockElement.remove();
352352
// 取消超级块
353353
if (parentElement.getAttribute("data-type") === "NodeSuperBlock" && parentElement.childElementCount === 2) {
354-
const sbData = cancelSB(protyle, parentElement);
354+
const sbData = await cancelSB(protyle, parentElement);
355355
transaction(protyle, doOperations.concat(sbData.doOperations), sbData.undoOperations.concat(undoOperations));
356356
} else {
357357
transaction(protyle, doOperations, undoOperations);
@@ -428,7 +428,7 @@ export const removeBlock = (protyle: IProtyle, blockElement: Element, range: Ran
428428
});
429429
}
430430
if (parentElement.getAttribute("data-type") === "NodeSuperBlock" && parentElement.childElementCount === 2) {
431-
const sbData = cancelSB(protyle, parentElement);
431+
const sbData = await cancelSB(protyle, parentElement);
432432
transaction(protyle, doOperations.concat(sbData.doOperations), sbData.undoOperations.concat(undoOperations));
433433
} else {
434434
transaction(protyle, doOperations, undoOperations);

0 commit comments

Comments
 (0)