UNPKG

6.81 kBJavaScriptView Raw
1import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
3import _createClass from "@babel/runtime/helpers/createClass";
4import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
5import _inherits from "@babel/runtime/helpers/inherits";
6import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
7import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
8import _defineProperty from "@babel/runtime/helpers/defineProperty";
9function _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); }; }
10function _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; } }
11function 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; }
12function _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; }
13import { Step, StepResult, StepMap, ReplaceStep } from 'prosemirror-transform';
14import { Slice } from 'prosemirror-model';
15export var analyticsStepType = 'atlaskit-analytics';
16export var analyticsInvertStepType = 'atlaskit-analytics-invert';
17var HISTORY_ACTIONS = /*#__PURE__*/function (HISTORY_ACTIONS) {
18 HISTORY_ACTIONS["UNDID"] = "undid";
19 HISTORY_ACTIONS["REDID"] = "redid";
20 return HISTORY_ACTIONS;
21}(HISTORY_ACTIONS || {});
22/** Creates undo event from a normal analytics event */
23function createUndoEvent(analyticsEvent) {
24 var _analyticsEvent$paylo;
25 return _objectSpread(_objectSpread({}, analyticsEvent), {}, {
26 payload: {
27 action: HISTORY_ACTIONS.UNDID,
28 actionSubject: analyticsEvent.payload.actionSubject,
29 actionSubjectId: analyticsEvent.payload.action,
30 attributes: _objectSpread(_objectSpread({}, analyticsEvent.payload.attributes), {}, {
31 actionSubjectId: analyticsEvent.payload.actionSubjectId,
32 inputMethod: ((_analyticsEvent$paylo = analyticsEvent.payload.attributes) === null || _analyticsEvent$paylo === void 0 ? void 0 : _analyticsEvent$paylo.inputMethod) || ''
33 }),
34 eventType: 'track'
35 }
36 });
37}
38
39/** Toggles event action between undo & redo */
40var toggleEventAction = function toggleEventAction(analyticsEvent) {
41 return _objectSpread(_objectSpread({}, analyticsEvent), {}, {
42 payload: _objectSpread(_objectSpread({}, analyticsEvent.payload), {}, {
43 action: analyticsEvent.payload.action === HISTORY_ACTIONS.UNDID ? HISTORY_ACTIONS.REDID : HISTORY_ACTIONS.UNDID
44 })
45 });
46};
47function isHistoryAnalyticsEvent(event) {
48 return event.payload.action === HISTORY_ACTIONS.UNDID || event.payload.action === HISTORY_ACTIONS.REDID;
49}
50/**
51 * Custom Prosemirror Step to fire our GAS V3 analytics events
52 * Using a Step means that it will work with prosemirror-history and we get
53 * undo/redo events for free
54 */
55export var AnalyticsStep = /*#__PURE__*/function (_Step) {
56 _inherits(AnalyticsStep, _Step);
57 var _super = _createSuper(AnalyticsStep);
58 function AnalyticsStep(analyticsEvents) {
59 var _this;
60 var actionsToIgnore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
61 var pos // Used to create the map, prevent splitting history.
62 = arguments.length > 2 ? arguments[2] : undefined;
63 _classCallCheck(this, AnalyticsStep);
64 _this = _super.call(this);
65 _defineProperty(_assertThisInitialized(_this), "analyticsEvents", []);
66 _defineProperty(_assertThisInitialized(_this), "actionsToIgnore", []);
67 _this.analyticsEvents = analyticsEvents;
68 _this.actionsToIgnore = actionsToIgnore;
69 _this.pos = pos;
70 return _this;
71 }
72
73 /**
74 * Generate new undo/redo analytics event when step is inverted
75 */
76 _createClass(AnalyticsStep, [{
77 key: "invert",
78 value: function invert() {
79 var _this2 = this;
80 var analyticsEvents = this.analyticsEvents.filter(function (analyticsEvent) {
81 return _this2.actionsToIgnore.indexOf(analyticsEvent.payload.action) === -1;
82 }).map(function (analyticsEvent) {
83 if (isHistoryAnalyticsEvent(analyticsEvent)) {
84 return toggleEventAction(analyticsEvent);
85 } else {
86 return createUndoEvent(analyticsEvent);
87 }
88 });
89 return new AnalyticsStep(analyticsEvents, []);
90 }
91 }, {
92 key: "apply",
93 value: function apply(doc) {
94 return StepResult.ok(doc);
95 }
96 }, {
97 key: "map",
98 value: function map(mapping) {
99 var newPos = this.pos;
100 if (typeof newPos === 'number') {
101 newPos = mapping.map(newPos);
102 }
103 // Return the same events, this step will never be removed
104 return new AnalyticsStep(this.analyticsEvents, this.actionsToIgnore, newPos);
105 }
106 }, {
107 key: "getMap",
108 value: function getMap() {
109 if (typeof this.pos === 'number') {
110 return new StepMap([this.pos, 0, 0]);
111 }
112 return new StepMap([]);
113 }
114 }, {
115 key: "merge",
116 value: function merge(other) {
117 if (other instanceof AnalyticsStep) {
118 var otherAnalyticsEvents = other.analyticsEvents;
119 return new AnalyticsStep([].concat(_toConsumableArray(otherAnalyticsEvents), _toConsumableArray(this.analyticsEvents)));
120 }
121 return null;
122 }
123 }, {
124 key: "toJSON",
125 value: function toJSON() {
126 return {
127 stepType: analyticsStepType
128 };
129 }
130 }], [{
131 key: "fromJSON",
132 value: function fromJSON() {
133 return new ReplaceStep(0, 0, Slice.empty);
134 }
135 }]);
136 return AnalyticsStep;
137}(Step);
138
139/** Register this step with Prosemirror */
140Step.jsonID(analyticsStepType, AnalyticsStep);
\No newline at end of file