Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// the last local step (`left`).
// Shared base step
let state = mkState()
state = type(state, "base")
state = state.apply(closeHistory(state.tr))
const baseDoc = state.doc
// Local unconfirmed step
//
// - left
// /
// base -
// \
// - right
let rightStep = new ReplaceStep(5, 5, new Slice(Fragment.from(schema.text(" right")), 0, 0))
state = state.apply(state.tr.step(rightStep))
ist(state.doc, doc(p("base right")), eq)
ist(undoDepth(state), 2)
let leftStep = new ReplaceStep(1, 1, new Slice(Fragment.from(schema.text("left ")), 0, 0))
// Receive remote step and rebase local unconfirmed step
//
// base --> left --> right'
const tr = state.tr
tr.step(rightStep.invert(baseDoc))
tr.step(leftStep)
tr.step(rightStep.map(tr.mapping.slice(1)))
tr.mapping.setMirror(0, tr.steps.length - 1)
tr.setMeta("addToHistory", false)
tr.setMeta("rebased", 1)
state = state.apply(tr)
state = type(state, "base")
state = state.apply(closeHistory(state.tr))
const baseDoc = state.doc
// Local unconfirmed step
//
// - left
// /
// base -
// \
// - right
let rightStep = new ReplaceStep(5, 5, new Slice(Fragment.from(schema.text(" right")), 0, 0))
state = state.apply(state.tr.step(rightStep))
ist(state.doc, doc(p("base right")), eq)
ist(undoDepth(state), 2)
let leftStep = new ReplaceStep(1, 1, new Slice(Fragment.from(schema.text("left ")), 0, 0))
// Receive remote step and rebase local unconfirmed step
//
// base --> left --> right'
const tr = state.tr
tr.step(rightStep.invert(baseDoc))
tr.step(leftStep)
tr.step(rightStep.map(tr.mapping.slice(1)))
tr.mapping.setMirror(0, tr.steps.length - 1)
tr.setMeta("addToHistory", false)
tr.setMeta("rebased", 1)
state = state.apply(tr)
ist(state.doc, doc(p("left base right")), eq)
ist(undoDepth(state), 2)
// Undo local unconfirmed step
if (node.content) {
node.content.forEach((node2, offset2) => {
console.log('node2content', node2, startingPoint + offset2);
tr.addMark(
startingPoint + offset2,
startingPoint + offset2 + node2.text.length,
deletionMark,
);
});
}
}
});
if (hasInsertion) {
const insertionMark = schema.marks.strong.create();
const start = mappedStep.to;
const newReplaceStep = new ReplaceStep(start, start, mappedStep.slice);
// console.log('&&&', newReplaceStep.getMap());
// mapping.appendMap(newReplaceStep.getMap());
mapping = avoidDoubleCountingMaps(mapping, newReplaceStep.getMap());
tr.step(newReplaceStep);
tr.addMark(start, start + mappedStep.slice.content.size, insertionMark);
// tr.replaceRange(mappedStep.from, mappedStep.from, mappedStep.slice);
}
}
if (hasInsertion && !hasDeletion) {
let startingPoint = mappedStep.from;
mappedStep.slice.content.forEach((node) => {
const insertionMark = schema.marks.strong.create();
if (node.type.name === 'text') {
tr.addMark(startingPoint, startingPoint + node.text.length, insertionMark);
startingPoint += node.text.length;
} else {
const createAdditionalSteps = (step, doc, schema) => {
const docSlice = doc.slice(step.from, step.to);
let sliceIsAddition = false;
docSlice.content.forEach((child) => {
if (child.marks.some((mark) => mark.type.name === 'addition')) {
sliceIsAddition = true;
}
});
if (step instanceof ReplaceStep) {
if (sliceIsAddition) {
return [];
}
// Invert the replacement...
const deletionSize = step.to - step.from;
const additionSize = step.slice.size;
const addReplacedStep = new ReplaceStep(step.from, step.from, docSlice);
const addDeletionMark = new AddMarkStep(step.from, step.to, schema.marks.deletion.create());
const addAdditionMark =
additionSize &&
new AddMarkStep(
step.from + deletionSize,
step.to + additionSize + deletionSize,
schema.marks.addition.create(),
);
return [addReplacedStep, addDeletionMark, addAdditionMark];
}
if (step instanceof ReplaceAroundStep) {
throw new Error("Can't handle this step yet");
}
return [];
};
const createReplaceWholeDocumentChange = (
startDocument,
endDocument,
draftBranchId,
isOrphanedVersionChange,
) => {
const replaceSlice = new Slice(endDocument.content, 0, 0);
const replaceStep = new ReplaceStep(0, startDocument.nodeSize - 2, replaceSlice);
return new Change(
[replaceStep],
PUBPUB_CLIENT_ID,
Date.now(),
draftBranchId,
isOrphanedVersionChange,
);
};