@lyra/form-builder
Version:
Lyra form builder
60 lines (50 loc) • 1.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.keyMaps = undefined;
exports.default = SetMarksOnKeyComboPlugin;
var _isHotkey = require('is-hotkey');
var _isHotkey2 = _interopRequireDefault(_isHotkey);
var _slate = require('slate');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// This plugin makes keyboard shortcuts for decorators
/*:: type Options = {
decorators?: string[]
}*/
const isStrongHotkey = (0, _isHotkey2.default)('mod+b');
const isEmphasisHotkey = (0, _isHotkey2.default)('mod+i');
const isUnderlinedHotkey = (0, _isHotkey2.default)('mod+u');
const isCodeHotKey = (0, _isHotkey2.default)("mod+'");
const modKeyPlatformName = (0, _isHotkey.toKeyName)('mod');
const keyMaps = exports.keyMaps = {
strong: `${modKeyPlatformName} + b`,
em: `${modKeyPlatformName} + i`,
underline: `${modKeyPlatformName} + u`,
code: `${modKeyPlatformName} + '`
};
function SetMarksOnKeyComboPlugin(options /*: Options*/ = {}) {
const decorators = options.decorators || [];
return {
onKeyDown(event /*: SyntheticKeyboardEvent<*>*/, change /*: Change*/) {
let mark;
if (isStrongHotkey(event)) {
mark = 'strong';
} else if (isEmphasisHotkey(event)) {
mark = 'em';
} else if (isUnderlinedHotkey(event)) {
mark = 'underline';
} else if (isCodeHotKey(event)) {
mark = 'code';
} else {
return undefined;
}
// Return if not supported by schema
if (!decorators.includes(mark)) {
return undefined;
}
event.preventDefault();
return change.toggleMark(mark);
}
};
}