Skip to content

Commit e803d07

Browse files
authored
fix: 修复mac下列表空项tab缩进失效的问题 (#1883)
1 parent 5bb02e3 commit e803d07

4 files changed

Lines changed: 10 additions & 26 deletions

File tree

‎.changeset/social-birds-swim.md‎

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: 修复mac下列表空项tab缩进失效的问题

‎packages/cherry-markdown/src/Cherry.js‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,30 +1086,6 @@ export default class Cherry extends CherryStatic {
10861086
* @param {KeyboardEvent} evt
10871087
*/
10881088
fireShortcutKey(evt) {
1089-
// 获取当前光标位置 - CodeMirror 6 API
1090-
const { view } = this.editor.editor;
1091-
const selection = view.state.selection.main;
1092-
const pos = selection.head;
1093-
const line = view.state.doc.lineAt(pos);
1094-
const lineContent = line.text;
1095-
const cursor = { line: line.number - 1, ch: pos - line.from };
1096-
1097-
// shift + tab 已经被绑定为缩进,所以这里不做处理
1098-
if (!evt.shiftKey && evt.key === 'Tab' && LIST_CONTENT.test(lineContent)) {
1099-
// 每按一次Tab,如果当前光标在行首或者行尾,就在行首加一个\t
1100-
if (cursor.ch === 0 || cursor.ch === lineContent.length || cursor.ch === lineContent.length + 1) {
1101-
evt.preventDefault();
1102-
// 使用 CodeMirror 6 API 替换整行内容
1103-
view.dispatch({
1104-
changes: {
1105-
from: line.from,
1106-
to: line.to,
1107-
insert: `\t${lineContent}`,
1108-
},
1109-
selection: { anchor: line.from + cursor.ch + 1 },
1110-
});
1111-
}
1112-
}
11131089
if (this.toolbar.matchShortcutKey(evt)) {
11141090
// 快捷键
11151091
const needPreventDefault = this.toolbar.fireShortcutKey(evt);

‎packages/cherry-markdown/src/Editor.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import {
4242
selectLine,
4343
} from '@codemirror/commands';
4444
import { closeBrackets, closeBracketsKeymap } from '@codemirror/autocomplete';
45-
import { syntaxHighlighting, defaultHighlightStyle, foldGutter, indentOnInput } from '@codemirror/language';
45+
import { syntaxHighlighting, defaultHighlightStyle, foldGutter, indentOnInput, indentUnit } from '@codemirror/language';
4646
import htmlParser from '@/utils/htmlparser';
4747
import pasteHelper from '@/utils/pasteHelper';
4848
import Logger from '@/Logger';
@@ -2162,6 +2162,8 @@ export default class Editor {
21622162
return false;
21632163
},
21642164
}),
2165+
// 设置缩进单位为2个空格
2166+
indentUnit.of(' '),
21652167
];
21662168

21672169
const state = EditorState.create({

‎packages/cherry-markdown/src/core/hooks/List.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export function makeChecklist(text) {
4141
// 缩进处理
4242
function handleIndent(str, node) {
4343
const indentRegex = /^(\t|[ ])/;
44-
let $str = str;
44+
// mac下按tab键会同时插入空格+\t,这种情况下直接去掉空格
45+
let $str = str.replace(/^[ ]+(\t+)/, '$1');
4546
while (indentRegex.test($str)) {
4647
node.space += $str[0] === '\t' ? TAB_SPACE_NUM : 1;
4748
$str = $str.replace(indentRegex, '');

0 commit comments

Comments
 (0)