@atlaskit/adf-schema
Version:
Shared package that contains the ADF-schema (json) and ProseMirror node/mark specs
60 lines • 1.87 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import { Step, StepResult, StepMap } from '@atlaskit/editor-prosemirror/transform';
export const overrideDocumentStepType = 'override-document';
export class OverrideDocumentStep extends Step {
constructor(opts) {
super();
_defineProperty(this, "inverted", false);
_defineProperty(this, "oldDocumentSize", 0);
const {
inverted,
nextDocument
} = opts;
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);
}
apply(doc) {
this.oldDocumentSize = doc.content.size;
return StepResult.ok(this.nextDocument);
}
map() {
return new OverrideDocumentStep({
nextDocument: this.nextDocument,
inverted: this.inverted
});
}
getMap() {
if (!this.nextDocument || !this.oldDocumentSize) {
return new StepMap([0, 0, 0]);
}
const oldSize = this.oldDocumentSize;
const nextDocumentSize = this.nextDocument.content.size;
return new StepMap([0, oldSize, nextDocumentSize]);
}
invert(doc) {
return new OverrideDocumentStep({
nextDocument: doc,
inverted: true
});
}
toJSON() {
return {
stepType: overrideDocumentStepType,
inverted: this.inverted,
nextDocument: this.nextDocument.toJSON()
};
}
static 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.jsonID(overrideDocumentStepType, OverrideDocumentStep);