PreviewMixin.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. },
  39. computed: {
  40. showLang() {
  41. return this.getLang() !== 'ZH';
  42. },
  43. },
  44. watch: {
  45. content: {
  46. handler(newVal) {
  47. if (this.type === 'edit') return;
  48. if (!newVal) return;
  49. this.data = JSON.parse(newVal);
  50. },
  51. immediate: true,
  52. },
  53. },
  54. components: {
  55. SerialNumberPosition,
  56. PinyinText,
  57. },
  58. created() {
  59. // 这里分为 预览 和 编辑调整位置、视频互动组件 三种情况
  60. // 预览时,content 直接传入
  61. // 编辑调整位置时,content 需要通过接口获取
  62. if (this.type === 'edit') {
  63. this.getCoursewareComponentContent();
  64. }
  65. },
  66. methods: {
  67. getCoursewareComponentContent() {
  68. ContentGetCoursewareComponentContent({ courseware_id: this.coursewareId, component_id: this.id }).then(
  69. ({ content }) => {
  70. if (content) this.data = JSON.parse(content);
  71. this.loader = true;
  72. },
  73. );
  74. },
  75. /**
  76. * 获取用户答案
  77. * @returns {array} 用户答案
  78. */
  79. getAnswer() {
  80. return this.answer;
  81. },
  82. /**
  83. * 显示答案
  84. * @param {boolean} isJudgingRightWrong 是否判断对错
  85. * @param {boolean} isShowRightAnswer 是否显示正确答案
  86. * @param {object} userAnswer 用户答案
  87. * @param {boolean} disabled 是否禁用
  88. */
  89. showAnswer(isJudgingRightWrong, isShowRightAnswer, userAnswer, disabled) {
  90. // if (this.loader === false) {
  91. // return setTimeout(() => this.showAnswer(isJudgingRightWrong, isShowRightAnswer, userAnswer, disabled), 100);
  92. // }
  93. this.isJudgingRightWrong = isJudgingRightWrong;
  94. this.isShowRightAnswer = isShowRightAnswer;
  95. this.disabled = disabled;
  96. if (userAnswer) this.answer = userAnswer;
  97. },
  98. /**
  99. * 得到序号外部样式
  100. */
  101. getAreaStyle() {
  102. if (!isEnable(this.data.property.sn_display_mode)) {
  103. return {
  104. grid: `"main" / 1fr`,
  105. };
  106. }
  107. const position = this.data.property.sn_position;
  108. let grid = '';
  109. if (position.includes('right')) {
  110. grid = `"main position" / 1fr auto`;
  111. } else if (position.includes('left')) {
  112. grid = `"position main" / auto 1fr`;
  113. } else if (position.includes('top')) {
  114. grid = `"position" auto "main" 1fr`;
  115. } else if (position.includes('bottom')) {
  116. grid = `"main" 1fr "position" auto`;
  117. }
  118. return {
  119. grid,
  120. };
  121. },
  122. },
  123. };
  124. export default mixin;