Mtextarea.vue 4.9 KB

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