TextQuestionView.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <template>
  2. <div v-if="isSyncState" class="question-view">
  3. <question-body
  4. :questionBody="question.body"
  5. :examQuestion="examQuestion"
  6. ></question-body>
  7. <div class="ops">
  8. <div class="score">({{ examQuestion.questionScore }}分)</div>
  9. </div>
  10. <div class="option">
  11. <!-- <textarea v-model="studentAnswer" maxlength="5000" type="text" /> -->
  12. <div>
  13. <div class="menu">
  14. <i-button type="info" class="text-ops" size="small" @click="textCopy">
  15. 复制
  16. </i-button>
  17. <i-button type="info" class="text-ops" size="small" @click="textCut">
  18. 剪切
  19. </i-button>
  20. <i-button
  21. type="info"
  22. class="text-ops"
  23. size="small"
  24. @click="textPaste"
  25. >
  26. 粘贴
  27. </i-button>
  28. <i-button type="info" class="text-ops" size="small" @click="textSup">
  29. 上标
  30. </i-button>
  31. <i-button
  32. type="info"
  33. class="text-ops"
  34. size="small"
  35. @click="undoTextSup"
  36. >
  37. 取消上标
  38. </i-button>
  39. <i-button type="info" class="text-ops" size="small" @click="textSub">
  40. 下标
  41. </i-button>
  42. <i-button
  43. type="info"
  44. class="text-ops"
  45. size="small"
  46. @click="undoTextSup"
  47. >
  48. 取消下标
  49. </i-button>
  50. </div>
  51. <div
  52. ref="answerDiv"
  53. @keydown="disableCtrl"
  54. :contenteditable="true"
  55. v-html="studentAnswer"
  56. v-once
  57. @input="$event => textInput($event)"
  58. @blur="$event => textInput($event)"
  59. class="stu-answer"
  60. ></div>
  61. </div>
  62. <div class="reset" style="padding-top: 20px">
  63. <i-button type="warning" size="large" @click="studentAnswer = null">
  64. 重置答案
  65. </i-button>
  66. <span v-if="examShouldShowAnswer()">
  67. &nbsp;&nbsp;&nbsp;<i-button
  68. type="info"
  69. size="large"
  70. @click="showAnswer"
  71. >
  72. 显示答案
  73. </i-button>
  74. </span>
  75. <div v-if="examShouldShowAnswer() && isShowAnswer">
  76. 正确答案:
  77. <div v-html="rightAnswerTransform"></div>
  78. </div>
  79. </div>
  80. </div>
  81. </div>
  82. </template>
  83. <script>
  84. import QuestionBody from "./QuestionBody";
  85. import { createNamespacedHelpers } from "vuex";
  86. const { mapMutations, mapGetters } = createNamespacedHelpers(
  87. "examingHomeModule"
  88. );
  89. export default {
  90. name: "TextQuestionView",
  91. data() {
  92. return {
  93. studentAnswer: this.examQuestion.studentAnswer,
  94. isShowAnswer: false
  95. };
  96. },
  97. props: {
  98. question: Object,
  99. examQuestion: Object
  100. },
  101. methods: {
  102. ...mapMutations(["updateExamQuestion"]),
  103. ...mapGetters(["examShouldShowAnswer"]),
  104. disableCtrl(e) {
  105. if (e.ctrlKey || e.metaKey || e.altKey) {
  106. // .ctrlKey tells that ctrl key was pressed.
  107. e.preventDefault();
  108. return false;
  109. }
  110. return true;
  111. },
  112. textCopy() {
  113. var selElm = getSelection();
  114. var selRange = selElm.getRangeAt(0);
  115. this.copyNode = selRange.cloneContents();
  116. },
  117. textCut() {
  118. var selElm = getSelection();
  119. var selRange = selElm.getRangeAt(0);
  120. this.copyNode = selRange.extractContents();
  121. this.studentAnswer = this.$refs.answerDiv.innerHTML;
  122. },
  123. textPaste() {
  124. var selElm = getSelection();
  125. var selRange = selElm.getRangeAt(0);
  126. selRange.deleteContents();
  127. selRange.insertNode(this.copyNode.cloneNode(true));
  128. this.studentAnswer = this.$refs.answerDiv.innerHTML;
  129. },
  130. textSup() {
  131. getSelection()
  132. .getRangeAt(0)
  133. .surroundContents(document.createElement("sup"));
  134. this.studentAnswer = this.$refs.answerDiv.innerHTML;
  135. },
  136. undoTextSup() {
  137. getSelection().modify("extend", "left", "character");
  138. let selRange = getSelection().getRangeAt(0);
  139. var documentFragment = selRange.extractContents();
  140. var text = new Text(documentFragment.textContent);
  141. selRange.insertNode(text);
  142. this.studentAnswer = this.$refs.answerDiv.innerHTML;
  143. },
  144. textSub() {
  145. getSelection()
  146. .getRangeAt(0)
  147. .surroundContents(document.createElement("sub"));
  148. this.studentAnswer = this.$refs.answerDiv.innerHTML;
  149. },
  150. textInput($event) {
  151. console.log($event.target.innerHTML);
  152. this.studentAnswer = $event.target.innerHTML;
  153. },
  154. showAnswer() {
  155. this.isShowAnswer = !this.isShowAnswer;
  156. }
  157. },
  158. watch: {
  159. examQuestion() {
  160. // console.log(this.examQuestion.studentAnswer);
  161. this.studentAnswer = this.examQuestion.studentAnswer;
  162. },
  163. studentAnswer() {
  164. let realAnswer = null;
  165. if (this.studentAnswer) {
  166. // 如果有实际内容
  167. realAnswer = this.studentAnswer
  168. .replace(/<sup><\/sup>/gi, "")
  169. .replace(/<sub><\/sub>/gi, "")
  170. .replace(/<script/gi, "&lt;script")
  171. .replace(/script>/gi, "script&gt;");
  172. // .replace(/</gi, "&lt;")
  173. // .replace(/>/gi, "&gt;")
  174. // .replace(/&lt;div&gt;&lt;br&gt;&lt;\/div&gt;/gi, "<div><br></div>");
  175. }
  176. if (realAnswer !== this.examQuestion.studentAnswer) {
  177. this.updateExamQuestion({
  178. order: this.examQuestion.order,
  179. studentAnswer: realAnswer
  180. });
  181. }
  182. }
  183. },
  184. computed: {
  185. isSyncState() {
  186. return this.examQuestion.order == this.$route.params.order;
  187. },
  188. rightAnswerTransform() {
  189. return this.question.rightAnswer.join("");
  190. }
  191. },
  192. components: {
  193. QuestionBody
  194. }
  195. };
  196. </script>
  197. <style scoped>
  198. .question-view {
  199. display: grid;
  200. grid-row-gap: 10px;
  201. }
  202. .question-body {
  203. font-size: 18px;
  204. margin-bottom: 10px;
  205. }
  206. .ops {
  207. display: flex;
  208. align-items: flex-end;
  209. }
  210. .text-ops {
  211. margin: 0 5px 5px 0;
  212. }
  213. .stu-answer {
  214. width: 100%;
  215. max-width: 500px;
  216. min-height: 300px;
  217. border: 1px solid grey;
  218. }
  219. </style>