index.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <div>
  3. <el-upload
  4. :action="uploadUrl"
  5. :on-success="handleUploadSuccess"
  6. :before-upload="handleBeforeUpload"
  7. :on-error="handleUploadError"
  8. name="file"
  9. :show-file-list="false"
  10. :headers="headers"
  11. style="display: none;"
  12. ref='upload'
  13. v-if='this.uploadUrl'
  14. >
  15. </el-upload>
  16. <div class="editor" ref="editor" :style="styles"></div>
  17. </div>
  18. </template>
  19. <script>
  20. import Quill from "quill";
  21. import "quill/dist/quill.core.css";
  22. import "quill/dist/quill.snow.css";
  23. import "quill/dist/quill.bubble.css";
  24. export default {
  25. name: "Editor",
  26. props: {
  27. /* 编辑器的内容 */
  28. value: {
  29. type: String,
  30. default: "",
  31. },
  32. /* 高度 */
  33. height: {
  34. type: Number,
  35. default: null,
  36. },
  37. /* 最小高度 */
  38. minHeight: {
  39. type: Number,
  40. default: null,
  41. },
  42. /* 只读 */
  43. readOnly: {
  44. type: Boolean,
  45. default: false,
  46. },
  47. /* 上传地址 */
  48. uploadUrl: {
  49. type: String,
  50. default: '',
  51. }
  52. },
  53. data() {
  54. return {
  55. Quill: null,
  56. currentValue: "",
  57. options: {
  58. theme: "snow",
  59. bounds: document.body,
  60. debug: "warn",
  61. modules: {
  62. // 工具栏配置
  63. toolbar: [
  64. ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
  65. ["blockquote", "code-block"], // 引用 代码块
  66. [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
  67. [{ indent: "-1" }, { indent: "+1" }], // 缩进
  68. [{ size: ["small", false, "large", "huge"] }], // 字体大小
  69. [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
  70. [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
  71. [{ align: [] }], // 对齐方式
  72. ["clean"], // 清除文本格式
  73. ["link", "image", "video"] // 链接、图片、视频
  74. ],
  75. },
  76. placeholder: "请输入内容",
  77. readOnly: this.readOnly,
  78. },
  79. };
  80. },
  81. computed: {
  82. styles() {
  83. let style = {};
  84. if (this.minHeight) {
  85. style.minHeight = `${this.minHeight}px`;
  86. }
  87. if (this.height) {
  88. style.height = `${this.height}px`;
  89. }
  90. return style;
  91. },
  92. },
  93. watch: {
  94. value: {
  95. handler(val) {
  96. if (val !== this.currentValue) {
  97. this.currentValue = val === null ? "" : val;
  98. if (this.Quill) {
  99. this.Quill.pasteHTML(this.currentValue);
  100. }
  101. }
  102. },
  103. immediate: true,
  104. },
  105. },
  106. mounted() {
  107. this.init();
  108. },
  109. beforeDestroy() {
  110. this.Quill = null;
  111. },
  112. methods: {
  113. init() {
  114. const editor = this.$refs.editor;
  115. this.Quill = new Quill(editor, this.options);
  116. // 如果设置了上传地址则自定义图片和视频的上传事件
  117. if (this.uploadUrl) {
  118. let toolbar = this.Quill.getModule('toolbar');
  119. toolbar.addHandler('image', (value) => {
  120. this.uploadType = 'image';
  121. if (value) {
  122. this.$refs.upload.$children[0].$refs.input.click();
  123. } else {
  124. this.quill.format('image', false);
  125. }
  126. });
  127. toolbar.addHandler('video', (value) => {
  128. this.uploadType = 'video';
  129. if (value) {
  130. this.$refs.upload.$children[0].$refs.input.click();
  131. } else {
  132. this.quill.format('video', false);
  133. }
  134. });
  135. }
  136. this.Quill.pasteHTML(this.currentValue);
  137. this.Quill.on("text-change", (delta, oldDelta, source) => {
  138. const html = this.$refs.editor.children[0].innerHTML;
  139. const text = this.Quill.getText();
  140. const quill = this.Quill;
  141. this.currentValue = html;
  142. this.$emit("input", html);
  143. this.$emit("on-change", { html, text, quill });
  144. });
  145. this.Quill.on("text-change", (delta, oldDelta, source) => {
  146. this.$emit("on-text-change", delta, oldDelta, source);
  147. });
  148. this.Quill.on("selection-change", (range, oldRange, source) => {
  149. this.$emit("on-selection-change", range, oldRange, source);
  150. });
  151. this.Quill.on("editor-change", (eventName, ...args) => {
  152. this.$emit("on-editor-change", eventName, ...args);
  153. });
  154. },
  155. },
  156. };
  157. </script>
  158. <style>
  159. .editor, .ql-toolbar {
  160. white-space: pre-wrap!important;
  161. line-height: normal !important;
  162. }
  163. .quill-img {
  164. display: none;
  165. }
  166. .ql-snow .ql-tooltip[data-mode="link"]::before {
  167. content: "请输入链接地址:";
  168. }
  169. .ql-snow .ql-tooltip.ql-editing a.ql-action::after {
  170. border-right: 0px;
  171. content: "保存";
  172. padding-right: 0px;
  173. }
  174. .ql-snow .ql-tooltip[data-mode="video"]::before {
  175. content: "请输入视频地址:";
  176. }
  177. .ql-snow .ql-picker.ql-size .ql-picker-label::before,
  178. .ql-snow .ql-picker.ql-size .ql-picker-item::before {
  179. content: "14px";
  180. }
  181. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="small"]::before,
  182. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="small"]::before {
  183. content: "10px";
  184. }
  185. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="large"]::before,
  186. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="large"]::before {
  187. content: "18px";
  188. }
  189. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="huge"]::before,
  190. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="huge"]::before {
  191. content: "32px";
  192. }
  193. .ql-snow .ql-picker.ql-header .ql-picker-label::before,
  194. .ql-snow .ql-picker.ql-header .ql-picker-item::before {
  195. content: "文本";
  196. }
  197. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="1"]::before,
  198. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="1"]::before {
  199. content: "标题1";
  200. }
  201. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="2"]::before,
  202. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="2"]::before {
  203. content: "标题2";
  204. }
  205. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="3"]::before,
  206. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="3"]::before {
  207. content: "标题3";
  208. }
  209. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="4"]::before,
  210. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="4"]::before {
  211. content: "标题4";
  212. }
  213. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="5"]::before,
  214. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="5"]::before {
  215. content: "标题5";
  216. }
  217. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="6"]::before,
  218. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before {
  219. content: "标题6";
  220. }
  221. .ql-snow .ql-picker.ql-font .ql-picker-label::before,
  222. .ql-snow .ql-picker.ql-font .ql-picker-item::before {
  223. content: "标准字体";
  224. }
  225. .ql-snow .ql-picker.ql-font .ql-picker-label[data-value="serif"]::before,
  226. .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="serif"]::before {
  227. content: "衬线字体";
  228. }
  229. .ql-snow .ql-picker.ql-font .ql-picker-label[data-value="monospace"]::before,
  230. .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="monospace"]::before {
  231. content: "等宽字体";
  232. }
  233. </style>