RichTextEditor.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <FlexCol>
  3. <view class="richtext-preview-box" @click="() => !disabled && !readonly ? edit() : preview()">
  4. <Parse v-if="modelValue" :content="modelValue" containerStyle="max-height:400px" />
  5. <Text v-else color="text.second">{{placeholder}}</Text>
  6. </view>
  7. <FlexRow :gap="20" align="center">
  8. <Button v-if="!disabled" icon="browse" text="预览" size="small" @click="preview" />
  9. <Button v-if="!disabled && !readonly" icon="edit" text="编辑" size="small" @click="edit" type="primary" />
  10. <Text v-if="maxLength > 0">{{ modelValue?.length || 0 }}/{{ maxLength }} 字</Text>
  11. </FlexRow>
  12. </FlexCol>
  13. </template>
  14. <script setup lang="ts">
  15. import { onPageShow } from '@dcloudio/uni-app';
  16. import { navTo } from '@/components/utils/PageAction';
  17. import Parse from '@/components/display/parse/Parse.vue';
  18. import Button from '@/components/basic/Button.vue';
  19. import Text from '@/components/basic/Text.vue';
  20. import FlexCol from '@/components/layout/FlexCol.vue';
  21. import FlexRow from '@/components/layout/FlexRow.vue';
  22. const props = defineProps({
  23. modelValue: {
  24. type: String,
  25. default: null
  26. },
  27. maxLength: {
  28. type: Number,
  29. default: -1,
  30. },
  31. placeholder: {
  32. type: String,
  33. default: '未编写内容,点击编写',
  34. },
  35. disabled: {
  36. type: Boolean,
  37. default: false,
  38. },
  39. readonly: {
  40. type: Boolean,
  41. default: false,
  42. },
  43. })
  44. const emit = defineEmits(['update:modelValue'])
  45. let editorOpened = false;
  46. function preview() {
  47. uni.setStorage({
  48. key: 'editorContent',
  49. data: props.modelValue,
  50. success: () => navTo('/pages/editor/preview'),
  51. })
  52. }
  53. function edit() {
  54. editorOpened = true;
  55. uni.setStorage({
  56. key: 'editorMaxLength',
  57. data: props.maxLength,
  58. })
  59. uni.setStorage({
  60. key: 'editorContent',
  61. data: props.modelValue,
  62. success: () => navTo('/pages/editor/editor'),
  63. })
  64. }
  65. onPageShow(() => {
  66. if (editorOpened) {
  67. editorOpened = false;
  68. uni.getStorage({
  69. key: 'editorContent',
  70. success: (success) => emit('update:modelValue', success.data),
  71. })
  72. }
  73. })
  74. </script>
  75. <style lang="scss" scoped>
  76. .richtext-preview-box {
  77. flex: 1;
  78. min-height: 400rpx;
  79. margin-bottom: 15rpx;
  80. }
  81. </style>