ProseMirror / prosemirror

The ProseMirror WYSIWYM editor
http://prosemirror.net/
MIT License
7.61k stars 336 forks source link

Creating range selection on document does not create new transaction with selection #1353

Closed domakas closed 1 year ago

domakas commented 1 year ago

When trying to create new range selection programmatically, this selection is not updated in prosemirror editor.

Eg.: when calling

  let el = document.querySelector(".ProseMirror");
  let range = document.createRange();
  range.selectNode(el);
  let selection = window.getSelection();

  if (selection !== null) {
    selection.removeAllRanges();
    selection.addRange(range);
  }

content inside editor is selected, however new transaction is not triggered and selection is not updated inside prosemirror state.

Demo for reproduction: https://codesandbox.io/s/green-bird-qr5s5b?file=/src/index.js In this example you can see that when clicking "Select All" button, new transaction is not triggered, however when, for example you click Cmd+A selection is changed and transaction is created.

Potential use cases, which are blocked by this issue: selecting content with puppeteer when running automated tests.

marijnh commented 1 year ago

The editor ignores selections that aren't inside of its editable element. By selecting the editor element, you're creating a range that starts before and ends after the editor, which is thus ignored. This is intentional behavior.