UNPKG

vexip-ui

Version:

A Vue 3 UI library, Highly customizability, full TypeScript, performance pretty good

1 lines 5.41 kB
{"version":3,"file":"index.mjs","sources":["../../../components/contextmenu/index.ts"],"sourcesContent":["import { createApp, createVNode, render } from 'vue'\n\nimport Component from './contextmenu.vue'\nimport { proxyExposed, unrefElement } from '@vexip-ui/hooks'\nimport { destroyObject, isClient } from '@vexip-ui/utils'\n\nimport type { App, MaybeRef } from 'vue'\nimport type { MaybeInstance } from '@vexip-ui/hooks'\nimport type { ContextmenuConfig, ContextmenuInstance, ContextmenuOptions, Key } from './symbol'\n\nexport type { ContextmenuConfig, ContextmenuOptions }\n\nexport class ContextmenuManager {\n name: string\n\n private _mountedApp: App<unknown> | null\n private _instance: ContextmenuInstance | null\n private _innerApp: App<unknown> | null\n private _container: HTMLElement | null\n private _pending: Promise<Key[] | null> | null\n private _wrapper: HTMLElement | SVGElement | null\n private _mountedEl: HTMLElement | null\n\n constructor() {\n this._mountedApp = null\n this._instance = null\n this._innerApp = null\n this._container = null\n this._pending = null\n this._wrapper = null\n this._mountedEl = null\n this.name = 'Contextmenu'\n }\n\n open(options: ContextmenuOptions) {\n if (!isClient) {\n return\n }\n\n const { target = document.body, ...others } = options\n\n this._pending = this._getInstance(target)!.openContextmenu(others)\n\n return this._pending\n }\n\n destroy() {\n this._mountedEl && this._wrapper?.removeChild(this._mountedEl)\n this._innerApp?.unmount()\n this._container && render(null, this._container)\n destroyObject(this)\n }\n\n isDestroyed() {\n return false\n }\n\n install(app: App, options: { property?: string } = {}) {\n const { property } = options\n\n this._mountedApp = app\n\n if (property || !app.config.globalProperties.$contextmenu) {\n app.config.globalProperties[property || '$contextmenu'] = this\n }\n }\n\n private _getInstance(target: MaybeRef<string | MaybeInstance>) {\n if (!isClient) return\n\n if (this._pending) {\n let innerApp = this._innerApp\n let container = this._container\n\n const unmount = () => {\n innerApp?.unmount()\n container && render(null, container)\n\n innerApp = null\n container = null\n }\n\n this._pending.finally(unmount)\n this._instance?.handleCancel()\n } else {\n this._innerApp?.unmount()\n this._container && render(null, this._container)\n }\n\n this._pending = null\n\n if (!this._mountedApp) {\n console.warn('[vexip-ui:Contextmenu]: App missing, the plugin maybe not installed.')\n\n this._container = document.createElement('div')\n this._innerApp = createApp(Component)\n this._instance = this._innerApp.mount(this._container) as ContextmenuInstance\n } else {\n const vnode = createVNode(Component, null, null)\n\n this._container = document.createElement('div')\n vnode.appContext = this._mountedApp._context\n\n render(vnode, this._container)\n\n this._instance = proxyExposed<ContextmenuInstance>(vnode)\n }\n\n this._mountedEl = this._container.firstElementChild as HTMLElement\n this._wrapper = unrefElement(target) || document.body\n\n this._wrapper.appendChild(this._mountedEl)\n\n return this._instance\n }\n}\n\nexport const Contextmenu = new ContextmenuManager()\n"],"names":["ContextmenuManager","__publicField","options","isClient","target","others","_a","_b","render","destroyObject","app","property","innerApp","container","unmount","createApp","Component","vnode","createVNode","proxyExposed","unrefElement","Contextmenu"],"mappings":";;;;;;;;AAYO,MAAMA,EAAmB;AAAA,EAW9B,cAAc;AAVd,IAAAC,EAAA;AAEQ,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AAGN,SAAK,cAAc,MACnB,KAAK,YAAY,MACjB,KAAK,YAAY,MACjB,KAAK,aAAa,MAClB,KAAK,WAAW,MAChB,KAAK,WAAW,MAChB,KAAK,aAAa,MAClB,KAAK,OAAO;AAAA,EAAA;AAAA,EAGd,KAAKC,GAA6B;AAChC,QAAI,CAACC;AACH;AAGF,UAAM,EAAE,QAAAC,IAAS,SAAS,MAAM,GAAGC,EAAW,IAAAH;AAE9C,gBAAK,WAAW,KAAK,aAAaE,CAAM,EAAG,gBAAgBC,CAAM,GAE1D,KAAK;AAAA,EAAA;AAAA,EAGd,UAAU;;AACR,SAAK,gBAAcC,IAAA,KAAK,aAAL,QAAAA,EAAe,YAAY,KAAK,eACnDC,I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