@atlaskit/adf-schema
Version: 
Shared package that contains the ADF-schema (json) and ProseMirror node/mark specs
105 lines (102 loc) • 3.64 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import { Step, StepResult, StepMap, ReplaceStep } from '@atlaskit/editor-prosemirror/transform';
import { Slice } from '@atlaskit/editor-prosemirror/model';
export const analyticsStepType = 'atlaskit-analytics';
export const 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 {
    ...analyticsEvent,
    payload: {
      action: HISTORY_ACTIONS.UNDID,
      actionSubject: analyticsEvent.payload.actionSubject,
      actionSubjectId: analyticsEvent.payload.action,
      attributes: {
        ...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 */
const toggleEventAction = analyticsEvent => ({
  ...analyticsEvent,
  payload: {
    ...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 class AnalyticsStep extends Step {
  constructor(analyticsEvents, actionsToIgnore = [], pos // Used to create the map, prevent splitting history.
  ) {
    super();
    _defineProperty(this, "analyticsEvents", []);
    _defineProperty(this, "actionsToIgnore", []);
    this.analyticsEvents = analyticsEvents;
    this.actionsToIgnore = actionsToIgnore;
    this.pos = pos;
  }
  /**
   * Generate new undo/redo analytics event when step is inverted
   */
  invert() {
    const analyticsEvents = this.analyticsEvents.filter(analyticsEvent => this.actionsToIgnore.indexOf(analyticsEvent.payload.action) === -1).map(analyticsEvent => {
      if (isHistoryAnalyticsEvent(analyticsEvent)) {
        return toggleEventAction(analyticsEvent);
      } else {
        return createUndoEvent(analyticsEvent);
      }
    });
    return new AnalyticsStep(analyticsEvents, []);
  }
  apply(doc) {
    return StepResult.ok(doc);
  }
  map(mapping) {
    let 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);
  }
  getMap() {
    if (typeof this.pos === 'number') {
      return new StepMap([this.pos, 0, 0]);
    }
    return new StepMap([]);
  }
  merge(other) {
    if (other instanceof AnalyticsStep) {
      const otherAnalyticsEvents = other.analyticsEvents;
      return new AnalyticsStep([...otherAnalyticsEvents, ...this.analyticsEvents]);
    }
    return null;
  }
  toJSON() {
    return {
      stepType: analyticsStepType
    };
  }
  static fromJSON() {
    return new ReplaceStep(0, 0, Slice.empty);
  }
}
/** Register this step with Prosemirror */
Step.jsonID(analyticsStepType, AnalyticsStep);