# Getting pasted value from ChangeSet

**URL:** https://discuss.codemirror.net/t/getting-pasted-value-from-changeset/5234
**Category:** v6
**Created:** [November 2, 2022, 10:36pm UTC](https://discuss.codemirror.net/t/getting-pasted-value-from-changeset/5234 "2022-11-02T22:36:26Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![camdecoster](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/camdecoster/32/2648_2.png) [@camdecoster](https://discuss.codemirror.net/u/camdecoster)
#### Post date: [November 2, 2022, 10:36pm UTC](https://discuss.codemirror.net/t/getting-pasted-value-from-changeset/5234/1 "2022-11-02T22:36:26Z")

</div>

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:

```nohighlight
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;
}

```

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)
#### Post date: [November 2, 2022, 10:56pm UTC](https://discuss.codemirror.net/t/getting-pasted-value-from-changeset/5234/2 "2022-11-02T22:56:30Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![camdecoster](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/camdecoster/32/2648_2.png) [@camdecoster](https://discuss.codemirror.net/u/camdecoster)
#### Post date: [November 2, 2022, 11:38pm UTC](https://discuss.codemirror.net/t/getting-pasted-value-from-changeset/5234/3 "2022-11-02T23:38:54Z")

</div>

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

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)
#### Post date: [November 3, 2022, 12:17am UTC](https://discuss.codemirror.net/t/getting-pasted-value-from-changeset/5234/4 "2022-11-03T00:17:09Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![camdecoster](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/camdecoster/32/2648_2.png) [@camdecoster](https://discuss.codemirror.net/u/camdecoster)
#### Post date: [November 3, 2022, 12:22am UTC](https://discuss.codemirror.net/t/getting-pasted-value-from-changeset/5234/5 "2022-11-03T00:22:11Z")

</div>

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