Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 23 additions & 44 deletions src/components/simple/simple.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import marked from '../../config/marked';
import marked from '../../config/marked';
import common from '../../mixins/common';

export default {
Expand Down Expand Up @@ -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);
Expand All @@ -59,10 +59,12 @@ export default {
lastFourCharts !== ' '
) {
const str = '\n' + initStr;
cursorSwitch = 1;
this.insertAfterText(str);
} else {
this.insertAfterText(initStr);
}
return cursorSwitch;
},
getCursortPosition() {
// 获取光标位置
Expand Down Expand Up @@ -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> ');
Expand All @@ -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('<u></u>');
if (lastChart !== '\n' && this.currentValue !== '') {
this.setCaretPosition(point + 3);
} else {
this.setCaretPosition(point + 5);
}
const cursorSwitch = this.insertContent('<u></u>');
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) {
// 插入标题
Expand Down