Skip to content

Commit 33107ee

Browse files
authored
fix: #1493 联想词特定场景下吞掉触发符号后的文本 (#1494)
1 parent e6846cc commit 33107ee

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cherry-markdown": patch
3+
---
4+
5+
fix: #1493 联想词特定场景下吞掉触发符号后的文本

packages/cherry-markdown/src/core/hooks/Suggester.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -610,14 +610,21 @@ class SuggesterPanel {
610610
* @param {KeyboardEvent} evt 键盘事件
611611
*/
612612
pasteSelectResult(idx, evt) {
613-
if (!this.cursorTo || this.cursorTo === this.cursorFrom) {
614-
this.cursorTo = JSON.parse(JSON.stringify(this.cursorFrom));
615-
}
616-
if (!this.cursorTo) {
613+
const { cursorFrom } = this;
614+
if (!cursorFrom) {
617615
return;
618616
}
619-
this.cursorTo.ch += 1;
620-
const { cursorFrom, cursorTo } = this; // 缓存光标位置
617+
let cursorTo;
618+
// 仅替换当前联想触发期间录入的字符,避免吞掉光标后的文本
619+
// Reference: issue #1493 https://github.com/Tencent/cherry-markdown/issues/1493
620+
const typedLength = Array.isArray(this.searchKeyCache) ? this.searchKeyCache.join('').length : 0;
621+
if (typedLength > 0) {
622+
cursorTo = { line: cursorFrom.line, ch: cursorFrom.ch + typedLength };
623+
} else if (this.cursorTo) {
624+
cursorTo = { line: this.cursorTo.line, ch: this.cursorTo.ch };
625+
} else {
626+
cursorTo = { line: cursorFrom.line, ch: cursorFrom.ch };
627+
}
621628
if (this.optionList[idx]) {
622629
let result = '';
623630
if (
@@ -635,6 +642,13 @@ class SuggesterPanel {
635642
} else {
636643
result = ` ${this.keyword}${this.optionList[idx]} `;
637644
}
645+
// 如果回填内容以空格结尾,同时光标位置后还有空格,则一并替换掉一个空格,避免残留双空格
646+
if (result.endsWith(' ') && this.editor?.editor?.getLine && cursorTo && cursorTo.ch !== null) {
647+
const lineText = this.editor.editor.getLine(cursorTo.line) || '';
648+
if (lineText[cursorTo.ch] === ' ') {
649+
cursorTo = { line: cursorTo.line, ch: cursorTo.ch + 1 };
650+
}
651+
}
638652
// this.cursorTo.ch = this.cursorFrom.ch + result.length;
639653
if (result) {
640654
this.editor.editor.replaceRange(result, cursorFrom, cursorTo);

0 commit comments

Comments
 (0)