add-comment.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <template>
  2. <view
  3. :class="{shadow: showShadow || showEmoji}"
  4. class="add-comment">
  5. <view class="input-box">
  6. <view class="textarea-core">
  7. <textarea
  8. ref="textarea"
  9. v-model="content"
  10. :focus="focusFlag"
  11. :show-confirm-bar="false"
  12. :fixed="true"
  13. confirm-type="done"
  14. maxlength="500"
  15. placeholder-class="wk-placeholder"
  16. placeholder="请输入内容"
  17. auto-height
  18. class="textarea"
  19. @focus="handleFocus"
  20. @blur="handleBlur"
  21. @confirm="submit"
  22. @input="changeText" />
  23. </view>
  24. <view class="control">
  25. <view class="submit-btn" @click="submit">
  26. 回复
  27. </view>
  28. </view>
  29. </view>
  30. <!-- <emoji-section
  31. :visible.sync="showEmoji"
  32. @select="handleEmoji" /> -->
  33. </view>
  34. </template>
  35. <script>
  36. // import EmojiSection from '@/components/emoji/index'
  37. import xss from 'xss'
  38. export default {
  39. name: 'AddComment',
  40. components: {
  41. // EmojiSection
  42. },
  43. props: {
  44. value: {
  45. type: String,
  46. default: ''
  47. },
  48. replay: {
  49. type: Object,
  50. default: null
  51. },
  52. immediateFocus: {
  53. type: Boolean,
  54. default: false
  55. }
  56. },
  57. data() {
  58. return {
  59. focusFlag: false,
  60. content: '',
  61. replayInfo: null,
  62. showEmoji: false,
  63. showShadow: false
  64. }
  65. },
  66. watch: {
  67. value: {
  68. handler(val) {
  69. this.content = val
  70. },
  71. immediate: true
  72. },
  73. replay: {
  74. handler(val) {
  75. this.replayInfo = val
  76. if (
  77. !this.focusFlag &&
  78. !this.$isEmpty(val)
  79. ) {
  80. this.$nextTick(() => {
  81. this.handleFocus()
  82. })
  83. }
  84. },
  85. immediate: true,
  86. deep: true
  87. },
  88. immediateFocus: {
  89. handler() {
  90. this.$nextTick(() => {
  91. if (this.immediateFocus) {
  92. this.handleFocus()
  93. }
  94. })
  95. },
  96. immediate: true
  97. },
  98. content(newVal, oldVal) {
  99. if (this.replayInfo) {
  100. let str = `回复@${this.replayInfo.user.realname} `
  101. if (!newVal.startsWith(str)) {
  102. this.replayInfo = null
  103. if (newVal.length > 0) {
  104. this.content = oldVal.slice(str.length)
  105. // console.log('newVal', newVal)
  106. // console.log('oldVal', oldVal)
  107. // console.log('content', this.content)
  108. }
  109. this.$emit('change-user', null)
  110. }
  111. }
  112. this.$emit('input', this.content)
  113. }
  114. },
  115. methods: {
  116. focus() {
  117. this.focusFlag = true
  118. },
  119. handleFocus() {
  120. this.showShadow = true
  121. this.focusFlag = true
  122. this.showEmoji = false
  123. },
  124. handleBlur() {
  125. this.showShadow = false
  126. this.focusFlag = false
  127. },
  128. handleEmoji(emoji) {
  129. this.content += emoji
  130. },
  131. formatValue(oldStr) {
  132. // 禁止输入 emoji
  133. const regStr = /[\ud800-\udbff]|[\udc00-\udfff]/g;
  134. return oldStr.replace(regStr, '');
  135. },
  136. changeText() {
  137. let oldStr = String(this.content)
  138. let valueStr = this.formatValue(this.content)
  139. this.content = oldStr
  140. this.$nextTick(function() {
  141. this.content = valueStr
  142. })
  143. },
  144. submit() {
  145. if (this.content == '') {
  146. this.$toast('回复内容不能为空')
  147. return
  148. }
  149. this.content = xss(this.content)
  150. this.$emit('submit', this.content)
  151. this.showEmoji = false
  152. this.content = ''
  153. this.replayInfo = null
  154. },
  155. }
  156. }
  157. </script>
  158. <style scoped lang="scss">
  159. .add-comment {
  160. position: fixed;
  161. left: 0;
  162. bottom: 0;
  163. bottom: constant(safe-area-inset-bottom);
  164. bottom: env(safe-area-inset-bottom);
  165. z-index: 10;
  166. width: 100%;
  167. font-size: 26rpx;
  168. min-height: 102rpx;
  169. overflow: hidden;
  170. &.shadow {
  171. /* #ifdef MP-WEIXIN */
  172. bottom: 32rpx;
  173. /* #endif */
  174. box-shadow: 0 -10rpx 15rpx rgba(0,0,0,0.05);
  175. }
  176. .input-box {
  177. position: relative;
  178. width: 100%;
  179. background-color: white;
  180. border-top: 1rpx solid $border-color;
  181. padding: 20rpx 30rpx;
  182. display: flex;
  183. justify-content: flex-start;
  184. align-items: flex-end;
  185. overflow: hidden;
  186. .textarea-core {
  187. flex: 1;
  188. min-height: 70rpx;
  189. border-radius: 3rpx;
  190. background-color: #f5f5f5;
  191. padding: 16rpx 20rpx;
  192. overflow: hidden;
  193. @include center;
  194. .textarea {
  195. width: 100%;
  196. max-height: 200rpx;
  197. font-size: 26rpx;
  198. }
  199. }
  200. .control {
  201. flex-shrink: 1;
  202. height: 70rpx;
  203. @include left;
  204. .face-icon {
  205. width: 36rpx;
  206. height: 36rpx;
  207. margin: 0 30rpx;
  208. }
  209. .submit-btn {
  210. font-size: $wk-font-sm;
  211. color: white;
  212. background-color: $theme-color;
  213. padding: 10rpx 20rpx;
  214. border-radius: 4rpx;
  215. margin-left: 30rpx;
  216. }
  217. }
  218. }
  219. }
  220. </style>