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

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 that has


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


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

  api.onToolbarCreate(function(toolbar) {

      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();
      const vel = toUni(sel);
        return   e.addText("\n--------------------new text--------------------\n" + vel + "\n--------------------end text--------------------");
      //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;
1 Like

Is there any api reference?

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?

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:

I think this should be available.

I did think it was somewhat of a long-shot. :slightly_smiling_face: 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 :slight_smile:) I think there are some good general pointers in the Developer’s guide to Discourse Themes if you’ve not seen that already?

But hopefully someone more knowledgable will turn up and be able to offer something actually helpful. :crossed_fingers: :slight_smile:

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.