WordEditor.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <!--
  2. Description: onlyoffice组件
  3. Author: BaiQi
  4. Created Date: 2024-6-21
  5. -->
  6. <template>
  7. <DocumentEditor
  8. id="docxEditor"
  9. documentServerUrl="http://139.196.252.184:8082/"
  10. :config="config"
  11. :events_onDocumentReady="onDocumentReady"
  12. :onLoadComponentError="onLoadComponentError"
  13. :events_onDocumentStateChange="onDocumentStateChange"
  14. v-if="props.documentKey && props.documentTitle && props.documentUrl"
  15. ></DocumentEditor>
  16. </template>
  17. <script lang="ts" setup>
  18. import "./WordEditor.module.scss"
  19. import {ref, watch, onMounted, defineProps, reactive} from 'vue';
  20. import { ElMessage } from 'element-plus';
  21. import { DocumentEditor } from '@onlyoffice/document-editor-vue'
  22. interface IProps {
  23. onValueChange?: (state: boolean) => void;
  24. documentKey: string;
  25. documentTitle: string;
  26. documentUrl: string;
  27. userId: number;
  28. username: string;
  29. }
  30. const props = defineProps<IProps>()
  31. // const documentServerUrl = 'http://139.196.252.184:8082/';
  32. // OnlyOffice编辑器配置
  33. let config = reactive({
  34. width: '100%',
  35. height: '100%',
  36. type: 'desktop',
  37. documentType: 'word',
  38. document: {
  39. fileType: 'docx',
  40. key: props.documentKey,
  41. title: props.documentTitle,
  42. url: props.documentUrl,
  43. permissions: {
  44. chat: false,
  45. comment: false,
  46. copy: true,
  47. deleteCommentAuthorOnly: false,
  48. download: false,
  49. edit: true,
  50. editCommentAuthorOnly: false,
  51. fillForms: false,
  52. modifyContentControl: false,
  53. print: false,
  54. protect: false,
  55. review: false,
  56. },
  57. },
  58. editorConfig: {
  59. lang: 'zh-CN',
  60. //不要用localhost和127.0.0.1访问,用局域网IP访问
  61. callbackUrl: 'http://139.196.252.184:8070/api/onlyoffice/callback',
  62. customization: {
  63. autosave: false,
  64. comments: false,
  65. compactHeader: true,
  66. compactToolbar: false,
  67. compatibleFeatures: false,
  68. forceSave: true,
  69. help: false,
  70. hideRightMenu: true,
  71. hideRulers: true,
  72. macros: false,
  73. mentionShare: false,
  74. mobileForceView: false,
  75. plugins: false,
  76. toolbarHideFileName: true,
  77. toolbarNoTabs: false,
  78. uiTheme: 'theme-light',
  79. },
  80. user: {
  81. id: props.userId.toString(),
  82. name: props.username,
  83. },
  84. },
  85. });
  86. // 监听 props 中的变化,更新 config
  87. watch(
  88. () => [props.documentKey, props.documentTitle, props.documentUrl, props.userId, props.username],
  89. () => {
  90. config = {
  91. ...config,
  92. document: {
  93. ...config.document,
  94. key: props.documentKey,
  95. title: props.documentTitle,
  96. url: props.documentUrl,
  97. },
  98. editorConfig: {
  99. ...config.editorConfig,
  100. user: {
  101. id: props.userId.toString(),
  102. name: props.username,
  103. },
  104. },
  105. };
  106. }
  107. );
  108. // 监听 OnlyOffice组件中的文档准备事件
  109. const onDocumentReady = (event: any) => {
  110. console.log('Document is loaded');
  111. console.log(props.documentUrl);
  112. };
  113. // 监听 OnlyOffice组件中的加载组件错误事件
  114. const onLoadComponentError = (errorCode: number, errorDescription: string) => {
  115. switch (errorCode) {
  116. case -1:// Unknown error loading component
  117. console.log(errorDescription);
  118. break;
  119. case -2:// Error load DocsAPI from http://documentserver/
  120. console.log(errorDescription);
  121. break;
  122. case -3:// DocsAPI is not defined
  123. console.log(errorDescription);
  124. break;
  125. }
  126. };
  127. // 监听 OnlyOffice组件中的文档状态变化事件
  128. const onDocumentStateChange = (event: any) => {
  129. console.log("监听 OnlyOffice组件中的文档状态变化事件", event.data)
  130. props.onValueChange?.(event.data);
  131. };
  132. </script>
  133. <script lang="ts">
  134. export default {
  135. name: "WordEditor"
  136. }
  137. </script>