mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-11-16 12:10:28 +03:00
Fix hotkeys (#1468)
* use hotkey using key instead of which (default) * remove shift from block formatting hotkeys * smartly exit formatting with backspace * set markdown to off by default * exit formatting with escape
This commit is contained in:
parent
5dc613cd79
commit
d5ff55e23e
9 changed files with 65 additions and 45 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import { isHotkey } from 'is-hotkey';
|
||||
import { isKeyHotkey } from 'is-hotkey';
|
||||
import { KeyboardEvent } from 'react';
|
||||
import { Editor } from 'slate';
|
||||
import { Editor, Range } from 'slate';
|
||||
import { isAnyMarkActive, isBlockActive, removeAllMark, toggleBlock, toggleMark } from './utils';
|
||||
import { BlockType, MarkType } from './types';
|
||||
|
||||
|
|
@ -15,10 +15,10 @@ export const INLINE_HOTKEYS: Record<string, MarkType> = {
|
|||
const INLINE_KEYS = Object.keys(INLINE_HOTKEYS);
|
||||
|
||||
export const BLOCK_HOTKEYS: Record<string, BlockType> = {
|
||||
'mod+shift+7': BlockType.OrderedList,
|
||||
'mod+shift+8': BlockType.UnorderedList,
|
||||
"mod+shift+'": BlockType.BlockQuote,
|
||||
'mod+shift+;': BlockType.CodeBlock,
|
||||
'mod+7': BlockType.OrderedList,
|
||||
'mod+8': BlockType.UnorderedList,
|
||||
"mod+'": BlockType.BlockQuote,
|
||||
'mod+;': BlockType.CodeBlock,
|
||||
};
|
||||
const BLOCK_KEYS = Object.keys(BLOCK_HOTKEYS);
|
||||
|
||||
|
|
@ -26,7 +26,36 @@ const BLOCK_KEYS = Object.keys(BLOCK_HOTKEYS);
|
|||
* @return boolean true if shortcut is toggled.
|
||||
*/
|
||||
export const toggleKeyboardShortcut = (editor: Editor, event: KeyboardEvent<Element>): boolean => {
|
||||
if (isHotkey('mod+e', event)) {
|
||||
if (isKeyHotkey('backspace', event) && editor.selection && Range.isCollapsed(editor.selection)) {
|
||||
const startPoint = Range.start(editor.selection);
|
||||
if (startPoint.offset !== 0) return false;
|
||||
|
||||
const [parentNode, parentPath] = Editor.parent(editor, startPoint);
|
||||
|
||||
if (Editor.isEditor(parentNode)) return false;
|
||||
|
||||
if (parentNode.type === BlockType.Heading) {
|
||||
toggleBlock(editor, BlockType.Paragraph);
|
||||
return true;
|
||||
}
|
||||
if (
|
||||
parentNode.type === BlockType.CodeLine ||
|
||||
parentNode.type === BlockType.QuoteLine ||
|
||||
parentNode.type === BlockType.ListItem
|
||||
) {
|
||||
// exit formatting only when line block
|
||||
// is first of last of it's parent
|
||||
const parentLocation = { at: parentPath };
|
||||
const [previousNode] = Editor.previous(editor, parentLocation) ?? [];
|
||||
const [nextNode] = Editor.next(editor, parentLocation) ?? [];
|
||||
if (!previousNode || !nextNode) {
|
||||
toggleBlock(editor, BlockType.Paragraph);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isKeyHotkey('mod+e', event) || isKeyHotkey('escape', event)) {
|
||||
if (isAnyMarkActive(editor)) {
|
||||
removeAllMark(editor);
|
||||
return true;
|
||||
|
|
@ -40,7 +69,7 @@ export const toggleKeyboardShortcut = (editor: Editor, event: KeyboardEvent<Elem
|
|||
}
|
||||
|
||||
const blockToggled = BLOCK_KEYS.find((hotkey) => {
|
||||
if (isHotkey(hotkey, event)) {
|
||||
if (isKeyHotkey(hotkey, event)) {
|
||||
event.preventDefault();
|
||||
toggleBlock(editor, BLOCK_HOTKEYS[hotkey]);
|
||||
return true;
|
||||
|
|
@ -52,7 +81,7 @@ export const toggleKeyboardShortcut = (editor: Editor, event: KeyboardEvent<Elem
|
|||
const inlineToggled = isBlockActive(editor, BlockType.CodeBlock)
|
||||
? false
|
||||
: INLINE_KEYS.find((hotkey) => {
|
||||
if (isHotkey(hotkey, event)) {
|
||||
if (isKeyHotkey(hotkey, event)) {
|
||||
event.preventDefault();
|
||||
toggleMark(editor, INLINE_HOTKEYS[hotkey]);
|
||||
return true;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue