tiankong.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <div class="fill-in-the-blank">
  3. <!-- 显示填空后的文本 -->
  4. <view class="formatted-text" v-html="formattedText" :question="question" :change:question="TK.watchQuestionChange"></view>
  5. <!-- 提供与占位符数量相匹配的输入框 -->
  6. <template v-for="(placeholder, index) in placeholders" :key="index">
  7. <FillItem :value="question.reply[index]" :ref="`popup${index}Ref`" :index="index" @blur="onBlur"></FillItem>
  8. </template>
  9. </div>
  10. </template>
  11. <script>
  12. import FillItem from "./FillItem.vue";
  13. export default {
  14. components: {
  15. FillItem
  16. },
  17. props: {
  18. placeholders: { // 占位符
  19. type: Array,
  20. required: true
  21. },
  22. question: {
  23. type: Object,
  24. },
  25. showError: {
  26. type: Boolean,
  27. default: false
  28. }
  29. },
  30. computed: {
  31. // 计算属性,用于生成填空后的文本
  32. formattedText() {
  33. let result = this.question.name;
  34. this.placeholders.forEach((placeholder, index) => {
  35. const regex2 = new RegExp(`\\<img `, 'g');
  36. result = result.replace(regex2, '<image ');
  37. // 使用正则表达式全局替换占位符
  38. const regex = new RegExp(`\\${placeholder}`, 'g');
  39. if (this.showError) {
  40. // 答题状态默认错误
  41. let answer_status = 'showError';
  42. // 更新答题状态正确
  43. this.question.reply[index] && this.question.result[index].some(citem => citem === this
  44. .question.reply[index].trim()) && (answer_status = 'active_right');
  45. result = result.replace(regex,
  46. `<input class="tiankong-input ${answer_status}" style="width: 50px;margin: 0 3px" id="t_${this.question.stId}_${index}" readonly data-index="${index}" value="${this.question.reply[index]}" />`
  47. );
  48. } else {
  49. result = result.replace(regex,
  50. `<input class="tiankong-input" style="width: 50px;margin: 0 3px" id="t_${this.question.stId}_${index}" readonly data-index="${index}" value="${this.question.reply[index]}" />`
  51. );
  52. }
  53. });
  54. return result;
  55. },
  56. },
  57. methods: {
  58. setResult(data) {
  59. const {
  60. index,
  61. result
  62. } = data;
  63. this.question.reply[index] = result;
  64. },
  65. onBlur(data) {
  66. const {
  67. index,
  68. result
  69. } = data;
  70. this.question.reply[index] = result;
  71. this.setResult(data);
  72. },
  73. showPopup(str) {
  74. this.$refs[str][0].showPopup();
  75. }
  76. },
  77. }
  78. </script>
  79. <script module="TK" lang="renderjs">
  80. export default {
  81. methods: {
  82. updateFn(e) {
  83. const str = `popup${e.target.dataset.index}Ref`;
  84. this.$ownerInstance.callMethod('showPopup',str)
  85. },
  86. watchQuestionChange(newValue, oldValue, ownerInstance, instance) {
  87. if (newValue) {
  88. this.initListener(newValue)
  89. }
  90. },
  91. initListener(question) {
  92. question.result.forEach((item, index) => {
  93. const dom = document.getElementById(`t_${question.stId}_${index}`)
  94. dom && dom.addEventListener('focus', (e) => this.updateFn(e))
  95. })
  96. },
  97. },
  98. }
  99. </script>
  100. <style scoped>
  101. .fill-in-the-blank {
  102. width: 100%;
  103. }
  104. .formatted-text {
  105. margin-bottom: 10px;
  106. font-size: 16px;
  107. line-height: 1.5;
  108. }
  109. .inputs-container {
  110. display: flex;
  111. flex-wrap: wrap;
  112. gap: 10px;
  113. }
  114. .input-box {
  115. padding: 5px;
  116. width: 150px;
  117. /* 可根据需要调整宽度 */
  118. border: 1px solid #ccc;
  119. border-radius: 4px;
  120. }
  121. .tiankong-input {
  122. width: 50px;
  123. }
  124. </style>