index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <template>
  2. <div class="editor">
  3. <quill-editor
  4. class="ql-editor"
  5. v-model="forms.content"
  6. ref="myQuillEditor"
  7. :options="editorOption"
  8. @change="onEditorChange($event)"
  9. ></quill-editor>
  10. <el-upload
  11. class="ivu-upload"
  12. :show-file-list="false"
  13. :headers="headers"
  14. :on-success="handleSuccess"
  15. accept=".jpg, .jpeg, .png"
  16. :max-size="2048"
  17. multiple
  18. action="/api-web/uploadFile"
  19. >
  20. <Button icon="ios-cloud-upload-outline"></Button>
  21. </el-upload>
  22. <el-dialog title="插入视频" width="500px" :visible.sync="dialogFormVisible" append-to-body>
  23. <el-form :model="dialogForm" ref="diologForm" :rules="dialogFormRules">
  24. <el-form-item label="封面图地址" label-width="90px">
  25. <el-upload
  26. class="avatar-uploader"
  27. style="line-height: 0; display: inline-block"
  28. action="/api-web/uploadFile"
  29. :headers="headers"
  30. :show-file-list="false"
  31. v-loading="uploadImgLoading"
  32. accept=".jpg, .jpeg, .png"
  33. :on-success="handleImgSuccess"
  34. :on-error="handleUploadImgError"
  35. :before-upload="beforeImgUpload"
  36. >
  37. <img
  38. width="300px"
  39. v-if="dialogForm.poster"
  40. :src="dialogForm.poster"
  41. class="avatar"
  42. />
  43. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  44. </el-upload>
  45. </el-form-item>
  46. <el-form-item label="视频类型" label-width="90px">
  47. <el-radio-group v-model="formRadio">
  48. <el-radio :label="1">外部链接</el-radio>
  49. <el-radio :label="2">上传</el-radio>
  50. </el-radio-group>
  51. </el-form-item>
  52. <el-form-item
  53. v-if="formRadio == 1"
  54. label="视频地址"
  55. label-width="90px"
  56. prop="url"
  57. >
  58. <el-input
  59. v-model="dialogForm.url"
  60. style="width: 100%"
  61. autocomplete="off"
  62. ></el-input>
  63. </el-form-item>
  64. <el-form-item
  65. v-if="formRadio == 2"
  66. label="上传视频"
  67. label-width="90px"
  68. prop="videoUrl"
  69. >
  70. <el-upload
  71. class="upload-demo"
  72. style="display: inline-block"
  73. v-loading="uploadLoading"
  74. action="/api-web/uploadFile"
  75. :before-upload="beforeUpload"
  76. :on-success="handleUploadSuccess"
  77. :on-error="handleUploadError"
  78. :show-file-list="false"
  79. accept=".mp4"
  80. :file-list="fileList"
  81. :on-exceed="handleExceed"
  82. >
  83. <video
  84. style="width: 120px; height: 120px"
  85. v-if="dialogForm.videoUrl"
  86. type="video/mp4"
  87. preload="auto"
  88. :src="dialogForm.videoUrl"
  89. ></video>
  90. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  91. </el-upload>
  92. <p class="imageSize">只能上传mp4文件, 且不超过100M</p>
  93. </el-form-item>
  94. </el-form>
  95. <div slot="footer" class="dialog-footer">
  96. <el-button @click="dialogFormVisible = false">取 消</el-button>
  97. <el-button type="primary" @click="onVideoComfirm('diologForm')"
  98. >确 定</el-button
  99. >
  100. </div>
  101. </el-dialog>
  102. </div>
  103. </template>
  104. <script>
  105. import { getToken } from "@/utils/auth";
  106. import "quill/dist/quill.core.css";
  107. import "quill/dist/quill.snow.css";
  108. import "quill/dist/quill.bubble.css";
  109. import Quill from "quill";
  110. import { quillEditor } from "vue-quill-editor";
  111. // 工具栏配置
  112. const toolbarOptions = [
  113. ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
  114. ["blockquote", "code-block"], // 引用 代码块
  115. [{ header: 1 }, { header: 2 }], // 1、2 级标题
  116. [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
  117. [{ script: "sub" }, { script: "super" }], // 上标/下标
  118. [{ indent: "-1" }, { indent: "+1" }], // 缩进
  119. // [{'direction': 'rtl'}], // 文本方向
  120. [{ size: ["small", false, "large", "huge"] }], // 字体大小
  121. [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
  122. [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
  123. [{ font: [] }], // 字体种类
  124. [{ align: [] }], // 对齐方式
  125. ["clean"], // 清除文本格式
  126. ["image"], // 链接、图片、视频 , "video"
  127. // ["link", "image", "video"] // 链接、图片、视频
  128. ];
  129. // 标题
  130. const titleConfig = {
  131. "ql-bold": "加粗",
  132. "ql-color": "颜色",
  133. "ql-font": "字体",
  134. "ql-code": "插入代码",
  135. "ql-italic": "斜体",
  136. // 'ql-link': '添加链接',
  137. "ql-background": "背景颜色",
  138. "ql-size": "字体大小",
  139. "ql-strike": "删除线",
  140. "ql-script": "上标/下标",
  141. "ql-underline": "下划线",
  142. "ql-blockquote": "引用",
  143. "ql-header": "标题",
  144. "ql-indent": "缩进",
  145. "ql-list": "列表",
  146. "ql-align": "文本对齐",
  147. "ql-direction": "文本方向",
  148. "ql-code-block": "代码块",
  149. "ql-formula": "公式",
  150. "ql-image": "图片",
  151. "ql-video": "视频",
  152. "ql-clean": "清除字体样式",
  153. "ql-upload": "文件",
  154. };
  155. // 这里引入修改过的video模块并注册
  156. import Video from "@/views/quill/video.js";
  157. import dayjs from "dayjs";
  158. Quill.register(Video, true);
  159. let that;
  160. export default {
  161. props: ["form"],
  162. name: "editor",
  163. components: { quillEditor },
  164. data() {
  165. return {
  166. content: null,
  167. headers: {
  168. Authorization: getToken(),
  169. },
  170. dialogFormVisible: false,
  171. dialogForm: {
  172. poster: null,
  173. url: null,
  174. videoUrl: null,
  175. },
  176. uploadLoading: false,
  177. uploadImgLoading: false,
  178. fileList: [],
  179. dialogFormRules: {
  180. url: [{ required: true, message: "请输入视频地址", trigger: "blur" }],
  181. videoUrl: [{ required: true, message: "请上传视频", trigger: "blur" }],
  182. },
  183. formRadio: 1,
  184. editorOption: {
  185. placeholder: "请输入内容",
  186. modules: {
  187. toolbar: {
  188. container: toolbarOptions,
  189. handlers: {
  190. image: function (value) {
  191. if (value) {
  192. // 调用iview图片上传
  193. document.querySelector(".ivu-upload .el-upload").click();
  194. } else {
  195. this.quill.format("image", false);
  196. }
  197. },
  198. video: function (value) {
  199. if (value) {
  200. that.dialogFormVisible = true;
  201. let editor = that.$refs.myQuillEditor.quill;
  202. // 光标所在位置
  203. that.editorIndex = editor.getSelection().index;
  204. } else {
  205. this.quill.format("image", false);
  206. }
  207. },
  208. },
  209. },
  210. },
  211. },
  212. };
  213. },
  214. mounted(){
  215. console.log(this.form)
  216. that = this;
  217. },
  218. methods: {
  219. onEditorChange({ quill, html, text }) {
  220. this.form.content = html;
  221. },
  222. onVideoComfirm(formName) {
  223. this.$refs[formName].validate((valid) => {
  224. if (valid) {
  225. let dialogForm = this.dialogForm;
  226. // 获取富文本组件实例
  227. let quill = this.editor;
  228. // 插入图片,res为服务器返回的图片链接地址
  229. const params = {
  230. poster: dialogForm.poster,
  231. url: this.formRadio == 1 ? dialogForm.url : dialogForm.videoUrl,
  232. };
  233. quill.insertEmbed(this.editorIndex, "video", params);
  234. // 调整光标到最后
  235. quill.setSelection(this.editorIndex + 1, { preload: false });
  236. this.dialogFormVisible = false;
  237. this.dialogForm = {
  238. poster: null,
  239. url: null,
  240. videoUrl: null,
  241. };
  242. } else {
  243. return false;
  244. }
  245. });
  246. },
  247. handleSuccess(res) {
  248. // 获取富文本组件实例
  249. let quill = this.editor;
  250. // 如果上传成功
  251. if (res.code) {
  252. // 获取光标所在位置
  253. let length = quill.getSelection().index;
  254. // 插入图片,res为服务器返回的图片链接地址
  255. quill.insertEmbed(length, "image", res.data.url);
  256. // 调整光标到最后
  257. quill.setSelection(length + 1);
  258. } else {
  259. // 提示信息,需引入Message
  260. this.$message.error("图片插入失败");
  261. }
  262. },
  263. addQuillTitle() {
  264. const oToolBar = document.querySelector(".ql-toolbar"),
  265. aButton = oToolBar.querySelectorAll("button"),
  266. aSelect = oToolBar.querySelectorAll("select");
  267. aButton.forEach(function (item) {
  268. if (item.className === "ql-script") {
  269. item.value === "sub" ? (item.title = "下标") : (item.title = "上标");
  270. } else if (item.className === "ql-indent") {
  271. item.value === "+1"
  272. ? (item.title = "向右缩进")
  273. : (item.title = "向左缩进");
  274. } else {
  275. item.title = titleConfig[item.classList[0]];
  276. }
  277. });
  278. aSelect.forEach(function (item) {
  279. item.parentNode.title = titleConfig[item.classList[0]];
  280. });
  281. },
  282. handleUploadImgError(file) {
  283. this.uploadImgLoading = false;
  284. this.$message.error("上传失败");
  285. },
  286. handleImgSuccess(res, file) {
  287. this.uploadImgLoading = false;
  288. this.dialogForm.poster = res.data.url;
  289. },
  290. beforeImgUpload(file) {
  291. const imageType = {
  292. "image/png": true,
  293. "image/jpeg": true,
  294. };
  295. const isImage = imageType[file.type];
  296. const isLt2M = file.size / 1024 / 1024 < 2;
  297. isImage, isLt2M;
  298. if (!isImage) {
  299. this.$message.error("只能上传图片格式!");
  300. }
  301. if (!isLt2M) {
  302. this.$message.error("上传图片大小不能超过 2MB!");
  303. }
  304. if (isImage && isLt2M) {
  305. this.uploadImgLoading = true;
  306. }
  307. return isImage && isLt2M;
  308. },
  309. handleAvatarSuccess(res, file) {
  310. this.form.coverImage = res.data.url;
  311. },
  312. beforeAvatarUpload(file) {
  313. const imageType = {
  314. "image/png": true,
  315. "image/jpeg": true,
  316. };
  317. const isImage = imageType[file.type];
  318. const isLt2M = file.size / 1024 / 1024 < 2;
  319. if (!isImage) {
  320. this.$message.error("只能上传图片格式!");
  321. }
  322. if (!isLt2M) {
  323. this.$message.error("上传图片大小不能超过 2M!");
  324. }
  325. return isImage && isLt2M;
  326. },
  327. beforeUpload(file) {
  328. // const isJPG = file.type === 'image/jpeg';
  329. const isLt2M = file.size / 1024 / 1024 < 100;
  330. // if (!isJPG) {
  331. // this.$message.error('上传头像图片只能是 JPG 格式!');
  332. // }
  333. if (!isLt2M) {
  334. this.$message.error("上传视频大小不能超过 100MB!");
  335. }
  336. this.uploadLoading = true;
  337. return isLt2M;
  338. },
  339. handleUploadError(file) {
  340. this.uploadLoading = false;
  341. this.$message.error("上传视频失败");
  342. },
  343. handleUploadSuccess(file, fileList) {
  344. this.uploadLoading = false;
  345. this.$message.success("上传视频成功");
  346. this.dialogForm.videoUrl = file.data.url;
  347. },
  348. handleExceed(files, fileList) {
  349. this.$message.error("您已上传过视频");
  350. },
  351. },
  352. computed: {
  353. editor() {
  354. return this.$refs.myQuillEditor.quill;
  355. },
  356. forms() {
  357. return this.form;
  358. },
  359. },
  360. };
  361. </script>
  362. <style lang='scss' scoped>
  363. /deep/.ql-editor {
  364. min-height: 300px;
  365. padding: 0;
  366. }
  367. /deep/.ql-container .ql-editor {
  368. max-height: 500px;
  369. }
  370. </style>