From 530905ed622b15da30025834787a849cbbbe5f30 Mon Sep 17 00:00:00 2001 From: bookh Date: Thu, 28 May 2020 00:18:34 +0200 Subject: [PATCH] Made key bindings configurable --- README.md | 17 +++++++++++++---- src/index.js | 20 ++++++++++++++++---- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 72453e0..c70b1f4 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,10 @@ Rete comment plugin ==== #### Rete.js plugin -- Add inline comment: `Shift + C` -- Add frame comment: select nodes, `Shift + F` -- Delete comment: `Select comment and press Delete` -- Edit comment: `Call context menu` +- Add inline comment: `Shift + C` (by default) +- Add frame comment: select nodes, `Shift + F` (by default) +- Delete comment: `Select comment and press Delete` (by default) +- Edit comment: `Call context menu` ```js import CommentPlugin from 'rete-comment-plugin'; @@ -29,4 +29,13 @@ editor.on('editcomment', async (comment) => { comment.text = await openEditModal(comment.text); comment.update(); }); +``` + +Add custom key bindings +```js +editor.use(CommentPlugin, { + frameCommentKeys: { code: 'KeyF', shiftKey: true, ctrlKey: false, altKey: false }, + inlineCommentKeys: { code: 'KeyC', shiftKey: true, ctrlKey: false, altKey: false }, + deleteCommentKeys: { code: 'Delete', shiftKey: false, ctrlKey: false, altKey: false } +}) ``` \ No newline at end of file diff --git a/src/index.js b/src/index.js index 522284f..b4186d2 100644 --- a/src/index.js +++ b/src/index.js @@ -5,7 +5,13 @@ import InlineComment from './inline-comment'; import { nodesBBox, listenWindow } from './utils'; // eslint-disable-next-line max-statements -function install(editor, { margin = 30, disableBuiltInEdit = false }) { +function install(editor, { + margin = 30, + disableBuiltInEdit = false, + frameCommentKeys = { code: 'KeyF', shiftKey: true, ctrlKey: false, altKey: false }, + inlineCommentKeys = { code: 'KeyC', shiftKey: true, ctrlKey: false, altKey: false }, + deleteCommentKeys = { code: 'Delete', shiftKey: false, ctrlKey: false, altKey: false } +}) { editor.bind('commentselected'); editor.bind('commentcreated'); editor.bind('commentremoved'); @@ -24,16 +30,22 @@ function install(editor, { margin = 30, disableBuiltInEdit = false }) { } const destroyKeyListener = listenWindow('keydown', function handleKey(e) { - if (e.code === 'KeyF' && e.shiftKey) { + const keyCombosMap = [frameCommentKeys, inlineCommentKeys, deleteCommentKeys] + .map(function(x) { + return e.code === x.code && e.shiftKey === x.shiftKey && + e.ctrlKey === x.ctrlKey && e.altKey === x.altKey; + }); + + if (keyCombosMap[0]) { const ids = editor.selected.list.map(node => node.id); const nodes = ids.map(id => editor.nodes.find(n => n.id === id)); editor.trigger('addcomment', ({ type: 'frame', nodes })) - } else if (e.code === 'KeyC' && e.shiftKey) { + } else if (keyCombosMap[1]) { const position = Object.values(editor.view.area.mouse); editor.trigger('addcomment', ({ type: 'inline', position })) - } else if (e.code === 'Delete') { + } else if (keyCombosMap[2]) { manager.deleteFocusedComment(); } });