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/14938

Vanessa9d11b90d0f42
diff --git a/app/src/index.ts b/app/src/index.ts
index 4d4f0d0372..13a60f896d 100644
--- a/app/src/index.ts
+++ b/app/src/index.ts
@@ -274,7 +274,6 @@ export class App {
altIsPressed: false,
ws: mainWs,
};
-
const notebookPromise = setNoteBook();
fetchPost("/api/system/getConf", {}, async (response) => {
addScriptSync(`${Constants.PROTYLE_CDN}/js/lute/lute.min.js?v=${Constants.SIYUAN_VERSION}`, "protyleLuteScript");
diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts
index e0fa6fdfb4..3bfa89b7c4 100644
--- a/app/src/protyle/toolbar/index.ts
+++ b/app/src/protyle/toolbar/index.ts
@@ -21,6 +21,7 @@ import {transaction, updateBatchTransaction, updateTransaction} from "../wysiwyg
import {Constants} from "../../constants";
import {
copyPlainText,
+ isNotCtrl,
readClipboard,
saveExportFile,
setStorageVal
@@ -1183,6 +1184,10 @@ export class Toolbar {
setPosition(this.subElement, nodeRect.right, bottom);
}
};
+ const closeRender = () => {
+ this.subElement.querySelector('[data-type="pin"]').setAttribute("aria-label", window.siyuan.languages.pin);
+ hideElements(["util"], protyle);
+ };
const headerElement = this.subElement.querySelector(".block__icons");
headerElement.addEventListener("click", (event: MouseEvent) => {
const target = event.target as HTMLElement;
@@ -1205,8 +1210,7 @@ export class Toolbar {
event.stopPropagation();
switch (btnElement.getAttribute("data-type")) {
case "close":
- this.subElement.querySelector('[data-type="pin"]').setAttribute("aria-label", window.siyuan.languages.pin);
- hideElements(["util"], protyle);
+ closeRender();
break;
case "pin":
if (btnElement.getAttribute("aria-label") === window.siyuan.languages.unpin) {
@@ -1272,6 +1276,7 @@ export class Toolbar {
textElement.value = Lute.UnEscapeHTMLStr(renderElement.getAttribute("data-content") || "");
}
const oldTextValue = textElement.value;
+ let focusBeforeRender = false;
textElement.addEventListener("input", (event) => {
if (!renderElement.parentElement) {
return;
@@ -1314,8 +1319,14 @@ export class Toolbar {
return;
}
if (event.key === "Escape" || matchHotKey("⌘↩", event)) {
- this.subElement.querySelector('[data-type="pin"]').setAttribute("aria-label", window.siyuan.languages.pin);
- hideElements(["util"], protyle);
+ closeRender();
+ } else if (types.includes("inline-math") && !event.altKey && !event.shiftKey && isNotCtrl(event) &&
+ textElement.selectionStart === textElement.selectionEnd &&
+ ((event.key === "ArrowLeft" && textElement.selectionStart === 0) ||
+ (event.key === "ArrowRight" && textElement.selectionEnd === textElement.value.length))) {
+ focusBeforeRender = event.key === "ArrowLeft";
+ closeRender();
+ event.preventDefault();
} else if (event.key === "Tab") {
// https://github.com/siyuan-note/siyuan/issues/5270
document.execCommand("insertText", false, "\t");
@@ -1413,7 +1424,11 @@ export class Toolbar {
focusByWbr(nodeElement, this.range);
}
} else if (renderElement.parentElement) {
- this.range.setStartAfter(renderElement);
+ if (focusBeforeRender) {
+ this.range.setStartBefore(renderElement);
+ } else {
+ this.range.setStartAfter(renderElement);
+ }
this.range.collapse(true);
focusByRange(this.range);
}
diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts
index 1e83082c30..4c4513bcd3 100644
--- a/app/src/protyle/wysiwyg/keydown.ts
+++ b/app/src/protyle/wysiwyg/keydown.ts
@@ -143,6 +143,48 @@ const showSelectAllTip = () => {
});
};
+const getAdjacentInlineMath = (range: Range, editableElement: Element, previous: boolean): HTMLElement | undefined => {
+ if (range.startContainer !== editableElement && !editableElement.contains(range.startContainer)) {
+ return;
+ }
+
+ let currentNode: Node | null = range.startContainer;
+ let adjacentNode: Node | false;
+ if (currentNode.nodeType === Node.TEXT_NODE) {
+ const text = currentNode.textContent || "";
+ const adjacentText = previous ? text.substring(0, range.startOffset) : text.substring(range.startOffset);
+ if (adjacentText.split(Constants.ZWSP).join("") !== "") {
+ return;
+ }
+ adjacentNode = previous ? hasPreviousSibling(currentNode) : hasNextSibling(currentNode);
+ } else {
+ adjacentNode = currentNode.childNodes[previous ? range.startOffset - 1 : range.startOffset] || false;
+ }
+
+ while (currentNode) {
+ while (adjacentNode &&
+ ((adjacentNode.nodeType === Node.TEXT_NODE &&
+ (adjacentNode.textContent || "").split(Constants.ZWSP).join("") === "") ||
+ (adjacentNode.nodeType === Node.ELEMENT_NODE && (adjacentNode as Element).tagName === "WBR"))) {
+ adjacentNode = previous ? hasPreviousSibling(adjacentNode) : hasNextSibling(adjacentNode);
+ }
+ if (adjacentNode) {
+ if (adjacentNode.nodeType === Node.ELEMENT_NODE &&
+ (adjacentNode as Element).matches("[data-type~='inline-math']")) {
+ return adjacentNode as HTMLElement;
+ }
+ return;
+ }
+ if (currentNode === editableElement) {
+ return;
+ }
+ currentNode = currentNode.parentNode;
+ if (currentNode) {
+ adjacentNode = previous ? hasPreviousSibling(currentNode) : hasNextSibling(currentNode);
+ }
+ }
+};
+
export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
editorElement.addEventListener("keydown", async (event: KeyboardEvent & { target: HTMLElement }) => {
if (event.target.localName === "protyle-html" || event.target.localName === "input") {
@@ -704,6 +746,18 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
}
}
const nodeEditableElement = (tdElement || getContenteditableElement(nodeElement) || nodeElement) as HTMLElement;
+ // 光标移向相邻行内公式时直接打开编辑框 https://github.com/siyuan-note/siyuan/issues/14938
+ const inlineMathElement = range.collapsed && (event.key === "ArrowLeft" || event.key === "ArrowRight") ?
+ getAdjacentInlineMath(range, nodeEditableElement, event.key === "ArrowLeft") : undefined;
+ if (inlineMathElement) {
+ event.stopPropagation();
+ event.preventDefault();
+ range.selectNode(inlineMathElement);
+ protyle.toolbar.range = range;
+ focusByRange(range);
+ protyle.toolbar.showRender(protyle, inlineMathElement);
+ return;
+ }
const position = getSelectionOffset(nodeEditableElement, protyle.wysiwyg.element, range);
if (nodeElement.classList.contains("code-block") && position.end === nodeEditableElement.innerText.length) {
// 代码块换最后一个 /n 肉眼是无法区分是否在其后的,因此统一在之前