Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: https://github.com/siyuan-note/siyuan/issues/16051

Vanessa599c90e0a8b4
diff --git a/app/src/layout/Wnd.ts b/app/src/layout/Wnd.ts
index 5c37ef7054..52188354a1 100644
--- a/app/src/layout/Wnd.ts
+++ b/app/src/layout/Wnd.ts
@@ -273,11 +273,13 @@ export class Wnd {
return;
}
- const nextTabHeaderElement = (Array.from(it.firstElementChild.childNodes).find((item: HTMLElement) => {
+ let nextTabHeaderElement: HTMLElement;
+ Array.from(it.firstElementChild.childNodes).find((item: HTMLElement) => {
if (item.style?.opacity === "0.38") {
+ nextTabHeaderElement = item.nextElementSibling as HTMLElement;
return true;
}
- }) as HTMLElement)?.nextElementSibling;
+ });
if (!it.contains(oldTab.headElement)) {
// 从其他 Wnd 拖动过来
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index d3b34762ba..72eb69f406 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -120,7 +120,8 @@ export class Gutter {
rowElement.querySelector(".av__firstcol use").setAttribute("xlink:href", "#iconCheck");
updateHeader(rowElement as HTMLElement);
avElement.querySelectorAll(".av__row--select:not(.av__row--header)").forEach(item => {
- const groupId = (hasClosestByClassName(item, "av__body") as HTMLElement)?.dataset.groupId || "";
+ const avBodyElement = hasClosestByClassName(item, "av__body") as HTMLElement;
+ const groupId = (avBodyElement ? avBodyElement.dataset.groupId : "") || "";
selectIds.push(item.getAttribute("data-id") + (groupId ? "@" + groupId : ""));
selectElements.push(item);
});
diff --git a/app/src/protyle/wysiwyg/enter.ts b/app/src/protyle/wysiwyg/enter.ts
index 39fc09fb6a..fb619b325e 100644
--- a/app/src/protyle/wysiwyg/enter.ts
+++ b/app/src/protyle/wysiwyg/enter.ts
@@ -118,13 +118,13 @@ export const enter = (blockElement: HTMLElement, range: Range, protyle: IProtyle
// bq || callout
const isCallout = blockElement.parentElement.classList.contains("callout-content");
+ const parentBlockElement = isCallout ? blockElement.parentElement.parentElement : blockElement.parentElement;
if (editableElement.textContent.replace(Constants.ZWSP, "").replace("\n", "") === "" &&
((blockElement.nextElementSibling && blockElement.nextElementSibling.classList.contains("protyle-attr") &&
blockElement.parentElement.getAttribute("data-type") === "NodeBlockquote") ||
(isCallout && !blockElement.nextElementSibling))) {
range.insertNode(document.createElement("wbr"));
const topElement = getTopEmptyElement(blockElement);
- const parentElement = isCallout ? blockElement.parentElement.parentElement : blockElement.parentElement;
const blockId = blockElement.getAttribute("data-node-id");
const topId = topElement.getAttribute("data-node-id");
const doInsert: IOperation = {
@@ -138,9 +138,9 @@ export const enter = (blockElement: HTMLElement, range: Range, protyle: IProtyle
data: topElement.outerHTML,
};
if (topId === blockId) {
- doInsert.previousID = parentElement.getAttribute("data-node-id");
+ doInsert.previousID = parentBlockElement.getAttribute("data-node-id");
undoInsert.previousID = blockElement.previousElementSibling.getAttribute("data-node-id");
- parentElement.after(blockElement);
+ parentBlockElement.after(blockElement);
} else {
doInsert.previousID = topElement.previousElementSibling ? topElement.previousElementSibling.getAttribute("data-node-id") : undefined;
doInsert.parentID = topElement.parentElement.getAttribute("data-node-id") || protyle.block.parentID;
@@ -156,8 +156,8 @@ export const enter = (blockElement: HTMLElement, range: Range, protyle: IProtyle
action: "delete",
id: blockId,
}, undoInsert]);
- if (topId === blockId && parentElement.classList.contains("sb") &&
- parentElement.getAttribute("data-sb-layout") === "col") {
+ if (topId === blockId && parentBlockElement.classList.contains("sb") &&
+ parentBlockElement.getAttribute("data-sb-layout") === "col") {
turnsIntoOneTransaction({
protyle,
selectsElement: [blockElement.previousElementSibling, blockElement],
@@ -197,7 +197,7 @@ export const enter = (blockElement: HTMLElement, range: Range, protyle: IProtyle
data: newElement.outerHTML,
id: newId,
previousID: blockElement.previousElementSibling ? blockElement.previousElementSibling.getAttribute("data-node-id") : "",
- parentID: (hasClosestBlock(blockElement.parentElement) as HTMLElement)?.getAttribute("data-node-id") || protyle.block.parentID
+ parentID: parentBlockElement.getAttribute("data-node-id") || protyle.block.parentID
}], [{
action: "delete",
id: newId,
@@ -209,7 +209,7 @@ export const enter = (blockElement: HTMLElement, range: Range, protyle: IProtyle
}
range.insertNode(document.createElement("wbr"));
const html = blockElement.outerHTML;
- const parentHTML = (hasClosestBlock(blockElement.parentElement) as HTMLElement)?.outerHTML;
+ const parentHTML = parentBlockElement.outerHTML;
if (range.toString() !== "") {
// 选中数学公式后回车取消选中 https://github.com/siyuan-note/siyuan/issues/12637#issuecomment-2381106949
const mathElement = hasClosestByAttribute(range.startContainer, "data-type", "inline-math");
@@ -347,8 +347,7 @@ export const enter = (blockElement: HTMLElement, range: Range, protyle: IProtyle
}
}
transaction(protyle, doOperation, undoOperation);
- const parentElement = currentElement.parentElement;
- if (parentElement.classList.contains("sb") && parentElement.getAttribute("data-sb-layout") === "col") {
+ if (currentElement.parentElement.classList.contains("sb") && currentElement.parentElement.getAttribute("data-sb-layout") === "col") {
turnsIntoOneTransaction({
protyle,
selectsElement,
diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts
index daef3a6cd4..068fee64d3 100644
--- a/app/src/protyle/wysiwyg/keydown.ts
+++ b/app/src/protyle/wysiwyg/keydown.ts
@@ -358,7 +358,10 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
}
} else if (!selectElements[0].parentElement.classList.contains("protyle-wysiwyg")) {
hideElements(["select"], protyle);
- (hasClosestBlock(selectElements[0].parentElement) as HTMLElement)?.classList.add("protyle-wysiwyg--select");
+ const parentBlockElement = hasClosestBlock(selectElements[0].parentElement);
+ if (parentBlockElement) {
+ parentBlockElement.classList.add("protyle-wysiwyg--select");
+ }
}
}
});
@@ -384,7 +387,10 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
}
} else if (!selectLastElement.parentElement.classList.contains("protyle-wysiwyg")) {
hideElements(["select"], protyle);
- (hasClosestBlock(selectLastElement.parentElement) as HTMLElement)?.classList.add("protyle-wysiwyg--select");
+ const parentBlockElement = hasClosestBlock(selectLastElement.parentElement);
+ if (parentBlockElement) {
+ parentBlockElement.classList.add("protyle-wysiwyg--select");
+ }
}
}
});
@@ -409,7 +415,10 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
}
} else if (!startEndElement.endElement.parentElement.classList.contains("protyle-wysiwyg")) {
hideElements(["select"], protyle);
- (hasClosestBlock(startEndElement.endElement.parentElement) as HTMLElement)?.classList.add("protyle-wysiwyg--select");
+ const parentBlockElement = hasClosestBlock(startEndElement.endElement.parentElement);
+ if (parentBlockElement) {
+ parentBlockElement.classList.add("protyle-wysiwyg--select");
+ }
}
} else {
startEndElement.endElement.classList.remove("protyle-wysiwyg--select");
@@ -443,7 +452,10 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
if (nextElement.getBoundingClientRect().width === 0) {
// https://github.com/siyuan-note/siyuan/issues/11194
hideElements(["select"], protyle);
- (hasClosestBlock(startEndElement.endElement.parentElement) as HTMLElement)?.classList.add("protyle-wysiwyg--select");
+ const parentBlockElement = hasClosestBlock(startEndElement.endElement.parentElement);
+ if (parentBlockElement) {
+ parentBlockElement.classList.add("protyle-wysiwyg--select");
+ }
} else {
nextElement.classList.add("protyle-wysiwyg--select");
nextElement.setAttribute("select-end", "true");
@@ -456,7 +468,10 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
}
} else if (!startEndElement.endElement.parentElement.classList.contains("protyle-wysiwyg")) {
hideElements(["select"], protyle);
- (hasClosestBlock(startEndElement.endElement.parentElement) as HTMLElement)?.classList.add("protyle-wysiwyg--select");
+ const parentBlockElement = hasClosestBlock(startEndElement.endElement.parentElement);
+ if (parentBlockElement) {
+ parentBlockElement.classList.add("protyle-wysiwyg--select");
+ }
}
} else {
startEndElement.endElement.classList.remove("protyle-wysiwyg--select");
diff --git a/app/src/protyle/wysiwyg/remove.ts b/app/src/protyle/wysiwyg/remove.ts
index 22c4d72662..b9c18f4802 100644
--- a/app/src/protyle/wysiwyg/remove.ts
+++ b/app/src/protyle/wysiwyg/remove.ts
@@ -126,12 +126,13 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang
}
previousID = unfoldData[foldId].previousID;
}
+ const parentElement = hasClosestBlock(topElement.parentElement);
inserts.push({
action: "insert",
data,
id,
previousID,
- parentID: (hasClosestBlock(topElement.parentElement) as HTMLElement)?.getAttribute("data-node-id") || protyle.block.parentID
+ parentID: (parentElement ? parentElement.getAttribute("data-node-id") : null) || protyle.block.parentID
});
if (topElement.getAttribute("data-subtype") === "o" && topElement.classList.contains("li")) {
listElement = topElement.parentElement;