Skip to content
This repository has been archived by the owner on May 19, 2020. It is now read-only.

Commit

Permalink
refactor(Slate): hotkey switch to object literal
Browse files Browse the repository at this point in the history
Signed-off-by: elit-altum <[email protected]>
  • Loading branch information
elit-altum authored and jolanglinais committed Mar 6, 2020
1 parent d7a4055 commit 8916f62
Showing 1 changed file with 49 additions and 34 deletions.
83 changes: 49 additions & 34 deletions src/SlateAsInputEditor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,43 +375,58 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
* @param {*} next
*/
const onKeyDown = async (event, editor, next) => {
switch (true) {

case event.key==='Enter':
return handleEnter(event, editor, next);

case event.key==='Backspace':
return handleBackspace(event, editor, next);

case isHotKey("mod+z", event) && editor.props.editorProps.onUndoOrRedo:
await editor.undo();
return editor.props.editorProps.onUndoOrRedo(editor);

case isHotKey('mod+shift+z', event) && editor.props.editorProps.onUndoOrRedo:
await editor.redo();
return editor.props.editorProps.onUndoOrRedo(editor);

case isHotKey("mod+b", event) :
return editor.toggleMark("bold");

case isHotKey("mod+i", event):
return editor.toggleMark("italic");

case isHotKey("mod+alt+c", event):
return editor.toggleMark("code");

case isHotKey("mod+q", event):
return handleBlockQuotes(editor)

case isHotKey("mod+shift+7", event):
return handleList(editor, "ol_list");
const isEnter = () => {
return handleEnter(event, editor, next);
}

const isBackSpace = () => {
return handleBackspace(event, editor, next);
}

const isSpecialKey = async () => {
switch (true) {
case isHotKey("mod+z", event) && editor.props.editorProps.onUndoOrRedo:
await editor.undo();
return editor.props.editorProps.onUndoOrRedo(editor);

case isHotKey('mod+shift+z', event) && editor.props.editorProps.onUndoOrRedo:
await editor.redo();
return editor.props.editorProps.onUndoOrRedo(editor);

case isHotKey("mod+shift+8", event):
return handleList(editor, "ul_list");
case isHotKey("mod+b", event) :
return editor.toggleMark("bold");

case isHotKey("mod+i", event):
return editor.toggleMark("italic");

default :
return next();
case isHotKey("mod+alt+c", event):
return editor.toggleMark("code");

case isHotKey("mod+q", event):
return handleBlockQuotes(editor)

case isHotKey("mod+shift+7", event):
return handleList(editor, "ol_list");

case isHotKey("mod+shift+8", event):
return handleList(editor, "ul_list");

default :
return next();
}
}

const inputHandler = (key) => {
const cases = {
'Enter': isEnter,
'Backspace': isBackSpace,
'default': isSpecialKey,
}

return (cases[key] || cases['default'])();
}

inputHandler(event.key);
}


Expand Down

0 comments on commit 8916f62

Please sign in to comment.