@atlaskit/adf-schema
Version:
Shared package that contains the ADF-schema (json) and ProseMirror node/mark specs
82 lines • 3.25 kB
JavaScript
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; })(); }
import { Step, StepResult, StepMap } from '@atlaskit/editor-prosemirror/transform';
export var overrideDocumentStepType = 'override-document';
export var OverrideDocumentStep = /*#__PURE__*/function (_Step) {
function OverrideDocumentStep(opts) {
var _this;
_classCallCheck(this, OverrideDocumentStep);
_this = _callSuper(this, OverrideDocumentStep);
_defineProperty(_this, "inverted", false);
_defineProperty(_this, "oldDocumentSize", 0);
var inverted = opts.inverted,
nextDocument = opts.nextDocument;
if (nextDocument.type.name !== 'doc') {
throw new Error('nextDocument should be the entire prosemirror doc node and not only its content.');
}
_this.nextDocument = nextDocument;
_this.inverted = Boolean(inverted);
return _this;
}
_inherits(OverrideDocumentStep, _Step);
return _createClass(OverrideDocumentStep, [{
key: "apply",
value: function apply(doc) {
this.oldDocumentSize = doc.content.size;
return StepResult.ok(this.nextDocument);
}
}, {
key: "map",
value: function map() {
return new OverrideDocumentStep({
nextDocument: this.nextDocument,
inverted: this.inverted
});
}
}, {
key: "getMap",
value: function getMap() {
if (!this.nextDocument || !this.oldDocumentSize) {
return new StepMap([0, 0, 0]);
}
var oldSize = this.oldDocumentSize;
var nextDocumentSize = this.nextDocument.content.size;
return new StepMap([0, oldSize, nextDocumentSize]);
}
}, {
key: "invert",
value: function invert(doc) {
return new OverrideDocumentStep({
nextDocument: doc,
inverted: true
});
}
}, {
key: "toJSON",
value: function toJSON() {
return {
stepType: overrideDocumentStepType,
inverted: this.inverted,
nextDocument: this.nextDocument.toJSON()
};
}
}], [{
key: "fromJSON",
value: function fromJSON(schema, json) {
if (!json || json.stepType !== overrideDocumentStepType) {
throw new RangeError('Invalid overrideDocument step OverrideDocumentStep.fromJSON');
}
return new OverrideDocumentStep({
inverted: json.inverted,
nextDocument: schema.nodeFromJSON(json.nextDocument)
});
}
}]);
}(Step);
Step.jsonID(overrideDocumentStepType, OverrideDocumentStep);