Skip to content

Commit

Permalink
add context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
amanharwara committed Mar 5, 2024
1 parent 123d742 commit 24da42d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
44 changes: 44 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -205,6 +206,8 @@ if (!singleInstanceLock) {

const mainWindow = createWindow();

contextMenu();

const defaultDownloadDir = getSettingWithDefault("defaultDownloadDir");
electronDl({
saveAs: getSettingWithDefault("showSaveDialog"),
Expand All @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/whatsapp.preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,7 @@ ipcRenderer.on("set-theme", (event, theme: Theme) => {
setThemeColors(theme.colors);
}
});

ipcRenderer.on("format-text", (e, wrapper) => {
formatSelectedText(wrapper);
});
23 changes: 23 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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==

[email protected]:
version "24.13.1"
resolved "https://registry.yarnpkg.com/electron-publish/-/electron-publish-24.13.1.tgz#57289b2f7af18737dc2ad134668cdd4a1b574a0c"
Expand Down

0 comments on commit 24da42d

Please sign in to comment.