Right.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <div class="h-full box-border py-6 px-7">
  3. <div class="w-full h-full relative bg-white box-border p-3 sm:p-16 pr-0">
  4. <!-- 展示在右上角 -->
  5. <el-button
  6. color="#846af7"
  7. v-show="showCopy"
  8. @click="copyContent"
  9. class="absolute top-2 right-2"
  10. >
  11. 复制
  12. </el-button>
  13. <!-- 展示在下面中间的位置 -->
  14. <el-button
  15. v-show="isWriting"
  16. class="absolute bottom-2 left-1/2 -translate-x-1/2"
  17. @click="emits('stopStream')"
  18. >
  19. 终止生成
  20. </el-button>
  21. <div ref="contentRef" class="w-full h-full pr-3 sm:pr-16 overflow-y-auto">
  22. <el-input
  23. id="inputId"
  24. type="textarea"
  25. v-model="compContent"
  26. autosize
  27. :input-style="{ boxShadow: 'none' }"
  28. resize="none"
  29. placeholder="生成的内容……"
  30. />
  31. </div>
  32. </div>
  33. </div>
  34. </template>
  35. <script setup lang="ts">
  36. import { useClipboard } from '@vueuse/core'
  37. const message = useMessage()
  38. const { copied, copy } = useClipboard()
  39. const props = defineProps({
  40. content: {
  41. // 生成的结果
  42. type: String,
  43. default: ''
  44. },
  45. isWriting: {
  46. // 是否正在生成文章
  47. type: Boolean,
  48. default: false
  49. }
  50. })
  51. const emits = defineEmits(['update:content', 'stopStream'])
  52. // 通过计算属性,双向绑定,更改生成的内容,考虑到用户想要更改生成文章的情况
  53. const compContent = computed({
  54. get() {
  55. return props.content
  56. },
  57. set(val) {
  58. emits('update:content', val)
  59. }
  60. })
  61. /** 滚动 */
  62. const contentRef = ref<HTMLDivElement>()
  63. defineExpose({
  64. scrollToBottom() {
  65. contentRef.value?.scrollTo(0, contentRef.value?.scrollHeight)
  66. }
  67. })
  68. /** 点击复制的时候复制内容 */
  69. const showCopy = computed(() => props.content && !props.isWriting) // 是否展示复制按钮,在生成内容完成的时候展示
  70. const copyContent = () => {
  71. copy(props.content)
  72. }
  73. // 复制成功的时候copied.value为true
  74. watch(copied, (val) => {
  75. if (val) {
  76. message.success('复制成功')
  77. }
  78. })
  79. </script>