UNPKG

@atlaskit/adf-schema

Version:

Shared package that contains the ADF-schema (json) and ProseMirror node/mark specs

140 lines (137 loc) 6.81 kB
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { 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 _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } import { Step, StepResult, StepMap, ReplaceStep } from 'prosemirror-transform'; import { Slice } from 'prosemirror-model'; export var analyticsStepType = 'atlaskit-analytics'; export var analyticsInvertStepType = 'atlaskit-analytics-invert'; var HISTORY_ACTIONS = /*#__PURE__*/function (HISTORY_ACTIONS) { HISTORY_ACTIONS["UNDID"] = "undid"; HISTORY_ACTIONS["REDID"] = "redid"; return HISTORY_ACTIONS; }(HISTORY_ACTIONS || {}); /** Creates undo event from a normal analytics event */ function createUndoEvent(analyticsEvent) { var _analyticsEvent$paylo; return _objectSpread(_objectSpread({}, analyticsEvent), {}, { payload: { action: HISTORY_ACTIONS.UNDID, actionSubject: analyticsEvent.payload.actionSubject, actionSubjectId: analyticsEvent.payload.action, attributes: _objectSpread(_objectSpread({}, analyticsEvent.payload.attributes), {}, { actionSubjectId: analyticsEvent.payload.actionSubjectId, inputMethod: ((_analyticsEvent$paylo = analyticsEvent.payload.attributes) === null || _analyticsEvent$paylo === void 0 ? void 0 : _analyticsEvent$paylo.inputMethod) || '' }), eventType: 'track' } }); } /** Toggles event action between undo & redo */ var toggleEventAction = function toggleEventAction(analyticsEvent) { return _objectSpread(_objectSpread({}, analyticsEvent), {}, { payload: _objectSpread(_objectSpread({}, analyticsEvent.payload), {}, { action: analyticsEvent.payload.action === HISTORY_ACTIONS.UNDID ? HISTORY_ACTIONS.REDID : HISTORY_ACTIONS.UNDID }) }); }; function isHistoryAnalyticsEvent(event) { return event.payload.action === HISTORY_ACTIONS.UNDID || event.payload.action === HISTORY_ACTIONS.REDID; } /** * Custom Prosemirror Step to fire our GAS V3 analytics events * Using a Step means that it will work with prosemirror-history and we get * undo/redo events for free */ export var AnalyticsStep = /*#__PURE__*/function (_Step) { _inherits(AnalyticsStep, _Step); var _super = _createSuper(AnalyticsStep); function AnalyticsStep(analyticsEvents) { var _this; var actionsToIgnore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; var pos // Used to create the map, prevent splitting history. = arguments.length > 2 ? arguments[2] : undefined; _classCallCheck(this, AnalyticsStep); _this = _super.call(this); _defineProperty(_assertThisInitialized(_this), "analyticsEvents", []); _defineProperty(_assertThisInitialized(_this), "actionsToIgnore", []); _this.analyticsEvents = analyticsEvents; _this.actionsToIgnore = actionsToIgnore; _this.pos = pos; return _this; } /** * Generate new undo/redo analytics event when step is inverted */ _createClass(AnalyticsStep, [{ key: "invert", value: function invert() { var _this2 = this; var analyticsEvents = this.analyticsEvents.filter(function (analyticsEvent) { return _this2.actionsToIgnore.indexOf(analyticsEvent.payload.action) === -1; }).map(function (analyticsEvent) { if (isHistoryAnalyticsEvent(analyticsEvent)) { return toggleEventAction(analyticsEvent); } else { return createUndoEvent(analyticsEvent); } }); return new AnalyticsStep(analyticsEvents, []); } }, { key: "apply", value: function apply(doc) { return StepResult.ok(doc); } }, { key: "map", value: function map(mapping) { var newPos = this.pos; if (typeof newPos === 'number') { newPos = mapping.map(newPos); } // Return the same events, this step will never be removed return new AnalyticsStep(this.analyticsEvents, this.actionsToIgnore, newPos); } }, { key: "getMap", value: function getMap() { if (typeof this.pos === 'number') { return new StepMap([this.pos, 0, 0]); } return new StepMap([]); } }, { key: "merge", value: function merge(other) { if (other instanceof AnalyticsStep) { var otherAnalyticsEvents = other.analyticsEvents; return new AnalyticsStep([].concat(_toConsumableArray(otherAnalyticsEvents), _toConsumableArray(this.analyticsEvents))); } return null; } }, { key: "toJSON", value: function toJSON() { return { stepType: analyticsStepType }; } }], [{ key: "fromJSON", value: function fromJSON() { return new ReplaceStep(0, 0, Slice.empty); } }]); return AnalyticsStep; }(Step); /** Register this step with Prosemirror */ Step.jsonID(analyticsStepType, AnalyticsStep);