Skip to content

Paste as plain text #24029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions desktop/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {app, dialog, BrowserWindow, Menu, MenuItem, shell, ipcMain} = require('electron');
const {app, dialog, clipboard, BrowserWindow, Menu, MenuItem, shell, ipcMain} = require('electron');
const _ = require('underscore');
const serve = require('electron-serve');
const contextMenu = require('electron-context-menu');
Expand All @@ -12,6 +12,7 @@ const CONST = require('../src/CONST').default;
const Localize = require('../src/libs/Localize');

const port = process.env.PORT || 8080;
const {DESKTOP_SHORTCUT_ACCELERATOR} = CONST;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought CONST is already imported on L11?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not importing here,just destructuing

Copy link
Collaborator

@mananjadhav mananjadhav Aug 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad 🤦


app.setName('New Expensify');

Expand All @@ -25,16 +26,32 @@ app.setName('New Expensify');
// See: https://github.com/electron/electron/issues/22597
app.commandLine.appendSwitch('enable-network-information-downlink-max');

/**
* Inserts the plain text from the clipboard into the provided browser window's web contents.
*
* @param {BrowserWindow} browserWindow - The Electron BrowserWindow instance where the text should be inserted.
*/
function pasteAsPlainText(browserWindow) {
const text = clipboard.readText();
browserWindow.webContents.insertText(text);
}

// Initialize the right click menu
// See https://github.com/sindresorhus/electron-context-menu
// Add the Paste and Match Style command to the context menu
contextMenu({
append: (defaultActions, parameters) => [
append: (defaultActions, parameters, browserWindow) => [
new MenuItem({
// Only enable the menu item for Editable context which supports paste
visible: parameters.isEditable && parameters.editFlags.canPaste,
role: 'pasteAndMatchStyle',
accelerator: 'CmdOrCtrl+Shift+V',
accelerator: DESKTOP_SHORTCUT_ACCELERATOR.PASTE_AND_MATCH_STYLE,
}),
new MenuItem({
label: Localize.translate(CONST.LOCALES.DEFAULT, 'desktopApplicationMenu.pasteAsPlainText'),
visible: parameters.isEditable && parameters.editFlags.canPaste && clipboard.readText().length > 0,
accelerator: DESKTOP_SHORTCUT_ACCELERATOR.PASTE_AS_PLAIN_TEXT,
click: () => pasteAsPlainText(browserWindow),
}),
],
});
Expand Down Expand Up @@ -323,7 +340,16 @@ const mainWindow = () => {
{id: 'cut', role: 'cut'},
{id: 'copy', role: 'copy'},
{id: 'paste', role: 'paste'},
{id: 'pasteAndMatchStyle', role: 'pasteAndMatchStyle'},
{
id: 'pasteAndMatchStyle',
role: 'pasteAndMatchStyle',
accelerator: DESKTOP_SHORTCUT_ACCELERATOR.PASTE_AND_MATCH_STYLE,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to add the accelerator here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, as per my testing, shortcuts that are defined in the App Menu work, while the shortcuts defined in contextMenu dont. Their only purpose seems to show their value next to their name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoretically, from what i understand, contextMenu shortcuts are meant to work only when contextMenu is open. However, practically, if you press any key, contextMenu is gone and the shortcut, thus, won't work

},
{
id: 'pasteAsPlainText',
accelerator: DESKTOP_SHORTCUT_ACCELERATOR.PASTE_AS_PLAIN_TEXT,
click: () => pasteAsPlainText(browserWindow),
},
{id: 'delete', role: 'delete'},
{id: 'selectAll', role: 'selectAll'},
{type: 'separator'},
Expand Down
5 changes: 5 additions & 0 deletions src/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ const CONST = {
MAX_AGE: 150,
},

DESKTOP_SHORTCUT_ACCELERATOR: {
PASTE_AND_MATCH_STYLE: 'Option+Shift+CmdOrCtrl+V',
PASTE_AS_PLAIN_TEXT: 'CmdOrCtrl+Shift+V',
},

// This is used to enable a rotation/transform style to any component.
DIRECTION: {
LEFT: 'left',
Expand Down
1 change: 1 addition & 0 deletions src/languages/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,7 @@ export default {
copy: 'Copy',
paste: 'Paste',
pasteAndMatchStyle: 'Paste and Match Style',
pasteAsPlainText: 'Paste as Plain Text',
delete: 'Delete',
selectAll: 'Select All',
speechSubmenu: 'Speech',
Expand Down
1 change: 1 addition & 0 deletions src/languages/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -1413,6 +1413,7 @@ export default {
copy: 'Copiar',
paste: 'Pegar',
pasteAndMatchStyle: 'Pegar adaptando el estilo',
pasteAsPlainText: 'Pegar como texto sin formato',
delete: 'Eliminar',
selectAll: 'Seleccionar todo',
speechSubmenu: 'Voz',
Expand Down