Commit 09c47d8c by 杨明

fixed

1 parent 33814a88
<template> <template>
<div class="w-textarea" ref="wTextarea"> <div class="w-textarea" ref="wTextarea">
<div contenteditable="true" class="w-textarea_input" ref="wTextareaContent" :id="contentId" @click="inputClick($event)" @focus="onFocus" @blur="onBlur" @keydown.delete="handleDelete($event)" @input="handleInput($event.target)"></div> <div contenteditable="true" class="w-textarea_input" ref="wTextareaContent" :id="contentId" @keydown="textareaKeydown($event)" @click="inputClick($event)" @focus="onFocus" @blur="onBlur" @keydown.delete="handleDelete($event)" @compositionend="handleInput($event.target)" @input="handleInput($event.target)"></div>
</div> </div>
</template> </template>
<script> <script>
...@@ -64,7 +64,8 @@ export default { ...@@ -64,7 +64,8 @@ export default {
// 创建模版标签的style // 创建模版标签的style
this.createStyle() this.createStyle()
// 每次光标变化的时候,保存 range // 每次光标变化的时候,保存 range
document.addEventListener('selectionchange', this.selectHandler) document.querySelector('.w-textarea_input').addEventListener('keyup', this.selectHandler)
document.querySelector('.w-textarea_input').addEventListener('click', this.selectHandler)
}, },
beforeDestroy() { beforeDestroy() {
// 卸载事件 // 卸载事件
...@@ -95,10 +96,7 @@ export default { ...@@ -95,10 +96,7 @@ export default {
}, },
textareaKeydown(event) { textareaKeydown(event) {
if (event.keyCode == 13) { if (event.keyCode == 13) {
this.insertHtml('<br>') this.insertContent('<br>')
let nn = document.createElement('br')
document.querySelector('.w-textarea_input').appendChild(nn)
return false
} }
}, },
/*换行*/ /*换行*/
...@@ -146,7 +144,7 @@ export default { ...@@ -146,7 +144,7 @@ export default {
node.innerText = text node.innerText = text
// 添加id便于删除 // 添加id便于删除
node.id = this.getGuid() node.id = this.getGuid()
this.insertNode(node) this.insertContent(node)
}, },
addNode() { addNode() {
let target = this.$refs.wTextareaContent let target = this.$refs.wTextareaContent
...@@ -165,7 +163,7 @@ export default { ...@@ -165,7 +163,7 @@ export default {
let node = document.createElement('img') let node = document.createElement('img')
node.src = url node.src = url
// node.setAttribute('ondragstart', 'return false;') // node.setAttribute('ondragstart', 'return false;')
this.insertNode(node) this.insertContent(node)
}, },
onFocus() { onFocus() {
this.isLocked = true this.isLocked = true
...@@ -174,6 +172,44 @@ export default { ...@@ -174,6 +172,44 @@ export default {
onBlur() { onBlur() {
this.isLocked = false this.isLocked = false
}, },
insertContent(el) {
let sel = null
if (document.selection) {
sel = document.selection
sel.createRange().pasteHTML(el)
} else {
sel = window.getSelection()
// 同上考虑到标签插入时光标会在之前插入过的标签内或者不在DIV上,这里作一下判断
// 光标在div的text上时后层组件是div,在插入标签时后两层是div,这样就不会插入到div外面或者出现标签内插入标签。
console.log(sel)
if (sel.rangeCount > 0) {
console.log(888)
const frag = document.createDocumentFragment()
const node = el
const lastNode = frag.appendChild(node)
this.savedRange.insertNode(frag)
const contentRange = this.savedRange.cloneRange()
contentRange.setStartAfter(lastNode)
contentRange.collapse(true)
sel.removeAllRanges()
sel.addRange(contentRange)
}
}
},
insertNode(node) { insertNode(node) {
// 在内容中插入标签 // 在内容中插入标签
// 删掉选中的内容(如有) // 删掉选中的内容(如有)
...@@ -276,6 +312,7 @@ export default { ...@@ -276,6 +312,7 @@ export default {
if (range && range.commonAncestorContainer.ownerDocument.activeElement.id === this.contentId) { if (range && range.commonAncestorContainer.ownerDocument.activeElement.id === this.contentId) {
this.savedRange = range this.savedRange = range
} }
console.log(this.savedRange)
} }
}, },
watch: { watch: {
...@@ -291,7 +328,7 @@ export default { ...@@ -291,7 +328,7 @@ export default {
<style lang="scss"> <style lang="scss">
.w-textarea { .w-textarea {
wise { wise {
user-select: none; // user-select: none;
border-radius: 2px; border-radius: 2px;
background-color: transparent; background-color: transparent;
font-size: 14px; font-size: 14px;
...@@ -302,7 +339,7 @@ export default { ...@@ -302,7 +339,7 @@ export default {
color: #26a2ff; color: #26a2ff;
// padding: 0 1px; // padding: 0 1px;
white-space: nowrap; white-space: nowrap;
user-select: none; user-select: auto !important;
cursor: default; cursor: default;
-webkit-user-modify: read-only !important; -webkit-user-modify: read-only !important;
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!