How to use the prosemirror-model.Slice.empty function in prosemirror-model

To help you get started, we’ve selected a few prosemirror-model examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pubpub / pubpub-editor / packages / pubpub-editor / src / schema / plugins / trackChangesPlugin.js View on Github external
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);
github ProseMirror / prosemirror-transform / src / replace.js View on Github external
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)
github guardian / prosemirror-noting / test / helpers / prosemirror.js View on Github external
deleteSelection() {
    return this.replaceSelection(Slice.empty);
  }
github ProseMirror / prosemirror-transform / test / test-step.js View on Github external
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))
}
github guardian / prosemirror-noting / test / helpers / prosemirror.js View on Github external
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;
  }
github ProseMirror / prosemirror-tables / src / cellselection.js View on Github external
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)
  }
github ProseMirror / prosemirror-transform / src / replace_step.js View on Github external
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
    }
  }
github ProseMirror / prosemirror-state / src / selection.js View on Github external
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)
    }
  }
github ProseMirror / prosemirror-transform / src / mark.js View on Github external
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
}