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
function revertCommit(commitId, state) {
let tState = trackPlugin.getState(state)
let found = tState.commits[commitId];
if (!found) return
const commit = found;
let actions = [];
if (tState.uncommittedSteps.length) return alert("Commit your changes first!")
let remap = new Mapping(tState.commits.slice(found).reduce((maps, c) => maps.concat(c.maps), []))
let tr = state.tr
for (let i = commit.steps.length - 1; i >= 0; i--) {
let remapped = commit.steps[i].map(remap.slice(i + 1))
let result = remapped && tr.maybeStep(remapped)
if (result && result.doc) remap.appendMap(remapped.getMap(), i)
}
if (tr.steps.length) {
console.log('removing action', tr.steps)
actions.push(tr.action());
// actions.push(commitAction(`Revert '${commit.message}'`));
} else {
console.log('could not revert!');
}
return actions;
}
const adjustSteps = (doc, schema, stepsToAdjust, startIndex) => {
const tr = new Transform(doc);
const mapping = new Mapping();
const newSteps = [];
stepsToAdjust.forEach((step, index) => {
if (index < startIndex) {
/* Before the track changes starts */
newSteps.push(step);
tr.step(step);
} else if (step.from === step.to) {
/* If it's an insertion */
const mappedStep = step.map(mapping);
// console.log('----');
// console.log(JSON.stringify(mapping.maps));
// console.log(JSON.stringify(step.toJSON()));
// console.log(JSON.stringify(mappedStep.toJSON()));
// console.log(mappedStep.slice.content)
newSteps.push(mappedStep);
newSteps.push(
/* Update currentDoc with steps at current changeKey */
const nextDoc = currentSteps.reduce((prev, curr) => {
const stepResult = curr.apply(prev);
if (stepResult.failed) {
console.error('Failed with ', stepResult.failed);
}
return stepResult.doc;
}, currentDoc);
currentDoc = nextDoc;
/* Map all discussions in newDiscussions */
const currentStepMaps = currentSteps.map((step) => {
return step.getMap();
});
const currentMapping = new Mapping(currentStepMaps);
Object.keys(newDiscussions).forEach((discussionId) => {
const prevSelection = newDiscussions[discussionId].selection;
newDiscussions[discussionId].selection = prevSelection.map(
currentDoc,
currentMapping,
);
});
/* Init discussions that were made at this currentDoc */
Object.keys(discussions).forEach((discussionId) => {
if (discussions[discussionId].currentKey === Number(currentKey)) {
newDiscussions[discussionId] = {
...discussions[discussionId],
selection: Selection.fromJSON(
currentDoc,
const avoidDoubleCountingMaps = (oldMapping, newMap) => {
const newestMapping = new Mapping();
// const properNewMapRanges = [];
console.log(oldMapping, newMap, '###');
oldMapping.maps.forEach((oldMap) => {
oldMap.forEach((om_oldStart, om_oldEnd, om_newStart, om_newEnd) => {
newMap.forEach((oldStart, oldEnd, newStart, newEnd) => {
console.log(om_oldStart, om_oldEnd, om_newStart, om_newEnd);
console.log(oldStart, oldEnd, newStart, newEnd);
// const offsetStart = Math.max(0, om_newStart - newStart);
// const offsetEnd = Math.max(0, om_newEnd - newEnd);
const offsetStart =
om_newStart > newStart && om_newStart < newEnd ? newEnd - om_newStart : 0;
const offsetEnd =
om_newEnd > newStart && om_newEnd < newEnd ? newEnd - om_newEnd : 0;
// Math.max(0, newStart - om_newStart);
// const offsetEnd = Math.max(0, newEnd - om_newEnd);
function revertCommit(commit) {
let trackState = trackPlugin.getState(state)
let index = trackState.commits.indexOf(commit)
// If this commit is not in the history, we can't revert it
if (index == -1) return
// Reverting is only possible if there are no uncommitted changes
if (trackState.uncommittedSteps.length)
return alert("Commit your changes first!")
// This is the mapping from the document as it was at the start of
// the commit to the current document.
let remap = new Mapping(trackState.commits.slice(index)
.reduce((maps, c) => maps.concat(c.maps), []))
let tr = state.tr
// Build up a transaction that includes all (inverted) steps in this
// commit, rebased to the current document. They have to be applied
// in reverse order.
for (let i = commit.steps.length - 1; i >= 0; i--) {
// The mapping is sliced to not include maps for this step and the
// ones before it.
let remapped = commit.steps[i].map(remap.slice(i + 1))
if (!remapped) continue
let result = tr.maybeStep(remapped)
// If the step can be applied, add its map to our mapping
// pipeline, so that subsequent steps are mapped over it.
if (result.doc) remap.appendMap(remapped.getMap(), i)
}
// Add a commit message and dispatch.
storiesOf('Editor', module).add('stepTesting', () => {
const schema = buildSchema();
const doc = Node.fromJSON(schema, emptyDoc);
const tr = new Transform(doc);
const hydratedSteps = steps.map((step) => {
return Step.fromJSON(schema, step);
});
const adjustedSteps = adjustSteps2(doc, schema, hydratedSteps, 1);
adjustedSteps.forEach((step) => {
tr.step(step);
});
const generatedDoc = tr.doc;
// console.log(JSON.stringify(generatedDoc.toJSON()));
return (
const adjustSteps = (doc, schema, stepsToAdjust, startIndex) => {
const tr = new Transform(doc);
const mapping = new Mapping();
const newSteps = [];
stepsToAdjust.forEach((step, index) => {
if (index < startIndex) {
/* Before the track changes starts */
newSteps.push(step);
tr.step(step);
} else if (step.from === step.to) {
/* If it's an insertion */
const mappedStep = step.map(mapping);
// console.log('----');
// console.log(JSON.stringify(mapping.maps));
// console.log(JSON.stringify(step.toJSON()));
// console.log(JSON.stringify(mappedStep.toJSON()));
// console.log(mappedStep.slice.content)
newSteps.push(mappedStep);
export const adjustSteps2 = (doc, schema, stepsToAdjust, startIndex) => {
/* The header and structure-gap-replace with replace around */
/* is wonky because the inputRules plugin is removing '# ' */
/* and replacing it with headers. It's a problem when we try to invert */
/* the removal of '# ' */
console.log('********');
stepsToAdjust.forEach((step) => {
console.log(JSON.stringify(step.toJSON()));
});
const tr = new Transform(doc);
let mapping = new Mapping();
// const newSteps = [];
stepsToAdjust.forEach((step, index) => {
console.log('Mapping is', mapping.maps, step.jsonID);
/* TODO: need to be more rigorous about detecting mightBeInputRule */
/* We should verify that the item that is being removed matches */
/* one of the regexes */
const mightBeInputRule =
stepsToAdjust.length > index + 1 &&
stepsToAdjust[index + 1].jsonID === 'replaceAround' &&
step.to - step.from === 1;
if (index < startIndex || step.jsonID !== 'replace' || mightBeInputRule) {
/* Before the track changes starts */
// newSteps.push(step);
tr.step(step);