Skip to content

Commit

Permalink
perf: optimize preview and ContextMenu functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vben-admin committed Oct 13, 2020
1 parent a7c0970 commit bbfb06f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
15 changes: 6 additions & 9 deletions src/components/ContextMenu/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import contextMenuVue from './src/index';
import { isClient } from '/@/utils/is';
import { Options, Props } from './src/types';
import { createApp } from 'vue';
import { createVNode, render } from 'vue';
const menuManager: {
doms: Element[];
resolve: Fn;
Expand All @@ -19,20 +19,20 @@ export const createContextMenu = function (options: Options) {

if (!isClient) return;
return new Promise((resolve) => {
const wrapDom = document.createElement('div');
const container = document.createElement('div');
const propsData: Partial<Props> = {};
if (options.styles !== undefined) propsData.styles = options.styles;
if (options.items !== undefined) propsData.items = options.items;
if (options.event !== undefined) {
propsData.customEvent = event;
propsData.axis = { x: event.clientX, y: event.clientY };
}
createApp(contextMenuVue, propsData).mount(wrapDom);
const vm = createVNode(contextMenuVue, propsData);
render(vm, container);
const bodyClick = function () {
menuManager.resolve('');
};
const contextMenuDom = wrapDom.children[0];
menuManager.doms.push(contextMenuDom);
menuManager.doms.push(container);
const remove = function () {
menuManager.doms.forEach((dom: Element) => {
try {
Expand All @@ -41,16 +41,13 @@ export const createContextMenu = function (options: Options) {
});
document.body.removeEventListener('click', bodyClick);
document.body.removeEventListener('scroll', bodyClick);
try {
(wrapDom as any) = null;
} catch (error) {}
};
menuManager.resolve = function (...arg: any) {
resolve(arg[0]);
remove();
};
remove();
document.body.appendChild(contextMenuDom);
document.body.appendChild(container);
document.body.addEventListener('click', bodyClick);
document.body.addEventListener('scroll', bodyClick);
});
Expand Down
13 changes: 7 additions & 6 deletions src/components/Preview/src/functional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ import { isClient } from '/@/utils/is';

import type { Options, Props } from './types';

import { createApp } from 'vue';
import { createVNode, render } from 'vue';

let instance: any = null;
export function createImgPreview(options: Options) {
if (!isClient) return;
const { imageList, show = true, index = 0 } = options;

const propsData: Partial<Props> = {};
const wrapDom = document.createElement('div');
const container = document.createElement('div');
propsData.imageList = imageList;
propsData.show = show;
propsData.index = index;
const imgDom = createApp(ImgPreview, propsData);
imgDom.mount(wrapDom);
const imgPreviewDom = wrapDom.children[0];
document.body.appendChild(imgPreviewDom);

instance = createVNode(ImgPreview, propsData);
render(instance, container);
document.body.appendChild(container);
}

0 comments on commit bbfb06f

Please sign in to comment.