UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

59 lines (55 loc) 2.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.keymap = keymap; var _w3cKeyname = require("w3c-keyname"); var _keymap = require("@atlaskit/editor-prosemirror/keymap"); var _safePlugin = require("../safe-plugin"); /** * A workaround for mostly Cyrillic but should have a positive affect * on other languages / layouts. Attempts a similar approach to OS X. * @see ED-7310 * @see https://github.com/ProseMirror/prosemirror/issues/957 * @param bindings */ // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any function keymap(bindings) { return new _safePlugin.SafePlugin({ props: { handleKeyDown: function handleKeyDown(view, event) { var name = (0, _w3cKeyname.keyName)(event); var keyboardEvent = event; // We will try to bypass the keycode only if any of mod keys are pressed, // to allow users to use non-latin and Dead characters. var isModKeyPressed = event.ctrlKey || event.metaKey; // Check the unicode of the character to assert that it's not an ASCII character. // These are characters outside latin's range. // Ignored via go/ees005 // eslint-disable-next-line require-unicode-regexp var isNonLatinKey = name.length === 1 && /[^\u0000-\u007f]/.test(name); // The `Dead` key is a key that combines with a following key to produce a combined character. // It will have `even.key === 'Dead'` in some browsers but the `keyCode` will be the same as in a qwerty-keyboard. // See https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key and https://en.wikipedia.org/wiki/Dead_key var isDeadKey = name === 'Dead'; if (isModKeyPressed && (isNonLatinKey || isDeadKey)) { keyboardEvent = new KeyboardEvent(event.type, { // Ignored via go/ees007 // eslint-disable-next-line @atlaskit/editor/enforce-todo-comment-format // FIXME: The event.keyCode is deprecated (see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode), // and could be removed in any time, but the w3c-keyname library doesn't provide a way to get // a key by event.code. key: _w3cKeyname.base[event.keyCode], code: event.code, ctrlKey: event.ctrlKey, altKey: event.altKey, metaKey: event.metaKey, shiftKey: event.shiftKey }); } return (0, _keymap.keydownHandler)(bindings)(view, keyboardEvent); } } }); }