fix(editor): 页面列表是否可以滚动判断不准确

This commit is contained in:
roymondchen 2022-09-14 20:01:15 +08:00 committed by jia000
parent 33a09cccc7
commit 3ff823a59f
3 changed files with 50 additions and 47 deletions

View File

@ -15,8 +15,8 @@
<el-collapse-item v-if="group.items && group.items.length" :key="index" :name="index"> <el-collapse-item v-if="group.items && group.items.length" :key="index" :name="index">
<template #title><i class="el-icon-s-grid"></i>{{ group.title }}</template> <template #title><i class="el-icon-s-grid"></i>{{ group.title }}</template>
<div <div
class="component-item"
v-for="item in group.items" v-for="item in group.items"
class="component-item"
draggable="true" draggable="true"
:key="item.type" :key="item.type"
@click="appendComponent(item)" @click="appendComponent(item)"

View File

@ -6,27 +6,33 @@
class="m-editor-page-bar-item m-editor-page-bar-item-icon" class="m-editor-page-bar-item m-editor-page-bar-item-icon"
@click="addPage" @click="addPage"
> >
<el-icon><plus></plus></el-icon> <Icon :icon="Plus"></Icon>
</div> </div>
<div v-else style="width: 21px"></div> <div v-else style="width: 21px"></div>
<div v-if="canScroll" class="m-editor-page-bar-item m-editor-page-bar-item-icon" @click="scroll('left')"> <div v-if="canScroll" class="m-editor-page-bar-item m-editor-page-bar-item-icon" @click="scroll('left')">
<el-icon><arrow-left-bold></arrow-left-bold></el-icon> <Icon :icon="ArrowLeftBold"></Icon>
</div> </div>
<div v-if="root" class="m-editor-page-bar-items" ref="itemsContainer" :style="`width: ${itemsContainerWidth}px`"> <div
v-if="pageLength"
class="m-editor-page-bar-items"
ref="itemsContainer"
:style="`width: ${itemsContainerWidth}px`"
>
<slot></slot> <slot></slot>
</div> </div>
<div v-if="canScroll" class="m-editor-page-bar-item m-editor-page-bar-item-icon" @click="scroll('right')"> <div v-if="canScroll" class="m-editor-page-bar-item m-editor-page-bar-item-icon" @click="scroll('right')">
<el-icon><arrow-right-bold></arrow-right-bold></el-icon> <Icon :icon="ArrowRightBold"></Icon>
</div> </div>
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { computed, inject, onMounted, onUnmounted, ref, toRaw, watch } from 'vue'; import { computed, inject, nextTick, onMounted, onUnmounted, ref, toRaw, watch } from 'vue';
import { ArrowLeftBold, ArrowRightBold, Plus } from '@element-plus/icons-vue'; import { ArrowLeftBold, ArrowRightBold, Plus } from '@element-plus/icons-vue';
import { MApp, NodeType } from '@tmagic/schema'; import { NodeType } from '@tmagic/schema';
import Icon from '../../components/Icon.vue';
import type { Services } from '../../type'; import type { Services } from '../../type';
import { generatePageNameByApp } from '../../utils/editor'; import { generatePageNameByApp } from '../../utils/editor';
@ -34,32 +40,40 @@ const services = inject<Services>('services');
const editorService = services?.editorService; const editorService = services?.editorService;
const uiService = services?.uiService; const uiService = services?.uiService;
const pageBar = ref<HTMLDivElement>();
const itemsContainer = ref<HTMLDivElement>(); const itemsContainer = ref<HTMLDivElement>();
const pageBarWidth = ref(0);
const canScroll = ref(false); const canScroll = ref(false);
const showAddPageButton = computed(() => uiService?.get('showAddPageButton')); const showAddPageButton = computed(() => uiService?.get('showAddPageButton'));
// const itemsContainerWidth = ref(0);
const itemsContainerWidth = computed(() => pageBarWidth.value - 35 * 2 - (showAddPageButton.value ? 35 : 21));
let translateLeft = 0;
const resizeObserver = new ResizeObserver((entries) => {
for (const { contentRect } of entries) {
const { width } = contentRect;
pageBarWidth.value = width || 0;
setCanScroll();
}
});
const setCanScroll = () => { const setCanScroll = () => {
if (itemsContainer.value) { //
canScroll.value = itemsContainer.value.scrollWidth > itemsContainerWidth.value; // 37 = icon width 16 + padding 10 * 2 + border-right 1
} itemsContainerWidth.value = (pageBar.value?.clientWidth || 0) - 37 * 2 - (showAddPageButton.value ? 37 : 21);
nextTick(() => {
if (itemsContainer.value) {
canScroll.value = itemsContainer.value.scrollWidth - itemsContainerWidth.value > 1;
}
});
}; };
const resizeObserver = new ResizeObserver(() => {
setCanScroll();
});
const pageBar = ref<HTMLDivElement>();
onMounted(() => {
pageBar.value && resizeObserver.observe(pageBar.value);
});
onUnmounted(() => {
resizeObserver.disconnect();
});
let translateLeft = 0;
const scroll = (type: 'left' | 'right' | 'start' | 'end') => { const scroll = (type: 'left' | 'right' | 'start' | 'end') => {
if (!itemsContainer.value) return; if (!itemsContainer.value) return;
@ -86,30 +100,19 @@ const scroll = (type: 'left' | 'right' | 'start' | 'end') => {
itemsContainer.value.style.transform = `translate(${translateLeft}px, 0px)`; itemsContainer.value.style.transform = `translate(${translateLeft}px, 0px)`;
}; };
onMounted(() => { const pageLength = computed(() => editorService?.get<number>('pageLength'));
pageBar.value && resizeObserver.observe(pageBar.value);
watch(pageLength, (length = 0, preLength = 0) => {
setTimeout(() => {
setCanScroll();
if (length < preLength) {
scroll('start');
} else {
scroll('end');
}
});
}); });
onUnmounted(() => {
resizeObserver.disconnect();
});
const root = computed(() => editorService?.get<MApp>('root'));
watch(
() => root.value?.items.length,
(length = 0, preLength = 0) => {
setTimeout(() => {
setCanScroll();
if (length < preLength) {
scroll('start');
} else {
scroll('end');
}
});
},
);
const addPage = () => { const addPage = () => {
if (!editorService) return; if (!editorService) return;
const pageConfig = { const pageConfig = {

View File

@ -1,5 +1,5 @@
<template> <template>
<div class="m-editor-workspace" tabindex="1" ref="workspace"> <div class="m-editor-workspace" tabindex="-1" ref="workspace">
<slot name="stage"> <slot name="stage">
<MagicStage :key="page?.id"></MagicStage> <MagicStage :key="page?.id"></MagicStage>
</slot> </slot>