-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
205 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,2 @@ | ||
import contextMenuVue from './src/index'; | ||
import { isClient } from '/@/utils/is'; | ||
import { Options, Props } from './src/types'; | ||
import { createVNode, render } from 'vue'; | ||
const menuManager: { | ||
domList: Element[]; | ||
resolve: Fn; | ||
} = { | ||
domList: [], | ||
resolve: () => {}, | ||
}; | ||
export const createContextMenu = function (options: Options) { | ||
const { event } = options || {}; | ||
try { | ||
event.preventDefault(); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
|
||
if (!isClient) return; | ||
return new Promise((resolve) => { | ||
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 }; | ||
} | ||
const vm = createVNode(contextMenuVue, propsData); | ||
render(vm, container); | ||
const bodyClick = function () { | ||
menuManager.resolve(''); | ||
}; | ||
menuManager.domList.push(container); | ||
const remove = function () { | ||
menuManager.domList.forEach((dom: Element) => { | ||
try { | ||
document.body.removeChild(dom); | ||
} catch (error) {} | ||
}); | ||
document.body.removeEventListener('click', bodyClick); | ||
document.body.removeEventListener('scroll', bodyClick); | ||
}; | ||
menuManager.resolve = function (...arg: any) { | ||
resolve(arg[0]); | ||
remove(); | ||
}; | ||
remove(); | ||
document.body.appendChild(container); | ||
document.body.addEventListener('click', bodyClick); | ||
document.body.addEventListener('scroll', bodyClick); | ||
}); | ||
}; | ||
export const unMountedContextMenu = function () { | ||
if (menuManager) { | ||
menuManager.resolve(''); | ||
menuManager.domList = []; | ||
} | ||
}; | ||
|
||
export { createContextMenu, destroyContextMenu } from './src/createContextMenu'; | ||
export * from './src/types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import contextMenuVue from './index'; | ||
import { isClient } from '/@/utils/is'; | ||
import { CreateContextOptions, ContextMenuProps } from './types'; | ||
import { createVNode, render } from 'vue'; | ||
|
||
const menuManager: { | ||
domList: Element[]; | ||
resolve: Fn; | ||
} = { | ||
domList: [], | ||
resolve: () => {}, | ||
}; | ||
|
||
export const createContextMenu = function (options: CreateContextOptions) { | ||
const { event } = options || {}; | ||
|
||
event && event?.preventDefault(); | ||
|
||
if (!isClient) return; | ||
return new Promise((resolve) => { | ||
const body = document.body; | ||
|
||
const container = document.createElement('div'); | ||
const propsData: Partial<ContextMenuProps> = {}; | ||
if (options.styles) { | ||
propsData.styles = options.styles; | ||
} | ||
|
||
if (options.items) { | ||
propsData.items = options.items; | ||
} | ||
|
||
if (options.event) { | ||
propsData.customEvent = event; | ||
propsData.axis = { x: event.clientX, y: event.clientY }; | ||
} | ||
|
||
const vm = createVNode(contextMenuVue, propsData); | ||
render(vm, container); | ||
|
||
const handleClick = function () { | ||
menuManager.resolve(''); | ||
}; | ||
|
||
menuManager.domList.push(container); | ||
|
||
const remove = function () { | ||
menuManager.domList.forEach((dom: Element) => { | ||
try { | ||
dom && body.removeChild(dom); | ||
} catch (error) {} | ||
}); | ||
body.removeEventListener('click', handleClick); | ||
body.removeEventListener('scroll', handleClick); | ||
}; | ||
|
||
menuManager.resolve = function (...arg: any) { | ||
remove(); | ||
resolve(arg[0]); | ||
}; | ||
remove(); | ||
body.appendChild(container); | ||
body.addEventListener('click', handleClick); | ||
body.addEventListener('scroll', handleClick); | ||
}); | ||
}; | ||
|
||
export const destroyContextMenu = function () { | ||
if (menuManager) { | ||
menuManager.resolve(''); | ||
menuManager.domList = []; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.