From be9c16cbdec789b557da118b1fc09b950be7171a Mon Sep 17 00:00:00 2001 From: btzr-io Date: Mon, 25 Jun 2018 14:42:49 -0600 Subject: [PATCH] fix context-menu of markdown editor --- .../common/form-components/form-field.jsx | 13 ++++++++- src/renderer/util/contextMenu.js | 28 +++++++++++++++++-- yarn.lock | 6 ++-- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/renderer/component/common/form-components/form-field.jsx b/src/renderer/component/common/form-components/form-field.jsx index 0b23e49e8..0cac65160 100644 --- a/src/renderer/component/common/form-components/form-field.jsx +++ b/src/renderer/component/common/form-components/form-field.jsx @@ -6,6 +6,7 @@ import MarkdownPreview from 'component/common/markdown-preview'; import SimpleMDE from 'react-simplemde-editor'; import 'simplemde/dist/simplemde.min.css'; import Toggle from 'react-toggle'; +import { openEditorMenu } from 'util/contextMenu'; type Props = { name: string, @@ -54,11 +55,21 @@ export class FormField extends React.PureComponent { ); } else if (type === 'markdown') { + const stopContextMenu = event => { + event.preventDefault(); + event.stopPropagation(); + }; + const handleEvents = { + contextmenu(codeMirror, event) { + openEditorMenu(event, codeMirror); + }, + }; input = ( -
+
0; + const isSomethingSelected = selection.length > 0 || window.getSelection().toString().length > 0; const isInput = event.target.matches('input') && (type === 'text' || type === 'number'); const isTextField = canEdit || isInput || event.target.matches('textarea'); @@ -61,6 +61,30 @@ export function openContextMenu(event, templates = [], canEdit = false) { injectDevelopmentTemplate(event, templates); remote.Menu.buildFromTemplate(templates).popup(); } + +export function openEditorMenu(event, codeMirror) { + const value = codeMirror.doc.getValue(); + const selection = codeMirror.doc.getSelection(); + const templates = [ + { + label: 'Select All', + accelerator: 'CmdOrCtrl+A', + role: 'selectall', + click: () => { + codeMirror.execCommand('selectAll'); + }, + enabled: value.length > 0, + }, + { + label: 'Cut', + accelerator: 'CmdOrCtrl+X', + role: 'cut', + enabled: selection.length > 0, + }, + ]; + openContextMenu(event, templates, true, selection); +} + export function openCopyLinkMenu(text, event) { const templates = [ { diff --git a/yarn.lock b/yarn.lock index e272149a0..8bb0709a8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7626,9 +7626,9 @@ react-redux@^5.0.3: loose-envify "^1.1.0" prop-types "^15.6.0" -react-simplemde-editor@^3.6.15: - version "3.6.15" - resolved "https://registry.yarnpkg.com/react-simplemde-editor/-/react-simplemde-editor-3.6.15.tgz#b4991304c7e1cac79258bb225579d008c13b5991" +react-simplemde-editor@^3.6.16: + version "3.6.16" + resolved "https://registry.yarnpkg.com/react-simplemde-editor/-/react-simplemde-editor-3.6.16.tgz#33633259478d3395f2c7b70deb56a1a40e863bea" dependencies: simplemde "^1.11.2"