Skip to content

Commit

Permalink
fix: in ul ol, the enter insert bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
lihuiqing authored and li-xiaoqing committed Mar 6, 2019
1 parent a609e5b commit 71218b8
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 27 deletions.
15 changes: 12 additions & 3 deletions lib/pmd.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -25616,6 +25616,8 @@ function insertEnter(textArea, e, $vue) {
if ('selectionStart' in textArea) {
var start = textArea.selectionStart;
var end = textArea.selectionEnd;
var newStart = 0;
var newEnd = 0;
var content = textArea.value; // get cursor lastLine

var lastLine = content.substring(0, start).split('\n').pop();
Expand All @@ -25630,12 +25632,14 @@ function insertEnter(textArea, e, $vue) {
if (subfix.search(/-/) >= 0) {
// ul
content = content.substring(0, start) + '\n' + subfix + content.substring(end, content.length);
textArea.setSelectionRange(start + subfix.length + 1, start + subfix.length + 1);
newStart = start + subfix.length + 1;
newEnd = start + subfix.length + 1;
} else {
// ol
var temp = subfix.replace(/(\d+)/, parse_int_default()(subfix) + 1);
content = content.substring(0, start) + '\n' + temp + content.substring(end, content.length);
textArea.setSelectionRange(start + temp.length + 1, start + temp.length + 1);
newStart = start + temp.length + 1;
newEnd = start + temp.length + 1;
}
} else {
var matchListNeedRemoveLine = lastLine.match(/^\s*(?:[0-9]+\.|-)\s+$/);
Expand All @@ -25644,12 +25648,17 @@ function insertEnter(textArea, e, $vue) {
e.preventDefault();
var preLength = matchListNeedRemoveLine.shift().length;
content = content.substring(0, start - preLength) + '\n' + content.substring(end, content.length);
textArea.setSelectionRange(start - preLength, start - preLength);
newStart = start - preLength;
newEnd = start - preLength;
} else {
newStart = start;
newEnd = end;
}
}

$vue.setTextareaContent(content);
$vue.$nextTick(function () {
textArea.setSelectionRange(newStart, newEnd);
textArea.focus();
});
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/pmd.common.js.map

Large diffs are not rendered by default.

15 changes: 12 additions & 3 deletions lib/pmd.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -25625,6 +25625,8 @@ function insertEnter(textArea, e, $vue) {
if ('selectionStart' in textArea) {
var start = textArea.selectionStart;
var end = textArea.selectionEnd;
var newStart = 0;
var newEnd = 0;
var content = textArea.value; // get cursor lastLine

var lastLine = content.substring(0, start).split('\n').pop();
Expand All @@ -25639,12 +25641,14 @@ function insertEnter(textArea, e, $vue) {
if (subfix.search(/-/) >= 0) {
// ul
content = content.substring(0, start) + '\n' + subfix + content.substring(end, content.length);
textArea.setSelectionRange(start + subfix.length + 1, start + subfix.length + 1);
newStart = start + subfix.length + 1;
newEnd = start + subfix.length + 1;
} else {
// ol
var temp = subfix.replace(/(\d+)/, parse_int_default()(subfix) + 1);
content = content.substring(0, start) + '\n' + temp + content.substring(end, content.length);
textArea.setSelectionRange(start + temp.length + 1, start + temp.length + 1);
newStart = start + temp.length + 1;
newEnd = start + temp.length + 1;
}
} else {
var matchListNeedRemoveLine = lastLine.match(/^\s*(?:[0-9]+\.|-)\s+$/);
Expand All @@ -25653,12 +25657,17 @@ function insertEnter(textArea, e, $vue) {
e.preventDefault();
var preLength = matchListNeedRemoveLine.shift().length;
content = content.substring(0, start - preLength) + '\n' + content.substring(end, content.length);
textArea.setSelectionRange(start - preLength, start - preLength);
newStart = start - preLength;
newEnd = start - preLength;
} else {
newStart = start;
newEnd = end;
}
}

$vue.setTextareaContent(content);
$vue.$nextTick(function () {
textArea.setSelectionRange(newStart, newEnd);
textArea.focus();
});
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/pmd.umd.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/pmd.umd.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/pmd.umd.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "perfect-markdown",
"version": "1.0.1",
"version": "1.0.2",
"main": "src/index.js",
"files": [
"lib",
Expand Down
39 changes: 24 additions & 15 deletions src/utils/insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import iconConfig from './toolbar-icon'

export function insertContentAtCaret(dom, icon, payload, $vue) {
switch (icon) {
case 'file':
fileInsert(dom, payload, $vue)
break
case 'link':
linkInsert(dom, payload, $vue)
break
case 'image':
imageInsert(dom, payload, $vue)
break
default:
txtInsert(dom, icon, payload, $vue)
break
case 'file':
fileInsert(dom, payload, $vue)
break
case 'link':
linkInsert(dom, payload, $vue)
break
case 'image':
imageInsert(dom, payload, $vue)
break
default:
txtInsert(dom, icon, payload, $vue)
break
}
}

Expand Down Expand Up @@ -177,6 +177,8 @@ export function insertEnter(textArea, e, $vue) {
if ('selectionStart' in textArea) {
const start = textArea.selectionStart
const end = textArea.selectionEnd
let newStart = 0
let newEnd = 0
let content = textArea.value
// get cursor lastLine
let lastLine = content.substring(0, start).split('\n').pop()
Expand All @@ -189,24 +191,31 @@ export function insertEnter(textArea, e, $vue) {
if (subfix.search(/-/) >= 0) {
// ul
content = content.substring(0, start) + '\n' + subfix + content.substring(end, content.length)
textArea.setSelectionRange(start + subfix.length + 1, start + subfix.length + 1)
newStart = start + subfix.length + 1
newEnd = start + subfix.length + 1
} else {
// ol
let temp = subfix.replace(/(\d+)/, parseInt(subfix) + 1)
content = content.substring(0, start) + '\n' + temp + content.substring(end, content.length)
textArea.setSelectionRange(start + temp.length + 1, start + temp.length + 1)
newStart = start + temp.length + 1
newEnd = start + temp.length + 1
}
} else {
let matchListNeedRemoveLine = lastLine.match(/^\s*(?:[0-9]+\.|-)\s+$/)
if (matchListNeedRemoveLine) {
e.preventDefault()
let preLength = matchListNeedRemoveLine.shift().length
content = content.substring(0, start - preLength) + '\n' + content.substring(end, content.length)
textArea.setSelectionRange(start - preLength, start - preLength)
newStart = start - preLength
newEnd = start - preLength
} else {
newStart = start
newEnd = end
}
}
$vue.setTextareaContent(content)
$vue.$nextTick(() => {
textArea.setSelectionRange(newStart, newEnd)
textArea.focus()
})
} else {
Expand Down

0 comments on commit 71218b8

Please sign in to comment.