mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-11 03:24:16 +00:00
improve empty parentElement
behavior
This commit is contained in:
parent
9a420bc4be
commit
802be5837b
@ -33,46 +33,48 @@ export default class ContextMenuCore extends Core {
|
|||||||
// if not found, use default context
|
// if not found, use default context
|
||||||
const parentElement = element.closest("[context-menu]")
|
const parentElement = element.closest("[context-menu]")
|
||||||
|
|
||||||
|
let contexts = []
|
||||||
|
|
||||||
if (parentElement) {
|
if (parentElement) {
|
||||||
let contexts = parentElement.getAttribute("context-menu") ?? []
|
contexts = parentElement.getAttribute("context-menu") ?? []
|
||||||
|
|
||||||
if (typeof contexts === "string") {
|
if (typeof contexts === "string") {
|
||||||
contexts = contexts.split(",").map((context) => context.trim())
|
contexts = contexts.split(",").map((context) => context.trim())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// if context includes ignore, return null
|
// if context includes ignore, return null
|
||||||
if (contexts.includes("ignore")) {
|
if (contexts.includes("ignore")) {
|
||||||
return null
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if context includes no-default, if not, push default context and remove no-default
|
||||||
|
if (contexts.includes("no-default")) {
|
||||||
|
contexts = contexts.filter((context) => context !== "no-default")
|
||||||
|
} else {
|
||||||
|
contexts.push("default-context")
|
||||||
|
}
|
||||||
|
|
||||||
|
for await (const context of contexts) {
|
||||||
|
let contextObject = this.contexts[context] || InternalContexts[context]
|
||||||
|
|
||||||
|
if (typeof contextObject === "function") {
|
||||||
|
contextObject = await contextObject(parentElement, element, {
|
||||||
|
close: this.hide,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if context includes no-default, if not, push default context and remove no-default
|
// push divider
|
||||||
if (contexts.includes("no-default")) {
|
if (items.length > 0) {
|
||||||
contexts = contexts.filter((context) => context !== "no-default")
|
items.push({
|
||||||
|
type: "separator"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(contextObject)) {
|
||||||
|
items.push(...contextObject)
|
||||||
} else {
|
} else {
|
||||||
contexts.push("default-context")
|
items.push(contextObject)
|
||||||
}
|
|
||||||
|
|
||||||
for await (const context of contexts) {
|
|
||||||
let contextObject = this.contexts[context] || InternalContexts[context]
|
|
||||||
|
|
||||||
if (typeof contextObject === "function") {
|
|
||||||
contextObject = await contextObject(parentElement, element, {
|
|
||||||
close: this.hide,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// push divider
|
|
||||||
if (items.length > 0) {
|
|
||||||
items.push({
|
|
||||||
type: "separator"
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Array.isArray(contextObject)) {
|
|
||||||
items.push(...contextObject)
|
|
||||||
} else {
|
|
||||||
items.push(contextObject)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user