VEditor.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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,
  39. // 要么为null,要么一定要遵循结构 body.sections[]
  40. // const EMPTY_RICH_TEXT
  41. default: () => JSON.stringify({ sections: [] }),
  42. },
  43. styles: { type: String, default: "" },
  44. folder: { type: String, default: "" },
  45. enableAnswerPoint: { type: Boolean, default: false },
  46. maxAudioSize: { type: Number, default: MAX_AUDIO_SIZE, required: false },
  47. maxImageSize: { type: Number, default: MAX_IMAGE_SIZE, required: false },
  48. imageExceedSizeAsAttachment: {
  49. type: Number,
  50. default: IMAGE_EXCEED_SIZE_AS_ATTACHMENT,
  51. required: false,
  52. },
  53. jsonExceedSizeAsAttachment: {
  54. type: Number,
  55. default: JSON_EXCEED_SIZE_AS_ATTACHMENT,
  56. required: false,
  57. },
  58. maxJsonSize: { type: Number, default: MAX_JSON_SIZE, required: false },
  59. emitType: {
  60. type: String,
  61. default: "json",
  62. },
  63. },
  64. data() {
  65. return {};
  66. },
  67. watch: {
  68. value(val, oldVal) {
  69. if (val !== oldVal) {
  70. if (this.$refs.editor !== document.activeElement) {
  71. this.initData();
  72. }
  73. }
  74. },
  75. },
  76. mounted() {
  77. this.initData();
  78. this.$refs.editor.addEventListener("paste", pasteHandle.bind(this));
  79. this.$refs.editor.addEventListener(
  80. "input",
  81. answerPointDragHandle.bind(this)
  82. );
  83. this.$refs.editor.addEventListener("wheel", this.wheelEventHandle);
  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. let content = { sections: [] };
  94. try {
  95. content = JSON.parse(this.value);
  96. renderRichText(content, this.$refs.editor, false);
  97. } catch (e) {
  98. this.$refs.editor.innerHTML = this.value;
  99. this.emitJSON();
  100. }
  101. }
  102. },
  103. emitJSON() {
  104. if (!this.$refs.editor.contentEditable) {
  105. // 不是出于contentEditable则不更新
  106. return;
  107. }
  108. if (this.emitType === "html") {
  109. const content = this.$refs.editor.innerHTML;
  110. this.$emit("input", content);
  111. this.$emit("change", content);
  112. return;
  113. }
  114. this.clearSetTs();
  115. // 延迟触发input任务,避免频繁触发,同时也等待图片渲染,方便获取图片显示尺寸
  116. this.addSetTime(() => {
  117. console.log("input:" + Math.random());
  118. this.inputDelaying = false;
  119. const json = toJSON(this.$refs.editor);
  120. // console.log("emit", json, this.$refs.editor);
  121. // 未来校验多了的话,考虑移除去
  122. if (json.length > this.maxJsonSize) {
  123. this.$message.error(
  124. `富文本长度超过限制,最长可以为 ${
  125. this.maxJsonSize / 1024 / 1024
  126. } MB.`
  127. );
  128. return;
  129. }
  130. // console.log(json, this.value, json === this.value);
  131. this.$emit("input", json);
  132. this.$emit("change", json);
  133. // this.$emit("on-result", json);
  134. // console.log(json);
  135. }, 200);
  136. // re-render 之后 restore cursor? 不现实,因为dom结构变了。
  137. },
  138. wheelEventHandle(e) {
  139. // console.log(e);
  140. // console.dir(e.target);
  141. const el = e.target;
  142. if (el.tagName && el.tagName === "IMG") {
  143. e.preventDefault();
  144. e.stopPropagation();
  145. const shift = e.deltaY > 0;
  146. const newWidth =
  147. +getComputedStyle(el).width.replace("px", "") + (shift ? 1 : -1);
  148. // console.log(newWidth, el.naturalWidth);
  149. if (newWidth >= 16 && newWidth <= el.naturalWidth) {
  150. el.style.width = newWidth + "px";
  151. el.style.height =
  152. (newWidth / el.naturalWidth) * el.naturalHeight + "px";
  153. // el.setAttribute("width", newWidth);
  154. this.emitJSON();
  155. }
  156. }
  157. },
  158. },
  159. };
  160. </script>
  161. <style>
  162. .v-editor {
  163. position: relative;
  164. line-height: 20px;
  165. }
  166. .v-editor-head {
  167. position: absolute;
  168. left: 2px;
  169. right: 2px;
  170. background-color: #fff;
  171. top: 2px;
  172. z-index: 9;
  173. border-bottom: 1px solid #f0f0f0;
  174. }
  175. .v-editor-body,
  176. .sourceView {
  177. border: 1px solid #e4e7ed;
  178. border-radius: 5px;
  179. min-height: 100px;
  180. max-height: 300px;
  181. padding: 8px;
  182. padding-top: 32px;
  183. overflow: scroll;
  184. outline: none;
  185. white-space: pre;
  186. }
  187. .sourceView {
  188. margin: -5px;
  189. }
  190. .v-editor-body[contenteditable="true"]:empty:not(:focus)::before {
  191. content: attr(data-placeholder);
  192. color: grey;
  193. }
  194. .v-editor-body:focus {
  195. border-color: #1886fe;
  196. }
  197. .v-editor-body div {
  198. min-height: 18px;
  199. line-height: 20px;
  200. }
  201. .v-editor-body img {
  202. max-width: 100%;
  203. }
  204. .v-editor-body img[data-is-answer-point] {
  205. max-height: 16px;
  206. display: inline-block;
  207. vertical-align: text-top;
  208. }
  209. .v-editor-body img.audio {
  210. height: 16px;
  211. padding: 0 2px;
  212. display: inline-block;
  213. margin-bottom: 4px;
  214. }
  215. .v-editor-body img[data-is-image] {
  216. /* max-height: 42px; */
  217. display: inline-block;
  218. }
  219. .v-editor-body audio {
  220. height: 16px;
  221. width: 180px;
  222. display: inline-block;
  223. margin-bottom: -4px;
  224. }
  225. .v-editor-body ::-webkit-media-controls-mute-button {
  226. display: none !important;
  227. }
  228. .v-editor-body ::-webkit-media-controls-volume-slider {
  229. display: none !important;
  230. }
  231. .v-editor-body b {
  232. font-weight: bold;
  233. }
  234. .v-editor-body i {
  235. font-style: italic;
  236. }
  237. .v-editor-body u {
  238. text-decoration: underline;
  239. }
  240. </style>