MessageInput.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <div class="message-input-wrap">
  3. <div class="message-input-border">
  4. <div class="message-input-clear" @click="clearMessage">
  5. <div :class="[readonly ? 'disabled' : '']">
  6. <img src="@/assets/icons/iconPark-clear.svg" width="15" />
  7. </div>
  8. </div>
  9. <a-textarea
  10. @keydown.enter="handleEnter"
  11. :max-length="maxLength"
  12. v-model="value"
  13. :show-word-limit="value.length > maxLength / 2"
  14. :auto-size="{ maxRows: 6 }"
  15. placeholder="输入您想了解的内容,Shift+Enter换行"
  16. :style="{
  17. flex: 1,
  18. borderRadius: '12px',
  19. marginTop: '20px',
  20. padding: '0px 30px 0 16px',
  21. background: 'transparent',
  22. border: '1px solid #e3e3e3',
  23. minHeight: '120px',
  24. paddingBottom: `${value.length > maxLength / 2 ? '20px' : ''}`
  25. }"
  26. >
  27. </a-textarea>
  28. </div>
  29. <div
  30. :class="['message-button', 'message-button-send', value.length > 0 && !readonly ? '' : 'disabled']"
  31. @click="sendMessage"
  32. >
  33. <icon-send size="30" />
  34. </div>
  35. </div>
  36. </template>
  37. <script>
  38. import { defineComponent, ref, inject, onMounted, onBeforeUnmount } from "vue";
  39. export default defineComponent({
  40. components: {},
  41. props: ["readonly", "eventName"],
  42. setup(props) {
  43. const { helper } = inject("$global");
  44. const value = ref("");
  45. const maxLength = ref(6000);
  46. const sendMessage = () => {
  47. if (value.value.length > 0 && !props.readonly) {
  48. const eventName = props.eventName || "send-message";
  49. helper.$bus.emit(eventName, value.value);
  50. value.value = "";
  51. }
  52. };
  53. const clearMessage = () => {
  54. if (!props.readonly) {
  55. helper.$bus.emit("clear-message");
  56. }
  57. };
  58. const handleEnter = (e) => {
  59. if (!e.shiftKey) {
  60. e.preventDefault();
  61. sendMessage();
  62. }
  63. };
  64. onMounted(() => {
  65. helper.$bus.on("set-input", (data) => {
  66. value.value = data;
  67. });
  68. });
  69. onBeforeUnmount(() => {
  70. helper.$bus.off("set-input");
  71. });
  72. return {
  73. value,
  74. maxLength,
  75. sendMessage,
  76. clearMessage,
  77. handleEnter
  78. };
  79. }
  80. });
  81. </script>
  82. <style scoped>
  83. .message-input-border {
  84. display: flex;
  85. }
  86. .message-input-clear {
  87. width: 30px;
  88. height: 100%;
  89. margin-right: 8px;
  90. display: none;
  91. div {
  92. width: 30px;
  93. height: 30px;
  94. border-radius: 15px;
  95. background-color: rgba(64, 95, 234, 0.05);
  96. position: absolute;
  97. bottom: 0;
  98. cursor: pointer;
  99. display: flex;
  100. align-items: center;
  101. justify-content: center;
  102. &.disabled {
  103. cursor: not-allowed;
  104. }
  105. &:hover {
  106. background-color: rgba(64, 95, 234, 0.1);
  107. }
  108. }
  109. }
  110. .message-input-wrap {
  111. position: relative;
  112. margin-bottom: 20px;
  113. }
  114. .message-button {
  115. position: absolute;
  116. width: 30px;
  117. height: 40px;
  118. display: flex;
  119. align-items: center;
  120. justify-content: center;
  121. cursor: pointer;
  122. bottom: 5px;
  123. right: 5px;
  124. z-index: 2;
  125. &.disabled {
  126. opacity: 0.5;
  127. cursor: not-allowed;
  128. }
  129. }
  130. .message-button-upload {
  131. left: 38px;
  132. }
  133. .message-button-send {
  134. color: rgb(64, 95, 234, 0.8);
  135. &:hover {
  136. color: rgb(64, 95, 234);
  137. }
  138. }
  139. </style>