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 11 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
47 changes: 43 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,8 @@ const CONST = require('../src/CONST').default;
const Localize = require('../src/libs/Localize');

const port = process.env.PORT || 8080;
const PASTE_AS_PLAIN_TEXT_ACCELERATOR = 'CmdOrCtrl+Shift+V';
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we have more than one accelerators, then we should move this to CONST.

DESKTOP_SHORTCUT_ACCELERATOR: {
    PASTE_AS_PLAIN_TEXT: ...,
    PASTE_AND_MATCH_STYLE:...
}


const PASTE_AND_MATCH_STYLE_ACCELERATOR = 'Option+Shift+CmdOrCtrl+V';

app.setName('New Expensify');

Expand All @@ -25,16 +27,42 @@ 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);
}

/**
* Checks if the clipboard contains text.
*
* @returns {boolean} - Returns `true` if the clipboard contains text, otherwise returns `false`.
*/
function clipboardHasText() {
const clipboardText = clipboard.readText();
return clipboardText.length > 0;
}

// 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: PASTE_AND_MATCH_STYLE_ACCELERATOR,
}),
new MenuItem({
label: Localize.translate(CONST.LOCALES.DEFAULT, 'desktopApplicationMenu.pasteAsPlainText'),
visible: parameters.isEditable && parameters.editFlags.canPaste && clipboardHasText(),
accelerator: PASTE_AS_PLAIN_TEXT_ACCELERATOR,
click: () => pasteAsPlainText(browserWindow),
}),
],
});
Expand Down Expand Up @@ -282,6 +310,8 @@ const mainWindow = () => {
browserWindow.setTitle('New Expensify');
}

const PASTE_AS_PLAIN_TEXT_EDIT_MENU_ID = 'pasteAsPlainText';

const initialMenuTemplate = [
{
id: 'mainMenu',
Expand Down Expand Up @@ -323,7 +353,16 @@ const mainWindow = () => {
{id: 'cut', role: 'cut'},
{id: 'copy', role: 'copy'},
{id: 'paste', role: 'paste'},
{id: 'pasteAndMatchStyle', role: 'pasteAndMatchStyle'},
{
id: 'pasteAndMatchStyle',
role: 'pasteAndMatchStyle',
accelerator: PASTE_AND_MATCH_STYLE_ACCELERATOR,
},
{
id: PASTE_AS_PLAIN_TEXT_EDIT_MENU_ID,
accelerator: PASTE_AS_PLAIN_TEXT_ACCELERATOR,
click: () => pasteAsPlainText(browserWindow),
},
{id: 'delete', role: 'delete'},
{id: 'selectAll', role: 'selectAll'},
{type: 'separator'},
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