All files / app/assets/javascripts/content_editor/extensions external_keydown_handler.js

40% Statements 2/5
100% Branches 0/0
66.66% Functions 2/3
40% Lines 2/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39                              197x         65x                                    
import { Extension } from '@tiptap/core';
import { Plugin, PluginKey } from '@tiptap/pm/state';
import { KEYDOWN_EVENT } from '../constants';
 
/**
 * This extension bubbles up the keydown event, captured by ProseMirror in the
 * contenteditale element, to the presentation layer implemented in vue.
 *
 * The purpose of this mechanism is allowing clients of the
 * content editor to attach keyboard shortcuts for behavior outside
 * of the Content Editor’s boundaries, i.e. submitting a form to save changes.
 */
export default Extension.create({
  name: 'keyboardShortcut',
  addOptions() {
    return {
      eventHub: null,
    };
  },
  addProseMirrorPlugins() {
    return [
      new Plugin({
        key: new PluginKey('keyboardShortcut'),
        props: {
          handleKeyDown: (_, event) => {
            const {
              options: { eventHub },
            } = this;
 
            eventHub.$emit(KEYDOWN_EVENT, event);
 
            return false;
          },
        },
      }),
    ];
  },
});