@atlaskit/editor-plugin-editor-viewmode-effects
Version:
Editor-Viewmode effects plugin for @atlaskit/editor-core
132 lines (131 loc) • 2.63 kB
JavaScript
import { Slice } from '@atlaskit/editor-prosemirror/model';
import { ReplaceStep, Step, StepMap, StepResult } from '@atlaskit/editor-prosemirror/transform';
export class ViewModeStep extends Step {
constructor({
inverted,
from,
to,
mark
}) {
super();
this.inverted = Boolean(inverted);
this.from = from;
this.to = to;
this.mark = mark;
}
invert(doc) {
return new ViewModeStep({
inverted: true,
from: this.from,
to: this.to,
mark: this.mark
});
}
apply(doc) {
return StepResult.ok(doc);
}
merge() {
return null;
}
map(mapping) {
const mappedFrom = mapping.mapResult(this.from, 1);
const mappedTo = mapping.mapResult(this.to, 1);
if (mappedFrom.deleted || mappedTo.deleted) {
return null;
}
return new ViewModeStep({
inverted: this.inverted,
from: mappedFrom.pos,
to: mappedTo.pos
});
}
getMap() {
return new StepMap([0, 0, 0]);
}
toJSON() {
// When serialized we should create a noop Replace step
return {
stepType: 'replace',
from: 0,
to: 0
};
}
static 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);
}
static from(step) {
const {
mark,
from,
to
} = step;
return new ViewModeStep({
mark,
from,
to
});
}
}
export class ViewModeNodeStep extends Step {
constructor({
inverted,
pos,
mark
}) {
super();
this.inverted = Boolean(inverted);
this.pos = pos;
this.mark = mark;
}
invert(doc) {
return new ViewModeNodeStep({
inverted: true,
pos: this.pos,
mark: this.mark
});
}
apply(doc) {
return StepResult.ok(doc);
}
merge() {
return null;
}
map(mapping) {
const mappedPos = mapping.mapResult(this.pos, 1);
if (mappedPos.deleted) {
return null;
}
return new ViewModeNodeStep({
inverted: this.inverted,
pos: mappedPos.pos
});
}
getMap() {
return new StepMap([0, 0, 0]);
}
toJSON() {
// When serialized we should create a noop Replace step
return {
stepType: 'replace',
from: 0,
to: 0
};
}
static 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);
}
static from(step) {
const {
mark,
pos
} = step;
return new ViewModeNodeStep({
mark,
pos
});
}
}