Mtextarea2.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <template>
  2. <div class="mtext-box">
  3. <span v-show="text_max != ''" class="text-max-tip">{{ text.length }}/{{ text_max }}</span>
  4. <div class="diy_text_box">
  5. <div v-if="label" class="van-cell van-field" :class="{ 'van-cell--required': required }">
  6. <div class="van-cell__title van-field__label">
  7. <span>{{ label }}</span>
  8. </div>
  9. <div class="van-cell__value van-field__value">
  10. <div class="van-field__body">
  11. <div v-html="s_text"></div>
  12. <textarea :placeholder="placeholder" v-model="text" class="diy_text"></textarea>
  13. </div>
  14. </div>
  15. </div>
  16. <div v-else>
  17. <div v-html="s_text"></div>
  18. <textarea :placeholder="placeholder" v-model="text" class="diy_text" @input="onTextInput"></textarea>
  19. </div>
  20. </div>
  21. <div v-show="imgs.length > 0" style="padding:0.24rem;" class="upload_box">
  22. <Uploader ref="uploader" class="needsclick" v-model="s_imgs" :accept="accept" :beforeRead="beforeRead" :max-count="3" />
  23. </div>
  24. <div style="text-align: left; min-height: 0.5rem;">
  25. <!-- <label href="javascript:void(0)" v-show="false && speech && isapp && !speech_start" @click="startRecognize" class="speech_btn">
  26. <icon name="speech" class="speech_icon"></icon>
  27. </label>
  28. <a href="javascript:void(0)" v-show="speech && isapp && speech_start" @click="stopRecognize" class="speech_btn"><icon name="speech_end" class="speech_icon"></icon></a>
  29. -->
  30. <a href="javascript:void(0)" class="images_btn" @click="imgs_max_tip">
  31. <Uploader ref="uploader" style="position: absolute; opacity: 0;" :accept="accept" :beforeRead="beforeRead" v-model="s_imgs" :max-count="imgs_max"></Uploader>
  32. <icon v-show="images" name="image_upload" class="images_icon"></icon>
  33. </a>
  34. </div>
  35. </div>
  36. </template>
  37. <script>
  38. import Uploader from '@/components/OssUploader'
  39. import {specialFilter} from "../utils/helper";
  40. export default {
  41. name: 'Mtextarea',
  42. model: {
  43. prop: 'value',
  44. event: 'value'
  45. },
  46. components: {
  47. Uploader
  48. },
  49. props: {
  50. label: { type: String, default: null },
  51. required: { type: Boolean, default: false },
  52. value: {
  53. type: String,
  54. default: ''
  55. },
  56. speech: {
  57. type: Boolean,
  58. default: false
  59. },
  60. imgs: {
  61. type: Array,
  62. default: () => {
  63. return []
  64. }
  65. },
  66. images: {
  67. type: Boolean,
  68. default: false
  69. },
  70. placeholder: {
  71. type: String,
  72. default: ''
  73. },
  74. text_max: {
  75. type: Number,
  76. default: 0
  77. },
  78. imgs_max: {
  79. type: Number,
  80. default: 0
  81. }
  82. },
  83. data () {
  84. let imgs_max = this.imgs_max != '' ? this.imgs_max * 1 : 3
  85. return {
  86. images_length: imgs_max,
  87. s_imgs: [],
  88. isapp: window.plus,
  89. speech_start: false,
  90. text: '',
  91. s_text: '',
  92. textarea_show: false,
  93. position: 0,
  94. accept:'image/jpeg,image/png,image/jpg',
  95. }
  96. },
  97. watch: {
  98. imgs (val, old) {
  99. this.s_imgs = val
  100. this.$emit('update:imgs', val)
  101. },
  102. s_imgs (val, old) {
  103. this.$emit('update:imgs', val)
  104. },
  105. text (val, old) {
  106. if (this.text_max * 1 > 0 && val.length > this.text_max * 1) {
  107. this.text = old
  108. return false
  109. }
  110. if (Math.abs(val.length - old.length) > 20) {
  111. setTimeout(() => {
  112. this.s_text = val.replace(/\n|\r|(\r\n)|(\u0085)|(\u2028)|(\u2029)/g, '<br>')
  113. this.$emit('value', val)
  114. }, 100)
  115. } else {
  116. this.s_text = val.replace(/\n|\r|(\r\n)|(\u0085)|(\u2028)|(\u2029)/g, '<br>')
  117. this.$emit('value', val)
  118. }
  119. },
  120. value (val) {
  121. this.text = val
  122. this.$emit('value', val)
  123. }
  124. },
  125. created () {
  126. this.text = specialFilter(this.value)
  127. this.s_imgs = this.imgs
  128. },
  129. methods: {
  130. onTextInput(e){
  131. this.text = specialFilter(e.target.value)
  132. },
  133. // 返回布尔值
  134. beforeRead(file) {
  135. const isJPG = /^image\/(jpeg|png|jpg)$/.test(file.type);
  136. // const isLt2M = file.size / 1024 / 1024 < 2;
  137. if (!isJPG) {
  138. this.$toast('上传图片只能是 jpeg|png|jpg 格式!');
  139. }
  140. // if (!isLt2M) {
  141. // this.$toast('上传图片大小不能超过 2MB!');
  142. // }
  143. return isJPG;
  144. },
  145. imgs_max_tip () {
  146. if (this.images_length == this.s_imgs.length) {
  147. this.$toast('最多只能上传' + this.images_length + '张图片')
  148. return false
  149. }
  150. },
  151. images_upload () {
  152. let uploader = this.$refs['uploader'].$el
  153. let inputs = document.querySelectorAll('.van-uploader__input')
  154. let input = null
  155. for (let i in inputs) {
  156. if (uploader.contains(inputs.item(i))) {
  157. input = inputs.item(i)
  158. }
  159. }
  160. if (input != null) {
  161. input.classList.add('needsclick')
  162. input.click()
  163. input.click()
  164. }
  165. },
  166. // startRecognize () {
  167. // let self = this
  168. // var options = {}
  169. // options.engine = 'baidu'
  170. // this.speech_start = true
  171. // if (window.plus) {
  172. // plus.speech.startRecognize(
  173. // options,
  174. // function (s) {
  175. // self.speech_start = false
  176. // },
  177. // function (e) {
  178. // self.speech_start = false
  179. // alert('语音识别失败:' + e.message)
  180. // }
  181. // )
  182. // }
  183. // },
  184. // stopRecognize () {
  185. // this.speech_start = false
  186. // if (window.plus) {
  187. // plus.speech.stopRecognize()
  188. // }
  189. // },
  190. }
  191. }
  192. </script>
  193. <style scoped>
  194. .diy_text_box {
  195. position: relative;
  196. min-height: 80px;
  197. font-size: 0.32rem;
  198. color: #666;
  199. line-height: 1.5;
  200. margin: 0rem;
  201. overflow: hidden;
  202. word-break: break-all;
  203. }
  204. .diy_text {
  205. width: 100%;
  206. top: 0;
  207. display: block;
  208. padding: 0;
  209. margin: 0;
  210. position: absolute;
  211. height: 100%;
  212. outline: none;
  213. border: none;
  214. font-size: 0.32rem;
  215. color: #000;
  216. line-height: 1.5;
  217. resize: none;
  218. }
  219. .focus_hidden_input.diy_text {
  220. top: 5000000px !important;
  221. }
  222. .speech_btn {
  223. padding: 0 0.16rem;
  224. }
  225. .speech_icon {
  226. width: 0.4rem;
  227. }
  228. .images_icon {
  229. width: 0.45rem;
  230. }
  231. .images_btn {
  232. overflow: hidden;
  233. }
  234. /deep/ .upload_box .van-uploader__upload {
  235. display: none;
  236. }
  237. /deep/ .images_btn .van-uploader__preview {
  238. display: none;
  239. }
  240. .text-max-tip {
  241. position: absolute;
  242. bottom: 0rem;
  243. right: 0;
  244. font-size: 0.24rem;
  245. color: #ccc;
  246. z-index: 1;
  247. }
  248. .mtext-box {
  249. position: relative;
  250. }
  251. </style>