use is-hotkey in slate plugin checks

This commit is contained in:
Shawn Erquhart 2017-12-14 13:39:50 -08:00
parent d321b9eb6d
commit 7c9c765c28

View File

@ -1,12 +1,13 @@
import { Text, Inline } from 'slate'; import { Text, Inline } from 'slate';
import isHotkey from 'is-hotkey';
import SlateSoftBreak from 'slate-soft-break'; import SlateSoftBreak from 'slate-soft-break';
import EditList from 'slate-edit-list'; import EditList from 'slate-edit-list';
import EditTable from 'slate-edit-table'; import EditTable from 'slate-edit-table';
const SoftBreak = (options = {}) => ({ const SoftBreak = (options = {}) => ({
onKeyDown(event, change) { onKeyDown(event, change) {
if (event.key != 'Enter') return; if (options.shift && !isHotkey('shift+enter', event)) return;
if (options.shift && event.shiftKey == false) return; if (!options.shift && !isHotkey('enter', event)) return;
const { onlyIn, ignoreIn, defaultBlock = 'paragraph' } = options; const { onlyIn, ignoreIn, defaultBlock = 'paragraph' } = options;
const { type, text } = change.value.startBlock; const { type, text } = change.value.startBlock;
@ -40,7 +41,7 @@ export const ParagraphSoftBreakConfigured = SoftBreak({ onlyIn: ['paragraph'], s
const BreakToDefaultBlock = ({ onlyIn = [], defaultBlock = 'paragraph' }) => ({ const BreakToDefaultBlock = ({ onlyIn = [], defaultBlock = 'paragraph' }) => ({
onKeyDown(event, change) { onKeyDown(event, change) {
const { value } = change; const { value } = change;
if (event.key != 'Enter' || event.shiftKey == true || value.isExpanded) return; if (!isHotkey('enter', event) || value.isExpanded) return;
if (onlyIn.includes(value.startBlock.type)) { if (onlyIn.includes(value.startBlock.type)) {
return change.insertBlock(defaultBlock); return change.insertBlock(defaultBlock);
} }