VEditor.vue 5.6 KB

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