diff --git a/src/core/hooks/CodeBlock.js b/src/core/hooks/CodeBlock.js index 0932a7322..b2c837dd1 100644 --- a/src/core/hooks/CodeBlock.js +++ b/src/core/hooks/CodeBlock.js @@ -201,10 +201,11 @@ export default class CodeBlock extends ParagraphBase { /** * 获取缩进代码块语法的正则 + * 缩进代码块必须要以连续两个以上的换行符开头 */ $getIndentedCodeReg() { const ret = { - begin: '(?:^|\\n)(?: {4}|\\t)', + begin: '(?:^|\\n\\s*\\n)(?: {4}|\\t)', end: '(?=$|\\n( {0,3}[^ \\t\\n]|\\n[^ \\t\\n]))', content: '([\\s\\S]+?)', }; @@ -219,7 +220,7 @@ export default class CodeBlock extends ParagraphBase { return str; } return this.$recoverCodeInIndent(str).replace(this.$getIndentedCodeReg(), (match, code) => { - const lineCount = (match.match(/\n/g) || []).length; + const lineCount = (match.match(/\n/g) || []).length - 1; const sign = this.$engine.md5(match); const html = `
${escapeHTMLSpecialChar(
         code.replace(/\n( {4}|\t)/g, '\n'),
diff --git a/src/toolbars/MenuBase.js b/src/toolbars/MenuBase.js
index f668de261..26a5d8c42 100644
--- a/src/toolbars/MenuBase.js
+++ b/src/toolbars/MenuBase.js
@@ -217,7 +217,7 @@ export default class MenuBase {
       const ret = selections.map((selection, index, srcArray) => this.onClick(selection, '', event) || srcArray[index]);
       if (!this.bubbleMenu && this.updateMarkdown) {
         // 非下拉菜单按钮保留selection
-        this.editor.editor.replaceSelections(ret);
+        this.editor.editor.replaceSelections(ret, 'around');
         this.editor.editor.focus();
       }
     }
@@ -232,7 +232,7 @@ export default class MenuBase {
    */
   onKeyDown(codemirror, selections, key) {
     const ret = selections.map((selection) => this.onClick(selection, key));
-    return codemirror.replaceSelections(ret);
+    return codemirror.replaceSelections(ret, 'around');
   }
 
   // 反转子菜单点击事件参数顺序
@@ -324,7 +324,7 @@ export default class MenuBase {
       // 当onClick返回null或undefined时,维持原样
       const ret = selections.map((selection, index, srcArray) => clickEventHandler(selection) || srcArray[index]);
       if (this.updateMarkdown) {
-        this.editor.editor.replaceSelections(ret);
+        this.editor.editor.replaceSelections(ret, 'around');
         this.editor.editor.focus();
       }
     }
diff --git a/src/toolbars/Toolbar.js b/src/toolbars/Toolbar.js
index 1f81c129e..ce76666e0 100644
--- a/src/toolbars/Toolbar.js
+++ b/src/toolbars/Toolbar.js
@@ -109,7 +109,7 @@ export default class Toolbar {
         const ret = selections.map(
           (selection, index, srcArray) => ext.onClick(selection, shortKey, callback) || srcArray[index],
         );
-        this.options.editor.editor.replaceSelections(ret);
+        this.options.editor.editor.replaceSelections(ret, 'around');
         this.options.editor.editor.focus();
       };
     });
diff --git a/src/toolbars/hooks/Color.js b/src/toolbars/hooks/Color.js
index b6da68717..1aa990adf 100644
--- a/src/toolbars/hooks/Color.js
+++ b/src/toolbars/hooks/Color.js
@@ -186,8 +186,14 @@ class BubbleColor {
 
   onClick() {
     if (this.type === 'text') {
+      if (/^!!#\S+ [\s\S]+?!!/.test(this.selection)) {
+        return this.selection.replace(/^!!#\S+ ([\s\S]+?)!!/, `!!${this.colorValue} $1!!`);
+      }
       return `!!${this.colorValue} ${this.selection}!!`;
     }
+    if (/^!!!#\S+ [\s\S]+?!!!/.test(this.selection)) {
+      return this.selection.replace(/^!!!#\S+ ([\s\S]+?)!!!/, `!!!${this.colorValue} $1!!!`);
+    }
     return `!!!${this.colorValue} ${this.selection}!!!`;
   }
 
@@ -204,7 +210,7 @@ class BubbleColor {
         this.type = target.closest('.cherry-color-text') ? 'text' : 'bg';
         const selections = this.editor.editor.getSelections();
         const res = selections.map((selection, index, srcArray) => this.onClick() || srcArray[index]);
-        self.editor.editor.replaceSelections(res);
+        self.editor.editor.replaceSelections(res, 'around');
         self.editor.editor.focus();
       },
       false,
diff --git a/src/toolbars/hooks/List.js b/src/toolbars/hooks/List.js
index 099730032..e36f1e002 100644
--- a/src/toolbars/hooks/List.js
+++ b/src/toolbars/hooks/List.js
@@ -53,6 +53,7 @@ export default class List extends MenuBase {
         pre = '- [x]';
         break;
     }
+    $selection = $selection.replace(/^(\s*)([0-9a-zA-Z]+\.|- \[x\]|- \[ \]|-) /gm, '$1');
     $selection = $selection.replace(/^(\s*)(\S[\s\S]*?)$/gm, `$1${pre} $2`);
     return $selection;
   }