Skip to content
Open
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
24 changes: 20 additions & 4 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ export default class RightMenu {
private version: string = version
private menu: HTMLElement | null = null
private config: ConfigType
private options: OptionsType
private eventList: Array<[Window | Document, string, LiType['callback']]> = []
private menuStyle = {
'min-width': '',
'max-width': '',
}

private bindEl: Element | null = null

constructor(
el: ConfigType,
options: OptionsType,
Expand All @@ -40,12 +43,25 @@ export default class RightMenu {
if (config.maxWidth) {
this.menuStyle['max-width'] = getValue(config.maxWidth)
}
this.options = options

// 获取dom并绑定事件
const dom = typeof config.el === 'string' ? document.querySelector(config.el) : config.el
dom?.addEventListener('contextmenu', (e) => {
const res = typeof options === 'function' ? options(e, config) : options
this.init(e as MouseEvent, res)
})
this.bindEl = dom

this.contextMenuHandler = this.contextMenuHandler.bind(this)

dom?.addEventListener('contextmenu', this.contextMenuHandler)
}

contextMenuHandler(e: Event) {
const { options, config } = this
const res = typeof options === 'function' ? options(e, config) : options
this.init(e as MouseEvent, res)
}

destroy() {
this.bindEl?.removeEventListener('contextmenu', this.contextMenuHandler)
}

/**
Expand Down