index.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import axios from 'axios'
  2. import dayjs from 'dayjs'
  3. import { NUpload, useMessage, NModal } from 'naive-ui'
  4. import { defineComponent, reactive, ref, watch } from 'vue'
  5. import { VueUeditorWrap } from 'vue-ueditor-wrap'
  6. import { policy } from '../upload-file/api'
  7. import path from 'path'
  8. declare const window: Window & { UE: any }
  9. export default defineComponent({
  10. name: 'u-editor',
  11. props: {
  12. isDisable: {
  13. type: Boolean,
  14. default: false
  15. },
  16. bucketName: {
  17. type: String,
  18. default: () => 'gyt'
  19. },
  20. path: {
  21. type: String,
  22. default: ''
  23. },
  24. modelValue: {
  25. type: [String, Number],
  26. default: ''
  27. }
  28. },
  29. emits: ['submit', 'update:modelValue'],
  30. setup(props, ctx) {
  31. const value = ref()
  32. const visiable = ref(false)
  33. const state = reactive({
  34. value: null as any,
  35. vueUeditor: null,
  36. ossUploadUrl: `https://${props.bucketName}.ks3-cn-beijing.ksyuncs.com/`,
  37. accept: '*'
  38. })
  39. const message = useMessage()
  40. const searchForm = reactive({
  41. fileList: [] as any,
  42. coverImg: ''
  43. })
  44. const UpLoadStates = reactive({
  45. policy: '',
  46. signature: '',
  47. key: '',
  48. KSSAccessKeyId: '',
  49. acl: 'public-read',
  50. name: ''
  51. }) as any
  52. const uploadRef = ref()
  53. const editorConfig = {
  54. UEDITOR_HOME_URL: './UEditor/', // 访问 UEditor 静态资源的根路径,可参考常见问题1
  55. // serverUrl: '/api-admin/uploadFile', // 服务端接口(这个地址是我为了方便各位体验文件上传功能搭建的临时接口,请勿在生产环境使用!!!)
  56. // uploadUrl: '/api-admin/uploadFile',
  57. autoHeightEnabled: false,
  58. // 初始容器高度
  59. initialFrameHeight: 400,
  60. // 初始容器宽度
  61. initialFrameWidth: '100%'
  62. }
  63. const ready = (editor: any) => {
  64. console.log('ready', editor)
  65. if (props.isDisable) {
  66. editor.setDisabled()
  67. } else {
  68. editor.setEnabled()
  69. }
  70. }
  71. const addCustimButtom = (e: any) => {
  72. window.UE.registerUI(
  73. 'preview',
  74. function (editor: any, uiName: String) {
  75. // 注册按钮执行时的 command 命令,使用命令默认就会带有回退操作
  76. editor.registerCommand(uiName, {
  77. execCommand: function () {
  78. editor.execCommand('inserthtml', ``)
  79. }
  80. })
  81. var btn = new window.UE.ui.Button({
  82. name: 'preview',
  83. title: '预览',
  84. cssRules: `background-position: -420px -18px;`,
  85. onclick: function () {
  86. // 渲染dialog
  87. // that.dialogVisible = true
  88. visiable.value = true
  89. editor.execCommand(uiName)
  90. }
  91. })
  92. return btn
  93. },
  94. 99
  95. )
  96. }
  97. watch(
  98. () => value.value,
  99. (val) => {
  100. ctx.emit('update:modelValue', val)
  101. },
  102. {
  103. deep: true
  104. // immediate: true // 必须要深度监听,否则初始值赋不进去
  105. }
  106. )
  107. watch(
  108. () => props.modelValue,
  109. (v1) => {
  110. value.value = v1
  111. },
  112. {
  113. deep: true,
  114. immediate: true // 必须要深度监听,否则初始值赋不进去
  115. }
  116. )
  117. return () => (
  118. <>
  119. <VueUeditorWrap
  120. style={{ with: '100%' }}
  121. v-model={value.value}
  122. config={editorConfig}
  123. ref={state.vueUeditor}
  124. editorId={'editor-demo-01' + new Date().getTime()}
  125. ready={ready}
  126. onBeforeInit={addCustimButtom}
  127. />
  128. <NModal
  129. v-model:show={visiable.value}
  130. preset="dialog"
  131. showIcon={false}
  132. title="预览"
  133. style={{ width: '400px' }}
  134. >
  135. {/* @cropper-no="error" @cropper-ok="success" */}
  136. <div v-html={value.value}></div>
  137. </NModal>
  138. </>
  139. )
  140. }
  141. })