Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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.
remapping(from, to) {
let maps = new Mapping
this.items.forEach((item, i) => {
let mirrorPos = item.mirrorOffset != null && i - item.mirrorOffset >= from
? maps.maps.length - item.mirrorOffset : null
maps.appendMap(item.map, mirrorPos)
}, from, to)
return maps
}
const rebaseCommit = ({ commit, view, doc, allCommits, newSteps, changesRef, clientID, latestKey, selfChanges }) => {
function compressedStepJSONToStep(compressedStepJSON) {
return Step.fromJSON(view.state.schema, uncompressStepJSON(compressedStepJSON)) }
const docMapping = new Mapping();
for (const step of newSteps) {
docMapping.appendMap(step.getMap());
}
let tr = view.state.tr;
const commitSteps = commit.steps;
const previousSteps = [];
for (const commit of allCommits) {
for (const steps of Object.values(commit.steps)) {
const compressedStepsJSON = steps.s;
previousSteps.push(...compressedStepsJSON.map(compressedStepJSONToStep));
}
}
export const revertCommit = (view, commit) => {
if (!view) return { state: "FAILED" };
const { dispatch, state } = view;
const trackPluginState = trackPluginKey.getState(state);
const { commits, uncommittedSteps } = trackPluginState;
const commitIndex = commits.indexOf(commit);
if (commitIndex < 0) return { state: "FAILED", message: "INVALID_COMMIT" };
if (uncommittedSteps.length > 0)
return { state: "FAILED", message: "UNCOMMITTED_CHANGES" };
const newCommits = commits.slice(commitIndex);
const remap = new Mapping(
newCommits.reduce((maps, c) => maps.concat(c.maps), [])
);
const { tr } = state;
for (let i = commit.steps.length - 1; i >= 0; i -= 1) {
const remapped = commit.steps[i].map(remap.slice(i + 1));
if (remapped) {
const result = tr.maybeStep(remapped);
if (result.doc) remap.appendMap(remapped.getMap(), i);
}
}
if (tr.docChanged) dispatch(tr.setMeta("track", `revert ${commit.message}`));
return { state: "SUCCESS" };
};
this.checkVersion(version)
if (this.version != version) return false
let doc = this.doc, maps = []
for (let i = 0; i < steps.length; i++) {
steps[i].clientID = clientID
let result = steps[i].apply(doc)
doc = result.doc
maps.push(steps[i].getMap())
}
this.doc = doc
this.version += steps.length
this.steps = this.steps.concat(steps)
if (this.steps.length > MAX_STEP_HISTORY)
this.steps = this.steps.slice(this.steps.length - MAX_STEP_HISTORY)
this.comments.mapThrough(new Mapping(maps))
if (comments) for (let i = 0; i < comments.length; i++) {
let event = comments[i]
if (event.type == "delete")
this.comments.deleted(event.id)
else
this.comments.created(event)
}
this.sendUpdates()
scheduleSave()
return {version: this.version, commentVersion: this.comments.version}
}