PreviewMixin.js 3.4 KB

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