VEditor.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <div class="v-editor">
  3. <VMenu class="v-editor-head" @audio-added="emitJSON" />
  4. <div
  5. :id="'ved' + _uid"
  6. ref="editor"
  7. class="v-editor-body"
  8. :data-placeholder="placeholder"
  9. contenteditable
  10. :style="styles"
  11. @input="emitJSON"
  12. ></div>
  13. </div>
  14. </template>
  15. <script>
  16. import VMenu from "./components/VMenu.vue";
  17. import { renderRichText } from "./renderJSON";
  18. import { toJSON } from "./toJSON";
  19. import {
  20. IMAGE_EXCEED_SIZE_AS_ATTACHMENT,
  21. JSON_EXCEED_SIZE_AS_ATTACHMENT,
  22. MAX_AUDIO_SIZE,
  23. MAX_IMAGE_SIZE,
  24. MAX_JSON_SIZE,
  25. } from "./constants";
  26. import { pasteHandle } from "./clipboard";
  27. import { answerPointDragHandle } from "./components/answerPoint";
  28. import timeMixin from "@/mixins/timeMixin";
  29. export default {
  30. name: "VEditor",
  31. components: {
  32. VMenu,
  33. },
  34. mixins: [timeMixin],
  35. props: {
  36. placeholder: { type: String, default: "请输入..." },
  37. value: {
  38. type: [String, Object],
  39. // 要么为null,要么一定要遵循结构 body.sections[]
  40. // const EMPTY_RICH_TEXT
  41. default: () => {
  42. return { sections: [] };
  43. },
  44. },
  45. styles: { type: String, default: "" },
  46. folder: { type: String, default: "" },
  47. enableAnswerPoint: { type: Boolean, default: false },
  48. maxAudioSize: { type: Number, default: MAX_AUDIO_SIZE, required: false },
  49. maxImageSize: { type: Number, default: MAX_IMAGE_SIZE, required: false },
  50. imageExceedSizeAsAttachment: {
  51. type: Number,
  52. default: IMAGE_EXCEED_SIZE_AS_ATTACHMENT,
  53. required: false,
  54. },
  55. jsonExceedSizeAsAttachment: {
  56. type: Number,
  57. default: JSON_EXCEED_SIZE_AS_ATTACHMENT,
  58. required: false,
  59. },
  60. maxJsonSize: { type: Number, default: MAX_JSON_SIZE, required: false },
  61. emitType: {
  62. type: String,
  63. default: "json",
  64. },
  65. },
  66. data() {
  67. return {};
  68. },
  69. watch: {
  70. value(val, oldVal) {
  71. if (val !== oldVal) {
  72. if (this.$refs.editor !== document.activeElement) {
  73. this.initData();
  74. }
  75. }
  76. },
  77. },
  78. mounted() {
  79. this.initData();
  80. this.$refs.editor.addEventListener("paste", pasteHandle.bind(this));
  81. this.$refs.editor.addEventListener(
  82. "input",
  83. answerPointDragHandle.bind(this)
  84. );
  85. this.$refs.editor.addEventListener("wheel", this.wheelEventHandle);
  86. },
  87. beforeDestroy() {
  88. this.clearSetTs();
  89. },
  90. methods: {
  91. initData() {
  92. if (this.emitType === "html") {
  93. this.$refs.editor.innerHTML = this.value;
  94. } else {
  95. const content = this.value || { sections: [] };
  96. renderRichText(content, this.$refs.editor, false);
  97. }
  98. },
  99. emitJSON() {
  100. if (!this.$refs.editor.contentEditable) {
  101. // 不是出于contentEditable则不更新
  102. return;
  103. }
  104. if (this.emitType === "html") {
  105. const content = this.$refs.editor.innerHTML;
  106. this.$emit("input", content);
  107. this.$emit("change", content);
  108. return;
  109. }
  110. this.clearSetTs();
  111. // 延迟触发input任务,避免频繁触发,同时也等待图片渲染,方便获取图片显示尺寸
  112. this.addSetTime(() => {
  113. // console.log("input:" + Math.random());
  114. this.inputDelaying = false;
  115. const json = toJSON(this.$refs.editor);
  116. this.$emit("input", json);
  117. this.$emit("change", json);
  118. // this.$emit("on-result", json);
  119. }, 200);
  120. },
  121. wheelEventHandle(e) {
  122. // console.log(e);
  123. // console.dir(e.target);
  124. const el = e.target;
  125. if (el.tagName && el.tagName === "IMG") {
  126. e.preventDefault();
  127. e.stopPropagation();
  128. const shift = e.deltaY > 0;
  129. const newWidth =
  130. +getComputedStyle(el).width.replace("px", "") + (shift ? 1 : -1);
  131. // console.log(newWidth, el.naturalWidth);
  132. if (newWidth >= 16 && newWidth <= el.naturalWidth) {
  133. el.style.width = newWidth + "px";
  134. el.style.height =
  135. (newWidth / el.naturalWidth) * el.naturalHeight + "px";
  136. // el.setAttribute("width", newWidth);
  137. this.emitJSON();
  138. }
  139. }
  140. },
  141. },
  142. };
  143. </script>
  144. <style>
  145. .v-editor {
  146. position: relative;
  147. line-height: 20px;
  148. }
  149. .v-editor-head {
  150. position: absolute;
  151. left: 2px;
  152. right: 2px;
  153. background-color: #fff;
  154. top: 2px;
  155. z-index: 9;
  156. border-bottom: 1px solid #f0f0f0;
  157. }
  158. .v-editor-body,
  159. .sourceView {
  160. border: 1px solid #e4e7ed;
  161. border-radius: 5px;
  162. min-height: 100px;
  163. max-height: 300px;
  164. padding: 8px;
  165. padding-top: 32px;
  166. overflow: scroll;
  167. outline: none;
  168. white-space: pre-wrap;
  169. }
  170. .sourceView {
  171. margin: -5px;
  172. }
  173. .v-editor-body[contenteditable="true"]:empty:not(:focus)::before {
  174. content: attr(data-placeholder);
  175. color: grey;
  176. }
  177. .v-editor-body:focus {
  178. border-color: #1886fe;
  179. }
  180. .v-editor-body div {
  181. min-height: 18px;
  182. line-height: 20px;
  183. }
  184. .v-editor-body img {
  185. max-width: 100%;
  186. }
  187. .v-editor-body img[data-is-answer-point] {
  188. max-height: 16px;
  189. display: inline-block;
  190. vertical-align: text-top;
  191. border-bottom: 1px solid #000;
  192. }
  193. .v-editor-body img.audio {
  194. height: 16px;
  195. padding: 0 2px;
  196. display: inline-block;
  197. margin-bottom: 4px;
  198. }
  199. .v-editor-body img[data-is-image] {
  200. /* max-height: 42px; */
  201. display: inline-block;
  202. }
  203. .v-editor-body audio {
  204. height: 16px;
  205. width: 180px;
  206. display: inline-block;
  207. margin-bottom: -4px;
  208. }
  209. .v-editor-body ::-webkit-media-controls-mute-button {
  210. display: none !important;
  211. }
  212. .v-editor-body ::-webkit-media-controls-volume-slider {
  213. display: none !important;
  214. }
  215. .v-editor-body b {
  216. font-weight: bold;
  217. }
  218. .v-editor-body i {
  219. font-style: italic;
  220. }
  221. .v-editor-body u {
  222. text-decoration: underline;
  223. }
  224. </style>