RichTextEditor.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <template>
  2. <div class="rich-text-editor">
  3. <div class="editor-title">
  4. <div class="title">
  5. <span>
  6. {{ title ? title : $i18n.t("hotspot.edit_article_title") }}
  7. </span>
  8. </div>
  9. </div>
  10. <Toolbar
  11. v-if="isShowToolbar"
  12. style="border-bottom: 1px solid #ccc"
  13. :editor="editor"
  14. :defaultConfig="toolbarConfig"
  15. :mode="mode"
  16. />
  17. <Editor
  18. style="height: 500px; overflow-y: hidden"
  19. v-model="html"
  20. :defaultConfig="editorConfig"
  21. :mode="mode"
  22. class="editor"
  23. @change="onEditorChange"
  24. @onMaxLength="onEditorMaxLength"
  25. @onCreated="onEditorCreated"
  26. />
  27. <div class="bottom-bar">
  28. <button class="ui-button deepcancel" @click="onClickCancel">
  29. {{ $i18n.t("common.cancel") }}
  30. </button>
  31. <button class="ui-button submit" @click="onClickOk">
  32. {{ $i18n.t("common.ok") }}
  33. </button>
  34. </div>
  35. <div class="dialog" style="z-index: 2000" v-if="isShowImageSelectionWindow">
  36. <MaterialSelector
  37. :title="$i18n.t('gather.select_material')"
  38. @cancle="isShowImageSelectionWindow = false"
  39. @submit="onSubmitFromImageMaterialSelector"
  40. :selectableType="['image']"
  41. :initialMaterialType="'image'"
  42. :isMultiSelection="true"
  43. />
  44. </div>
  45. <div class="dialog" style="z-index: 2000" v-if="isShowVideoSelectionWindow">
  46. <MaterialSelector
  47. :title="$i18n.t('gather.select_material')"
  48. @cancle="isShowVideoSelectionWindow = false"
  49. @submit="onSubmitFromVideoMaterialSelector"
  50. :selectableType="['video']"
  51. :initialMaterialType="'video'"
  52. :isMultiSelection="true"
  53. />
  54. </div>
  55. </div>
  56. </template>
  57. <script>
  58. import Vue from "vue";
  59. import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
  60. import { i18nChangeLanguage, i18nGetResources } from "@wangeditor/editor";
  61. import browser from "@/utils/browser";
  62. import MaterialSelector from "@/components/materialSelector.vue";
  63. export default Vue.extend({
  64. components: {
  65. Editor,
  66. Toolbar,
  67. MaterialSelector,
  68. },
  69. props: {
  70. title: {
  71. type: String,
  72. default: "",
  73. },
  74. limit: {
  75. type: Number,
  76. default: 0,
  77. },
  78. placeholder: {
  79. type: String,
  80. default: "",
  81. },
  82. initialHtml: {
  83. type: String,
  84. default: "",
  85. },
  86. isShowToolbar: {
  87. type: Boolean,
  88. default: true,
  89. },
  90. },
  91. data() {
  92. return {
  93. editor: null,
  94. html: this.initialHtml,
  95. size: 0,
  96. toolbarConfig: {},
  97. editorConfig: {
  98. placeholder: this.placeholder,
  99. maxLength: this.limit > 0 ? this.limit : null,
  100. MENU_CONF: {
  101. uploadImage: {
  102. customBrowseAndUpload: (insertFn) => {
  103. this.isShowImageSelectionWindow = true;
  104. this.insertFn = insertFn;
  105. },
  106. },
  107. uploadVideo: {
  108. customBrowseAndUpload: (insertFn) => {
  109. this.isShowVideoSelectionWindow = true;
  110. this.insertFn = insertFn;
  111. },
  112. },
  113. },
  114. },
  115. mode: "default", // or 'simple',
  116. isShowImageSelectionWindow: false,
  117. isShowVideoSelectionWindow: false,
  118. insertFn: null,
  119. };
  120. },
  121. methods: {
  122. onEditorCreated(editor) {
  123. this.editor = Object.seal(editor); // 一定要用 Object.seal() ,否则会报错
  124. console.log("this.editor", this.editor);
  125. let lang = browser.urlQueryValue("lang");
  126. if (lang === "en") {
  127. i18nChangeLanguage("en");
  128. }
  129. if (lang === "zh") {
  130. i18nChangeLanguage("zh-CN");
  131. }
  132. },
  133. onClickOk() {
  134. this.$emit("ok", this.html);
  135. },
  136. onClickCancel() {
  137. this.$emit("cancel");
  138. },
  139. onSubmitFromImageMaterialSelector(selected) {
  140. this.isShowImageSelectionWindow = false;
  141. for (const selectedItem of selected) {
  142. this.insertFn(
  143. selectedItem.icon,
  144. `[${this.$i18n.t("gather.image")}: ${selectedItem.name}]`
  145. );
  146. }
  147. },
  148. onSubmitFromVideoMaterialSelector(selected) {
  149. this.isShowVideoSelectionWindow = false;
  150. for (const selectedItem of selected) {
  151. console.log(selectedItem);
  152. this.insertFn(
  153. selectedItem.ossPath,
  154. selectedItem.ossPath + this.$videoImgOriginalSize
  155. );
  156. }
  157. },
  158. onEditorChange(content) {
  159. console.log("content", content);
  160. this.size = content.size;
  161. },
  162. onEditorMaxLength() {
  163. console.log("超出大小");
  164. },
  165. },
  166. mounted() {},
  167. beforeDestroy() {
  168. const editor = this.editor;
  169. if (editor == null) return;
  170. editor.destroy(); // 组件销毁时,及时销毁编辑器
  171. },
  172. });
  173. </script>
  174. <style src="@wangeditor/editor/dist/css/style.css"></style>
  175. <style lang="less" scoped>
  176. .rich-text-editor {
  177. // border: 1px solid #ccc;
  178. .editor {
  179. border: 1px solid #545454;
  180. }
  181. .editor-title {
  182. height: 64px;
  183. .title {
  184. font-size: 18px;
  185. color: rgba(255, 255, 255, 0.6);
  186. line-height: 24px;
  187. }
  188. }
  189. border-radius: 4px;
  190. overflow: hidden;
  191. padding: 26px;
  192. background-color: rgba(26, 27, 29, 1);
  193. > .bottom-bar {
  194. padding: 10px 26px 0 26px;
  195. margin: 40px -26px 0 -26px;
  196. background-color: rgba(26, 27, 29, 1);
  197. // border-top: 1px solid #ccc;
  198. display: flex;
  199. justify-content: flex-end;
  200. align-items: center;
  201. > button {
  202. margin-left: 10px;
  203. }
  204. }
  205. }
  206. </style>
  207. <style>
  208. :root {
  209. --w-e-textarea-bg-color: #333;
  210. --w-e-textarea-color: #fff;
  211. --w-e-toolbar-active-bg-color: #666;
  212. --w-e-toolbar-color: #fff;
  213. --w-e-toolbar-bg-color: rgba(0, 0, 0, 0.9);
  214. --w-e-toolbar-active-color: #fff;
  215. --w-e-modal-button-bg-color: rgba(0, 0.5);
  216. }
  217. </style>