fix context-menu generation

This commit is contained in:
SrGooglo 2022-12-09 10:11:05 +00:00
parent 67b4cfa64b
commit 1115396fcf
2 changed files with 61 additions and 48 deletions

View File

@ -2,8 +2,36 @@ import download from "utils/download"
import { copyToClipboard } from "utils"
export default {
"ignore": () => {
return false
"default-context": () => {
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) => {
const items = [

View File

@ -5,7 +5,6 @@ import { DOMWindow } from "components/RenderWindow"
import ContextMenu from "./components/contextMenu"
import InternalContexts from "schemas/menu-contexts"
import { copyToClipboard } from "utils"
export default class ContextMenuCore extends Core {
static namespace = "ContextMenu"
@ -13,32 +12,6 @@ export default class ContextMenuCore extends Core {
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({
id: "contextMenu",
className: "contextMenuWrapper",
@ -61,28 +34,46 @@ export default class ContextMenuCore extends Core {
const parentElement = element.closest("[context-menu]")
if (parentElement) {
let contexts = parentElement.getAttribute("context-menu")
let contexts = parentElement.getAttribute("context-menu") ?? []
if (!contexts) {
return
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 (contexts.includes("ignore")) {
return null
}
// generate items
contexts.forEach(async (context) => {
// 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(element, parentElement, {
close: this.hide()
contextObject = await contextObject(parentElement, element, {
close: this.hide,
})
}
if (contextObject) {
items.push(...contextObject)
// push divider
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
@ -97,14 +88,8 @@ export default class ContextMenuCore extends Core {
// short items (if has declared index)
items = items.sort((a, b) => a.index - b.index)
// push default items
if (items.length > 0) {
items.push({
type: "separator"
})
}
items.push(...this.defaultContext)
// remove undefined items
items = items.filter((item) => item !== undefined)
return items
}