Skip to content

Commit

Permalink
fix: 通过工具栏插入内容时,默认选中被插入的内容 #206
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsonliu committed May 10, 2022
1 parent 20d6ddf commit 678f695
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/core/hooks/CodeBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]+?)',
};
Expand All @@ -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 = `<pre data-sign="${sign}" data-lines="${lineCount}"><code>${escapeHTMLSpecialChar(
code.replace(/\n( {4}|\t)/g, '\n'),
Expand Down
6 changes: 3 additions & 3 deletions src/toolbars/MenuBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand All @@ -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');
}

// 反转子菜单点击事件参数顺序
Expand Down Expand Up @@ -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();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/toolbars/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
});
Expand Down
8 changes: 7 additions & 1 deletion src/toolbars/hooks/Color.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}!!!`;
}

Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/toolbars/hooks/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 678f695

Please sign in to comment.