Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(editor): 多选菜单支持复制粘贴删除 #217

Merged
merged 6 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/editor/src/layouts/workspace/Stage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@dragover="dragoverHandler"
></div>
<teleport to="body">
<viewer-menu ref="menu"></viewer-menu>
<viewer-menu ref="menu" :is-multi-select="isMultiSelect"></viewer-menu>
</teleport>
</scroll-viewer>
</template>
Expand Down Expand Up @@ -67,23 +67,24 @@ export default defineComponent({
},

setup() {
let stage: StageCore | null = null;
let runtime: Runtime | null = null;

const services = inject<Services>('services');
const stageOptions = inject<StageOptions>('stageOptions');

const stageWrap = ref<InstanceType<typeof ScrollViewer>>();
const stageContainer = ref<HTMLDivElement>();
const menu = ref<InstanceType<typeof ViewerMenu>>();

const isMultiSelect = computed(() => services?.editorService.get('nodes')?.length > 1);
const stageRect = computed(() => services?.uiService.get<StageRect>('stageRect'));
const uiSelectMode = computed(() => services?.uiService.get<boolean>('uiSelectMode'));
const root = computed(() => services?.editorService.get<MApp>('root'));
const page = computed(() => services?.editorService.get<MPage>('page'));
const zoom = computed(() => services?.uiService.get<number>('zoom') || 1);
const node = computed(() => services?.editorService.get<MNode>('node'));

let stage: StageCore | null = null;
let runtime: Runtime | null = null;

const getGuideLineKey = (key: string) => `${key}_${root.value?.id}_${page.value?.id}`;

watchEffect(() => {
Expand Down Expand Up @@ -131,6 +132,10 @@ export default defineComponent({
services?.editorService.highlight(el.id);
});

stage?.on('multiSelect', (els: HTMLElement[]) => {
services?.editorService.multiSelect(els.map((el) => el.id));
});

stage?.on('update', (ev: UpdateEventData) => {
if (ev.parentEl) {
services?.editorService.moveToContainer({ id: ev.el.id, style: ev.style }, ev.parentEl.id);
Expand Down Expand Up @@ -206,6 +211,7 @@ export default defineComponent({
menu,
stageRect,
zoom,
isMultiSelect,

contextmenuHandler(e: MouseEvent) {
e.preventDefault();
Expand Down
238 changes: 123 additions & 115 deletions packages/editor/src/layouts/workspace/ViewerMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import { computed, defineComponent, inject, markRaw, onMounted, reactive, ref, watch } from 'vue';
import { Bottom, Delete, DocumentCopy, Top } from '@element-plus/icons-vue';

import { NodeType } from '@tmagic/schema';
import type StageCore from '@tmagic/stage';
import { MNode, NodeType } from '@tmagic/schema';
import StageCore from '@tmagic/stage';
import { isPage } from '@tmagic/utils';

import ContentMenu from '@editor/components/ContentMenu.vue';
import { LayerOffset, Layout, MenuItem, Services } from '@editor/type';
Expand All @@ -16,19 +17,130 @@ import { COPY_STORAGE_KEY } from '@editor/utils/editor';
export default defineComponent({
components: { ContentMenu },

setup() {
props: {
isMultiSelect: {
type: Boolean,
default: false,
},
},

setup(props) {
const services = inject<Services>('services');
const editorService = services?.editorService;
const menu = ref<InstanceType<typeof ContentMenu>>();
const canPaste = ref(false);
const canCenter = ref(false);

const node = computed(() => editorService?.get('node'));
const node = computed(() => editorService?.get<MNode>('node'));
const nodes = computed(() => editorService?.get<MNode[]>('nodes'));
const parent = computed(() => editorService?.get('parent'));
const isPage = computed(() => node.value?.type === NodeType.PAGE);
const stage = computed(() => editorService?.get<StageCore>('stage'));

const stageContentMenu = inject<MenuItem[]>('stageContentMenu', []);

const menuData = reactive<MenuItem[]>([
{
type: 'button',
text: '水平居中',
display: () => canCenter.value && !props.isMultiSelect,
handler: () => {
if (!node.value) return;
editorService?.alignCenter(node.value);
},
},
{
type: 'button',
text: '复制',
icon: markRaw(DocumentCopy),
handler: () => {
nodes.value && editorService?.copy(nodes.value);
canPaste.value = true;
},
},
{
type: 'button',
text: '粘贴',
display: () => canPaste.value,
handler: () => {
const rect = menu.value?.$el.getBoundingClientRect();
const parentRect = stage.value?.container?.getBoundingClientRect();
const initialLeft = (rect?.left || 0) - (parentRect?.left || 0);
const initialTop = (rect?.top || 0) - (parentRect?.top || 0);

if (!nodes.value || nodes.value.length === 0) return;
editorService?.paste({ left: initialLeft, top: initialTop });
},
},
{
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: '置顶',
display: () => !isPage(node.value) && !props.isMultiSelect,
handler: () => {
editorService?.moveLayer(LayerOffset.TOP);
},
},
{
type: 'button',
text: '置底',
display: () => !isPage(node.value) && !props.isMultiSelect,
handler: () => {
editorService?.moveLayer(LayerOffset.BOTTOM);
},
},
{
type: 'divider',
direction: 'horizontal',
display: () => !isPage(node.value) && !props.isMultiSelect,
},
{
type: 'button',
text: '删除',
icon: Delete,
display: () => !isPage(node.value),
handler: () => {
nodes.value && editorService?.remove(nodes.value);
},
},
{
type: 'divider',
direction: 'horizontal',
},
{
type: 'button',
text: '清空参考线',
handler: () => {
editorService?.get<StageCore>('stage').clearGuides();
},
},
...stageContentMenu,
]);

onMounted(() => {
const data = globalThis.localStorage.getItem(COPY_STORAGE_KEY);
canPaste.value = data !== 'undefined' && !!data;
Expand All @@ -39,122 +151,18 @@ export default defineComponent({
async () => {
if (!parent.value || !editorService) return (canCenter.value = false);
const layout = await editorService.getLayout(parent.value);
canCenter.value =
[Layout.ABSOLUTE, Layout.FIXED].includes(layout) &&
![NodeType.ROOT, NodeType.PAGE, 'pop'].includes(`${node.value?.type}`);
const isLayoutConform = [Layout.ABSOLUTE, Layout.FIXED].includes(layout);
const isTypeConform = nodes.value?.every(
(selectedNode) => ![NodeType.ROOT, NodeType.PAGE, 'pop'].includes(`${selectedNode?.type}`),
);
canCenter.value = isLayoutConform && !!isTypeConform;
},
{ immediate: true },
);

return {
menu,
menuData: reactive<MenuItem[]>([
{
type: 'button',
text: '水平居中',
display: () => canCenter.value,
handler: () => {
node.value && editorService?.alignCenter(node.value);
},
},
{
type: 'button',
text: '复制',
icon: markRaw(DocumentCopy),
handler: () => {
node.value && editorService?.copy(node.value);
canPaste.value = true;
},
},
{
type: 'button',
text: '粘贴',
display: () => canPaste.value,
handler: () => {
const stage = editorService?.get<StageCore>('stage');

const rect = menu.value?.$el.getBoundingClientRect();
const parentRect = stage?.container?.getBoundingClientRect();
let left = (rect?.left || 0) - (parentRect?.left || 0);
let top = (rect?.top || 0) - (parentRect?.top || 0);

if (node.value?.items && stage) {
const parentEl = stage.renderer.contentWindow?.document.getElementById(`${node.value.id}`);
const parentElRect = parentEl?.getBoundingClientRect();
left = left - (parentElRect?.left || 0);
top = top - (parentElRect?.top || 0);
}

editorService?.paste({ left, top });
},
},
{
type: 'divider',
direction: 'horizontal',
display: () => !isPage.value,
},
{
type: 'button',
text: '上移一层',
icon: markRaw(Top),
display: () => !isPage.value,
handler: () => {
editorService?.moveLayer(1);
},
},
{
type: 'button',
text: '下移一层',
icon: markRaw(Bottom),
display: () => !isPage.value,
handler: () => {
editorService?.moveLayer(-1);
},
},
{
type: 'button',
text: '置顶',
display: () => !isPage.value,
handler: () => {
editorService?.moveLayer(LayerOffset.TOP);
},
},
{
type: 'button',
text: '置底',
display: () => !isPage.value,
handler: () => {
editorService?.moveLayer(LayerOffset.BOTTOM);
},
},
{
type: 'divider',
direction: 'horizontal',
display: () => !isPage.value,
},
{
type: 'button',
text: '删除',
icon: Delete,
display: () => !isPage.value,
handler: () => {
node.value && editorService?.remove(node.value);
},
},
{
type: 'divider',
direction: 'horizontal',
},
{
type: 'button',
text: '清空参考线',
handler: () => {
editorService?.get<StageCore>('stage').clearGuides();
},
},
...stageContentMenu,
]),

menuData,
show(e: MouseEvent) {
menu.value?.show(e);
},
Expand Down
20 changes: 10 additions & 10 deletions packages/editor/src/layouts/workspace/Workspace.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default defineComponent({
setup() {
const services = inject<Services>('services');
const workspace = ref<HTMLDivElement>();
const node = computed(() => services?.editorService.get<MNode>('node'));
const nodes = computed(() => services?.editorService.get<MNode[]>('nodes'));
let keycon: KeyController;

const mouseenterHandler = () => {
Expand All @@ -58,27 +58,27 @@ export default defineComponent({
keycon
.keyup('delete', (e) => {
e.inputEvent.preventDefault();
if (!node.value || isPage(node.value)) return;
services?.editorService.remove(node.value);
if (!nodes.value || isPage(nodes.value[0])) return;
services?.editorService.remove(nodes.value);
})
.keyup('backspace', (e) => {
e.inputEvent.preventDefault();
if (!node.value || isPage(node.value)) return;
services?.editorService.remove(node.value);
if (!nodes.value || isPage(nodes.value[0])) return;
services?.editorService.remove(nodes.value);
})
.keydown([ctrl, 'c'], (e) => {
e.inputEvent.preventDefault();
node.value && services?.editorService.copy(node.value);
nodes.value && services?.editorService.copy(nodes.value);
})
.keydown([ctrl, 'v'], (e) => {
e.inputEvent.preventDefault();
node.value && services?.editorService.paste();
nodes.value && services?.editorService.paste();
})
.keydown([ctrl, 'x'], (e) => {
e.inputEvent.preventDefault();
if (!node.value || isPage(node.value)) return;
services?.editorService.copy(node.value);
services?.editorService.remove(node.value);
if (!nodes.value || isPage(nodes.value[0])) return;
services?.editorService.copy(nodes.value);
services?.editorService.remove(nodes.value);
})
.keydown([ctrl, 'z'], (e) => {
e.inputEvent.preventDefault();
Expand Down
Loading