AudioPreview.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <template>
  2. <div ref="audioArea" class="audio-preview" :style="[getAreaStyle(), getComponentStyle()]">
  3. <SerialNumberPosition v-if="isEnable(data.property.sn_display_mode)" :property="data.property" />
  4. <div ref="audioAreaBox" class="main">
  5. <div v-if="'independent' === data.property.view_method">
  6. <ul v-if="data.property.style_mode == 'big'" class="view-list">
  7. <li v-for="(file, i) in data.file_list" :key="i">
  8. <AudioPlay
  9. :view-size="data.property.style_mode"
  10. :view-method="data.property.view_method"
  11. :file-id="file.file_id"
  12. :file-url="file.file_url"
  13. :file-name-display="data.property.file_name_display_mode"
  14. :file-name="file.file_name.slice(0, file.file_name.lastIndexOf('.'))"
  15. :audio-index="i"
  16. :topic-color="data.unified_attrib?.topic_color"
  17. />
  18. </li>
  19. </ul>
  20. <ul v-else class="view-independent">
  21. <li v-for="(file, i) in data.file_list" :key="i">
  22. <AudioPlay
  23. :view-size="data.property.style_mode"
  24. :view-method="data.property.view_method"
  25. :file-id="file.file_id"
  26. :file-url="file.file_url"
  27. :file-name-display="data.property.file_name_display_mode"
  28. :file-name="file.file_name.slice(0, file.file_name.lastIndexOf('.'))"
  29. :audio-index="i"
  30. :topic-color="data.unified_attrib?.topic_color"
  31. />
  32. </li>
  33. </ul>
  34. </div>
  35. <ul v-else-if="'icon' === data.property.view_method" class="view-icon">
  36. <li v-for="(file, i) in data.file_list" :key="i">
  37. <AudioPlay
  38. view-size="small"
  39. :file-id="file.file_id"
  40. :file-url="file.file_url"
  41. :audio-index="i"
  42. :topic-color="data.unified_attrib?.topic_color"
  43. />
  44. </li>
  45. </ul>
  46. <div v-else class="view-list">
  47. <el-carousel
  48. ref="audio_carousel"
  49. indicator-position="none"
  50. direction="vertical"
  51. :autoplay="false"
  52. :interval="0"
  53. :style="{ '--carousel-height': data.property.file_name_display_mode === 'true' ? '128px' : '80px' }"
  54. >
  55. <el-carousel-item v-for="(file, i) in data.file_list" :key="i">
  56. <AudioPlay
  57. view-size="big"
  58. :view-method="data.property.view_method"
  59. :file-id="file.file_id"
  60. :file-url="file.file_url"
  61. :file-name-display="data.property.file_name_display_mode"
  62. :file-name="file.file_name.slice(0, file.file_name.lastIndexOf('.'))"
  63. :show-slider="true"
  64. :cur-audio-index="curAudioIndex"
  65. :topic-color="data.unified_attrib?.topic_color"
  66. @changeFile="changeFile"
  67. />
  68. </el-carousel-item>
  69. </el-carousel>
  70. <div :style="{ height: elementHeight - 140 + 'px', overflowY: viewScroll }">
  71. <ul class="view-list-bottom">
  72. <li v-for="(file, i) in data.file_list" :key="i" @click="handleAudioClick(i, file.file_id)">
  73. <AudioPlay
  74. :ref="file.file_id"
  75. view-size="list"
  76. :file-id="file.file_id"
  77. :file-url="file.file_url"
  78. :file-name="file.file_name.slice(0, file.file_name.lastIndexOf('.'))"
  79. :show-slider="false"
  80. :audio-index="i"
  81. :cur-audio-index="curAudioIndex"
  82. :topic-color="data.unified_attrib?.topic_color"
  83. :assist-color="data.unified_attrib?.assist_color"
  84. />
  85. </li>
  86. </ul>
  87. </div>
  88. </div>
  89. </div>
  90. </div>
  91. </template>
  92. <script>
  93. import { getAudioData } from '@/views/book/courseware/data/audio';
  94. import PreviewMixin from '../common/PreviewMixin';
  95. import AudioPlay from '../common/AudioPlay.vue';
  96. import { getRandomNumber } from '@/utils';
  97. export default {
  98. name: 'AudioPreview',
  99. components: { AudioPlay },
  100. mixins: [PreviewMixin],
  101. inject: ['getDragStatus'],
  102. data() {
  103. return {
  104. carouselHeight: 138,
  105. data: getAudioData(),
  106. curAudioIndex: 0,
  107. elementWidth: 0,
  108. elementHeight: 0,
  109. fileLen: 0,
  110. viewScroll: 'hidden',
  111. elementID: '',
  112. audioObserversMap: {},
  113. };
  114. },
  115. watch: {
  116. data: {
  117. handler(val) {
  118. this.fileLen = val.file_list.length;
  119. if (this.fileLen > 0 && this.data.property.view_method === 'list') {
  120. const ele = this.$refs.audioAreaBox;
  121. // this.elementWidth = ele.clientWidth;
  122. // this.elementHeight = ele.clientHeight;
  123. const sn_position = this.data.property.sn_position;
  124. // 序号在左和右补齐序号高度,去掉padding(8*2)
  125. if (sn_position.includes('left') || sn_position.includes('right')) {
  126. this.elementWidth = ele.clientWidth - 16;
  127. this.elementHeight = ele.clientHeight + 30;
  128. } else {
  129. this.elementWidth = ele.clientWidth;
  130. this.elementHeight = ele.clientHeight;
  131. }
  132. const mainEle = this.$refs.audioArea;
  133. // 检查元素是否包含已知的类名
  134. mainEle.classList.forEach((className) => {
  135. // 排除已知的类名
  136. if (className !== 'audio-area') {
  137. // 打印另一个类名
  138. this.elementID = className;
  139. }
  140. });
  141. }
  142. },
  143. deep: true,
  144. },
  145. elementWidth(newWidth) {
  146. if (this.data.property.view_method === 'list') this.elementWidth = newWidth;
  147. },
  148. elementHeight(newHeight) {
  149. if (this.data.property.view_method === 'list') {
  150. this.elementHeight = newHeight;
  151. this.isViewScroll();
  152. }
  153. },
  154. },
  155. mounted() {
  156. this.$nextTick(() => {
  157. const canvasElement = document.querySelector('.canvas');
  158. if (!canvasElement) return;
  159. const instanceName = `observer_${this.elementID}`;
  160. this.audioObserversMap[instanceName] = new ResizeObserver((entries) => {
  161. if (!this.getDragStatus()) return;
  162. for (let entry of entries) {
  163. window.requestAnimationFrame(() => {
  164. const sn_position = this.data.property.sn_position;
  165. // 序号在上方和下方减去序号高度,在左右去掉padding(8*2)
  166. if (sn_position.includes('top') || sn_position.includes('bottom')) {
  167. this.elementWidth = entry.contentRect.width;
  168. this.elementHeight = entry.contentRect.height - 30;
  169. } else {
  170. this.elementWidth = entry.contentRect.width - 16;
  171. this.elementHeight = entry.contentRect.height;
  172. }
  173. });
  174. }
  175. });
  176. this.audioObserversMap[instanceName].observe(this.$el);
  177. });
  178. },
  179. beforeDestroy() {
  180. Object.values(this.audioObserversMap).forEach((observer) => {
  181. observer.disconnect();
  182. });
  183. },
  184. methods: {
  185. handleAudioClick(index, id) {
  186. // 获取 Carousel 实例
  187. const carousel = this.$refs.audio_carousel;
  188. // 切换到对应索引的文件
  189. carousel.setActiveItem(index);
  190. this.curAudioIndex = index;
  191. if (this.$refs[id] && this.$refs[id][0]) this.$refs[id][0].changeAudio(id);
  192. },
  193. changeFile(type) {
  194. // 获取 Carousel 实例
  195. const carousel = this.$refs.audio_carousel;
  196. // 切换到对应索引的文件
  197. if (type === 'prev') {
  198. carousel.prev();
  199. this.curAudioIndex += -1;
  200. } else {
  201. carousel.next();
  202. this.curAudioIndex += 1;
  203. }
  204. if (this.curAudioIndex >= this.data.file_id_list.length) {
  205. this.curAudioIndex = 0;
  206. }
  207. if (this.curAudioIndex < 0) {
  208. this.curAudioIndex = this.data.file_id_list.length - 1;
  209. }
  210. },
  211. isViewScroll() {
  212. const ulHeight = this.fileLen * 44;
  213. if (ulHeight > this.elementHeight - 140) {
  214. this.viewScroll = 'scroll';
  215. } else {
  216. this.viewScroll = 'hidden';
  217. }
  218. },
  219. /**
  220. * 获取无文本内容的数据结构,用于保存为个人模板时的样式模板
  221. */
  222. getNoTextContentData() {
  223. let noTextContentData = JSON.parse(JSON.stringify(this.data));
  224. const resetFieldMap = {
  225. analysis_list: [],
  226. answer_list: [],
  227. };
  228. Object.assign(noTextContentData, resetFieldMap);
  229. if (noTextContentData.answer) {
  230. noTextContentData.answer.answer_list = [];
  231. noTextContentData.answer.reference_answer = '';
  232. }
  233. return noTextContentData;
  234. },
  235. },
  236. };
  237. </script>
  238. <style lang="scss" scoped>
  239. @use '@/styles/mixin.scss' as *;
  240. .audio-preview {
  241. @include preview-base;
  242. .view-independent {
  243. display: flex;
  244. flex-wrap: wrap;
  245. gap: 16px;
  246. > li {
  247. min-width: 280px;
  248. }
  249. }
  250. .view-icon {
  251. display: flex;
  252. flex-wrap: wrap;
  253. gap: 16px 50px;
  254. }
  255. .view-list {
  256. display: flex;
  257. flex-direction: column;
  258. :deep .el-carousel {
  259. flex: 1;
  260. background-color: #f8f8f8;
  261. border-radius: 4px;
  262. &__container {
  263. height: var(--carousel-height, 128px);
  264. }
  265. &__item {
  266. transition: none !important;
  267. }
  268. }
  269. .view-list-bottom {
  270. display: flex;
  271. flex-direction: column;
  272. li {
  273. border-top: 1px solid #ccc;
  274. }
  275. }
  276. }
  277. }
  278. </style>