-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.vue
More file actions
167 lines (165 loc) · 6.2 KB
/
index.vue
File metadata and controls
167 lines (165 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<template>
<div class="textarea" contenteditable="true"></div>
</template>
<!--使用该组件注意一个问题就是不要在可视化区域的节点上使用-webkit-user-select: none;样式,否则会出现当鼠标焦点小时光标和小水滴无法消失的情况-->
<script type="text/babel">
export default {
data () {
return {}
},
computed: {},
props: {},
beforeMount () {
},
mounted() {
this.addInputEvent();
this.addFocusEvent();
this.addEventPaste(this.$el);
},
methods: {
/**
* 监听鼠标input事件
*/
addInputEvent(){
this.$el.addEventListener('input', () => {
this.$emit('input', this.getValue());
})
},
/**
* 监听鼠标获取焦点事件
*/
addFocusEvent(){
this.$el.addEventListener('focus', () => {
setTimeout(() => {
// 解决:如果ios手机使用的不是原生键盘(也可能不止IOS手机有这个问题),则会出现键盘挡住输入框问题,当bottom=0的情况,使用这个属性就可以滚动屏幕中央
this.$el.scrollIntoView(true);
}, 300);
this.$emit('focus');
})
},
/**
* 追加
* @param value
* @param bool
*/
appendValue(value, bool) {
this.$el.innerText += value;
this.$emit('input', this.getValue());
},
/**
* 监听复制事件,去除样式得到纯文本
*/
addEventPaste: function (el) {
// 干掉IE http之类地址自动加链接
try {
document.execCommand("AutoUrlDetect", false, false);
} catch (e) {
}
// 监听复制paste事件,目的是为了让-webkit-user-modify属性兼容IE8,毕竟该属性在IE兼容性不好
el.addEventListener('paste', function (e) {
e.preventDefault();
var text = null;
if (window.clipboardData && clipboardData.setData) {
// IE
text = window.clipboardData.getData('text');
} else {
text = (e.originalEvent || e).clipboardData.getData('text/plain') || prompt('在这里输入文本');
}
// 这里的目的是为了将鼠标的光标移动到复制之后文本的末尾的末尾
if (document.body.createTextRange) {
if (document.selection) {
textRange = document.selection.createRange();
} else if (window.getSelection) {
sel = window.getSelection();
var range = sel.getRangeAt(0);
// 创建临时元素,使得TextRange可以移动到正确的位置
var tempEl = document.createElement("span");
tempEl.innerHTML = "&#FEFF;";
range.deleteContents();
range.insertNode(tempEl);
textRange = document.body.createTextRange();
textRange.moveToElementText(tempEl);
tempEl.parentNode.removeChild(tempEl);
}
textRange.text = text;
textRange.collapse(false);
textRange.select();
} else {
// Chrome之类浏览器
document.execCommand("insertText", false, text);
}
});
},
/**
* 替换
* @param value
*/
setValue(value) {
this.$el.innerText = value;
this.$emit('input', this.getValue());
},
/**
* 获取值
* @returns {*}
*/
getValue() {
return this.getHtmlToText();
},
/**
* 获取HTML转换之后的文本(去除div标签,替换<br/>为换行)
* @returns {string}
*/
getHtmlToText() {
return this.replaceToBreak(this.getHtml());
},
/**
* 获取HTML
*/
getHtml() {
return document.querySelector('.textarea').innerHTML
},
/**
* 替换DIV到换行符
* @returns {string}
*/
replaceToBreak(html) {
html = String(html).replace(/<\/div>/gi, '');
html = html.replace(/<div>(<br>)?(<br\/>)?/gi, '\n');
html = html.replace(/<br>|<br\/>/gi, '\n');
return html;
},
/**
* 获取纯text文本
* @returns {string}
*/
getText(){
if (window.navigator.appName.indexOf("Explorer") > -1)
return this.$el.innerText;
else
return this.$el.textContent;
}
},
}
</script>
<style scoped lang="less" rel="stylesheet/less">
.textarea {
box-sizing: border-box;
min-height: 136px;
max-height: 300px;
margin-left: auto;
margin-right: auto;
padding: 3px;
outline: 0;
border: 1px solid #a0b3d6;
font-size: 12px;
word-wrap: break-word;
overflow-x: hidden;
overflow-y: auto;
_overflow-y: visible;
-webkit-user-modify: read-write-plaintext-only; // 只是编辑text文本,只能解决webkit内核里面问题,手机端适用
-webkit-user-select: text; // 解决IOS部分手机不支持contenteditable=true属性问题
p {
margin: 0;
}
}
</style>