Skip to content

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

:bug: Restore the caret after undoing emoji insertion https://github.com/siyuan-note/siyuan/issues/18929

Danielb4802c3cb2f0
diff --git a/app/src/protyle/hint/blockHintRange.test.ts b/app/src/protyle/hint/blockHintRange.test.ts
index a48669bc2d..3d3901a4b4 100644
--- a/app/src/protyle/hint/blockHintRange.test.ts
+++ b/app/src/protyle/hint/blockHintRange.test.ts
@@ -99,6 +99,11 @@ describe("shouldCaptureHintUndoFocus", () => {
assert.equal(shouldCaptureHintUndoFocus("、", blockHintKeys, true), true);
});
+ it("captures slash emoji focus in regular editors", () => {
+ assert.equal(shouldCaptureHintUndoFocus("/", blockHintKeys, false, "emoji"), true);
+ assert.equal(shouldCaptureHintUndoFocus("、", blockHintKeys, false, "emoji"), true);
+ });
+
it("does not change regular editor slash hint focus handling", () => {
assert.equal(shouldCaptureHintUndoFocus("/", blockHintKeys, false), false);
assert.equal(shouldCaptureHintUndoFocus("#", blockHintKeys, true), false);
diff --git a/app/src/protyle/hint/blockHintRange.ts b/app/src/protyle/hint/blockHintRange.ts
index 6f4903a882..85dbbe4522 100644
--- a/app/src/protyle/hint/blockHintRange.ts
+++ b/app/src/protyle/hint/blockHintRange.ts
@@ -19,8 +19,9 @@ export const shouldIgnoreHintTrigger = (activeHint: string, candidateHint: strin
return activeHint === "#" && ["/", "、"].includes(candidateHint);
};
-export const shouldCaptureHintUndoFocus = (splitChar: string, blockHintKeys: string[], lite: boolean) => {
- return blockHintKeys.includes(splitChar) || (lite && ["/", "、"].includes(splitChar));
+export const shouldCaptureHintUndoFocus = (splitChar: string, blockHintKeys: string[], lite: boolean,
+ value = "") => {
+ return value === "emoji" || blockHintKeys.includes(splitChar) || (lite && ["/", "、"].includes(splitChar));
};
export const endsWithMultiCharHintPrefix = (key: string, hintKeys: string[]) => {
diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.ts
index 5083ce7467..9c4328cc2f 100644
--- a/app/src/protyle/hint/index.ts
+++ b/app/src/protyle/hint/index.ts
@@ -583,7 +583,7 @@ ${genHintItemHTML(item)}
}
}
- private openEmojiInsertPanel(protyle: IProtyle, range: Range) {
+ private openEmojiInsertPanel(protyle: IProtyle, range: Range, undoContext?: Record<string, string>) {
const targetElement = hasClosestBlock(range.startContainer);
const targetID = targetElement ? targetElement.getAttribute("data-node-id") : protyle.block.rootID;
const textareaPosition = getSelectionPosition(protyle.wysiwyg.element);
@@ -598,7 +598,8 @@ ${genHintItemHTML(item)}
return;
}
focusByRange(protyle.toolbar.range);
- insertHTML(protyle.lute.SpinBlockDOM(genEmojiInsertHTML(unicode)), protyle, false, true);
+ insertHTML(protyle.lute.SpinBlockDOM(genEmojiInsertHTML(unicode)), protyle, false, true,
+ false, undefined, undoContext);
}, undefined, {targetID});
}
@@ -704,7 +705,7 @@ ${genHintItemHTML(item)}
id = nodeElement.getAttribute("data-node-id");
}
const html = nodeElement.outerHTML;
- const undoContext = shouldCaptureHintUndoFocus(this.splitChar, Constants.BLOCK_HINT_KEYS, protyle.lite) ?
+ const undoContext = shouldCaptureHintUndoFocus(this.splitChar, Constants.BLOCK_HINT_KEYS, protyle.lite, value) ?
getUndoFocusContext(protyle.wysiwyg.element, range, true) : undefined;
// 自顶向下法新建文档后光标定位问题 https://github.com/siyuan-note/siyuan/issues/299
if (this.lastIndex > -1) {
@@ -881,7 +882,7 @@ ${genHintItemHTML(item)}
return;
} else if (value === "emoji") {
// 保留斜杠命令选区,选择表情时由 insertHTML 作为一个事务替换,确保撤销恢复命令文本
- this.openEmojiInsertPanel(protyle, range);
+ this.openEmojiInsertPanel(protyle, range, undoContext);
return;
} else if (value.startsWith("style")) {
range.deleteContents();