fix context-menu generation

This commit is contained in:
SrGooglo 2022-12-09 10:11:05 +00:00
parent 7b56bd04f1
commit c3219aed12
2 changed files with 61 additions and 48 deletions

View File

@ -2,8 +2,36 @@ import download from "utils/download"
import { copyToClipboard } from "utils" import { copyToClipboard } from "utils"
export default { export default {
"ignore": () => { "default-context": () => {
return false const items = []
const text = window.getSelection().toString()
if (text) {
items.push({
label: "Copy",
icon: "Copy",
action: (clickedItem, ctx) => {
copyToClipboard(text)
ctx.close()
}
})
}
items.push({
label: "Report a bug",
icon: "AlertTriangle",
action: (clickedItem, ctx) => {
app.eventBus.emit("app.reportBug", {
clickedItem,
})
ctx.close()
}
})
return items
}, },
"postCard-context": (parent, element, control) => { "postCard-context": (parent, element, control) => {
const items = [ const items = [

View File

@ -5,7 +5,6 @@ import { DOMWindow } from "components/RenderWindow"
import ContextMenu from "./components/contextMenu" import ContextMenu from "./components/contextMenu"
import InternalContexts 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"
@ -13,32 +12,6 @@ export default class ContextMenuCore extends Core {
contexts = Object() contexts = Object()
defaultContext = [
{
label: "Copy",
icon: "Copy",
action: (clickedItem, ctx) => {
// get selected text
const selectedText = window.getSelection().toString()
copyToClipboard(selectedText)
ctx.close()
}
},
{
label: "Report a bug",
icon: "AlertTriangle",
action: (clickedItem, ctx) => {
app.eventBus.emit("app.reportBug", {
clickedItem,
})
ctx.close()
}
}
]
DOMWindow = new DOMWindow({ DOMWindow = new DOMWindow({
id: "contextMenu", id: "contextMenu",
className: "contextMenuWrapper", className: "contextMenuWrapper",
@ -61,28 +34,46 @@ export default class ContextMenuCore extends Core {
const parentElement = element.closest("[context-menu]") const parentElement = element.closest("[context-menu]")
if (parentElement) { if (parentElement) {
let contexts = parentElement.getAttribute("context-menu") let contexts = parentElement.getAttribute("context-menu") ?? []
if (!contexts) { if (typeof contexts === "string") {
return contexts = contexts.split(",").map((context) => context.trim())
} }
contexts = contexts.split(",").map((context) => context.trim()) // if context includes ignore, return null
if (contexts.includes("ignore")) {
return null
}
// generate items // check if context includes no-default, if not, push default context and remove no-default
contexts.forEach(async (context) => { 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] let contextObject = this.contexts[context] || InternalContexts[context]
if (typeof contextObject === "function") { if (typeof contextObject === "function") {
contextObject = await contextObject(element, parentElement, { contextObject = await contextObject(parentElement, element, {
close: this.hide() close: this.hide,
}) })
} }
if (contextObject) { // push divider
items.push(...contextObject) if (items.length > 0) {
items.push({
type: "separator"
})
} }
})
if (Array.isArray(contextObject)) {
items.push(...contextObject)
} else {
items.push(contextObject)
}
}
} }
// fullfill each item with a correspondent index if missing declared // fullfill each item with a correspondent index if missing declared
@ -97,14 +88,8 @@ 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 // remove undefined items
if (items.length > 0) { items = items.filter((item) => item !== undefined)
items.push({
type: "separator"
})
}
items.push(...this.defaultContext)
return items return items
} }