How to use the prosemirror-state.TextSelection 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 chanzuckerberg / czi-prosemirror / src / sinkListItem.js View on Github external
const {bullet_list, ordered_list, list_item} = schema.nodes;
  if (
    !bullet_list ||
    !ordered_list ||
    !list_item
  ) {
    return tr;
  }

  const {selection} = tr;
  if (!selection) {
    return tr;
  }

  const {$from, $to} = selection;
  const fromSelection = new TextSelection($from, $from);
  const fromResult =
    findParentNodeOfType(bullet_list)(fromSelection) ||
    findParentNodeOfType(ordered_list)(fromSelection);

  if (!fromResult || !fromResult.node) {
    return tr;
  }
  const toSelection = new TextSelection($to, $to);
  const toResult =
    findParentNodeOfType(bullet_list)(toSelection) ||
    findParentNodeOfType(ordered_list)(toSelection);
  if (!toResult || !toResult.node) {
    return tr;
  }
  if (fromResult.node !== toResult.node) {
    // Select across multiple lists.
github zodiac-team / zodiac-ui / libs / editor / src / plugins / block-type / insert-block.ts View on Github external
let content
    let depth
    if (nodeType === blockquote) {
        depth = 3
        content = [paragraph.create({}, (currentNode as PMNode).content)]
    } else {
        depth = 2
        content = (currentNode as PMNode).content
    }
    const newNode = nodeType.create(attrs, content)

    // Add new node.
    tr = tr
        .setSelection(new NodeSelection(tr.doc.resolve(start + 1)))
        .replaceSelectionWith(newNode)
        .setSelection(new TextSelection(tr.doc.resolve(start + depth)))
    return tr
}
github ProseMirror / prosemirror-view / src / capturekeys.js View on Github external
} else if (view.endOfTextblock(dir > 0 ? "right" : "left")) {
      let next = moveSelectionBlock(view.state, dir)
      if (next && (next instanceof NodeSelection)) return apply(view, next)
      return false
    } else {
      let $head = sel.$head, node = $head.textOffset ? null : dir < 0 ? $head.nodeBefore : $head.nodeAfter, desc
      if (!node || node.isText) return false
      let nodePos = dir < 0 ? $head.pos - node.nodeSize : $head.pos
      if (!(node.isAtom || (desc = view.docView.descAt(nodePos)) && !desc.contentDOM)) return false
      if (NodeSelection.isSelectable(node)) {
        return apply(view, new NodeSelection(dir < 0 ? view.state.doc.resolve($head.pos - node.nodeSize) : $head))
      } else if (browser.webkit) {
        // Chrome and Safari will introduce extra pointless cursor
        // positions around inline uneditable nodes, so we have to
        // take over and move the cursor past them (#937)
        return apply(view, new TextSelection(view.state.doc.resolve(dir < 0 ? nodePos : nodePos + node.nodeSize)))
      } else {
        return false
      }
    }
  } else if (sel instanceof NodeSelection && sel.node.isInline) {
    return apply(view, new TextSelection(dir > 0 ? sel.$to : sel.$from))
  } else {
    let next = moveSelectionBlock(view.state, dir)
    if (next) return apply(view, next)
    return false
  }
}
github chanzuckerberg / czi-prosemirror / xxx / liftSelection.js View on Github external
const range = nullthrows(sel.$from.blockRange(sel.$to));
      const target = liftTarget(range);
      if (target || target === 0) {
        tr = tr.lift(range, target);
      }
    }
  });

  startPos = tr.mapping.map(startPos);
  endPos = tr.mapping.map(endPos);

  // We want to select the entire node
  endPos = tr.doc.resolve(endPos).end(tr.doc.resolve(endPos).depth);

  tr = tr.setSelection(
    new TextSelection(
      tr.doc.resolve(startPos),
      tr.doc.resolve(endPos),
    ),
  );

  return tr;
}
github ProseMirror / prosemirror-commands / test / test-commands.js View on Github external
function selFor(doc) {
  let a = doc.tag.a
  if (a != null) {
    let $a = doc.resolve(a)
    if ($a.parent.inlineContent) return new TextSelection($a, doc.tag.b != null ? doc.resolve(doc.tag.b) : undefined)
    else return new NodeSelection($a)
  }
  return Selection.atStart(doc)
}
github ifiokjr / remirror / @remirror / core / src / __tests__ / test-utils.tsx View on Github external
function selectionFor(docNode: TaggedProsemirrorNode) {
  const aTag = docNode.tag.a;
  if (aTag != null) {
    const $aTag = docNode.resolve(aTag);
    if ($aTag.parent.inlineContent) {
      return new TextSelection($aTag, docNode.tag.b != null ? docNode.resolve(docNode.tag.b) : undefined);
    } else {
      return new NodeSelection($aTag);
    }
  }
  return Selection.atStart(docNode);
}
github ifiokjr / remirror / packages / jest-prosemirror / src / jest-prosemirror-nodes.ts View on Github external
export const initSelection = (
  taggedDoc: TaggedProsemirrorNode,
) => {
  const { cursor, node, start, end, anchor, all, gap } = taggedDoc.tag;
  if (all) {
    return new AllSelection(taggedDoc);
  }

  if (node) {
    return new NodeSelection(taggedDoc.resolve(node));
  }

  if (cursor) {
    return new TextSelection(taggedDoc.resolve(cursor));
  }

  if (gap) {
    const $pos = taggedDoc.resolve(gap);
    return new GapCursor($pos, $pos);
  }

  if (start) {
    return createTextSelection({ taggedDoc, start, end });
  }

  const $anchor = resolveCell(taggedDoc, anchor);
  if ($anchor) {
    return Cast>(
      new CellSelection($anchor, resolveCell(taggedDoc, taggedDoc.tag.head) ?? undefined),
    );
github ifiokjr / remirror / packages / jest-prosemirror / src / jest-prosemirror-nodes.ts View on Github external
const createTextSelection = ({
  taggedDoc,
  start,
  end,
}: CreateTextSelectionParams) => {
  const $start = taggedDoc.resolve(start);
  const $end = end && start <= end ? taggedDoc.resolve(end) : taggedDoc.resolve($start.end());
  return new TextSelection($start, $end);
};
github nib-edit / Nib / packages / core / src / plugins / common / keymaps.js View on Github external
"Mod-a": (state, dispatch) => {
    const textSelection = new TextSelection(
      Selection.atStart(state.doc).$anchor,
      Selection.atEnd(state.doc).$head
    );
    dispatch(state.tr.setSelection(textSelection));
    return true;
  }
});