RichTextEditor.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <template>
  2. <div class="rich-text-editor-container">
  3. <Toolbar
  4. class="rich-text-toolbar"
  5. :editor="editorRef"
  6. :defaultConfig="toolbarConfig"
  7. mode="default"
  8. />
  9. <Editor
  10. class="rich-text-editor"
  11. :style="{ height: '300px', overflowY: 'hidden' }"
  12. v-model="localValue"
  13. :defaultConfig="editorConfig"
  14. mode="default"
  15. @onCreated="handleCreated"
  16. @customPaste="customPaste"
  17. />
  18. </div>
  19. </template>
  20. <script setup>
  21. import { ref, defineProps, defineEmits, watch, onMounted } from 'vue';
  22. import '@wangeditor/editor/dist/css/style.css';
  23. import { onBeforeUnmount, shallowRef } from 'vue';
  24. import { Editor, Toolbar } from '@wangeditor/editor-for-vue';
  25. import axios from 'axios';
  26. const props = defineProps({
  27. modelValue: String
  28. });
  29. const emits = defineEmits(['update:modelValue']);
  30. const localValue = ref(props.modelValue);
  31. const editorRef = shallowRef();
  32. const toolbarConfig = ref();
  33. const editorConfig = ref({ placeholder: '请输入内容...', MENU_CONF: {} });
  34. // 自定义图片上传
  35. editorConfig.value.MENU_CONF['uploadImage'] = {
  36. async customUpload(file, insertFn) {
  37. try {
  38. const formData = new FormData();
  39. formData.append('image', file);
  40. const response = await axios.post('https://soilgd.com:5000/upload-image', formData, {
  41. headers: {
  42. 'Content-Type': 'multipart/form-data'
  43. }
  44. });
  45. const imageUrl = response.data.imageUrl;
  46. insertFn(imageUrl, 'img');
  47. } catch (error) {
  48. console.error('图片上传失败:', error);
  49. }
  50. },
  51. };
  52. // 自定义视频上传
  53. editorConfig.value.MENU_CONF['uploadVideo'] = {
  54. async customUpload(file, insertFn) {
  55. try {
  56. const formData = new FormData();
  57. formData.append('video', file);
  58. const response = await axios.post('https://soilgd.com:5000/upload-video', formData, {
  59. headers: {
  60. 'Content-Type': 'multipart/form-data'
  61. }
  62. });
  63. const videoUrl = response.data.videoUrl;
  64. insertFn(videoUrl, 'video');
  65. } catch (error) {
  66. console.error('视频上传失败:', error);
  67. }
  68. },
  69. };
  70. const handleCreated = (editor) => {
  71. editorRef.value = editor;
  72. console.log(editorConfig.value.MENU_CONF, 'editorConfig.value');
  73. console.log('Editor created:', editor);
  74. };
  75. const customPaste = (editor, event, callback) => {
  76. const text = event.clipboardData.getData('text/plain');
  77. if (text) {
  78. console.log('Pasted text:', text);
  79. editor.insertText(text);
  80. event.preventDefault();
  81. callback(false);
  82. }
  83. };
  84. onBeforeUnmount(() => {
  85. const editor = editorRef.value;
  86. if (editor) {
  87. editor.destroy();
  88. }
  89. });
  90. const updateValue = () => {
  91. emits('update:modelValue', localValue.value);
  92. };
  93. // 监听 localValue 变化,更新父组件的值
  94. watch(localValue, (newValue, oldValue) => {
  95. if (newValue!== props.modelValue) {
  96. updateValue();
  97. }
  98. });
  99. onMounted(() => {
  100. const editorElement = document.querySelector('.rich-text-editor');
  101. if (editorElement) {
  102. editorElement.addEventListener('play', (event) => {
  103. if (event.target instanceof HTMLVideoElement) {
  104. const video = event.target;
  105. video.style.width = '80%';
  106. video.style.maxWidth = '80%';
  107. video.style.height = 'auto';
  108. video.style.objectFit = 'contain';
  109. }
  110. }, true);
  111. }
  112. });
  113. </script>
  114. <style scoped lang="scss">
  115. .rich-text-editor-container {
  116. width: 90%;
  117. border: 1px solid #e0e0e0;
  118. border-radius: 8px;
  119. box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  120. overflow: hidden;
  121. margin: 0 auto;
  122. /* 让容器在小屏幕下也能自适应 */
  123. @media (max-width: 768px) {
  124. width: 100%;
  125. }
  126. }
  127. .rich-text-toolbar {
  128. background-color: #f8f9fa;
  129. border-bottom: 1px solid #e0e0e0;
  130. padding: 5px 8px;
  131. display: flex;
  132. align-items: center;
  133. flex-wrap: wrap;
  134. /* 工具栏按钮样式 */
  135. .w-e-menu {
  136. margin-right: 5px;
  137. margin-bottom: 4px;
  138. button {
  139. color: #333;
  140. border: none;
  141. background: transparent;
  142. cursor: pointer;
  143. padding: 4px 6px;
  144. border-radius: 4px;
  145. transition: background-color 0.3s ease;
  146. &:hover {
  147. background-color: #e9ecef;
  148. }
  149. &:active {
  150. background-color: #dce0e4;
  151. }
  152. }
  153. /* 下拉菜单样式 */
  154. .w-e-select {
  155. color: #333;
  156. border: 1px solid #e0e0e0;
  157. background-color: white;
  158. border-radius: 4px;
  159. padding: 3px 6px;
  160. cursor: pointer;
  161. transition: border-color 0.3s ease;
  162. &:hover {
  163. border-color: #999;
  164. }
  165. }
  166. }
  167. }
  168. .rich-text-editor {
  169. padding: 12px;
  170. font-size: 16px;
  171. line-height: 1.6;
  172. color: #333;
  173. /* 编辑区域滚动条样式 */
  174. &::-webkit-scrollbar {
  175. width: 8px;
  176. }
  177. &::-webkit-scrollbar-thumb {
  178. background-color: #ccc;
  179. border-radius: 4px;
  180. }
  181. &::-webkit-scrollbar-track {
  182. background-color: #f1f1f1;
  183. }
  184. video {
  185. width: 80%;
  186. max-width: 80%;
  187. height: auto;
  188. object-fit: contain;
  189. }
  190. }
  191. </style>