Template tag: selection, with fallback to entire draft

Hi, when I’m creating custom actions I often seem to come back to this dilemma and I never managed to figure out if it is possible. What I want to achieve is for my actions to act on [[selection]], but if nothing is selected, then use the entire [[draft]].

For example one of the actions I’d like to use this on is “Call phone number”. Often a phone number is all I have in a draft, so nothing more than [[draft]] is needed. However, sometimes there is more stuff in the draft and I want to select the text and act on that instead. I would like to avoid having two different actions.

Is this something that is already possible?

You could define your own tag in JavaScript to use. This illustrates how.

if (editor.getSelectedText() == "") draft.setTemplateTag("grab", draft.content);
else draft.setTemplateTag("grab", editor.getSelectedText());

alert(draft.processTemplate("[[grab]]"));
1 Like

What you describe is already the behavior of the [[selection]] tag. From the template docs:

  • [[selection]] If text was selected within the draft before selecting an action, this tag will return only that selected text. If no text was selected, it will return the full text of the draft.

There is a separate [[selection_only]] tag that does not revert to the full draft if there is no selection.

2 Likes

Dang! I thought I already tried this. But apparently I messed something up. Really glad it works this way. Thanks!

That’s obviously a clear sign that I need to re-read the docs as I don’t remember that feature of the tag at all :roll_eyes:

Great tip though!

2 Likes