Getting pasted value from ChangeSet

I’m trying to add a transactionFilter that will create a markdown link with the selected text during a paste event. What would be the proper way to get the selected text and the pasted text? I think that I’ve got the selected text figured out, but I’m not sure about the pasted text. This is what I’ve got so far:

function pasteTransactionFilter(t: Transaction): TransactionSpec {
  if (t.isUserEvent("input.paste")) {
    const {startState} = t;
    const {from, to} = startState.selection.ranges[0];
    const selectedText = startState.sliceDoc(from, to);

    return startState.replaceSelection(
      `[${selectedText}](${t.changes.inserted.at(-1)})`
    );
  }

  return t;
}

The raw pasted text is not available in the transaction. You could iterate the changes to figure out which range of the document was pasted, and get the content from that. Or you could store the clipboard content from the paste event somewhere.

Would you use t.changes.iterChanges and check the inserted value for each change?

That works. Though you might have to take multi-selection and line-wise paste into account (paste will happen for all selection ranges, but with only one line per selection if the amount of pasted lines matches the amount of selected ranges).

I’ll try that and follow up if I have trouble. Thank you!