VEditor.vue 6.0 KB

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