From e196e2d4030290b4d7b690904edeaae31a1c7889 Mon Sep 17 00:00:00 2001 From: huzhi1351 Date: Sun, 12 Jan 2020 17:03:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=80=89=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E5=AD=97=E5=90=8E=E7=82=B9=E5=87=BB=E5=B7=A5=E5=85=B7=E6=A0=8F?= =?UTF-8?q?=E4=B8=8A=E7=9A=84=E6=8C=89=E9=92=AE=E5=90=8E=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E5=85=89=E6=A0=87=E5=AE=9A=E4=BD=8D=E4=B8=8D=E5=87=86=E7=A1=AE?= =?UTF-8?q?=E7=9A=84bug=20Signed-off-by:=20huzhi1351=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/simple/simple.js | 67 +++++++++++---------------------- 1 file changed, 23 insertions(+), 44 deletions(-) diff --git a/src/components/simple/simple.js b/src/components/simple/simple.js index 7ee9af7..50190ad 100644 --- a/src/components/simple/simple.js +++ b/src/components/simple/simple.js @@ -1,4 +1,4 @@ -import marked from '../../config/marked'; +import marked from '../../config/marked'; import common from '../../mixins/common'; export default { @@ -43,9 +43,9 @@ export default { }) }, 20); }, - insertContent(initStr) { // 插入文本 + let cursorSwitch = 0; // 0: 默认值, 适配原逻辑; 1: 增加了换行符; this.lastInsert = initStr; const point = this.getCursortPosition(); const lastChart = this.currentValue.substring(point - 1, point); @@ -59,10 +59,12 @@ export default { lastFourCharts !== ' ' ) { const str = '\n' + initStr; + cursorSwitch = 1; this.insertAfterText(str); } else { this.insertAfterText(initStr); } + return cursorSwitch; }, getCursortPosition() { // 获取光标位置 @@ -125,6 +127,13 @@ export default { range.select(); } }, + setCaretBySwitch(cursorSwitch, point, offset) { + if (cursorSwitch === 1) { + this.setCaretPosition(point + offset + 1); + } else if (!cursorSwitch) { + this.setCaretPosition(point + offset); + } + }, insertQuote() { // 引用 this.insertContent('\n> '); @@ -148,68 +157,38 @@ export default { insertCode() { // 插入code const point = this.getCursortPosition(); - const lastChart = this.currentValue.substring(point - 1, point); - this.insertContent('\n```\n\n```'); - if (lastChart !== '\n' && this.currentValue !== '') { - this.setCaretPosition(point + 5); - } else { - this.setCaretPosition(point + 5); - } + const cursorSwitch = this.insertContent('\n```\n\n```'); + this.setCaretBySwitch(cursorSwitch, point, 5); }, insertStrong() { // 粗体 const point = this.getCursortPosition(); - const lastChart = this.currentValue.substring(point - 1, point); - this.insertContent('****'); - if (lastChart !== '\n' && this.currentValue !== '') { - this.setCaretPosition(point + 2); - } else { - this.setCaretPosition(point + 2); - } + const cursorSwitch = this.insertContent('****'); + this.setCaretBySwitch(cursorSwitch, point, 2); }, insertItalic() { // 斜体 const point = this.getCursortPosition(); - const lastChart = this.currentValue.substring(point - 1, point); - this.insertContent('**'); - if (lastChart !== '\n' && this.currentValue !== '') { - this.setCaretPosition(point + 1); - } else { - this.setCaretPosition(point + 1); - } + const cursorSwitch = this.insertContent('**'); + this.setCaretBySwitch(cursorSwitch, point, 1); }, insertBg() { // 背景色 const point = this.getCursortPosition(); - const lastChart = this.currentValue.substring(point - 1, point); - this.insertContent('===='); - if (lastChart !== '\n' && this.currentValue !== '') { - this.setCaretPosition(point + 5); - } else { - this.setCaretPosition(point + 5); - } + const cursorSwitch = this.insertContent('===='); + this.setCaretBySwitch(cursorSwitch, point, 5); }, insertUnderline() { // 下划线 const point = this.getCursortPosition(); - const lastChart = this.currentValue.substring(point - 1, point); - this.insertContent(''); - if (lastChart !== '\n' && this.currentValue !== '') { - this.setCaretPosition(point + 3); - } else { - this.setCaretPosition(point + 5); - } + const cursorSwitch = this.insertContent(''); + this.setCaretBySwitch(cursorSwitch, point, 3); }, insertOverline() { // overline const point = this.getCursortPosition(); - const lastChart = this.currentValue.substring(point - 1, point); - this.insertContent('~~~~'); - if (lastChart !== '\n' && this.currentValue !== '') { - this.setCaretPosition(point + 2); - } else { - this.setCaretPosition(point + 2); - } + const cursorSwitch = this.insertContent('~~~~'); + this.setCaretBySwitch(cursorSwitch, point, 2); }, insertTitle(level) { // 插入标题