Skip to content

Commit

Permalink
Merge pull request #21 from BookHouseEffect/configurable-key-bindings
Browse files Browse the repository at this point in the history
Made key bindings configurable
  • Loading branch information
Ni55aN authored Jun 20, 2020
2 parents 3f8f64f + 530905e commit 0b6ea48
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 }
})
```
20 changes: 16 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();
}
});
Expand Down

0 comments on commit 0b6ea48

Please sign in to comment.