PreviewMixin.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import SerialNumberPosition from './SerialNumberPosition.vue';
  2. import PinyinText from '@/components/PinyinText.vue';
  3. import { isEnable } from '@/views/book/courseware/data/common';
  4. import { ContentGetCoursewareComponentContent } from '@/api/book';
  5. import { sanitizeHTML } from '@/utils/common';
  6. const mixin = {
  7. data() {
  8. return {
  9. sanitizeHTML,
  10. typeList: ['interaction'],
  11. answer: { answer_list: [], is_right: false }, // 用户答案
  12. isJudgingRightWrong: false, // 是否判断对错
  13. isShowRightAnswer: false, // 是否显示正确答案
  14. disabled: false, // 是否禁用
  15. isShowParse: false, // 是否显示解析
  16. isEnable,
  17. loader: false,
  18. };
  19. },
  20. inject: ['getLang', 'getChinese', 'convertText'],
  21. props: {
  22. id: {
  23. type: String,
  24. default: '',
  25. },
  26. content: {
  27. type: String,
  28. default: '',
  29. },
  30. coursewareId: {
  31. type: String,
  32. default: '',
  33. },
  34. type: {
  35. type: String,
  36. default: '',
  37. },
  38. isMobile: {
  39. type: Boolean,
  40. default: false,
  41. },
  42. },
  43. computed: {
  44. showLang() {
  45. return this.getLang() !== 'ZH';
  46. },
  47. },
  48. watch: {
  49. content: {
  50. handler(newVal) {
  51. if (this.type === 'edit') return;
  52. if (!newVal) return;
  53. this.data = JSON.parse(newVal);
  54. },
  55. immediate: true,
  56. },
  57. },
  58. components: {
  59. SerialNumberPosition,
  60. PinyinText,
  61. },
  62. created() {
  63. // 这里分为 预览 和 编辑调整位置、视频互动组件 三种情况
  64. // 预览时,content 直接传入
  65. // 编辑调整位置时,content 需要通过接口获取
  66. if (this.type === 'edit') {
  67. this.getCoursewareComponentContent();
  68. }
  69. },
  70. methods: {
  71. getCoursewareComponentContent() {
  72. ContentGetCoursewareComponentContent({ courseware_id: this.coursewareId, component_id: this.id }).then(
  73. ({ content }) => {
  74. if (content) this.data = JSON.parse(content);
  75. this.loader = true;
  76. },
  77. );
  78. },
  79. /**
  80. * 获取用户答案
  81. * @returns {array} 用户答案
  82. */
  83. getAnswer() {
  84. return this.answer;
  85. },
  86. /**
  87. * 显示答案
  88. * @param {boolean} isJudgingRightWrong 是否判断对错
  89. * @param {boolean} isShowRightAnswer 是否显示正确答案
  90. * @param {object} userAnswer 用户答案
  91. * @param {boolean} disabled 是否禁用
  92. */
  93. showAnswer(isJudgingRightWrong, isShowRightAnswer, userAnswer, disabled) {
  94. // if (this.loader === false) {
  95. // return setTimeout(() => this.showAnswer(isJudgingRightWrong, isShowRightAnswer, userAnswer, disabled), 100);
  96. // }
  97. this.isJudgingRightWrong = isJudgingRightWrong;
  98. this.isShowRightAnswer = isShowRightAnswer;
  99. this.disabled = disabled;
  100. if (userAnswer) this.answer = userAnswer;
  101. },
  102. /**
  103. * 得到序号外部样式
  104. */
  105. getAreaStyle() {
  106. if (!isEnable(this.data.property.sn_display_mode)) {
  107. return {
  108. grid: `"main" / 1fr`,
  109. };
  110. }
  111. const position = this.data.property.sn_position;
  112. let grid = '';
  113. if (position.includes('right')) {
  114. grid = `"main position" / 1fr auto`;
  115. } else if (position.includes('left')) {
  116. grid = `"position main" / auto 1fr`;
  117. } else if (position.includes('top')) {
  118. grid = `"position" auto "main" 1fr`;
  119. } else if (position.includes('bottom')) {
  120. grid = `"main" 1fr "position" auto`;
  121. }
  122. return {
  123. grid,
  124. };
  125. },
  126. },
  127. };
  128. export default mixin;