index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. //TODO placeholder: "请输入内容", 此处翻译暂时先记录
  52. placeholder: "",
  53. readOnly: false,
  54. },
  55. };
  56. },
  57. computed: {
  58. styles() {
  59. let style = {};
  60. if (this.minHeight) {
  61. style.minHeight = `${this.minHeight}px`;
  62. }
  63. if (this.height) {
  64. style.height = `${this.height}px`;
  65. }
  66. return style;
  67. },
  68. },
  69. watch: {
  70. value: {
  71. handler(val) {
  72. if (val !== this.currentValue) {
  73. this.currentValue = val === null ? "" : val;
  74. if (this.Quill) {
  75. this.Quill.pasteHTML(this.currentValue);
  76. }
  77. }
  78. },
  79. immediate: true,
  80. },
  81. },
  82. mounted() {
  83. this.init();
  84. },
  85. beforeDestroy() {
  86. this.Quill = null;
  87. },
  88. methods: {
  89. init() {
  90. const editor = this.$refs.editor;
  91. this.Quill = new Quill(editor, this.options);
  92. this.Quill.pasteHTML(this.currentValue);
  93. this.Quill.on("text-change", (delta, oldDelta, source) => {
  94. const html = this.$refs.editor.children[0].innerHTML;
  95. const text = this.Quill.getText();
  96. const quill = this.Quill;
  97. this.currentValue = html;
  98. this.$emit("input", html);
  99. this.$emit("on-change", { html, text, quill });
  100. });
  101. this.Quill.on("text-change", (delta, oldDelta, source) => {
  102. this.$emit("on-text-change", delta, oldDelta, source);
  103. });
  104. this.Quill.on("selection-change", (range, oldRange, source) => {
  105. this.$emit("on-selection-change", range, oldRange, source);
  106. });
  107. this.Quill.on("editor-change", (eventName, ...args) => {
  108. this.$emit("on-editor-change", eventName, ...args);
  109. });
  110. },
  111. },
  112. };
  113. </script>
  114. <style>
  115. .editor, .ql-toolbar {
  116. white-space: pre-wrap!important;
  117. line-height: normal !important;
  118. }
  119. .quill-img {
  120. display: none;
  121. }
  122. .ql-snow .ql-tooltip[data-mode="link"]::before {
  123. content: "请输入链接地址:";
  124. }
  125. .ql-snow .ql-tooltip.ql-editing a.ql-action::after {
  126. border-right: 0px;
  127. content: "保存";
  128. padding-right: 0px;
  129. }
  130. .ql-snow .ql-tooltip[data-mode="video"]::before {
  131. content: "请输入视频地址:";
  132. }
  133. .ql-snow .ql-picker.ql-size .ql-picker-label::before,
  134. .ql-snow .ql-picker.ql-size .ql-picker-item::before {
  135. content: "14px";
  136. }
  137. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="small"]::before,
  138. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="small"]::before {
  139. content: "10px";
  140. }
  141. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="large"]::before,
  142. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="large"]::before {
  143. content: "18px";
  144. }
  145. .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="huge"]::before,
  146. .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="huge"]::before {
  147. content: "32px";
  148. }
  149. .ql-snow .ql-picker.ql-header .ql-picker-label::before,
  150. .ql-snow .ql-picker.ql-header .ql-picker-item::before {
  151. content: "文本";
  152. }
  153. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="1"]::before,
  154. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="1"]::before {
  155. content: "标题1";
  156. }
  157. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="2"]::before,
  158. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="2"]::before {
  159. content: "标题2";
  160. }
  161. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="3"]::before,
  162. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="3"]::before {
  163. content: "标题3";
  164. }
  165. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="4"]::before,
  166. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="4"]::before {
  167. content: "标题4";
  168. }
  169. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="5"]::before,
  170. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="5"]::before {
  171. content: "标题5";
  172. }
  173. .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="6"]::before,
  174. .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before {
  175. content: "标题6";
  176. }
  177. .ql-snow .ql-picker.ql-font .ql-picker-label::before,
  178. .ql-snow .ql-picker.ql-font .ql-picker-item::before {
  179. content: "标准字体";
  180. }
  181. .ql-snow .ql-picker.ql-font .ql-picker-label[data-value="serif"]::before,
  182. .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="serif"]::before {
  183. content: "衬线字体";
  184. }
  185. .ql-snow .ql-picker.ql-font .ql-picker-label[data-value="monospace"]::before,
  186. .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="monospace"]::before {
  187. content: "等宽字体";
  188. }
  189. </style>