This repository has been archived on 2023-08-30. You can view files and clone it, but cannot push or open issues/pull-requests.
bytedo
/
wcui
Archived
1
0
Fork 0

优化meditor的输入交互

old
宇天 2021-04-07 13:50:34 +08:00
parent be857d0f45
commit d8653d6d1a
1 changed files with 49 additions and 24 deletions

View File

@ -677,8 +677,10 @@ export default class Meditor {
dom.value = dom.value =
dom.value.slice(0, start) + val + dom.value.slice(end, dom.value.length) dom.value.slice(0, start) + val + dom.value.slice(end, dom.value.length)
dom.selectionStart = (isSelect ? start : start + val.length) + offset this.select(
dom.selectionEnd = start + val.length - offset (isSelect ? start : start + val.length) + offset,
start + val.length - offset
)
dom.scrollTop = scrollTop dom.scrollTop = scrollTop
dom.focus() dom.focus()
} else { } else {
@ -746,14 +748,10 @@ export default class Meditor {
_getCursorText(n) { _getCursorText(n) {
var dom = this.__EDITOR__ var dom = this.__EDITOR__
var start = dom.selectionStart var start = dom.selectionStart
var end = dom.selectionEnd var pos = dom.selectionEnd
var pos = end
if (n < 0) { if (n < 0) {
pos = start - 1 pos = start - 1
if (pos < 0) {
pos = 0
}
} }
return this.value[pos] || '' return this.value[pos] || ''
} }
@ -860,6 +858,7 @@ export default class Meditor {
if ( if (
(prev === next && (prev === '"' || prev === "'")) || (prev === next && (prev === '"' || prev === "'")) ||
(prev === next && prev === '`') ||
(prev === '[' && next === ']') || (prev === '[' && next === ']') ||
(prev === '{' && next === '}') || (prev === '{' && next === '}') ||
(prev === '(' && next === ')') (prev === '(' && next === ')')
@ -867,7 +866,6 @@ export default class Meditor {
this.select(pos - 1, pos + 1) this.select(pos - 1, pos + 1)
} }
} }
break break
// ( // (
@ -879,47 +877,74 @@ export default class Meditor {
this.cursor(select ? 0 : -1) this.cursor(select ? 0 : -1)
} }
break break
// )
case 48:
if (ev.shiftKey) {
let prev = this._getCursorText(-1)
let next = this._getCursorText(1)
if (prev === '(' && next === ')') {
ev.preventDefault()
this.cursor(1)
}
}
break
case 219: // [ & { case 219: // [ & {
ev.preventDefault() ev.preventDefault()
this.insert( this.insert(
ev.shiftKey ? `{${wrap}}` : `[${wrap}]`, ev.shiftKey ? `{${wrap}}` : `[${wrap}]`,
select, select,
(select && 1) || 0 select ^ 0
) )
this.cursor(select ? 0 : -1) this.cursor(select ? 0 : -1)
break break
// } & ]
// ' & " case 221: {
case 222:
{
ev.preventDefault()
let val = "'"
let prev = this._getCursorText(-1) let prev = this._getCursorText(-1)
let next = this._getCursorText(1) let next = this._getCursorText(1)
if (ev.shiftKey) {
if (prev === '{' && next === '}') {
ev.preventDefault()
this.cursor(1)
}
} else {
if (prev === '[' && next === ']') {
ev.preventDefault()
this.cursor(1)
}
}
break
}
// `
case 192:
// ' & "
case 222:
if (ev.shiftKey && ev.keyCode === 192) {
break
} else {
let val = ev.keyCode === 192 ? '`' : ev.shiftKey ? '"' : "'"
let prev = this._getCursorText(-1)
let next = this._getCursorText(1)
if (prev === '' && next === val) {
break
}
ev.preventDefault()
if (select) { if (select) {
this.insert(val + wrap + val, true, 1) this.insert(val + wrap + val, true, 1)
} else { } else {
if (ev.shiftKey) {
val = '"'
}
if (prev === next && prev === val) { if (prev === next && prev === val) {
this.cursor(1) this.cursor(1)
} else { } else {
if (prev === val || next === val) { this.insert(val + wrap + val)
this.insert(val)
} else {
this.insert(val.repeat(2))
this.cursor(-1) this.cursor(-1)
} }
} }
}
}
break break
} }
}
}) })
this._pasteFn = $.bind(this.__EDITOR__, 'paste', ev => { this._pasteFn = $.bind(this.__EDITOR__, 'paste', ev => {