Skip to content

Commit

Permalink
feat(editor): 页面/页面片不再使用tab分开显示,新增搜索页面
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen authored and jia000 committed Dec 9, 2024
1 parent fc38fc3 commit ed6d9b2
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 215 deletions.
1 change: 1 addition & 0 deletions packages/editor/src/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
</template>

<template #page-bar><slot name="page-bar"></slot></template>
<template #page-bar-add-button><slot name="page-bar-add-button"></slot></template>
<template #page-bar-title="{ page }"><slot name="page-bar-title" :page="page"></slot></template>
<template #page-bar-popover="{ page }"><slot name="page-bar-popover" :page="page"></slot></template>
<template #page-list-popover="{ list }"><slot name="page-list-popover" :list="list"></slot></template>
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/layouts/Framework.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

<slot name="page-bar">
<PageBar :disabled-page-fragment="disabledPageFragment" :page-bar-sort-options="pageBarSortOptions">
<template #page-bar-add-button><slot name="page-bar-add-button"></slot></template>
<template #page-bar-title="{ page }"><slot name="page-bar-title" :page="page"></slot></template>
<template #page-bar-popover="{ page }"><slot name="page-bar-popover" :page="page"></slot></template>
<template #page-list-popover="{ list }"><slot name="page-list-popover" :list="list"></slot></template>
Expand Down
38 changes: 29 additions & 9 deletions packages/editor/src/layouts/page-bar/AddButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,31 @@
v-if="showAddPageButton"
id="m-editor-page-bar-add-icon"
class="m-editor-page-bar-item m-editor-page-bar-item-icon"
@click="addPage"
>
<Icon :icon="Plus"></Icon>
<TMagicPopover popper-class="data-source-list-panel-add-menu">
<template #reference>
<Icon :icon="Plus"></Icon>
</template>

<ToolButton
:data="{
type: 'button',
text: '页面',
handler: () => {
addPage(NodeType.PAGE);
},
}"
></ToolButton>
<ToolButton
:data="{
type: 'button',
text: '页面片',
handler: () => {
addPage(NodeType.PAGE_FRAGMENT);
},
}"
></ToolButton>
</TMagicPopover>
</div>
<div v-else style="width: 21px"></div>
</template>
Expand All @@ -15,32 +37,30 @@ import { computed, inject, toRaw } from 'vue';
import { Plus } from '@element-plus/icons-vue';
import { NodeType } from '@tmagic/core';
import { TMagicPopover } from '@tmagic/design';
import Icon from '@editor/components/Icon.vue';
import ToolButton from '@editor/components/ToolButton.vue';
import type { Services } from '@editor/type';
import { generatePageNameByApp } from '@editor/utils/editor';
defineOptions({
name: 'MEditorPageBarAddButton',
});
const props = defineProps<{
type: NodeType.PAGE | NodeType.PAGE_FRAGMENT;
}>();
const services = inject<Services>('services');
const uiService = services?.uiService;
const editorService = services?.editorService;
const showAddPageButton = computed(() => uiService?.get('showAddPageButton'));
const addPage = () => {
const addPage = (type: NodeType.PAGE | NodeType.PAGE_FRAGMENT) => {
if (!editorService) return;
const root = toRaw(editorService.get('root'));
if (!root) throw new Error('root 不能为空');
const pageConfig = {
type: props.type,
name: generatePageNameByApp(root, props.type),
type,
name: generatePageNameByApp(root, type),
items: [],
};
editorService.add(pageConfig);
Expand Down
86 changes: 25 additions & 61 deletions packages/editor/src/layouts/page-bar/PageBar.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<template>
<div class="m-editor-page-bar-tabs">
<SwitchTypeButton v-if="!disabledPageFragment" v-model="active" />

<PageBarScrollContainer :type="active" :page-bar-sort-options="pageBarSortOptions">
<PageBarScrollContainer :page-bar-sort-options="pageBarSortOptions" :length="list.length">
<template #prepend>
<AddButton :type="active"></AddButton>
<slot name="page-bar-add-button"><AddButton></AddButton></slot>

<Search v-model:query="query"></Search>
<PageList :list="list">
<template #page-list-popover="{ list }"><slot name="page-list-popover" :list="list"></slot></template>
</PageList>
Expand Down Expand Up @@ -63,21 +63,19 @@
</template>

<script lang="ts" setup>
import { computed, inject, ref, watch } from 'vue';
import { computed, inject, ref } from 'vue';
import { CaretBottom, Delete, DocumentCopy } from '@element-plus/icons-vue';
import { type Id, type MPage, type MPageFragment, NodeType } from '@tmagic/core';
import { TMagicIcon, TMagicPopover } from '@tmagic/design';
import { isPage, isPageFragment } from '@tmagic/utils';
import ToolButton from '@editor/components/ToolButton.vue';
import type { PageBarSortOptions, Services } from '@editor/type';
import { getPageFragmentList, getPageList } from '@editor/utils';
import AddButton from './AddButton.vue';
import PageBarScrollContainer from './PageBarScrollContainer.vue';
import PageList from './PageList.vue';
import SwitchTypeButton from './SwitchTypeButton.vue';
import Search from './Search.vue';
defineOptions({
name: 'MEditorPageBar',
Expand All @@ -88,69 +86,35 @@ defineProps<{
pageBarSortOptions?: PageBarSortOptions;
}>();
const active = ref<NodeType.PAGE | NodeType.PAGE_FRAGMENT>(NodeType.PAGE);
const services = inject<Services>('services');
const editorService = services?.editorService;
const root = computed(() => editorService?.get('root'));
const page = computed(() => editorService?.get('page'));
const pageList = computed(() => getPageList(root.value));
const pageFragmentList = computed(() => getPageFragmentList(root.value));
const list = computed(() => (active.value === NodeType.PAGE ? pageList.value : pageFragmentList.value));
const activePage = ref<Id>('');
const activePageFragment = ref<Id>('');
watch(
page,
(page) => {
if (!page) {
if (active.value === NodeType.PAGE) {
activePage.value = '';
}
if (active.value === NodeType.PAGE_FRAGMENT) {
activePageFragment.value = '';
}
return;
}
const query = ref<{
pageType: NodeType[];
keyword: string;
}>({
pageType: [NodeType.PAGE, NodeType.PAGE_FRAGMENT],
keyword: '',
});
if (isPage(page)) {
activePage.value = page?.id;
if (active.value !== NodeType.PAGE) {
active.value = NodeType.PAGE;
}
} else if (isPageFragment(page)) {
activePageFragment.value = page?.id;
if (active.value !== NodeType.PAGE_FRAGMENT) {
active.value = NodeType.PAGE_FRAGMENT;
}
}
},
{
immediate: true,
},
);
watch(active, (active) => {
if (active === NodeType.PAGE) {
if (!activePage.value && !pageList.value.length) {
editorService?.selectRoot();
return;
}
switchPage(activePage.value);
return;
const list = computed(() => {
const { pageType, keyword } = query.value;
if (pageType.length === 0) {
return [];
}
if (active === NodeType.PAGE_FRAGMENT) {
// 之前没有选中过页面片并且当前没有页面片
if (!activePageFragment.value && !pageFragmentList.value.length) {
editorService?.selectRoot();
return;
return (root.value?.items || []).filter((item) => {
if (pageType.includes(item.type)) {
if (keyword) {
return item.name?.includes(keyword);
}
return true;
}
switchPage(activePageFragment.value || pageFragmentList.value[0].id);
}
return false;
});
});
const switchPage = (id: Id) => {
Expand Down
129 changes: 45 additions & 84 deletions packages/editor/src/layouts/page-bar/PageBarScrollContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
<Icon :icon="ArrowLeftBold"></Icon>
</div>

<div
v-if="(type === NodeType.PAGE && pageLength) || (type === NodeType.PAGE_FRAGMENT && pageFragmentLength)"
class="m-editor-page-bar-items"
ref="itemsContainer"
:style="`width: ${itemsContainerWidth}px`"
>
<div v-if="length" class="m-editor-page-bar-items" ref="itemsContainer" :style="`width: ${itemsContainerWidth}px`">
<slot></slot>
</div>

Expand All @@ -22,21 +17,11 @@
</template>

<script setup lang="ts">
import {
computed,
type ComputedRef,
inject,
nextTick,
onBeforeUnmount,
onMounted,
ref,
watch,
type WatchStopHandle,
} from 'vue';
import { computed, inject, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue';
import { ArrowLeftBold, ArrowRightBold } from '@element-plus/icons-vue';
import Sortable, { SortableEvent } from 'sortablejs';
import Sortable, { type SortableEvent } from 'sortablejs';
import { Id, NodeType } from '@tmagic/core';
import type { Id } from '@tmagic/core';
import Icon from '@editor/components/Icon.vue';
import type { PageBarSortOptions, Services } from '@editor/type';
Expand All @@ -46,8 +31,8 @@ defineOptions({
});
const props = defineProps<{
type: NodeType.PAGE | NodeType.PAGE_FRAGMENT;
pageBarSortOptions?: PageBarSortOptions;
length: number;
}>();
const services = inject<Services>('services');
Expand All @@ -63,11 +48,12 @@ const showPageListButton = computed(() => uiService?.get('showPageListButton'));
const itemsContainerWidth = ref(0);
const setCanScroll = () => {
// 减去新增、左移、右移三个按钮的宽度
// 减去新增、搜索、页面列表、左移、右移5个按钮的宽度
// 37 = icon width 16 + padding 10 * 2 + border-right 1
itemsContainerWidth.value =
(pageBar.value?.clientWidth || 0) -
37 * 2 -
37 -
(showAddPageButton.value ? 37 : 21) -
(showPageListButton.value ? 37 : 0);
Expand Down Expand Up @@ -119,71 +105,46 @@ const scroll = (type: 'left' | 'right' | 'start' | 'end') => {
itemsContainer.value.style.transform = `translate(${translateLeft}px, 0px)`;
};
const pageLength = computed(() => editorService?.get('pageLength') || 0);
const pageFragmentLength = computed(() => editorService?.get('pageFragmentLength') || 0);
const crateWatchLength = (length: ComputedRef<number>) =>
watch(
length,
(length = 0, preLength = 0) => {
setTimeout(() => {
setCanScroll();
if (length < preLength) {
scroll('start');
} else {
scroll('end');
}
if (length > 1) {
const el = document.querySelector('.m-editor-page-bar-items') as HTMLElement;
let beforeDragList: Id[] = [];
const options = {
...{
dataIdAttr: 'page-id', // 获取排序后的数据
onStart: async (event: SortableEvent) => {
if (typeof props.pageBarSortOptions?.beforeStart === 'function') {
await props.pageBarSortOptions.beforeStart(event, sortable);
}
beforeDragList = sortable.toArray();
},
onUpdate: async (event: SortableEvent) => {
await editorService?.sort(
beforeDragList[event.oldIndex as number],
beforeDragList[event.newIndex as number],
);
if (typeof props.pageBarSortOptions?.afterUpdate === 'function') {
await props.pageBarSortOptions.afterUpdate(event, sortable);
}
},
watch(
() => props.length,
(length = 0, preLength = 0) => {
setTimeout(() => {
setCanScroll();
if (length < preLength) {
scroll('start');
} else {
scroll('end');
}
if (length > 1) {
const el = document.querySelector('.m-editor-page-bar-items') as HTMLElement;
let beforeDragList: Id[] = [];
const options = {
...{
dataIdAttr: 'page-id', // 获取排序后的数据
onStart: async (event: SortableEvent) => {
if (typeof props.pageBarSortOptions?.beforeStart === 'function') {
await props.pageBarSortOptions.beforeStart(event, sortable);
}
beforeDragList = sortable.toArray();
},
...{
...(props.pageBarSortOptions ? props.pageBarSortOptions : {}),
onUpdate: async (event: SortableEvent) => {
await editorService?.sort(
beforeDragList[event.oldIndex as number],
beforeDragList[event.newIndex as number],
);
if (typeof props.pageBarSortOptions?.afterUpdate === 'function') {
await props.pageBarSortOptions.afterUpdate(event, sortable);
}
},
};
if (!el) return;
const sortable = new Sortable(el, options);
}
});
},
{
immediate: true,
},
);
let unWatchPageLength: WatchStopHandle | null;
let unWatchPageFragmentLength: WatchStopHandle | null;
watch(
() => props.type,
(type) => {
if (type === NodeType.PAGE) {
unWatchPageFragmentLength?.();
unWatchPageFragmentLength = null;
unWatchPageLength = crateWatchLength(pageLength);
} else {
unWatchPageLength?.();
unWatchPageLength = null;
unWatchPageFragmentLength = crateWatchLength(pageFragmentLength);
}
},
...{
...(props.pageBarSortOptions ? props.pageBarSortOptions : {}),
},
};
if (!el) return;
const sortable = new Sortable(el, options);
}
});
},
{
immediate: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/layouts/page-bar/PageList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defineOptions({
});
defineProps<{
list: MPage[] | MPageFragment[];
list: (MPage | MPageFragment)[];
}>();
const services = inject<Services>('services');
Expand Down
Loading

0 comments on commit ed6d9b2

Please sign in to comment.