How to use the prosemirror-state.Selection.near function in prosemirror-state

To help you get started, we’ve selected a few prosemirror-state 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 guardian / prosemirror-noting / src / js / NoteTransaction.js View on Github external
addNotes(ranges, cursorToEnd = false, insideLast = false) {
    const { tr, noteTracker, markType } = this;
    const notes = ranges
      .map(({ from, to, meta, id }) => noteTracker.addNote(from, to, meta, id))
      .filter(note => note); // remove notes that couldn't be added

    this.tr = notes.reduce((_tr, { id, meta, start, end }) => {
      const newMark = markType.create({ id, meta });
      return _tr.removeMark(start, end, markType).addMark(start, end, newMark);
    }, tr);

    if (cursorToEnd && ranges.length) {
      const { to } = ranges[ranges.length - 1];
      const { end } = noteTracker.noteAt(to, -1);
      const $end = this.tr.doc.resolve(end);
      this.tr = this.tr.setSelection(Selection.near($end), 1);
    } else if (insideLast && notes.length) {
      this.currentNoteID = notes[notes.length - 1].id;
    }

    return this;
  }
github gamejolt / frontend-lib / components / content / content-editor / controls / block / controls.ts View on Github external
private insertNewNode(newNode: Node) {
		const tr = this.view.state.tr;
		tr.replaceWith(
			this.view.state.selection.from - 1,
			this.view.state.selection.to + 1,
			newNode
		);

		const resolvedCursorPos = tr.doc.resolve(this.view.state.selection.from);
		const selection = Selection.near(resolvedCursorPos);
		tr.setSelection(selection);
		ContentEditorService.ensureEndNode(tr, this.view.state.schema.nodes.paragraph);

		this.view.focus();
		this.view.dispatch(tr);

		this.setCollapsed(true);
	}
github ProseMirror / prosemirror-tables / src / input.js View on Github external
return (state, dispatch, view) => {
    let sel = state.selection
    if (sel instanceof CellSelection) {
      return maybeSetSelection(state, dispatch, Selection.near(sel.$headCell, dir))
    }
    if (axis != "horiz" && !sel.empty) return false
    let end = atEndOfCell(view, axis, dir)
    if (end == null) return false
    if (axis == "horiz") {
      return maybeSetSelection(state, dispatch, Selection.near(state.doc.resolve(sel.head + dir), dir))
    } else {
      let $cell = state.doc.resolve(end), $next = nextCell($cell, axis, dir), newSel
      if ($next) newSel = Selection.near($next, 1)
      else if (dir < 0) newSel = Selection.near(state.doc.resolve($cell.before(-1)), -1)
      else newSel = Selection.near(state.doc.resolve($cell.after(-1)), 1)
      return maybeSetSelection(state, dispatch, newSel)
    }
  }
}
github zodiac-team / zodiac-ui / libs / editor / src / plugins / code / code.nodeview.ts View on Github external
return (state, dispatch, view) => {
        if (state.selection.empty && view.endOfTextblock(dir)) {
            const side = dir === "left" || dir === "up" ? -1 : 1,
                $head = state.selection.$head
            const nextPos = Selection.near(
                state.doc.resolve(side > 0 ? $head.after() : $head.before()),
                side,
            )
            if (nextPos.$head && nextPos.$head.parent.type.name === "codeBlock") {
                dispatch(state.tr.setSelection(nextPos))
                return true
            }
        }
        return false
    }
}
github ProseMirror / prosemirror-tables / src / cellselection.js View on Github external
resolve(doc) {
    let $anchorCell = doc.resolve(this.anchor), $headCell = doc.resolve(this.head)
    if ($anchorCell.parent.type.spec.tableRole == "row" &&
        $headCell.parent.type.spec.tableRole == "row" &&
        $anchorCell.index() < $anchorCell.parent.childCount &&
        $headCell.index() < $headCell.parent.childCount &&
        inSameTable($anchorCell, $headCell))
      return new CellSelection($anchorCell, $headCell)
    else
      return Selection.near($headCell, 1)
  }
}
github ProseMirror / prosemirror-commands / src / commands.js View on Github external
export function exitCode(state, dispatch) {
  let {$head, $anchor} = state.selection
  if (!$head.parent.type.spec.code || !$head.sameParent($anchor)) return false
  let above = $head.node(-1), after = $head.indexAfter(-1), type = defaultBlockAt(above.contentMatchAt(after))
  if (!above.canReplaceWith(after, after, type)) return false
  if (dispatch) {
    let pos = $head.after(), tr = state.tr.replaceWith(pos, pos, type.createAndFill())
    tr.setSelection(Selection.near(tr.doc.resolve(pos), 1))
    dispatch(tr.scrollIntoView())
  }
  return true
}
github zodiac-team / zodiac-ui / libs / editor / src / plugins / block-type / block-type.command.ts View on Github external
return function(state: EditorState, dispatch) {
        const { tr } = state
        const { $to } = state.selection
        const { codeBlock } = state.schema.nodes

        const getNextNode = state.doc.nodeAt($to.pos + 1)
        const addPos = getNextNode && getNextNode.isText ? 0 : 1

        /** We always want to append a block type */
        tr.replaceRangeWith($to.pos + addPos, $to.pos + addPos, codeBlock.createAndFill() as Node)
        tr.setSelection(Selection.near(tr.doc.resolve(state.selection.to + addPos)))
        if (dispatch) {
            dispatch(tr)
        }
        return true
    }
}
github zodiac-team / zodiac-ui / libs / editor / src / plugins / code / code.nodeview.ts View on Github external
maybeEscape(unit, dir) {
        const pos = this.cm.getCursor()
        if (
            this.cm.somethingSelected() ||
            pos.line !== (dir < 0 ? this.cm.firstLine() : this.cm.lastLine()) ||
            (unit === "char" && pos.ch !== (dir < 0 ? 0 : this.cm.getLine(pos.line).length))
        )
            return CodeMirror.Pass
        this.view.focus()
        const targetPos = this.getPos() + (dir < 0 ? 0 : this.node.nodeSize)
        const selection = Selection.near(this.view.state.doc.resolve(targetPos), dir)
        this.view.dispatch(this.view.state.tr.setSelection(selection).scrollIntoView())
        this.view.focus()
    }
github tinacms / tinacms / packages / tinacms / fields / src / Wysiwyg / commands / codeblock-commands.ts View on Github external
export function exitCodeUp(state: EditorState, dispatch: any) {
  const { $head, $anchor } = state.selection
  if (!$head.parent.type.spec.code || !$head.sameParent($anchor)) return false
  const above: any = $head.node(-1)
  const after = $head.indexAfter(-1)
  const type = above.defaultContentType(after)
  if (!above.canReplaceWith(after, after, type)) return false
  if (dispatch) {
    const pos = $head.before()
    const tr: any = state.tr.replaceWith(pos, pos, type.createAndFill())
    tr.setSelection(Selection.near(tr.doc.resolve(pos), -1))
    dispatch(tr.scrollIntoView())
  }
  return true
}
github ProseMirror / prosemirror-tables / src / input.js View on Github external
return (state, dispatch, view) => {
    let sel = state.selection
    if (sel instanceof CellSelection) {
      return maybeSetSelection(state, dispatch, Selection.near(sel.$headCell, dir))
    }
    if (axis != "horiz" && !sel.empty) return false
    let end = atEndOfCell(view, axis, dir)
    if (end == null) return false
    if (axis == "horiz") {
      return maybeSetSelection(state, dispatch, Selection.near(state.doc.resolve(sel.head + dir), dir))
    } else {
      let $cell = state.doc.resolve(end), $next = nextCell($cell, axis, dir), newSel
      if ($next) newSel = Selection.near($next, 1)
      else if (dir < 0) newSel = Selection.near(state.doc.resolve($cell.before(-1)), -1)
      else newSel = Selection.near(state.doc.resolve($cell.after(-1)), 1)
      return maybeSetSelection(state, dispatch, newSel)
    }
  }
}