From 187fb1b85ddd8fa31022bd94d91ab69d6bb0d8fc Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Tue, 26 Jun 2018 00:52:46 +0100 Subject: [PATCH] Electron: Resolves #619: Context menu to cut, copy and paste. Also added menu to copy link in web view --- ElectronClient/app/ElectronAppWrapper.js | 6 ++++++ ElectronClient/app/gui/NoteText.jsx | 15 ++++++++++++--- ElectronClient/app/gui/note-viewer/index.html | 16 +++++++++++++++- ElectronClient/app/package-lock.json | 12 ++++++------ 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/ElectronClient/app/ElectronAppWrapper.js b/ElectronClient/app/ElectronAppWrapper.js index 5589e8aa3..b57c8a9f9 100644 --- a/ElectronClient/app/ElectronAppWrapper.js +++ b/ElectronClient/app/ElectronAppWrapper.js @@ -60,6 +60,12 @@ class ElectronAppWrapper { // Fix: https://github.com/electron-userland/electron-builder/issues/2269 if (shim.isLinux()) windowOptions.icon = __dirname + '/build/icons/128x128.png'; + require('electron-context-menu')({ + shouldShowMenu: (event, params) => { + return params.isEditable; + }, + }); + this.win_ = new BrowserWindow(windowOptions) this.win_.loadURL(url.format({ diff --git a/ElectronClient/app/gui/NoteText.jsx b/ElectronClient/app/gui/NoteText.jsx index c1ff392ce..0b635eb31 100644 --- a/ElectronClient/app/gui/NoteText.jsx +++ b/ElectronClient/app/gui/NoteText.jsx @@ -20,7 +20,6 @@ const MenuItem = bridge().MenuItem; const { shim } = require('lib/shim.js'); const eventManager = require('../eventManager'); const fs = require('fs-extra'); -const {clipboard} = require('electron') const md5 = require('md5'); const mimeUtils = require('lib/mime-utils.js').mime; const ArrayUtils = require('lib/ArrayUtils'); @@ -29,6 +28,7 @@ const dialogs = require('./dialogs'); const markdownUtils = require('lib/markdownUtils'); const ExternalEditWatcher = require('lib/services/ExternalEditWatcher'); const { toSystemSlashes, safeFilename } = require('lib/path-utils'); +const { clipboard } = require('electron'); require('brace/mode/markdown'); // https://ace.c9.io/build/kitchen-sink.html @@ -467,7 +467,9 @@ class NoteTextComponent extends React.Component { const menu = new Menu() - if (itemType === "image" || itemType === "link") { + console.info(itemType); + + if (itemType === "image" || itemType === "resource") { const resource = await Resource.load(arg0.resourceId); const resourcePath = Resource.fullPath(resource); @@ -485,9 +487,16 @@ class NoteTextComponent extends React.Component { }})); menu.append(new MenuItem({label: _('Copy path to clipboard'), click: async () => { - const { clipboard } = require('electron'); clipboard.writeText(toSystemSlashes(resourcePath)); }})); + } else if (itemType === "text") { + menu.append(new MenuItem({label: _('Copy'), click: async () => { + clipboard.writeText(arg0.textToCopy); + }})); + } else if (itemType === "link") { + menu.append(new MenuItem({label: _('Copy Link Address'), click: async () => { + clipboard.writeText(arg0.textToCopy); + }})); } else { reg.logger().error('Unhandled item type: ' + itemType); return; diff --git a/ElectronClient/app/gui/note-viewer/index.html b/ElectronClient/app/gui/note-viewer/index.html index 764307e60..b65e69074 100644 --- a/ElectronClient/app/gui/note-viewer/index.html +++ b/ElectronClient/app/gui/note-viewer/index.html @@ -229,9 +229,23 @@ if (element && element.getAttribute('data-resource-id')) { ipcRenderer.sendToHost('contextMenu', { - type: element.getAttribute('src') ? 'image' : 'link', + type: element.getAttribute('src') ? 'image' : 'resource', resourceId: element.getAttribute('data-resource-id'), }); + } else { + const selectedText = window.getSelection().toString(); + + if (selectedText) { + ipcRenderer.sendToHost('contextMenu', { + type: 'text', + textToCopy: selectedText, + }); + } else if (event.target.getAttribute('href')) { + ipcRenderer.sendToHost('contextMenu', { + type: 'link', + textToCopy: event.target.getAttribute('href'), + }); + } } }); diff --git a/ElectronClient/app/package-lock.json b/ElectronClient/app/package-lock.json index 6be73c760..f78e98244 100644 --- a/ElectronClient/app/package-lock.json +++ b/ElectronClient/app/package-lock.json @@ -2051,9 +2051,9 @@ } }, "electron-dl": { - "version": "1.10.0", - "resolved": "https://registry.npmjs.org/electron-dl/-/electron-dl-1.10.0.tgz", - "integrity": "sha1-+UQWBkBW/G8qhq5JhhTJNSaJCvk=", + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/electron-dl/-/electron-dl-1.12.0.tgz", + "integrity": "sha512-UMc2CL45Ybpvu66LDPYzwmDRmYK4Ivz+wdnTM0eXcNMztvQwhixAk2UPme1c7McqG8bAlKEkQpZn3epmQy4EWg==", "requires": { "ext-name": "^5.0.0", "pupa": "^1.0.0", @@ -4345,9 +4345,9 @@ "integrity": "sha512-OEUllcVoydBHGN1z84yfQDimn58pZNNNXgZlHXSboxMlFvgI6MXSWpWKpFRra7H1HxpVhHTkrghfRW49k6yjeg==" }, "mime-db": { - "version": "1.31.0", - "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.31.0.tgz", - "integrity": "sha512-oB3w9lx50CMd6nfonoV5rBRUbJtjMifUHaFb5MfzjC8ksAIfVjT0BsX46SjjqBz7n9JGTrTX3paIeLSK+rS5fQ==" + "version": "1.34.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.34.0.tgz", + "integrity": "sha1-RS0Oz/XDA0am3B5kseruDTcZ/5o=" }, "mime-types": { "version": "2.1.18",