This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
outline/app/components/Editor/plugins/KeyboardShortcuts.js
2017-11-10 14:14:30 -08:00

48 lines
1.2 KiB
JavaScript

// @flow
export default function KeyboardShortcuts() {
return {
/**
* On key down, check for our specific key shortcuts.
*
* @param {Event} e
* @param {Data} data
* @param {State} state
* @return {State or Null} state
*/
onKeyDown(ev: SyntheticEvent, data: Object, state: Object) {
if (!data.isMeta) return null;
switch (data.key) {
case 'b':
return this.toggleMark(state, 'bold');
case 'i':
return this.toggleMark(state, 'italic');
case 'u':
return this.toggleMark(state, 'underlined');
case 'd':
return this.toggleMark(state, 'deleted');
case 'k':
return state
.transform()
.wrapInline({ type: 'link', data: { href: '' } })
.apply();
default:
return null;
}
},
toggleMark(state: Object, type: string) {
// don't allow formatting of document title
const firstNode = state.document.nodes.first();
if (firstNode === state.startBlock) return;
state = state
.transform()
.toggleMark(type)
.apply();
return state;
},
};
}