MCPcopy Create free account
hub / github.com/editablejs/editable / withShortcuts

Function withShortcuts

packages/plugins/heading/src/plugin/with-shortcuts.ts:37–70  ·  view source on GitHub ↗
(editor: Editable, shortcuts: Record<string, HeadingType>)

Source from the content-addressed store, hash-verified

35 }
36}
37export 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}

Callers 1

withHeadingFunction · 0.90

Calls 1

findMatchedRangeFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…