mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-11 03:24:16 +00:00
improve generateItems
This commit is contained in:
parent
7b8d284046
commit
766c4725cb
@ -4,22 +4,37 @@ import Core from "evite/src/core"
|
|||||||
import { DOMWindow } from "components/RenderWindow"
|
import { DOMWindow } from "components/RenderWindow"
|
||||||
import ContextMenu from "./components/contextMenu"
|
import ContextMenu from "./components/contextMenu"
|
||||||
|
|
||||||
import Contexts from "schemas/menu-contexts"
|
import InternalContexts from "schemas/menu-contexts"
|
||||||
|
import { copyToClipboard } from "utils"
|
||||||
|
|
||||||
export default class ContextMenuCore extends Core {
|
export default class ContextMenuCore extends Core {
|
||||||
static namespace = "ContextMenu"
|
static namespace = "ContextMenu"
|
||||||
static public = ["show", "hide", "registerContext"]
|
static public = ["show", "hide", "registerContext"]
|
||||||
|
|
||||||
contexts = Object()
|
contexts = Object()
|
||||||
|
|
||||||
defaultContext = [
|
defaultContext = [
|
||||||
|
{
|
||||||
|
label: "Copy",
|
||||||
|
icon: "Copy",
|
||||||
|
action: (clickedItem, ctx) => {
|
||||||
|
// get selected text
|
||||||
|
const selectedText = window.getSelection().toString()
|
||||||
|
|
||||||
|
copyToClipboard(selectedText)
|
||||||
|
|
||||||
|
ctx.close()
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
label: "Report a bug",
|
label: "Report a bug",
|
||||||
icon: "AlertTriangle",
|
icon: "AlertTriangle",
|
||||||
action: (parent, element) => {
|
action: (clickedItem, ctx) => {
|
||||||
app.eventBus.emit("app.reportBug", {
|
app.eventBus.emit("app.reportBug", {
|
||||||
parent,
|
clickedItem,
|
||||||
element
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
ctx.close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@ -38,7 +53,7 @@ export default class ContextMenuCore extends Core {
|
|||||||
this.contexts[element] = context
|
this.contexts[element] = context
|
||||||
}
|
}
|
||||||
|
|
||||||
generateItems = (element) => {
|
generateItems = async (element) => {
|
||||||
let items = []
|
let items = []
|
||||||
|
|
||||||
// find the closest context with attribute (context-menu)
|
// find the closest context with attribute (context-menu)
|
||||||
@ -46,28 +61,28 @@ export default class ContextMenuCore extends Core {
|
|||||||
const parentElement = element.closest("[context-menu]")
|
const parentElement = element.closest("[context-menu]")
|
||||||
|
|
||||||
if (parentElement) {
|
if (parentElement) {
|
||||||
const context = parentElement.getAttribute("context-menu")
|
let contexts = parentElement.getAttribute("context-menu")
|
||||||
|
|
||||||
// if context is not registered, try to fetch it from the constants contexts object
|
if (!contexts) {
|
||||||
if (!this.contexts[context]) {
|
return
|
||||||
items = Contexts[context] || []
|
|
||||||
} else {
|
|
||||||
items = this.contexts[context] ?? []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof items === "function") {
|
contexts = contexts.split(",").map((context) => context.trim())
|
||||||
items = items(
|
|
||||||
parentElement,
|
|
||||||
element,
|
|
||||||
{
|
|
||||||
close: this.hide
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!items) {
|
// generate items
|
||||||
return null
|
contexts.forEach(async (context) => {
|
||||||
|
let contextObject = this.contexts[context] || InternalContexts[context]
|
||||||
|
|
||||||
|
if (typeof contextObject === "function") {
|
||||||
|
contextObject = await contextObject(element, parentElement, {
|
||||||
|
close: this.hide()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (contextObject) {
|
||||||
|
items.push(...contextObject)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// fullfill each item with a correspondent index if missing declared
|
// fullfill each item with a correspondent index if missing declared
|
||||||
@ -82,6 +97,7 @@ export default class ContextMenuCore extends Core {
|
|||||||
// short items (if has declared index)
|
// short items (if has declared index)
|
||||||
items = items.sort((a, b) => a.index - b.index)
|
items = items.sort((a, b) => a.index - b.index)
|
||||||
|
|
||||||
|
// push default items
|
||||||
if (items.length > 0) {
|
if (items.length > 0) {
|
||||||
items.push({
|
items.push({
|
||||||
type: "separator"
|
type: "separator"
|
||||||
@ -93,7 +109,7 @@ export default class ContextMenuCore extends Core {
|
|||||||
return items
|
return items
|
||||||
}
|
}
|
||||||
|
|
||||||
handleEvent = (event) => {
|
handleEvent = async (event) => {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
|
|
||||||
// get the cords of the mouse
|
// get the cords of the mouse
|
||||||
@ -108,7 +124,7 @@ export default class ContextMenuCore extends Core {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const items = this.generateItems(component)
|
const items = await this.generateItems(component)
|
||||||
|
|
||||||
if (!items) {
|
if (!items) {
|
||||||
console.warn("No context menu items found, aborting")
|
console.warn("No context menu items found, aborting")
|
||||||
@ -122,6 +138,9 @@ export default class ContextMenuCore extends Core {
|
|||||||
},
|
},
|
||||||
clickedComponent: component,
|
clickedComponent: component,
|
||||||
items: items,
|
items: items,
|
||||||
|
ctx: {
|
||||||
|
close: this.hide
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user