From 1115396fcfb72ff78a882b1e5d9baa500c6dde88 Mon Sep 17 00:00:00 2001 From: SrGooglo Date: Fri, 9 Dec 2022 10:11:05 +0000 Subject: [PATCH] fix context-menu generation --- packages/app/constants/menu-contexts.js | 32 ++++++++- packages/app/src/cores/contextMenu/index.js | 77 +++++++++------------ 2 files changed, 61 insertions(+), 48 deletions(-) diff --git a/packages/app/constants/menu-contexts.js b/packages/app/constants/menu-contexts.js index 6fa4507b..599bbfc4 100755 --- a/packages/app/constants/menu-contexts.js +++ b/packages/app/constants/menu-contexts.js @@ -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 = [ diff --git a/packages/app/src/cores/contextMenu/index.js b/packages/app/src/cores/contextMenu/index.js index f1456f62..e111ffec 100755 --- a/packages/app/src/cores/contextMenu/index.js +++ b/packages/app/src/cores/contextMenu/index.js @@ -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 }