Przeglądaj źródła

修复位置调整页中图片组件的序号显示在上方或下方时,序号那一行的高度没有被预留。手动拖拽高度,图片才恢复正常大小

dsy 6 dni temu
rodzic
commit
7f2bf3f0dd

+ 45 - 70
src/views/book/courseware/preview/components/picture/PicturePreview.vue

@@ -104,24 +104,9 @@ export default {
       handler(val) {
         this.fileLen = val.file_list.length;
         if (this.fileLen === 0) return;
-        if (this.data.property.view_method === 'list') {
-          const ele = this.$refs.pictureAreaBox;
-          const sn_position = this.data.property.sn_position;
-          const viewMemo = this.isEnable(this.data.property.view_memo);
-          // 序号在左和右补齐序号高度,去掉padding(8*2)
-          if (sn_position.includes('left') || sn_position.includes('right')) {
-            this.elementWidth = viewMemo ? (ele.clientWidth - 16) * 0.8 : ele.clientWidth - 16;
-            this.elementHeight = ele.clientHeight;
-          } else {
-            this.elementWidth = viewMemo ? ele.clientWidth * 0.8 : ele.clientWidth;
-            this.elementHeight = ele.clientHeight;
-          }
-          // 统一图片播放模式位置调整和预览初始高度
-          // if (ele.clientHeight <= 0) {
-          //    this.elementHeight = this.data.min_height - 60;
-          // }
-
-          const mainEle = this.$refs.pictureArea;
+
+        const mainEle = this.$refs.pictureArea;
+        if (mainEle) {
           // 检查元素是否包含已知的类名
           mainEle.classList.forEach((className) => {
             // 排除已知的类名
@@ -132,25 +117,12 @@ export default {
           });
         }
 
-        if (this.data.property.view_method === 'independent') {
-          const ele = this.$refs.pictureAreaBox;
-          const sn_position = this.data.property.sn_position;
-          // 序号在左和右补齐序号高度,去掉padding(8*2)
-          if (sn_position.includes('left') || sn_position.includes('right')) {
-            this.elementWidth = ele.clientWidth - 16;
-            this.elementHeight = ele.clientHeight;
-          } else {
-            this.elementWidth = ele.clientWidth;
-            this.elementHeight = ele.clientHeight;
-          }
-          // 统一图片播放模式位置调整和预览初始高度
-          // if (ele.clientHeight <= 0) {
-          //    this.elementHeight = this.data.min_height - 60;
-          // }
-        }
-
-        this.elementHeight = Math.max(this.elementHeight, this.data.min_height);
-        this.$emit('handleHeightChange', this.id, `${this.elementHeight}px`);
+        // 序号位置/显示方式变化会改变组件内部布局,等 DOM 更新后再测量,否则量到的是切换前的布局
+        this.$nextTick(() => {
+          this.updateElementSize();
+          // 上报组件整体高度(含序号占用高度),否则图片会挤占序号的位置
+          this.$emit('handleHeightChange', this.id, `${this.elementHeight + this.getSerialNumberHeight()}px`);
+        });
       },
       deep: true,
     },
@@ -163,45 +135,17 @@ export default {
   },
   mounted() {
     this.$nextTick(() => {
-      const ele = this.$refs.pictureAreaBox;
-      if (!ele) return;
-      const sn_position = this.data.property.sn_position;
-      const viewMemo = this.isEnable(this.data.property.view_memo);
-      // 序号在左和右补齐序号高度,去掉padding(8*2)
-      if (sn_position.includes('left') || sn_position.includes('right')) {
-        this.elementWidth = viewMemo ? (ele.clientWidth - 16) * 0.8 : ele.clientWidth - 16;
-        this.elementHeight = ele.clientHeight;
-      } else {
-        this.elementWidth = viewMemo ? ele.clientWidth * 0.8 : ele.clientWidth;
-        this.elementHeight = ele.clientHeight;
-      }
       this.fileLen = this.data.file_list.length;
+      this.updateElementSize();
 
-      this.resizeObserver = new ResizeObserver((entries) => {
+      this.resizeObserver = new ResizeObserver(() => {
         if (!this.getDragStatus()) return;
         this.isResizing = true; // 标记为调整中
 
-        for (let entry of entries) {
-          window.requestAnimationFrame(() => {
-            const sn_position = this.data.property.sn_position;
-            const viewMemo = this.isEnable(this.data.property.view_memo);
-            // 序号在上方和下方减去序号高度,在左右去掉padding(8*2)
-            let w = viewMemo ? (entry.contentRect.width - 16) * 0.8 : entry.contentRect.width - 16;
-            let h = entry.contentRect.height;
-            if (sn_position.includes('top') || sn_position.includes('bottom')) {
-              w = viewMemo ? entry.contentRect.width * 0.8 : entry.contentRect.width;
-              h = entry.contentRect.height - 30;
-            }
-            if (this.elementWidth === w) {
-              this.elementHeight = h;
-            } else {
-              this.elementWidth = w;
-            }
-            this.elementHeight = Math.max(this.elementHeight, this.data.min_height);
-          });
-        }
+        // 拖拽调整期间按组件实际尺寸重新计算显示区域
+        window.requestAnimationFrame(() => this.updateElementSize());
 
-        // 防抖:100ms 后恢复监听
+        // 防抖:500ms 后恢复监听
         setTimeout(() => {
           this.isResizing = false;
         }, 500);
@@ -215,6 +159,34 @@ export default {
     }
   },
   methods: {
+    /**
+     * 序号占用的高度:序号高度 24 + 网格间距 6(序号在左右时不占用高度)
+     */
+    getSerialNumberHeight() {
+      const { sn_display_mode: snDisplayMode, sn_position: snPosition = '' } = this.data.property;
+      if (!this.isEnable(snDisplayMode)) return 0;
+      return snPosition.includes('top') || snPosition.includes('bottom') ? 30 : 0;
+    },
+    /**
+     * 按组件实际尺寸计算图片显示区域尺寸(高度不含序号占用的高度)
+     */
+    updateElementSize() {
+      const root = this.$refs.pictureArea;
+      const box = this.$refs.pictureAreaBox;
+      if (!root || !box) return;
+
+      const { view_method: viewMethod, view_memo: viewMemo, sn_position: snPosition = '' } = this.data.property;
+      const isSidePosition = snPosition.includes('left') || snPosition.includes('right');
+      const viewMemoEnable = viewMethod === 'list' && this.isEnable(viewMemo);
+      // 序号在左右时去掉 padding(8*2)
+      const boxWidth = isSidePosition ? box.clientWidth - 16 : box.clientWidth;
+
+      this.elementWidth = viewMemoEnable ? boxWidth * 0.8 : boxWidth;
+      this.elementHeight = Math.max(
+        root.clientHeight - this.getSerialNumberHeight(),
+        Number(this.data.min_height) || 0,
+      );
+    },
     getVideoItemStyle() {
       if (this.data.property.view_method !== 'independent') {
         return {};
@@ -301,6 +273,9 @@ export default {
 
   .main {
     position: relative;
+
+    // 保证图片内容不会撑高 1fr 行、挤占序号所占的行
+    min-height: 0;
     overflow: auto hidden !important;
 
     // Firefox 滚动条样式