index.vue 6.0 KB

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