How to use the prosemirror-state.NodeSelection.create 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 pubpub / pubpub-editor / src / plugins / onChange.js View on Github external
return (newAttrs) => {
		const start = editorView.state.selection.from;
		if (start !== undefined) {
			const oldNodeAttrs = editorView.state.selection.node.attrs;
			const transaction = editorView.state.tr.setNodeMarkup(start, null, {
				...oldNodeAttrs,
				...newAttrs,
			});
			if (editorView.state.selection.node.type.isInline) {
				/* Inline nodeviews lose focus on content change */
				/* this fixes that issue. */
				const sel = NodeSelection.create(transaction.doc, start);
				transaction.setSelection(sel);
			}
			editorView.dispatch(transaction);
		}
	};
};
github ProseMirror / prosemirror-commands / src / commands.js View on Github external
// If there is no node after this, there's nothing to do
  if (!$cut) return false

  let after = $cut.nodeAfter
  // Try the joining algorithm
  if (deleteBarrier(state, $cut, dispatch)) return true

  // If the node above has no content and the node below is
  // selectable, delete the node above and select the one below.
  if ($cursor.parent.content.size == 0 &&
      (textblockAt(after, "start") || NodeSelection.isSelectable(after))) {
    if (dispatch) {
      let tr = state.tr.deleteRange($cursor.before(), $cursor.after())
      tr.setSelection(textblockAt(after, "start") ? Selection.findFrom(tr.doc.resolve(tr.mapping.map($cut.pos)), 1)
                      : NodeSelection.create(tr.doc, tr.mapping.map($cut.pos)))
      dispatch(tr.scrollIntoView())
    }
    return true
  }

  // If the next node is an atom, delete it
  if (after.isAtom && $cut.depth == $cursor.depth - 1) {
    if (dispatch) dispatch(state.tr.delete($cut.pos, $cut.pos + after.nodeSize).scrollIntoView())
    return true
  }

  return false
}
github pubpub / pubpub-editor / src / schema / reactView.js View on Github external
updateAttrs(nodeAttrs) {
		const start = this.getPos();
		if (start !== undefined) {
			const oldNodeAttrs = this.node.attrs;
			const transaction = this.view.state.tr.setNodeMarkup(
				start,
				null,
				{ ...oldNodeAttrs, ...nodeAttrs }
			);
			if (this.node.type.isInline) {
				/* Inline nodeviews lose focus on content change */
				/* this fixes that issue. */
				const pos = this.getPos();
				const sel = NodeSelection.create(this.view.state.doc, pos);
				transaction.setSelection(sel);
			}
			this.view.dispatch(transaction);
		}
	}
github pubpub / pubpub-editor / packages / pubpub-editor / src / schema / editable / latexView.js View on Github external
forceSelection() {
    const pos = this.getPos();
    const sel = NodeSelection.create(this.view.state.doc, pos);
    const transaction = this.view.state.tr.setSelection(sel);
    // this.reactElement.focusAndSelect();
    this.view.dispatch(transaction);
  }
github ProseMirror / prosemirror-view / src / input.js View on Github external
handlers.dragstart = (view, e) => {
  let mouseDown = view.mouseDown
  if (mouseDown) mouseDown.done()
  if (!e.dataTransfer) return

  let sel = view.state.selection
  let pos = sel.empty ? null : view.posAtCoords(eventCoords(e))
  if (pos && pos.pos >= sel.from && pos.pos <= (sel instanceof NodeSelection ? sel.to - 1: sel.to)) {
    // In selection
  } else if (mouseDown && mouseDown.mightDrag) {
    view.dispatch(view.state.tr.setSelection(NodeSelection.create(view.state.doc, mouseDown.mightDrag.pos)))
  } else if (e.target && e.target.nodeType == 1) {
    let desc = view.docView.nearestDesc(e.target, true)
    if (!desc || !desc.node.type.spec.draggable || desc == view.docView) return
    view.dispatch(view.state.tr.setSelection(NodeSelection.create(view.state.doc, desc.posBefore)))
  }
  let slice = view.state.selection.content(), {dom, text} = serializeForClipboard(view, slice)
  e.dataTransfer.clearData()
  e.dataTransfer.setData(brokenClipboardAPI ? "Text" : "text/html", dom.innerHTML)
  if (!brokenClipboardAPI) e.dataTransfer.setData("text/plain", text)
  view.dragging = new Dragging(slice, !e[dragCopyModifier])
}
github pubpub / pubpub-editor / packages / pubpub-editor / src / schema / editable / htmlView.js View on Github external
forceSelection() {
    const pos = this.getPos();
    const sel = NodeSelection.create(this.view.state.doc, pos);
    const transaction = this.view.state.tr.setSelection(sel);
    // this.reactElement.focusAndSelect();
    this.view.dispatch(transaction);
  }
github ProseMirror / prosemirror-view / test / test-selection.js View on Github external
it("allows moving directly from an inline node to a block node", () => {
    let view = tempEditor({doc: doc(p("foo", img), hr, p(img, "bar"))})
    setSel(view, NodeSelection.create(view.state.doc, 4))
    view.dispatchEvent(event(DOWN))
    ist(view.state.selection.from, 6)
    setSel(view, NodeSelection.create(view.state.doc, 8))
    view.dispatchEvent(event(UP))
    ist(view.state.selection.from, 6)
  })
github ProseMirror / prosemirror-tables / test / test-cellselection.js View on Github external
it("converts a table node selection into a selection of all cells in the table", () => {
    ist(normalize(NodeSelection.create(t, 0)), CellSelection.create(t, 2, 46), eq)
  })
github ProseMirror / prosemirror-commands / src / commands.js View on Github external
export function selectNodeBackward(state, dispatch, view) {
  let {$cursor} = state.selection
  if (!$cursor || (view ? !view.endOfTextblock("backward", state)
                        : $cursor.parentOffset > 0))
    return false

  let $cut = findCutBefore($cursor), node = $cut && $cut.nodeBefore
  if (!node || !NodeSelection.isSelectable(node)) return false
  if (dispatch)
    dispatch(state.tr.setSelection(NodeSelection.create(state.doc, $cut.pos - node.nodeSize)).scrollIntoView())
  return true
}
github ProseMirror / prosemirror-commands / src / commands.js View on Github external
export function selectNodeForward(state, dispatch, view) {
  let {$cursor} = state.selection
  if (!$cursor || (view ? !view.endOfTextblock("forward", state)
                        : $cursor.parentOffset < $cursor.parent.content.size))
    return false

  let $cut = findCutAfter($cursor), node = $cut && $cut.nodeAfter
  if (!node || !NodeSelection.isSelectable(node)) return false
  if (dispatch)
    dispatch(state.tr.setSelection(NodeSelection.create(state.doc, $cut.pos)).scrollIntoView())
  return true
}