PreviewMixin.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. import SerialNumberPosition from './SerialNumberPosition.vue';
  2. import PinyinText from '@/components/PinyinText.vue';
  3. import AnswerAnalysis from '@/views/book/courseware/preview/common/AnswerAnalysis.vue';
  4. import PreviewOperation from '@/views/book/courseware/preview/common/PreviewOperation.vue';
  5. import AnswerCorrect from '@/views/book/courseware/preview/common/AnswerCorrect.vue';
  6. import { isEnable } from '@/views/book/courseware/data/common';
  7. import { ContentGetCoursewareComponentContent, ContentSaveCoursewareComponentContent } from '@/api/book';
  8. import { sanitizeHTML } from '@/utils/common';
  9. const mixin = {
  10. data() {
  11. return {
  12. sanitizeHTML,
  13. typeList: ['interaction'],
  14. answer: { answer_list: [], is_right: false }, // 用户答案
  15. isJudgingRightWrong: false, // 是否判断对错
  16. isShowRightAnswer: false, // 是否显示正确答案
  17. disabled: false, // 是否禁用
  18. isShowParse: false, // 是否显示解析
  19. isEnable,
  20. loader: false,
  21. visibleAnswerAnalysis: false, // 是否显示答案解析弹窗
  22. answerAnalysisState: null, // 答案解析弹窗前的状态快照
  23. visibleAnswerCorrect: false, // 是否显示批改弹窗
  24. isCheckCorrect: false, // 是否查看批改信息
  25. };
  26. },
  27. provide() {
  28. return {
  29. openAnswerCorrect: (isCheckCorrect) => this.openAnswerCorrect(isCheckCorrect),
  30. };
  31. },
  32. inject: ['getLang', 'getChinese', 'convertText', 'getTitleList', 'getPermissionControl'],
  33. props: {
  34. id: {
  35. type: String,
  36. default: '',
  37. },
  38. content: {
  39. type: String,
  40. default: '',
  41. },
  42. background: {
  43. type: Object,
  44. default: () => ({}),
  45. },
  46. coursewareId: {
  47. type: String,
  48. default: '',
  49. },
  50. type: {
  51. type: String,
  52. default: '',
  53. },
  54. isMobile: {
  55. type: Boolean,
  56. default: false,
  57. },
  58. },
  59. computed: {
  60. showLang() {
  61. return this.getLang() !== 'ZH';
  62. },
  63. permissionControl() {
  64. return this.getPermissionControl();
  65. },
  66. },
  67. watch: {
  68. content: {
  69. handler(newVal) {
  70. if (this.type === 'edit') return;
  71. if (!newVal) return;
  72. this.data = JSON.parse(newVal);
  73. },
  74. immediate: true,
  75. },
  76. 'permissionControl.can_answer': {
  77. handler(newVal) {
  78. this.disabled = !newVal;
  79. },
  80. immediate: true,
  81. },
  82. },
  83. components: {
  84. SerialNumberPosition,
  85. PinyinText,
  86. AnswerAnalysis,
  87. PreviewOperation,
  88. AnswerCorrect,
  89. },
  90. created() {
  91. // 这里分为 预览 和 编辑调整位置、视频互动组件 三种情况
  92. // 预览时,content 直接传入
  93. // 编辑调整位置时,content 需要通过接口获取
  94. if (this.type === 'edit') {
  95. this.getCoursewareComponentContent();
  96. }
  97. },
  98. methods: {
  99. getCoursewareComponentContent() {
  100. ContentGetCoursewareComponentContent({ courseware_id: this.coursewareId, component_id: this.id }).then(
  101. ({ content }) => {
  102. if (content) {
  103. let oldData = JSON.parse(content);
  104. if (oldData.type === 'audio') {
  105. let p = oldData.property || {};
  106. if (!p.file_name_display_mode) p.file_name_display_mode = 'true';
  107. if (p.view_method === 'independent' && !p.style_mode) {
  108. p.style_mode = 'middle';
  109. }
  110. if (!p.style_mode) p.style_mode = 'big';
  111. if (p.view_method === 'icon') {
  112. p.file_name_display_mode = 'false';
  113. p.view_method = 'independent';
  114. p.style_mode = 'small';
  115. }
  116. }
  117. this.data = oldData;
  118. }
  119. this.loader = true;
  120. },
  121. );
  122. },
  123. /**
  124. * 获取用户答案
  125. * @returns {array} 用户答案
  126. */
  127. getAnswer() {
  128. return this.answer;
  129. },
  130. /**
  131. * 显示答案
  132. * @param {boolean} isJudgingRightWrong 是否判断对错
  133. * @param {boolean} isShowRightAnswer 是否显示正确答案
  134. * @param {object} userAnswer 用户答案
  135. * @param {boolean} disabled 是否禁用
  136. */
  137. showAnswer(isJudgingRightWrong, isShowRightAnswer, userAnswer, disabled) {
  138. // if (this.loader === false) {
  139. // return setTimeout(() => this.showAnswer(isJudgingRightWrong, isShowRightAnswer, userAnswer, disabled), 100);
  140. // }
  141. this.isJudgingRightWrong = isJudgingRightWrong;
  142. this.isShowRightAnswer = isShowRightAnswer;
  143. this.disabled = disabled;
  144. if (userAnswer) this.answer = userAnswer;
  145. },
  146. judgeCorrect() {
  147. this.isJudgingRightWrong = true;
  148. this.isShowRightAnswer = false;
  149. },
  150. /**
  151. * 获取批改信息
  152. * @returns {string} 批改信息
  153. */
  154. getAnswerCorrect() {
  155. return this.data.answer_correct || '';
  156. },
  157. /**
  158. * 设置批改信息
  159. * @param {string} correct 批改信息
  160. */
  161. setAnswerCorrect(correct) {
  162. this.$set(this.data, 'answer_correct', correct);
  163. },
  164. /**
  165. * 得到序号外部样式
  166. */
  167. getAreaStyle() {
  168. if (!isEnable(this.data.property.sn_display_mode)) {
  169. return {
  170. grid: `"main" / 1fr`,
  171. };
  172. }
  173. const position = this.data.property.sn_position;
  174. let grid = '';
  175. if (position.includes('right')) {
  176. grid = `"main position" / 1fr auto`;
  177. } else if (position.includes('left')) {
  178. grid = `"position main" / auto 1fr`;
  179. } else if (position.includes('top')) {
  180. grid = `"position" auto "main" 1fr`;
  181. } else if (position.includes('bottom')) {
  182. grid = `"main" 1fr "position" auto`;
  183. }
  184. return {
  185. grid,
  186. };
  187. },
  188. /**
  189. * 计算组件背景样式
  190. * @returns {Object} 组件背景样式对象
  191. */
  192. getComponentStyle() {
  193. const {
  194. background_image_url: bcImgUrl = '',
  195. background_position: pos = {},
  196. background: back,
  197. } = this.background || {};
  198. let canvasStyle = {
  199. backgroundSize: bcImgUrl ? `${pos.width}% ${pos.height}%` : '',
  200. backgroundPosition: bcImgUrl ? `${pos.left}% ${pos.top}%` : '',
  201. backgroundImage: bcImgUrl ? `url(${bcImgUrl})` : '',
  202. backgroundColor: bcImgUrl ? `rgba(255, 255, 255, ${1 - back?.image_opacity / 100})` : '',
  203. };
  204. if (back) {
  205. if (back.mode === 'image') {
  206. canvasStyle['backgroundBlendMode'] = 'lighten';
  207. } else {
  208. canvasStyle['backgroundBlendMode'] = '';
  209. }
  210. if (back.imageMode === 'fill') {
  211. canvasStyle['backgroundRepeat'] = 'repeat';
  212. canvasStyle['backgroundSize'] = '';
  213. canvasStyle['backgroundPosition'] = '';
  214. } else {
  215. canvasStyle['backgroundRepeat'] = 'no-repeat';
  216. }
  217. if (back.imageMode === 'stretch') {
  218. canvasStyle['backgroundSize'] = '100% 100%';
  219. }
  220. if (back.imageMode === 'adapt') {
  221. canvasStyle['backgroundSize'] = 'contain';
  222. }
  223. if (back.imageMode === 'auto') {
  224. canvasStyle['backgroundPosition'] = `${pos.imgX}% ${pos.imgY}%`;
  225. }
  226. if (back.mode === 'color') {
  227. canvasStyle['backgroundColor'] = back.color;
  228. canvasStyle['backgroundImage'] = '';
  229. canvasStyle['backgroundRepeat'] = '';
  230. canvasStyle['backgroundPosition'] = '';
  231. canvasStyle['backgroundSize'] = '';
  232. }
  233. if (back.enable_border) {
  234. canvasStyle['border'] = `${back.border_width}px ${back.border_style} ${back.border_color}`;
  235. } else {
  236. canvasStyle['border'] = 'none';
  237. }
  238. if (back.enable_radius) {
  239. canvasStyle['border-top-left-radius'] = `${back.top_left_radius}px`;
  240. canvasStyle['border-top-right-radius'] = `${back.top_right_radius}px`;
  241. canvasStyle['border-bottom-left-radius'] = `${back.bottom_left_radius}px`;
  242. canvasStyle['border-bottom-right-radius'] = `${back.bottom_right_radius}px`;
  243. } else {
  244. canvasStyle['border-radius'] = '0';
  245. }
  246. }
  247. return canvasStyle;
  248. },
  249. // 显示答案与解析页面
  250. showAnswerAnalysis() {
  251. if (!this.answerAnalysisState) {
  252. this.answerAnalysisState = {
  253. disabled: this.disabled,
  254. isJudgingRightWrong: this.isJudgingRightWrong,
  255. };
  256. }
  257. this.visibleAnswerAnalysis = true;
  258. this.disabled = true;
  259. this.isJudgingRightWrong = this.permissionControl.can_judge_correct;
  260. this.isShowRightAnswer = this.permissionControl.can_show_answer;
  261. },
  262. // 关闭答案与解析页面
  263. closeAnswerAnalysis() {
  264. if (this.answerAnalysisState) {
  265. this.disabled = this.answerAnalysisState.disabled;
  266. this.isJudgingRightWrong = this.answerAnalysisState.isJudgingRightWrong;
  267. this.isShowRightAnswer = false;
  268. this.answerAnalysisState = null;
  269. } else {
  270. this.disabled = false;
  271. this.isJudgingRightWrong = false;
  272. this.isShowRightAnswer = false;
  273. }
  274. },
  275. /**
  276. * 显示批改页面
  277. * @param {boolean} isCheckCorrect 是否查看批改
  278. */
  279. openAnswerCorrect(isCheckCorrect) {
  280. this.visibleAnswerCorrect = true;
  281. this.isCheckCorrect = isCheckCorrect;
  282. },
  283. /**
  284. * @description 关闭批改页面,并传递批改信息
  285. * @param {string} correct 批改信息
  286. */
  287. closeAnswerCorrect(correct) {
  288. this.visibleAnswerCorrect = false;
  289. this.$set(this.data, 'answer_correct', correct);
  290. },
  291. getNoTextContentData() {
  292. let noTextContentData = JSON.parse(JSON.stringify(this.data));
  293. if (noTextContentData.content) {
  294. noTextContentData.content = '';
  295. }
  296. return noTextContentData;
  297. },
  298. /**
  299. * @description 保存样式模板
  300. * @param {string} courseware_id 课件id
  301. */
  302. saveStyleTemplate(courseware_id) {
  303. const content = this.getNoTextContentData();
  304. return ContentSaveCoursewareComponentContent({
  305. courseware_id,
  306. component_id: this.id,
  307. component_type: this.data.type,
  308. content: JSON.stringify(content),
  309. });
  310. },
  311. },
  312. };
  313. export default mixin;