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