PicturePreview.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <template>
  2. <div ref="pictureArea" class="picture-preview" :style="getAreaStyle()">
  3. <SerialNumberPosition v-if="isEnable(data.property.sn_display_mode)" :property="data.property" />
  4. <div ref="pictureAreaBox" class="main">
  5. <div class="view-area">
  6. <template v-if="isMore">
  7. <div v-if="isMore" class="picture-area">
  8. <template v-if="'list' === data.property.view_method">
  9. <el-carousel
  10. ref="pictureCarousel"
  11. class="view-list"
  12. indicator-position="none"
  13. :autoplay="false"
  14. :style="{ height: elementHeight - 144 - 32 + 'px' }"
  15. @change="handleChange"
  16. >
  17. <el-carousel-item v-for="(file, i) in data.file_list" :key="i">
  18. <el-image
  19. :id="file.file_id"
  20. :src="file.file_url"
  21. fit="contain"
  22. :preview-src-list="data.file_list.map((x) => x.file_url)"
  23. />
  24. </el-carousel-item>
  25. </el-carousel>
  26. <div class="container-box">
  27. <button v-if="viewLeftRightBtn" class="arrow left" @click="scroll(-1)">
  28. <i class="el-icon-arrow-left"></i>
  29. </button>
  30. <ul ref="container" class="view-list-bottom" :style="{ width: elementWidth + 'px' }">
  31. <li v-for="(file, i) in data.file_list" :key="i" @click="handleIndicatorClick(i)">
  32. <el-image :id="file.file_id" :src="file.file_url" fit="contain" />
  33. </li>
  34. </ul>
  35. <button v-if="viewLeftRightBtn" class="arrow right" @click="scroll(1)">
  36. <i class="el-icon-arrow-right"></i>
  37. </button>
  38. </div>
  39. </template>
  40. <ul v-else class="view-independent">
  41. <li v-for="(file, i) in data.file_list" :key="i" @click="handleIndicatorClick(i)">
  42. <el-image :id="file.file_id" :src="file.file_url" fit="contain" />
  43. </li>
  44. </ul>
  45. </div>
  46. </template>
  47. <template v-else>
  48. <div v-for="(file, i) in data.file_list" :key="i" class="alone-pic-area">
  49. <el-image
  50. :id="file.file_id"
  51. :src="file.file_url"
  52. fit="contain"
  53. :preview-src-list="data.file_list.map((x) => x.file_url)"
  54. :style="{ height: elementHeight + 'px' }"
  55. />
  56. </div>
  57. </template>
  58. <div v-if="'list' === data.property.view_method && isEnable(data.property.view_memo)" class="memo-area">
  59. <div v-for="(file, i) in data.file_info_list" :key="i">
  60. <div v-if="curPictureMemoIndex === i" class="title-div">{{ file.title ?? file.title }}</div>
  61. <div v-if="curPictureMemoIndex === i" class="memo-div">{{ file.intro ?? file.intro }}</div>
  62. </div>
  63. </div>
  64. </div>
  65. </div>
  66. </div>
  67. </template>
  68. <script>
  69. import { getPictureData } from '@/views/book/courseware/data/picture';
  70. import PreviewMixin from '../common/PreviewMixin';
  71. export default {
  72. name: 'PicturePreview',
  73. mixins: [PreviewMixin],
  74. inject: ['getDragStatus'],
  75. data() {
  76. return {
  77. data: getPictureData(),
  78. curImgIndex: 0,
  79. elementWidth: 0,
  80. elementHeight: 0,
  81. viewLeftRightBtn: false,
  82. fileLen: 0,
  83. elementID: '',
  84. curPictureMemoIndex: 0,
  85. isResizing: false,
  86. resizeObserver: null,
  87. isMore: false,
  88. };
  89. },
  90. watch: {
  91. data: {
  92. handler(val) {
  93. this.fileLen = val.file_list.length;
  94. if (this.fileLen > 1) {
  95. this.isMore = true;
  96. }
  97. if ((this.fileLen > 0 && this.data.property.view_method === 'list') || this.fileLen === 1) {
  98. const ele = this.$refs.pictureAreaBox;
  99. const sn_position = this.data.property.sn_position;
  100. const viewMemo = this.isEnable(this.data.property.view_memo);
  101. // 序号在左和右补齐序号高度,去掉padding(8*2)
  102. if (sn_position.includes('left') || sn_position.includes('right')) {
  103. this.elementWidth = viewMemo ? (ele.clientWidth - 16) * 0.8 : ele.clientWidth - 16;
  104. this.elementHeight = ele.clientHeight;
  105. } else {
  106. this.elementWidth = viewMemo ? ele.clientWidth * 0.8 : ele.clientWidth;
  107. this.elementHeight = ele.clientHeight;
  108. }
  109. // 统一图片播放模式位置调整和预览初始高度
  110. if (ele.clientHeight <= 0) {
  111. this.elementHeight = this.data.min_height - 60;
  112. }
  113. const mainEle = this.$refs.pictureArea;
  114. // 检查元素是否包含已知的类名
  115. mainEle.classList.forEach((className) => {
  116. // 排除已知的类名
  117. if (className !== 'audio-area') {
  118. // 打印另一个类名
  119. this.elementID = className;
  120. }
  121. });
  122. }
  123. },
  124. deep: true,
  125. },
  126. elementWidth() {
  127. this.isViewLeftRightBtn();
  128. },
  129. },
  130. mounted() {
  131. this.$nextTick(() => {
  132. const canvasElement = document.querySelector('.canvas');
  133. if (!canvasElement) {
  134. const ele = this.$refs.pictureAreaBox;
  135. const sn_position = this.data.property.sn_position;
  136. const viewMemo = this.isEnable(this.data.property.view_memo);
  137. // 序号在左和右补齐序号高度,去掉padding(8*2)
  138. if (sn_position.includes('left') || sn_position.includes('right')) {
  139. this.elementWidth = viewMemo ? (ele.clientWidth - 16) * 0.8 : ele.clientWidth - 16;
  140. this.elementHeight = ele.clientHeight;
  141. } else {
  142. this.elementWidth = viewMemo ? ele.clientWidth * 0.8 : ele.clientWidth;
  143. this.elementHeight = ele.clientHeight;
  144. }
  145. this.fileLen = this.data.file_list.length;
  146. if (this.fileLen > 1) {
  147. this.isMore = true;
  148. }
  149. this.isViewLeftRightBtn();
  150. return;
  151. }
  152. this.resizeObserver = new ResizeObserver((entries) => {
  153. if (!this.getDragStatus()) return;
  154. this.isResizing = true; // 标记为调整中
  155. for (let entry of entries) {
  156. window.requestAnimationFrame(() => {
  157. const sn_position = this.data.property.sn_position;
  158. const viewMemo = this.isEnable(this.data.property.view_memo);
  159. // 序号在上方和下方减去序号高度,在左右去掉padding(8*2)
  160. let w = viewMemo ? (entry.contentRect.width - 16) * 0.8 : entry.contentRect.width - 16;
  161. let h = entry.contentRect.height;
  162. if (sn_position.includes('top') || sn_position.includes('bottom')) {
  163. w = viewMemo ? entry.contentRect.width * 0.8 : entry.contentRect.width;
  164. h = entry.contentRect.height - 30;
  165. }
  166. if (this.elementWidth === w) {
  167. this.elementHeight = h;
  168. } else {
  169. this.elementWidth = w;
  170. }
  171. });
  172. }
  173. // 防抖:100ms 后恢复监听
  174. setTimeout(() => {
  175. this.isResizing = false;
  176. }, 500);
  177. });
  178. this.resizeObserver.observe(this.$el);
  179. });
  180. },
  181. beforeDestroy() {
  182. if (this.resizeObserver) {
  183. this.resizeObserver.disconnect();
  184. }
  185. },
  186. methods: {
  187. // 是否显示左右箭头
  188. isViewLeftRightBtn() {
  189. // 计算底部列表图片宽度
  190. let listWidth = this.fileLen * this.data.min_width + 13 * (this.fileLen - 1);
  191. if (listWidth > this.elementWidth) {
  192. this.viewLeftRightBtn = true;
  193. } else {
  194. this.viewLeftRightBtn = false;
  195. }
  196. },
  197. handleIndicatorClick(index) {
  198. // 获取 Carousel 实例
  199. const carousel = this.$refs.pictureCarousel;
  200. // 切换到对应索引的图片
  201. carousel.setActiveItem(index);
  202. this.curPictureMemoIndex = index;
  203. },
  204. handleChange(index) {
  205. this.curPictureMemoIndex = index;
  206. },
  207. // 滚动图片列表
  208. scroll(direction) {
  209. const container = this.$refs.container;
  210. const step = Number(this.data.min_width) + 13; // 每次滚动的距离
  211. this.scrollPosition += step * direction;
  212. container.scrollLeft += step * direction;
  213. },
  214. autoResize(entry) {
  215. window.requestAnimationFrame(() => {
  216. const sn_position = this.data.property.sn_position;
  217. const viewMemo = this.isEnable(this.data.property.view_memo);
  218. // 序号在上方和下方减去序号高度,在左右去掉padding(8*2)
  219. if (sn_position.includes('top') || sn_position.includes('bottom')) {
  220. this.elementWidth = viewMemo ? entry.contentRect.width * 0.8 : entry.contentRect.width;
  221. // this.elementHeight = entry.contentRect.height - 30;
  222. } else {
  223. this.elementWidth = viewMemo ? (entry.contentRect.width - 16) * 0.8 : entry.contentRect.width - 16;
  224. this.elementHeight = entry.contentRect.height;
  225. }
  226. });
  227. },
  228. },
  229. };
  230. </script>
  231. <style lang="scss" scoped>
  232. @use '@/styles/mixin.scss' as *;
  233. .picture-preview {
  234. @include preview-base;
  235. .view-area {
  236. display: flex;
  237. column-gap: 7px;
  238. .alone-pic-area {
  239. display: flex;
  240. align-items: center;
  241. justify-content: center;
  242. width: 100%;
  243. .el-image {
  244. min-height: 128px;
  245. cursor: pointer;
  246. }
  247. }
  248. .memo-area {
  249. padding-left: 5px;
  250. text-align: left;
  251. border-left: 1px solid #eee;
  252. .title-div {
  253. font-size: 16px;
  254. font-weight: 600;
  255. }
  256. .memo-div {
  257. color: #706f78;
  258. overflow-wrap: break-word;
  259. }
  260. }
  261. :deep .el-carousel {
  262. margin-bottom: 17px;
  263. background-color: #d9d9d9;
  264. &__container::before {
  265. display: inline-block;
  266. padding-bottom: 55%;
  267. content: '';
  268. }
  269. &__container {
  270. height: 100%;
  271. }
  272. &__item {
  273. display: flex;
  274. justify-content: center;
  275. text-align: center;
  276. }
  277. }
  278. .container-box {
  279. position: relative;
  280. .left {
  281. left: 0;
  282. }
  283. .arrow {
  284. position: absolute;
  285. top: 0;
  286. z-index: 10;
  287. height: 144px;
  288. text-align: center;
  289. background-color: rgba(0, 0, 0, 10%);
  290. border-radius: 0;
  291. }
  292. .arrow:hover {
  293. background-color: rgba(0, 0, 0, 30%);
  294. }
  295. .right {
  296. right: 0;
  297. }
  298. .view-list-bottom {
  299. display: flex;
  300. flex-wrap: nowrap;
  301. column-gap: 13px;
  302. min-width: 144px;
  303. overflow: hidden;
  304. li {
  305. display: flex;
  306. align-items: center;
  307. justify-content: center;
  308. background-color: #d9d9d9;
  309. }
  310. .el-image {
  311. width: 144px;
  312. height: 144px;
  313. cursor: pointer;
  314. }
  315. }
  316. }
  317. .view-independent {
  318. display: flex;
  319. flex-wrap: wrap;
  320. gap: 40px;
  321. li {
  322. display: flex;
  323. align-items: center;
  324. justify-content: center;
  325. background-color: #d9d9d9;
  326. }
  327. .el-image {
  328. width: 144px;
  329. height: 144px;
  330. }
  331. }
  332. }
  333. }
  334. </style>