Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dep(insert): extract logic to insert-text-2 #46

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"prepublish": "npm run build"
},
"main": "dist/vue-at.js",
"dependencies": {},
"dependencies": {
"insert-text-2": "0.0.4"
},
"peerDependencies": {
"vue": "2.x"
},
Expand Down
28 changes: 6 additions & 22 deletions src/At.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
scrollIntoView, getAtAndIndex
} from './util'
import AtTemplate from './AtTemplate.vue'
import insertText from 'insert-text-2'

export default {
name: 'At',
Expand Down Expand Up @@ -83,7 +84,7 @@ export default {
atItems () {
return this.at ? [this.at] : this.ats
},

style () {
if (this.atwho) {
const { list, cur, x, y } = this.atwho
Expand Down Expand Up @@ -226,10 +227,10 @@ export default {
const range = getPrecedingRange()
if (range) {
const { atItems, avoidEmail, allowSpaces } = this

let show = true
const text = range.toString()

const { at, index } = getAtAndIndex(text, atItems)

if (index < 0) show = false
Expand All @@ -246,7 +247,7 @@ export default {
if (!allowSpaces && /\s/.test(chunk)) {
show = false
}

// chunk以空白字符开头不匹配 避免`@ `也匹配
if (/^\s/.test(chunk)) show = false

Expand Down Expand Up @@ -324,23 +325,6 @@ export default {
}
},

// 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下必传
applyRange(r)
},
insertItem () {
const { range, offset, list, cur } = this.atwho
const { suffix, atItems, itemName } = this
Expand All @@ -353,7 +337,7 @@ export default {
applyRange(r)
applyRange(r)
const t = itemName(list[cur]) + suffix
this.insertText(t, r)
insertText(t)
this.handleInput()
}
}
Expand Down
13 changes: 2 additions & 11 deletions src/AtTextarea.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script>
import At from './At.vue'
import getCaretCoordinates from 'textarea-caret'
import insertText from 'insert-text-2'
import { getAtAndIndex } from './util'

export default {
Expand Down Expand Up @@ -118,16 +119,6 @@ export default {
}
},

// todo: 抽离成库并测试
insertText (text, ta) {
const start = ta.selectionStart
const end = ta.selectionEnd
ta.value = ta.value.slice(0, start) +
text + ta.value.slice(end)
const newEnd = start + text.length
ta.selectionStart = newEnd
ta.selectionEnd = newEnd
},
insertItem () {
const { chunk, offset, list, cur, atEnd } = this.atwho
const { suffix, atItems, itemName } = this
Expand All @@ -138,7 +129,7 @@ export default {
el.selectionStart = start
el.focus() // textarea必须focus回来
const t = itemName(list[cur]) + suffix
this.insertText(t, el)
insertText(t)
this.handleInput()
}
}
Expand Down