How to use the prosemirror-state.TextSelection.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 ifiokjr / remirror / @remirror / extension-enhanced-link / src / enhanced-link-utils.ts View on Github external
export const enhancedLinkHandler = ({ state, url, from, to, tr, type }: EnhancedLinkHandlerProps) => {
  const endPosition = state.selection.to;
  const enhancedLink = type.create({ href: extractHref(url) });

  tr = (tr ?? state.tr).replaceWith(from, to, state.schema.text(url, [enhancedLink]));

  // Ensure that the selection doesn't jump when the the current selection is within the range
  if (endPosition < to) {
    return tr.setSelection(TextSelection.create(tr.doc, endPosition));
  }

  return tr;
};
github ProseMirror / prosemirror-history / test / test-history.js View on Github external
it("can go back and forth through history when preserving items", () => {
    let state = mkState()
    state = type(state, "one")
    state = type(state, " two")
    state = state.apply(closeHistory(state.tr))
    state = state.apply(state.tr.insertText("xxx", state.selection.head).setMeta("addToHistory", false))
    state = type(state, " three")
    state = state.apply(state.tr.insertText("zero ", 1))
    state = state.apply(closeHistory(state.tr))
    state = state.apply(state.tr.split(1))
    state = state.apply(state.tr.setSelection(TextSelection.create(state.doc, 1)))
    state = type(state, "top")
    state = state.apply(state.tr.insertText("yyy", 1).setMeta("addToHistory", false))
    for (let i = 0; i < 3; i++) {
      if (i == 2) compress(state)
      for (let j = 0; j < 4; j++) state = command(state, undo)
      ist(state.doc, doc(p("yyyxxx")), eq)
      for (let j = 0; j < 4; j++) state = command(state, redo)
      ist(state.doc, doc(p("yyytop"), p("zero one twoxxx three")), eq)
    }
  })
github ProseMirror / prosemirror-view / src / input.js View on Github external
function defaultTripleClick(view, inside) {
  let doc = view.state.doc
  if (inside == -1) {
    if (doc.inlineContent) {
      updateSelection(view, TextSelection.create(doc, 0, doc.content.size), "pointer")
      return true
    }
    return false
  }

  let $pos = doc.resolve(inside)
  for (let i = $pos.depth + 1; i > 0; i--) {
    let node = i > $pos.depth ? $pos.nodeAfter : $pos.node(i)
    let nodePos = $pos.before(i)
    if (node.inlineContent)
      updateSelection(view, TextSelection.create(doc, nodePos + 1, nodePos + 1 + node.content.size), "pointer")
    else if (NodeSelection.isSelectable(node))
      updateSelection(view, NodeSelection.create(doc, nodePos), "pointer")
    else
      continue
    return true
  }
}
github ifiokjr / remirror / packages / jest-remirror / src / transactions.ts View on Github external
export const dispatchTextSelection = ({ view, start, end }: DispatchTextSelectionParams) => {
  const { state } = view;
  const tr = state.tr.setSelection(TextSelection.create(state.doc, start, end));

  view.dispatch(tr);
};
github tinacms / tinacms / packages / tinacms / fields / src / Wysiwyg / node-views / CodeBlockView / CodeBlockView.tsx View on Github external
asProseMirrorSelection(doc: Node): Selection {
    const offset = this.getPos() + 1
    const anchor = this.cm.indexFromPos(this.cm.getCursor('anchor')) + offset
    const head = this.cm.indexFromPos(this.cm.getCursor('head')) + offset
    return TextSelection.create(doc, anchor, head)
  }
github chanzuckerberg / czi-prosemirror / src / selectBodyContent.js View on Github external
return tr;
  }
  const body = schema.nodes['body'];
  if (!body) {
    return tr;
  }
  const {doc} = tr;
  if (!doc || doc.nodeSize < 2) {
    return tr;
  }
  const node = doc.nodeAt(0);
  if (!node || node.type !== body || node.nodeSize <= 4) {
    return tr;
  }

  tr = tr.setSelection(TextSelection.create(
    doc,
    2,
    node.nodeSize - 2,
  ));

  return tr;
}
github ifiokjr / remirror / @remirror / core-utils / src / dom-utils.ts View on Github external
const getChar = (start: number, end: number) =>
    getTextContentFromSlice(TextSelection.create(state.doc, start, end).content());
github cosmocode / dokuwiki-plugin-prosemirror / script / plugins / InputRules / inputrules.js View on Github external
return new InputRule(SmileyConf.getRegex(), ((state, match) => {
        const { tr } = state;
        const syntax = match[1];
        const icon = SmileyConf.getSmileys()[syntax];

        tr.setSelection(TextSelection.create(tr.doc, tr.selection.from, tr.selection.from - syntax.length + 1));
        return tr.replaceSelectionWith(state.schema.nodes.smiley.create({ icon, syntax }));
    }));
}
github vm-mishchenko / ngx-wall / src / app / sarapose / sarapose.component.ts View on Github external
setFocus() {
    let tr = this.view.state.tr;

    const textSelection = TextSelection.create(tr.doc, 10, 16);

    tr = tr.setSelection(textSelection).scrollIntoView();

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