# Need help to \*\*getSelected\*\* and \*\*replace\*\* from Toolbarevent

**URL:** https://meta.discourse.org/t/need-help-to-getselected-and-replace-from-toolbarevent/226905
**Category:** Development
**Created:** [May 13, 2022, 12:01am UTC](https://meta.discourse.org/t/need-help-to-getselected-and-replace-from-toolbarevent/226905 "2022-05-13T00:01:20Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![bksubhuti](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bksubhuti/32/254360_2.png) [@bksubhuti](https://meta.discourse.org/u/bksubhuti)
#### Post date: [May 13, 2022, 12:01am UTC](https://meta.discourse.org/t/need-help-to-getselected-and-replace-from-toolbarevent/226905/1 "2022-05-13T00:01:20Z")

</div>

i’m trying to write a simple button component to call a function called toUni to process either all text or selected text (selected is preferred). I can do a simple button from the tutorial and process the entire composer text and add my processed text to the composer area. (double repeated text is the result).

**How do I replace all of text from the composer window (once I have done some processing**  
**How do I get the selected text from the composer window**  
**How do I replace the selected text from the composer window (once I have done processing).**

I see some [code](https://github.com/discourse/discourse/blob/86a783b3ad0f11674c0786daf5484d64f624a0af/app/assets/javascripts/discourse/app/mixins/textarea-text-manipulation.js) that has

```plaintext
    getSelected();
     replaceText();

```

But when I combine it with e.getSelected(); or e.replaceText(old, new);, it does not work and I assume that there is some function mismatch or a crash because it does not show my alert box to give me feedback.

I can use getText(); and addText() successfully from your git ref code [here](https://github.com/discourse/discourse/blob/0d30c19b7e1034c39e9eba01d8ac11570815e15f/app/assets/javascripts/discourse/app/components/d-editor.js). But I cannot figure out how to delete the contents after I process (so i can addText() and then replace everything.

But I prefer to find the selected text and replace that.

Currently my code is below which adds a second copy of the entire composer text directly below but with some modifications. This is confusing to the user.  
I need to either replace the entire text totally … or only process the “selected” words. (or both)

I made a live version [here](https://classicaltheravada.org/t/enter-feedback-here/94/14?u=bksubhuti)

```plaintext
 

<script type="text/discourse-plugin" version="0.8">

  api.onToolbarCreate(function(toolbar) {
      
    toolbar.addButton({

      trimLeading: true,
      id: "buttonID",
      group: "insertions",
      label: "Pāḷi", // why does this show up as en.pali ?
      title: "add pali",  
      perform: function(e) {
      const sel = e.getText();
      //alert(sel);
      const vel = toUni(sel);
        return e.addText("\n--------------------new text--------------------\n" + vel + "\n--------------------end text--------------------");
      }
    });
  });
      //e.replaceText(sel,"hello");
      //e.replaceSeelc
  
      //const sel = e.selected();
     
      //let txt = "hello"; 
      //const sel = e.getSelected(0,100 );
  
function toUni(input) {
	if(!input || input == '') return input;
	//var nigahita = (DPR_prefs['nigahita']?'ṁ':'ṃ');
	//var Nigahita = (DPR_prefs['nigahita']?'Ṁ':'Ṃ');

	var nigahita = 'ṃ';
	var Nigahita = 'Ṃ';

	input = input.replace(/aa/g, 'ā').replace(/ii/g, 'ī').replace(/uu/g, 'ū').replace(/\.t/g, 'ṭ').replace(/\.d/g, 'ḍ').replace(/\"nk/g, 'ṅk').replace(/\"ng/g, 'ṅg').replace(/\.n/g, 'ṇ').replace(/\.m/g, nigahita).replace(/\u1E41/g, nigahita).replace(/\~n/g, 'ñ').replace(/\.l/g, 'ḷ').replace(/AA/g, 'Ā').replace(/II/g, 'Ī').replace(/UU/g, 'Ū').replace(/\.T/g, 'Ṭ').replace(/\.D/g, 'Ḍ').replace(/\"N/g, 'Ṅ').replace(/\.N/g, 'Ṇ').replace(/\.M/g, Nigahita).replace(/\~N/g, 'Ñ').replace(/\.L/g, 'Ḷ').replace(/\.ll/g,'ḹ').replace(/\.r/g,'ṛ').replace(/\.rr/g,'ṝ').replace(/\.s/g,'ṣ').replace(/"s/g,'ś').replace(/\.h/g,'ḥ');

	return input;
}
  
</script>
  

```

---

<div class="post-metadata">

### Author: ![bksubhuti](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bksubhuti/32/254360_2.png) [@bksubhuti](https://meta.discourse.org/u/bksubhuti)
#### Post date: [May 14, 2022, 12:04am UTC](https://meta.discourse.org/t/need-help-to-getselected-and-replace-from-toolbarevent/226905/2 "2022-05-14T00:04:32Z")

</div>

Is there any api reference?

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [May 14, 2022, 10:56am UTC](https://meta.discourse.org/t/need-help-to-getselected-and-replace-from-toolbarevent/226905/3 "2022-05-14T10:56:38Z")

</div>

My API skills are barely emergent, so I apologise if I’ve misunderstood, but are you looking for [How to reverse engineer the Discourse API](https://meta.discourse.org/t/how-to-reverse-engineer-the-discourse-api/20576)?

---

<div class="post-metadata">

### Author: ![bksubhuti](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bksubhuti/32/254360_2.png) [@bksubhuti](https://meta.discourse.org/u/bksubhuti)
#### Post date: [May 14, 2022, 11:32am UTC](https://meta.discourse.org/t/need-help-to-getselected-and-replace-from-toolbarevent/226905/4 "2022-05-14T11:32:57Z")

</div>

I am not sure this is what I want. I just want to call a few javascript calls. I think I see them but the toolevent only gives access to a small set of calls.

I can gettext and addtext, but not much else.  
I’m looking for functions that mimic this functionality:  
`getselectedtext`  
`replaceselectedtext`

I think this should be available.

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [May 14, 2022, 11:49am UTC](https://meta.discourse.org/t/need-help-to-getselected-and-replace-from-toolbarevent/226905/5 "2022-05-14T11:49:49Z")

</div>

I did think it was somewhat of a long-shot. 🙂 I saw ‘api reference’ and thought I’d chance it.

I think my ridiculously wide-of-the-mark answer may have already revealed my inexperience in this area, but (at the risk of doing exactly the same thing again 🙂) I think there are some good general pointers in the [Developing Discourse Themes & Theme Components](https://meta.discourse.org/t/developer-s-guide-to-discourse-themes/93648) if you’ve not seen that already?

> [@Developing Discourse Themes & Theme Components](https://meta.discourse.org/t/developing-discourse-themes-theme-components/93648/1):
>
> Modifying Discourse templates

But hopefully someone more knowledgable will turn up and be able to offer something actually helpful. 🤞 🙂

---

<div class="post-metadata">

### Author: ![bksubhuti](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bksubhuti/32/254360_2.png) [@bksubhuti](https://meta.discourse.org/u/bksubhuti)
#### Post date: [May 15, 2022, 12:19am UTC](https://meta.discourse.org/t/need-help-to-getselected-and-replace-from-toolbarevent/226905/6 "2022-05-15T00:19:27Z")

</div>

Well.. I have done a bit of that tutorial. That is how I got to the button part. Hopefully someone will respond on how to` getselected and replaceselected from a toolbarevent class.`
