@vericus/slate-kit-bind-hotkey
Version:
a slate helper plugin to associate hotkey(s) to slate change
190 lines (146 loc) • 5.06 kB
JavaScript
exports.__esModule = true;
exports.default = void 0;
var _immutable = require("immutable");
var _isHotkey = _interopRequireDefault(require("is-hotkey"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _setPrototypeOf(subClass, superClass);
}
function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
function _isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
return true;
} catch (e) {
return false;
}
}
function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _possibleConstructorReturn(self, call) {
if (call && (typeof call === "object" || typeof call === "function")) {
return call;
}
return _assertThisInitialized(self);
}
function _createSuper(Derived) {
var hasNativeReflectConstruct = _isNativeReflectConstruct();
return function () {
var Super = _getPrototypeOf(Derived),
result;
if (hasNativeReflectConstruct) {
var NewTarget = _getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn(this, result);
};
}
function _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
}
function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
}
function _iterableToArray(iter) {
if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
var defaultOptions = {
hotkeys: undefined,
commandName: undefined,
commandArgs: undefined
};
var Options = /*#__PURE__*/function (_Record) {
_inherits(Options, _Record);
var _super = _createSuper(Options);
function Options() {
_classCallCheck(this, Options);
return _super.apply(this, arguments);
}
return Options;
}((0, _immutable.Record)(defaultOptions));
function createOnKeyDown(opts) {
var hotkeys = opts.hotkeys;
var commandName = opts.commandName,
commandArgs = opts.commandArgs;
var hotkeyArrays = Array.isArray(hotkeys) ? hotkeys : [hotkeys];
function checkHotKey(event) {
return hotkeyArrays.some(function (hotkey) {
return (0, _isHotkey.default)(hotkey)(event);
});
}
return function (event, editor, next) {
if (checkHotKey(event) && editor[commandName]) {
event.preventDefault();
event.stopPropagation();
if (commandArgs) {
editor[commandName].apply(editor, _toConsumableArray(commandArgs));
} else {
editor[commandName]();
}
return true;
}
return next();
};
}
function createHotKeyPlugin() {
var pluginOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = new Options(pluginOptions);
var onKeyDown = createOnKeyDown(options);
return {
onKeyDown: onKeyDown
};
}
var _default = createHotKeyPlugin;
exports.default = _default;