From 64ed2a856b395244add90ae5c8ecc19e72b92db0 Mon Sep 17 00:00:00 2001 From: Abhishek Mehandiratta <36722596+abhi12299@users.noreply.github.com> Date: Thu, 10 Feb 2022 19:09:56 +0530 Subject: [PATCH] Feature: Add "copy as plain text" in edit menu (PR #1351, fix #1144) This closes #1144 by adding a "Copy as plain text" function in Edit menu. Co-authored-by: Ronan Jouchet --- app/src/components/menu.ts | 11 ++++++++++- app/src/helpers/helpers.test.ts | 7 +++++++ app/src/helpers/helpers.ts | 5 +++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/src/components/menu.ts b/app/src/components/menu.ts index 5e47388..03663f8 100644 --- a/app/src/components/menu.ts +++ b/app/src/components/menu.ts @@ -10,7 +10,7 @@ import { } from 'electron'; import * as log from 'loglevel'; -import { isOSX, openExternal } from '../helpers/helpers'; +import { cleanupPlainText, isOSX, openExternal } from '../helpers/helpers'; import { clearAppData, getCurrentURL, @@ -94,6 +94,15 @@ export function generateMenu( accelerator: 'CmdOrCtrl+C', role: 'copy', }, + { + label: 'Copy as Plain Text', + accelerator: 'CmdOrCtrl+Shift+C', + click: (): void => { + // We use clipboard.readText to strip down formatting + const text = clipboard.readText('selection'); + clipboard.writeText(cleanupPlainText(text), 'clipboard'); + }, + }, { label: 'Copy Current URL', accelerator: 'CmdOrCtrl+L', diff --git a/app/src/helpers/helpers.test.ts b/app/src/helpers/helpers.test.ts index af97a65..242e9d6 100644 --- a/app/src/helpers/helpers.test.ts +++ b/app/src/helpers/helpers.test.ts @@ -2,6 +2,7 @@ import { linkIsInternal, getCounterValue, removeUserAgentSpecifics, + cleanupPlainText, } from './helpers'; const internalUrl = 'https://medium.com/'; @@ -261,3 +262,9 @@ describe('removeUserAgentSpecifics', () => { ); }); }); + +describe('cleanupPlainText', () => { + test('removes extra spaces from text', () => { + expect(cleanupPlainText(' this is a test ')).toBe('this is a test'); + }); +}); diff --git a/app/src/helpers/helpers.ts b/app/src/helpers/helpers.ts index 9c37dcb..aec6465 100644 --- a/app/src/helpers/helpers.ts +++ b/app/src/helpers/helpers.ts @@ -185,3 +185,8 @@ export function removeUserAgentSpecifics( .replace(`Electron/${process.versions.electron} `, '') .replace(`${appName}/${appVersion} `, ' '); } + +/** Removes extra spaces from a text */ +export function cleanupPlainText(text: string): string { + return text.trim().replace(/\s+/g, ' '); +}