From 24da42d42780aef3287033ee5874306951aaeff0 Mon Sep 17 00:00:00 2001 From: Aman Harwara Date: Wed, 6 Mar 2024 00:36:33 +0530 Subject: [PATCH] add context menu --- package.json | 1 + src/main.ts | 44 +++++++++++++++++++++++++++++++++++++++++ src/whatsapp.preload.ts | 4 ++++ yarn.lock | 23 +++++++++++++++++++++ 4 files changed, 72 insertions(+) diff --git a/package.json b/package.json index d064e135..6231f001 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "@kobalte/utils": "^0.9.0", "@solid-primitives/resize-observer": "^2.0.22", "auto-launch": "^5.0.6", + "electron-context-menu": "^3.6.1", "electron-dl": "^3.5.0", "electron-store": "^8.1.0", "nanoid": "^4.0.2", diff --git a/src/main.ts b/src/main.ts index 80216673..96d9d3d6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -29,6 +29,7 @@ import { } from "./stores/settings/common"; import AutoLaunch from "auto-launch"; import electronDl from "electron-dl"; +import contextMenu from "electron-context-menu"; const windowState = new Store<{ width: number | null; @@ -205,6 +206,8 @@ if (!singleInstanceLock) { const mainWindow = createWindow(); + contextMenu(); + const defaultDownloadDir = getSettingWithDefault("defaultDownloadDir"); electronDl({ saveAs: getSettingWithDefault("showSaveDialog"), @@ -231,6 +234,47 @@ if (!singleInstanceLock) { }); app.on("web-contents-created", (_, webContents) => { + if (webContents.getType() === "webview") { + contextMenu({ + window: { + webContents: webContents, + inspectElement: webContents.inspectElement.bind(webContents), + } as unknown as BrowserWindow, + showSaveImageAs: true, + showInspectElement: true, + append: (def, params, window) => [ + { + label: "Bold", + visible: params.isEditable, + click: () => { + (window as BrowserWindow).webContents.send("format-text", "*"); + }, + }, + { + label: "Italic", + visible: params.isEditable, + click: () => { + (window as BrowserWindow).webContents.send("format-text", "_"); + }, + }, + { + label: "Strike", + visible: params.isEditable, + click: () => { + (window as BrowserWindow).webContents.send("format-text", "~"); + }, + }, + { + label: "Monospaced", + visible: params.isEditable, + click: () => { + (window as BrowserWindow).webContents.send("format-text", "```"); + }, + }, + ], + }); + } + webContents.on("before-input-event", (event, input) => { if (getSettingWithDefault("preventEnter")) { if (input.key === "Enter" && !input.shift && !input.control) { diff --git a/src/whatsapp.preload.ts b/src/whatsapp.preload.ts index f3c21f55..de434c79 100644 --- a/src/whatsapp.preload.ts +++ b/src/whatsapp.preload.ts @@ -126,3 +126,7 @@ ipcRenderer.on("set-theme", (event, theme: Theme) => { setThemeColors(theme.colors); } }); + +ipcRenderer.on("format-text", (e, wrapper) => { + formatSelectedText(wrapper); +}); diff --git a/yarn.lock b/yarn.lock index de67662b..561dc417 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2496,6 +2496,24 @@ ejs@^3.1.8: dependencies: jake "^10.8.5" +electron-context-menu@^3.6.1: + version "3.6.1" + resolved "https://registry.yarnpkg.com/electron-context-menu/-/electron-context-menu-3.6.1.tgz#42f117e15309687b22283e6f8f7a0d95a19afe84" + integrity sha512-lcpO6tzzKUROeirhzBjdBWNqayEThmdW+2I2s6H6QMrwqTVyT3EK47jW3Nxm60KTxl5/bWfEoIruoUNn57/QkQ== + dependencies: + cli-truncate "^2.1.0" + electron-dl "^3.2.1" + electron-is-dev "^2.0.0" + +electron-dl@^3.2.1: + version "3.5.2" + resolved "https://registry.yarnpkg.com/electron-dl/-/electron-dl-3.5.2.tgz#9e83d624473ec90682928b19ae9221a0e0b83a39" + integrity sha512-i104cl+u8yJ0lhpRAtUWfeGuWuL1PL6TBiw2gLf0MMIBjfgE485Ags2mcySx4uWU9P9uj/vsD3jd7X+w1lzZxw== + dependencies: + ext-name "^5.0.0" + pupa "^2.0.1" + unused-filename "^2.1.0" + electron-dl@^3.5.0: version "3.5.0" resolved "https://registry.yarnpkg.com/electron-dl/-/electron-dl-3.5.0.tgz#7a80bf13f168f7e5204774eee89dbc7c86de957b" @@ -2515,6 +2533,11 @@ electron-installer-dmg@^4.0.0: optionalDependencies: appdmg "^0.6.4" +electron-is-dev@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/electron-is-dev/-/electron-is-dev-2.0.0.tgz#833487a069b8dad21425c67a19847d9064ab19bd" + integrity sha512-3X99K852Yoqu9AcW50qz3ibYBWY79/pBhlMCab8ToEWS48R0T9tyxRiQhwylE7zQdXrMnx2JKqUJyMPmt5FBqA== + electron-publish@24.13.1: version "24.13.1" resolved "https://registry.yarnpkg.com/electron-publish/-/electron-publish-24.13.1.tgz#57289b2f7af18737dc2ad134668cdd4a1b574a0c"