Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
handleKeyDown: function (view, event) {
if (event.code === 'Backspace') {
const sel = view.state.selection;
const pos = sel.$from;
let tr = view.state.tr;
// what if they delete a region??
const beforeSel = Selection.findFrom(view.state.doc.resolve(sel.from - 1), -1, true);
const marks = beforeSel.$from.marks();
const hasDiff = marks.find((mark) => {
return (mark.type.name === 'diff_plus');
});
const deleteStep = replaceStep(tr.doc, beforeSel.from, sel.from, Slice.empty);
if (hasDiff) {
// need to actually delete it but then avoid random deletions
this.storeStep(deleteStep);
tr.step(deleteStep);
tr.setMeta('backdelete', true);
view.dispatch(tr);
return true;
}
// is this step size always 1??
const newOffset = { index: beforeSel.from, size: 1 };
this.storeStep(deleteStep);
this.stepOffsets.push(newOffset);
this.storeOffset(newOffset);
tr = tr.addMark(beforeSel.from, sel.from, schema.mark('diff_minus', { commitID: this.tracker.uuid }));
tr = tr.setSelection(beforeSel);
export function replaceStep(doc, from, to = from, slice = Slice.empty) {
if (from == to && !slice.size) return null
let $from = doc.resolve(from), $to = doc.resolve(to)
// Optimization -- avoid work if it's obvious that it's not needed.
if (fitsTrivially($from, $to, slice)) return new ReplaceStep(from, to, slice)
let placed = placeSlice($from, slice)
let fittedLeft = fitLeft($from, placed)
let fitted = fitRight($from, $to, fittedLeft)
if (!fitted) return null
if (fittedLeft.size != fitted.size && canMoveText($from, $to, fittedLeft)) {
let d = $to.depth, after = $to.after(d)
while (d > 1 && after == $to.end(--d)) ++after
let fittedAfter = fitRight($from, doc.resolve(after), fittedLeft)
if (fittedAfter)
return new ReplaceAroundStep(from, after, to, $to.end(), fittedAfter, fittedLeft.size)
deleteSelection() {
return this.replaceSelection(Slice.empty);
}
function mkStep(from, to, val) {
if (val == "+em")
return new AddMarkStep(from, to, schema.marks.em.create())
else if (val == "-em")
return new RemoveMarkStep(from, to, schema.marks.em.create())
else
return new ReplaceStep(from, to, val == null ? Slice.empty : new Slice(Fragment.from(schema.text(val)), 0, 0))
}
backspace(n = 1) {
for (let i = 0; i < n; i += 1) {
const { $cursor } = this.selection;
const { from, to } = $cursor
? new Selection(this.leftPos.$from, this.selection.$from)
: this.selection;
this.apply(this.tr.replace(from, to, Slice.empty));
}
return this;
}
replace(tr, content = Slice.empty) {
let mapFrom = tr.steps.length, ranges = this.ranges
for (let i = 0; i < ranges.length; i++) {
let {$from, $to} = ranges[i], mapping = tr.mapping.slice(mapFrom)
tr.replace(mapping.map($from.pos), mapping.map($to.pos), i ? Slice.empty : content)
}
let sel = Selection.findFrom(tr.doc.resolve(tr.mapping.slice(mapFrom).map(this.to)), -1)
if (sel) tr.setSelection(sel)
}
merge(other) {
if (!(other instanceof ReplaceStep) || other.structure != this.structure) return null
if (this.from + this.slice.size == other.from && !this.slice.openEnd && !other.slice.openStart) {
let slice = this.slice.size + other.slice.size == 0 ? Slice.empty
: new Slice(this.slice.content.append(other.slice.content), this.slice.openStart, other.slice.openEnd)
return new ReplaceStep(this.from, this.to + (other.to - other.from), slice, this.structure)
} else if (other.to == this.from && !this.slice.openStart && !other.slice.openEnd) {
let slice = this.slice.size + other.slice.size == 0 ? Slice.empty
: new Slice(other.slice.content.append(this.slice.content), other.slice.openStart, this.slice.openEnd)
return new ReplaceStep(other.from, this.to, slice, this.structure)
} else {
return null
}
}
replace(tr, content = Slice.empty) {
super.replace(tr, content)
if (content == Slice.empty) {
let marks = this.$from.marksAcross(this.$to)
if (marks) tr.ensureMarks(marks)
}
}
Transform.prototype.clearIncompatible = function(pos, parentType, match = parentType.contentMatch) {
let node = this.doc.nodeAt(pos)
let delSteps = [], cur = pos + 1
for (let i = 0; i < node.childCount; i++) {
let child = node.child(i), end = cur + child.nodeSize
let allowed = match.matchType(child.type, child.attrs)
if (!allowed) {
delSteps.push(new ReplaceStep(cur, end, Slice.empty))
} else {
match = allowed
for (let j = 0; j < child.marks.length; j++) if (!parentType.allowsMarkType(child.marks[j].type))
this.step(new RemoveMarkStep(cur, end, child.marks[j]))
}
cur = end
}
if (!match.validEnd) {
let fill = match.fillBefore(Fragment.empty, true)
this.replace(cur, cur, new Slice(fill, 0, 0))
}
for (let i = delSteps.length - 1; i >= 0; i--) this.step(delSteps[i])
return this
}