Skip to content

Commit

Permalink
feat(editor): 新增自定义右键菜单函数配置
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen committed Dec 18, 2023
1 parent 4ec0b69 commit 698c345
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 110 deletions.
4 changes: 2 additions & 2 deletions packages/editor/src/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<template #sidebar>
<slot name="sidebar" :editorService="editorService">
<Sidebar :data="sidebar" :layer-content-menu="layerContentMenu">
<Sidebar :data="sidebar" :layer-content-menu="layerContentMenu" :custom-content-menu="customContentMenu">
<template #layer-panel-header>
<slot name="layer-panel-header"></slot>
</template>
Expand Down Expand Up @@ -58,7 +58,7 @@

<template #workspace>
<slot name="workspace" :editorService="editorService">
<Workspace :stage-content-menu="stageContentMenu">
<Workspace :stage-content-menu="stageContentMenu" :custom-content-menu="customContentMenu">
<template #stage><slot name="stage"></slot></template>
<template #workspace-content><slot name="workspace-content" :editorService="editorService"></slot></template>
<template #page-bar-title="{ page }"><slot name="page-bar-title" :page="page"></slot></template>
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/editorProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export interface EditorProps {
collectorOptions?: CustomTargetOptions;
guidesOptions?: Partial<GuidesOptions>;
disabledMultiSelect?: boolean;
customContentMenu?: (menus: (MenuButton | MenuComponent)[], type: string) => (MenuButton | MenuComponent)[];
}

export const defaultEditorProps = {
Expand Down
2 changes: 2 additions & 0 deletions packages/editor/src/layouts/sidebar/Sidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ const props = withDefaults(
defineProps<{
data: SideBarData;
layerContentMenu: (MenuButton | MenuComponent)[];
customContentMenu?: (menus: (MenuButton | MenuComponent)[], type: string) => (MenuButton | MenuComponent)[];
}>(),
{
data: () => ({ type: 'tabs', status: '组件', items: ['component-list', 'layer', 'code-block', 'data-source'] }),
Expand Down Expand Up @@ -187,6 +188,7 @@ const getItemConfig = (data: SideItem): SideComponent => {
text: '已选组件',
props: {
layerContentMenu: props.layerContentMenu,
customContentMenu: props.customContentMenu,
},
component: LayerPanel,
slots: {},
Expand Down
64 changes: 38 additions & 26 deletions packages/editor/src/layouts/sidebar/layer/LayerMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ defineOptions({
name: 'MEditorLayerMenu',
});
const props = defineProps<{
layerContentMenu: (MenuButton | MenuComponent)[];
}>();
const props = withDefaults(
defineProps<{
layerContentMenu: (MenuButton | MenuComponent)[];
customContentMenu?: (menus: (MenuButton | MenuComponent)[], type: string) => (MenuButton | MenuComponent)[];
}>(),
{
layerContentMenu: () => [],
customContentMenu: (menus: (MenuButton | MenuComponent)[]) => menus,
},
);
const emit = defineEmits<{
'collapse-all': [];
Expand Down Expand Up @@ -82,29 +89,34 @@ const getSubMenuData = computed<MenuButton[]>(() => {
return [];
});
const menuData = computed<(MenuButton | MenuComponent)[]>(() => [
{
type: 'button',
text: '全部折叠',
icon: FolderMinusIcon,
display: () => isPage(node.value),
handler: () => {
emit('collapse-all');
},
},
{
type: 'button',
text: '新增',
icon: markRaw(Plus),
display: () => node.value?.items && nodes.value?.length === 1,
items: getSubMenuData.value,
},
useCopyMenu(),
usePasteMenu(),
useDeleteMenu(),
useMoveToMenu(services),
...props.layerContentMenu,
]);
const menuData = computed<(MenuButton | MenuComponent)[]>(() =>
props.customContentMenu(
[
{
type: 'button',
text: '全部折叠',
icon: FolderMinusIcon,
display: () => isPage(node.value),
handler: () => {
emit('collapse-all');
},
},
{
type: 'button',
text: '新增',
icon: markRaw(Plus),
display: () => node.value?.items && nodes.value?.length === 1,
items: getSubMenuData.value,
},
useCopyMenu(),
usePasteMenu(),
useDeleteMenu(),
useMoveToMenu(services),
...props.layerContentMenu,
],
'layer',
),
);
const show = (e: MouseEvent) => {
menu.value?.show(e);
Expand Down
8 changes: 7 additions & 1 deletion packages/editor/src/layouts/sidebar/layer/LayerPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@
</Tree>

<Teleport to="body">
<LayerMenu ref="menu" :layer-content-menu="layerContentMenu" @collapse-all="collapseAllHandler"></LayerMenu>
<LayerMenu
ref="menu"
:layer-content-menu="layerContentMenu"
:custom-content-menu="customContentMenu"
@collapse-all="collapseAllHandler"
></LayerMenu>
</Teleport>
</TMagicScrollbar>
</template>
Expand Down Expand Up @@ -65,6 +70,7 @@ defineOptions({
defineProps<{
layerContentMenu: (MenuButton | MenuComponent)[];
customContentMenu?: (menus: (MenuButton | MenuComponent)[], type: string) => (MenuButton | MenuComponent)[];
}>();
const services = inject<Services>('services');
Expand Down
7 changes: 6 additions & 1 deletion packages/editor/src/layouts/workspace/Workspace.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
<Breadcrumb></Breadcrumb>

<slot name="stage">
<MagicStage v-if="page" :stage-content-menu="stageContentMenu"></MagicStage>
<MagicStage
v-if="page"
:stage-content-menu="stageContentMenu"
:custom-content-menu="customContentMenu"
></MagicStage>
</slot>

<slot name="workspace-content"></slot>
Expand Down Expand Up @@ -32,6 +36,7 @@ defineOptions({
defineProps<{
stageContentMenu: (MenuButton | MenuComponent)[];
customContentMenu?: (menus: (MenuButton | MenuComponent)[], type: string) => (MenuButton | MenuComponent)[];
}>();
const services = inject<Services>('services');
Expand Down
8 changes: 7 additions & 1 deletion packages/editor/src/layouts/workspace/viewer/Stage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@
<NodeListMenu></NodeListMenu>

<Teleport to="body">
<ViewerMenu ref="menu" :is-multi-select="isMultiSelect" :stage-content-menu="stageContentMenu"></ViewerMenu>
<ViewerMenu
ref="menu"
:is-multi-select="isMultiSelect"
:stage-content-menu="stageContentMenu"
:custom-content-menu="customContentMenu"
></ViewerMenu>
</Teleport>
</ScrollViewer>
</template>
Expand All @@ -52,6 +57,7 @@ defineOptions({
defineProps<{
stageContentMenu: (MenuButton | MenuComponent)[];
customContentMenu?: (menus: (MenuButton | MenuComponent)[], type: string) => (MenuButton | MenuComponent)[];
}>();
let stage: StageCore | null = null;
Expand Down
171 changes: 92 additions & 79 deletions packages/editor/src/layouts/workspace/viewer/ViewerMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ defineOptions({
});
const props = withDefaults(
defineProps<{ isMultiSelect?: boolean; stageContentMenu: (MenuButton | MenuComponent)[] }>(),
{ isMultiSelect: false },
defineProps<{
isMultiSelect?: boolean;
stageContentMenu: (MenuButton | MenuComponent)[];
customContentMenu?: (menus: (MenuButton | MenuComponent)[], type: string) => (MenuButton | MenuComponent)[];
}>(),
{
isMultiSelect: false,
stageContentMenu: () => [],
customContentMenu: (menus: (MenuButton | MenuComponent)[]) => menus,
},
);
const services = inject<Services>('services');
Expand All @@ -32,83 +40,88 @@ const node = computed(() => editorService?.get('node'));
const nodes = computed(() => editorService?.get('nodes'));
const parent = computed(() => editorService?.get('parent'));
const menuData = computed<(MenuButton | MenuComponent)[]>(() => [
{
type: 'button',
text: '水平居中',
icon: markRaw(CenterIcon),
display: () => canCenter.value,
handler: () => {
if (!nodes.value) return;
editorService?.alignCenter(nodes.value);
},
},
useCopyMenu(),
usePasteMenu(menu),
{
type: 'divider',
direction: 'horizontal',
display: () => {
if (!node.value) return false;
return !isPage(node.value);
},
},
{
type: 'button',
text: '上移一层',
icon: markRaw(Top),
display: () => !isPage(node.value) && !props.isMultiSelect,
handler: () => {
editorService?.moveLayer(1);
},
},
{
type: 'button',
text: '下移一层',
icon: markRaw(Bottom),
display: () => !isPage(node.value) && !props.isMultiSelect,
handler: () => {
editorService?.moveLayer(-1);
},
},
{
type: 'button',
text: '置顶',
icon: markRaw(Top),
display: () => !isPage(node.value) && !props.isMultiSelect,
handler: () => {
editorService?.moveLayer(LayerOffset.TOP);
},
},
{
type: 'button',
text: '置底',
icon: markRaw(Bottom),
display: () => !isPage(node.value) && !props.isMultiSelect,
handler: () => {
editorService?.moveLayer(LayerOffset.BOTTOM);
},
},
useMoveToMenu(services),
{
type: 'divider',
direction: 'horizontal',
display: () => !isPage(node.value) && !props.isMultiSelect,
},
useDeleteMenu(),
{
type: 'divider',
direction: 'horizontal',
},
{
type: 'button',
text: '清空参考线',
handler: () => {
editorService?.get('stage')?.clearGuides();
},
},
...props.stageContentMenu,
]);
const menuData = computed<(MenuButton | MenuComponent)[]>(() =>
props.customContentMenu(
[
{
type: 'button',
text: '水平居中',
icon: markRaw(CenterIcon),
display: () => canCenter.value,
handler: () => {
if (!nodes.value) return;
editorService?.alignCenter(nodes.value);
},
},
useCopyMenu(),
usePasteMenu(menu),
{
type: 'divider',
direction: 'horizontal',
display: () => {
if (!node.value) return false;
return !isPage(node.value);
},
},
{
type: 'button',
text: '上移一层',
icon: markRaw(Top),
display: () => !isPage(node.value) && !props.isMultiSelect,
handler: () => {
editorService?.moveLayer(1);
},
},
{
type: 'button',
text: '下移一层',
icon: markRaw(Bottom),
display: () => !isPage(node.value) && !props.isMultiSelect,
handler: () => {
editorService?.moveLayer(-1);
},
},
{
type: 'button',
text: '置顶',
icon: markRaw(Top),
display: () => !isPage(node.value) && !props.isMultiSelect,
handler: () => {
editorService?.moveLayer(LayerOffset.TOP);
},
},
{
type: 'button',
text: '置底',
icon: markRaw(Bottom),
display: () => !isPage(node.value) && !props.isMultiSelect,
handler: () => {
editorService?.moveLayer(LayerOffset.BOTTOM);
},
},
useMoveToMenu(services),
{
type: 'divider',
direction: 'horizontal',
display: () => !isPage(node.value) && !props.isMultiSelect,
},
useDeleteMenu(),
{
type: 'divider',
direction: 'horizontal',
},
{
type: 'button',
text: '清空参考线',
handler: () => {
editorService?.get('stage')?.clearGuides();
},
},
...props.stageContentMenu,
],
'viewer',
),
);
watch(
parent,
Expand Down

0 comments on commit 698c345

Please sign in to comment.