UNPKG

@atlaskit/editor-common

Version:

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

60 lines (58 loc) 3.14 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.PassiveTransaction = void 0; exports.editorCommandToPMCommand = editorCommandToPMCommand; var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _state = require("@atlaskit/editor-prosemirror/state"); function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } /** * Convert a EditorCommand to a standard Prosemirror Command. * The preferred approach to dispatching a `EditorCommand` is via the * `core.actions.execute` on `pluginInjectionAPI`. In some cases * the type may require a Command until we refactor this out and this * function is suitable for those cases. * * @param command A plugin command (a function that modifies and returns a `Transaction`) * @returns Command */ function editorCommandToPMCommand(command) { return function (_ref, dispatch) { var tr = _ref.tr; var newTr = command === null || command === void 0 ? void 0 : command({ tr: tr }); if (!newTr) { return false; } if (newTr instanceof PassiveTransaction) { return true; } dispatch === null || dispatch === void 0 || dispatch(newTr); return true; }; } /** * PassiveTransaction is used only to indicate that * an `EditorCommand` should return `true` but should not dispatch. */ var PassiveTransaction = exports.PassiveTransaction = /*#__PURE__*/function (_Transaction) { (0, _inherits2.default)(PassiveTransaction, _Transaction); var _super = _createSuper(PassiveTransaction); // This is very cheeky but this should never be used outside its intended // purpose - it will likely crash the editor so we should get an early warning // signal function PassiveTransaction() { (0, _classCallCheck2.default)(this, PassiveTransaction); // eslint-disable-next-line @typescript-eslint/no-explicit-any return _super.call(this, {}); } return (0, _createClass2.default)(PassiveTransaction); }(_state.Transaction);