index.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <template>
  2. <div ref="body">
  3. <el-upload :action="uploadUrl" :before-upload="handleBeforeUpload" :on-success="handleUploadSuccess"
  4. :on-error="handleUploadError" :data="data" name="file" :show-file-list="false" :headers="headers"
  5. style="display: none" ref="upload" v-if="type == 'url'">
  6. </el-upload>
  7. <div class="editor" ref="editor" :style="styles" />
  8. <!-- 视频弹出框 -->
  9. <el-popover popper-class="ql-popover" width="398" ref="popover">
  10. <el-form>
  11. <el-form-item label="视频链接">
  12. <el-input v-model="videoUrl" placeholder="请输入视频链接">
  13. <template slot="append">
  14. <Upload @upload="upload" :size="500" />
  15. </template>
  16. </el-input>
  17. </el-form-item>
  18. <el-form-item style="margin-top: 15px;">
  19. <el-button @click="closeVideo">取消</el-button>
  20. <el-button type="primary" @click="handleUploadSuccess()" :disabled="videoUrl == ''">确定</el-button>
  21. </el-form-item>
  22. </el-form>
  23. </el-popover>
  24. </div>
  25. </template>
  26. <script>
  27. import Quill from "quill";
  28. import "quill/dist/quill.core.css";
  29. import "quill/dist/quill.snow.css";
  30. import "quill/dist/quill.bubble.css";
  31. import Upload from '../Upload/index.vue';
  32. import { getToken } from "@/utils/auth";
  33. import { View } from "@/store/modules/editor";
  34. Quill.register(View, true);
  35. export default {
  36. components: { Upload },
  37. name: "Editor",
  38. props: {
  39. /* 编辑器的内容 */
  40. value: {
  41. type: String,
  42. default: "",
  43. },
  44. /* 高度 */
  45. height: {
  46. type: Number,
  47. default: null,
  48. },
  49. /* 最小高度 */
  50. minHeight: {
  51. type: Number,
  52. default: null,
  53. },
  54. /* 只读 */
  55. readOnly: {
  56. type: Boolean,
  57. default: false,
  58. },
  59. /* 类型(base64格式、url格式) */
  60. type: {
  61. type: String,
  62. default: "url",
  63. }
  64. },
  65. data() {
  66. return {
  67. // 遮罩层
  68. fullScreenLoading: false,
  69. loading: false,
  70. uploadUrl: process.env.VUE_APP_BASE_API + "/system/file/picture/upload", // 上传的图片服务器地址
  71. headers: {
  72. Authorization: "Bearer " + getToken()
  73. },
  74. data: {},
  75. audioData: {},
  76. Quill: null,
  77. currentValue: "",
  78. options: {
  79. theme: "snow",
  80. bounds: document.body,
  81. debug: "warn",
  82. modules: {
  83. // 工具栏配置
  84. toolbar: {
  85. container: [
  86. ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
  87. ["blockquote", "code-block"], // 引用 代码块
  88. [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
  89. // [{ indent: "-1" }, { indent: "+1" }], // 缩进
  90. [{ size: ["small", false, "large", "huge"] }], // 字体大小
  91. [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
  92. [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
  93. [{ align: [] }], // 对齐方式
  94. ["clean"], // 清除文本格式
  95. ["link", "image", "view"], // "audio" // 链接、图片、视频
  96. ]
  97. }
  98. },
  99. // 自定义按钮
  100. customButton: function (className, icon) {
  101. const btn = document.querySelector(className)
  102. btn.classList.add(icon)
  103. btn.style.cssText = 'font-size: 16px; font-weight: bold'
  104. },
  105. placeholder: "请输入内容",
  106. readOnly: this.readOnly,
  107. },
  108. // 视频表单
  109. videoUrl: ''
  110. };
  111. },
  112. computed: {
  113. styles() {
  114. let style = {};
  115. if (this.minHeight) {
  116. style.minHeight = `${this.minHeight}px`;
  117. }
  118. if (this.height) {
  119. style.height = `${this.height}px`;
  120. }
  121. return style;
  122. },
  123. },
  124. watch: {
  125. value: {
  126. handler(val) {
  127. if (val !== this.currentValue) {
  128. this.currentValue = val === null ? "" : val;
  129. if (this.Quill) {
  130. this.Quill.pasteHTML(this.currentValue);
  131. }
  132. }
  133. },
  134. immediate: true,
  135. }
  136. },
  137. mounted() {
  138. this.init();
  139. },
  140. beforeDestroy() {
  141. this.Quill = null;
  142. },
  143. methods: {
  144. init() {
  145. const editor = this.$refs.editor;
  146. this.Quill = new Quill(editor, this.options);
  147. // 自定义按钮
  148. this.customEditor('image')
  149. this.customEditor('view')
  150. this.options.customButton('.ql-view', 'el-icon-video-camera')
  151. this.Quill.pasteHTML(this.currentValue);
  152. this.Quill.on("text-change", (delta, oldDelta, source) => {
  153. const html = this.$refs.editor.children[0].innerHTML;
  154. const text = this.Quill.getText();
  155. const quill = this.Quill;
  156. this.currentValue = html;
  157. this.$emit("input", html);
  158. this.$emit("on-change", { html, text, quill });
  159. });
  160. this.Quill.on("text-change", (delta, oldDelta, source) => {
  161. this.$emit("on-text-change", delta, oldDelta, source);
  162. });
  163. this.Quill.on("selection-change", (range, oldRange, source) => {
  164. this.$emit("on-selection-change", range, oldRange, source);
  165. });
  166. this.Quill.on("editor-change", (eventName, ...args) => {
  167. this.$emit("on-editor-change", eventName, ...args);
  168. });
  169. },
  170. // 自定义按钮
  171. customEditor(key) {
  172. let toolbar = this.Quill.getModule("toolbar");
  173. toolbar.addHandler(key, (value) => {
  174. if (value) {
  175. if (key === 'image') { this.$refs.upload.$children[0].$refs.input.click() }
  176. if (key === 'view') { this.$refs.popover.showPopper = true }
  177. } else {
  178. this.quill.format(key, false);
  179. }
  180. });
  181. },
  182. // 上传视频
  183. upload(e) {
  184. this.videoUrl = e.file
  185. },
  186. // 上传图片之前
  187. handleBeforeUpload(file) {
  188. this.data.multipartFile = file
  189. },
  190. // 图片上传失败
  191. handleUploadError() {
  192. this.$message.error("图片插入失败");
  193. },
  194. // 成功添加
  195. handleUploadSuccess(res) {
  196. // 获取富文本组件实例
  197. let quill = this.Quill;
  198. // 如果上传成功
  199. if (quill.getSelection()) {
  200. let length = quill.getSelection().index
  201. let key = res ? "image" : "view"
  202. let form = res ? res.data : this.videoUrl
  203. quill.insertEmbed(length, key, form)
  204. quill.setSelection(length + 1)
  205. this.closeVideo()
  206. } else {
  207. this.$message.error("请先聚焦于富文本输入框,再插入视频")
  208. }
  209. },
  210. // 关闭视频弹框
  211. closeVideo() {
  212. this.$refs.popover.showPopper = false
  213. this.videoUrl = ''
  214. },
  215. },
  216. };
  217. </script>
  218. <style lang="scss">
  219. .editor,
  220. .ql-toolbar {
  221. white-space: pre-wrap !important;
  222. line-height: normal !important;
  223. }
  224. .quill-img {
  225. display: none;
  226. }
  227. .ql-snow .ql-tooltip[data-mode='link']::before {
  228. content: '请输入链接地址:';
  229. }
  230. .ql-snow .ql-tooltip.ql-editing a.ql-action::after {
  231. border-right: 0px;
  232. content: '保存';
  233. padding-right: 0px;
  234. }
  235. .ql-snow .ql-tooltip[data-mode='video']::before {
  236. content: '请输入视频地址:';
  237. }
  238. .ql-snow .ql-picker.ql-size .ql-picker-label::before,
  239. .ql-snow .ql-picker.ql-size .ql-picker-item::before {
  240. content: '14px';
  241. }
  242. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value='small']::before,
  243. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value='small']::before {
  244. content: '10px';
  245. }
  246. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value='large']::before,
  247. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value='large']::before {
  248. content: '18px';
  249. }
  250. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value='huge']::before,
  251. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value='huge']::before {
  252. content: '32px';
  253. }
  254. .ql-snow .ql-picker.ql-header .ql-picker-label::before,
  255. .ql-snow .ql-picker.ql-header .ql-picker-item::before {
  256. content: '文本';
  257. }
  258. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value='1']::before,
  259. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value='1']::before {
  260. content: '标题1';
  261. }
  262. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value='2']::before,
  263. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value='2']::before {
  264. content: '标题2';
  265. }
  266. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value='3']::before,
  267. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value='3']::before {
  268. content: '标题3';
  269. }
  270. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value='4']::before,
  271. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value='4']::before {
  272. content: '标题4';
  273. }
  274. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value='5']::before,
  275. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value='5']::before {
  276. content: '标题5';
  277. }
  278. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value='6']::before,
  279. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value='6']::before {
  280. content: '标题6';
  281. }
  282. .ql-snow .ql-picker.ql-font .ql-picker-label::before,
  283. .ql-snow .ql-picker.ql-font .ql-picker-item::before {
  284. content: '标准字体';
  285. }
  286. .ql-snow .ql-picker.ql-font .ql-picker-label[data-value='serif']::before,
  287. .ql-snow .ql-picker.ql-font .ql-picker-item[data-value='serif']::before {
  288. content: '衬线字体';
  289. }
  290. .ql-snow .ql-picker.ql-font .ql-picker-label[data-value='monospace']::before,
  291. .ql-snow .ql-picker.ql-font .ql-picker-item[data-value='monospace']::before {
  292. content: '等宽字体';
  293. }
  294. .ql-popover {
  295. top: 66px;
  296. left: 300px;
  297. .el-input-group__append,
  298. .el-input-group__prepend {
  299. color: #FFFFFF;
  300. background-color: #1890ff;
  301. border-color: #1890ff;
  302. }
  303. }
  304. </style>