@atlaskit/adf-schema
Version:
Shared package that contains the ADF-schema (json) and ProseMirror node/mark specs
137 lines (134 loc) • 6.2 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { Step, StepResult, StepMap, ReplaceStep } from '@atlaskit/editor-prosemirror/transform';
import { Slice } from '@atlaskit/editor-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) {
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 = _callSuper(this, AnalyticsStep);
_defineProperty(_this, "analyticsEvents", []);
_defineProperty(_this, "actionsToIgnore", []);
_this.analyticsEvents = analyticsEvents;
_this.actionsToIgnore = actionsToIgnore;
_this.pos = pos;
return _this;
}
/**
* Generate new undo/redo analytics event when step is inverted
*/
_inherits(AnalyticsStep, _Step);
return _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);
}
}]);
}(Step);
/** Register this step with Prosemirror */
Step.jsonID(analyticsStepType, AnalyticsStep);