(editor: Editable, shortcuts: Record<string, HeadingType>)
| 35 | } |
| 36 | } |
| 37 | export const withShortcuts = (editor: Editable, shortcuts: Record<string, HeadingType>) => { |
| 38 | const { onKeydown } = editor |
| 39 | |
| 40 | editor.onKeydown = event => { |
| 41 | const { selection } = editor |
| 42 | if ( |
| 43 | selection && |
| 44 | Range.isCollapsed(selection) && |
| 45 | !Editable.isComposing(editor) && |
| 46 | Hotkey.match('space', event) && |
| 47 | !HeadingEditor.queryActive(editor) |
| 48 | ) { |
| 49 | const anchor = Range.start(selection) |
| 50 | const match = findMatchedRange(editor, anchor, shortcuts) |
| 51 | if (match) { |
| 52 | event.preventDefault() |
| 53 | const { shortcut, range, start } = match |
| 54 | const type = shortcuts[shortcut] |
| 55 | const ref = Editor.rangeRef(editor, range) |
| 56 | Transforms.delete(editor, { |
| 57 | at: { |
| 58 | anchor: start, |
| 59 | focus: range.anchor, |
| 60 | }, |
| 61 | }) |
| 62 | editor.selection = ref.unref() |
| 63 | HeadingEditor.toggle(editor, type) |
| 64 | Transforms.collapse(editor, { edge: 'start' }) |
| 65 | return |
| 66 | } |
| 67 | } |
| 68 | onKeydown(event) |
| 69 | } |
| 70 | } |
no test coverage detected
searching dependent graphs…