index.vue 5.9 KB

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