From b349178a818788bd5b01eaa6f4de43da88067b3d Mon Sep 17 00:00:00 2001 From: Fritz Lin Date: Mon, 2 Oct 2017 19:04:05 +0800 Subject: [PATCH] fix(range): regression bug of handleDelete 'n improve insertText methods, ref #22 --- src/At.vue | 26 +++++++++++++++++++++----- src/AtTextarea.vue | 22 +++++++++++++++------- src/util.js | 25 ------------------------- 3 files changed, 36 insertions(+), 37 deletions(-) diff --git a/src/At.vue b/src/At.vue index e8c5408..beb953b 100644 --- a/src/At.vue +++ b/src/At.vue @@ -2,7 +2,7 @@ import { closest, getOffset, getPrecedingRange, getRange, applyRange, - scrollIntoView, getAtAndIndex, insertText + scrollIntoView, getAtAndIndex } from './util' import AtTemplate from './AtTemplate.vue' @@ -274,20 +274,36 @@ export default { cur: nextCur } }, + + // todo: 抽离成库并测试 + insertText (text, r) { + r.deleteContents() + const node = r.endContainer + if (node.nodeType === Node.TEXT_NODE) { + const cut = r.endOffset + node.data = node.data.slice(0, cut) + + text + node.data.slice(cut) + r.setEnd(node, cut + text.length) + } else { + const t = document.createTextNode(text) + r.insertNode(t) + r.setEndAfter(t) + } + r.collapse(false) // 参数在IE下必传 + }, insertItem () { const { range, offset, list, cur } = this.atwho const { atItems, itemName } = this const r = range.cloneRange() const text = range.toString() const { at, index } = getAtAndIndex(text, atItems) - - r.setStart(r.endContainer, index + at.length) // 从@后第一位开始 + const start = index + at.length // 从@后第一位开始 + r.setStart(r.endContainer, start) // hack: 连续两次 可以确保click后 focus回来 range真正生效 applyRange(r) applyRange(r) const t = itemName(list[cur]) + ' ' - // document.execCommand('insertText', 0, t) - insertText(t) + this.insertText(t, r) this.handleInput() } } diff --git a/src/AtTextarea.vue b/src/AtTextarea.vue index e04d98f..ce2baa8 100644 --- a/src/AtTextarea.vue +++ b/src/AtTextarea.vue @@ -1,7 +1,7 @@