@atlaskit/adf-schema
Version:
Shared package that contains the ADF-schema (json) and ProseMirror node/mark specs
103 lines (99 loc) • 3.94 kB
JavaScript
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
var _excluded = ["sourceEvent"];
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; } }
import { Slice } from 'prosemirror-model';
import { ReplaceStep, Step, StepMap, StepResult } from 'prosemirror-transform';
export var stepType = 'editor-linking-meta';
export var invertStepType = 'editor-linking-meta-invert';
/**
* Custom Prosemirror Step to attach metadata about user interactions with links
* Using a Step means that it will work with prosemirror-history and we get utilise when
* firing events on history change
*/
export var LinkMetaStep = /*#__PURE__*/function (_Step) {
_inherits(LinkMetaStep, _Step);
var _super = _createSuper(LinkMetaStep);
function LinkMetaStep(pos, metadata) {
var _this;
var isInverted = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
_classCallCheck(this, LinkMetaStep);
_this = _super.call(this);
_this.pos = pos;
_this.metadata = metadata;
_this.isInverted = isInverted;
return _this;
}
_createClass(LinkMetaStep, [{
key: "getMetadata",
value: function getMetadata() {
return this.metadata;
}
/**
* Generate new undo/redo analytics event when step is inverted
*/
}, {
key: "invert",
value: function invert() {
/**
* Omit sourceEvent in history
*/
var _this$metadata = this.metadata,
sourceEvent = _this$metadata.sourceEvent,
metadata = _objectWithoutProperties(_this$metadata, _excluded);
return new LinkMetaStep(this.pos, metadata, true);
}
// Should make no modifications to the doc
}, {
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 LinkMetaStep(newPos, this.metadata, this.isInverted);
}
}, {
key: "getMap",
value: function getMap() {
return new StepMap([this.pos || 0, 0, 0]);
}
// Return null to avoid merging events
}, {
key: "merge",
value: function merge() {
return null;
}
}, {
key: "toJSON",
value: function toJSON() {
// When serialized we should create a noop Replace step
return {
stepType: 'replace',
from: 0,
to: 0
};
}
}], [{
key: "fromJSON",
value: function fromJSON(_, __) {
// This is a "local custom step" once serialized
// we need to transform it in a no-operation action
return new ReplaceStep(0, 0, Slice.empty);
}
}]);
return LinkMetaStep;
}(Step);
/** Register this step with Prosemirror */
Step.jsonID(stepType, LinkMetaStep);