javascript - Electron 右键菜单的自定义样式

标签 javascript node.js electron

目前,我的代码(请参见下文)提供了OS右键菜单。我的目标是添加自定义样式,例如更改背景颜色或添加填充。我想知道当前代码是否可行,或者是否必须使用其他Package。
这是我的代码:

const remote = electron.remote
const Menu = remote.Menu
const MenuItem = remote.MenuItem

const sceneCardElements = document.querySelectorAll('.scene-card')

let rightClickPosition = null

const sceneCardMenu = new Menu()
const sceneCardMenuItem1 = new MenuItem({
  label: 'Edit',
  click: () => {
    remote.getCurrentWindow().loadFile('./sceneSettings.html')
  }
})
sceneCardMenu.append(sceneCardMenuItem1)

sceneCardElements.forEach((sceneCardElement) => {
    if (sceneCardElement == null) return
    sceneCardElement.addEventListener('contextmenu', (e) => {
        e.preventDefault()
        rightClickPosition = {x: e.x, y: e.y}
        sceneCardMenu.popup(remote.getCurrentWindow())
      }, false)
})
这是返回结果的屏幕截图:
My Code's Output
这是我要执行的操作的屏幕截图(来自Discord的示例):
An Example Software's Output
谢谢!

最佳答案

因此,我最终使用了一个名为tippy.js的库,并使用了它们的右键菜单template
这是将来查看此帖子的任何人的最终代码:

// Get the Element you want to Right-Click
const element = document.querySelector('#rightClickable')

// Define the Tippy.js Rightclick Element
const instance = tippy(element, {
  // Get the Template you made into JS
  content: document.getElementById('rightClickMenuTemplate').innerHTML,
  placement: 'right-start',
  trigger: 'manual',
  interactive: true,
  arrow: false,
  offset: [0, 0],
  allowHTML: true,
});

// Add the Event Listener
element.addEventListener('contextmenu', (event) => {
  event.preventDefault();

  instance.setProps({
    getReferenceClientRect: () => ({
      width: 0,
      height: 0,
      top: event.clientY,
      bottom: event.clientY,
      left: event.clientX,
      right: event.clientX,
    }),
  });

  instance.show();
});
<!-- Right Click the Text Below -->
<p id="rightClickable">Right Click Me!</p>

<!-- Right-Click Menu Template Used by Tippy.js -->
<div id="rightClickMenuTemplate" style="display: none; visibility: hidden;">
  <strong>Hi from the Right-Click Menu</strong>
</div>

<!-- Import Tippy.js -->
<script src="https://unpkg.com/@popperjs/core@2"></script>
<script src="https://unpkg.com/tippy.js@6"></script>

关于javascript - Electron 右键菜单的自定义样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66658736/

相关文章:

javascript - 如何使用 jquery 创建单击按钮时出现的 iframe

javascript - 在 try/catch 中返回与在 .catch 中返回

node.js - 如何在nodejs上使用RabbitMQ的消息

node.js - 多个文件: can't use same `global` 中的拆分 Electron 主过程

javascript - 对数组中的值进行分组和计数

javascript - Mixpanel 匿名用户转换为识别用户跟踪

javascript - 如何在 Node/Javascript 中以编程方式减小 PDF 大小

node.js - Mongoose 虚拟不工作

javascript - Electron - 创建文件时出现问题,错误 "EROFS: read-only file system"

javascript - 从平面文件保存和加载 html 输入