Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How can I pass selected text to a user macro parameter? Edited

I have a user macro that ideally would take as one parameter the text that the user has selected in the editor. I know that many macros added via the macro browser will take the user's selection and turn it into the body of the macro, e.g. wrapping an excerpt macro around it. Is there a way to fill one parameter, instead of the body, with the user's selected text? 

In other words, my ideal situation would be something like:

## @Param text:title=Text|type=string|required=true|desc=Body text|default=[some way of accessing the text selected by the user in the editor]

2 answers

1 accepted

1 vote
Answer accepted

I answered my own question, but ended up answering a couple of other questions along the way. It's not possible to define the user macro itself to pull the selection into a parameter. But!

What I really wanted to do was create a custom button on the editor toolbar that opened the macro browser and fed any selected text to the first parameter of my user macro. I don't have to ability to do the kind of plugin development that would "properly" add a button to the toolbar, so I had to resort to JavaScript hacks to make it all happen.

I'll leave out the JS that adds the custom button to the editor toolbar (basically: use an existing button to create the right HTML, use .after() to put it after an existing button). Here's the click event function that I attach to that custom button, with abstracted values.

AJS.$('#id-of-custom-button').click( function(){
var settings = {};
settings.selectedMacro = {
"name":"parametername",
"schemaVersion":1,
"body":"",
"defaultParameterValue":"",
"params":{
"param1":AJS.Rte.getEditor().selection.getContent(),
"param2":""}
};
settings.onComplete = tinyMCE.confluence.macrobrowser.macroBrowserComplete;
settings.onCancel = tinyMCE.confluence.macrobrowser.macroBrowserCancel;

AJS.MacroBrowser.open(settings);
function setFocus() {
if (!AJS.MacroBrowser.fields.param2) {
window.requestAnimationFrame(setFocus);
} else {
AJS.MacroBrowser.fields.param2.input.select();
}
};
setFocus();
});

Here's the breakdown:

  • Create an empty settings object
  • Add a selectedMacro object to the settings, with some minimum required items
    • including feeding the value of the currently selected text to the first parameter
  • Add the standard complete and cancel functions to the settings (which tells the macro browser what to do when you click "Save" or "Cancel", and if not specified, it causes problems)
  • Open the macro browser with the constructed settings, which fills the text parameter properly
  • Set focus on the second parameter, so the user isn't redundantly focused on the pre-filled text parameter. (But the focus setter is wrapped in a function that waits until the macro browser is rendered, because otherwise: problems.)

All of this JS goes at the very bottom of the Main Layout template. 

Again, this is just the click event handler. There's some preceding JS that adds the custom button.

So that's one way, at least, to pass selected text into a parameter, not the body, of a macro.

P.S. My user macro has two required parameters. If it had only one required parameter, it would be possible to use a different MacroBrowser function to insert it immediately on button click.

Bill Bailey Community Leader Jun 25, 2019

Well you might be surprised that that there is the ability to turn/package a user macro into a plug in (uses the same macro code, with some reworking). And within the same atlassian-plugin.xml file, you can add links to your macro in the insert macro menu.

For example, here is the Insert menu with two of my user macro (table title and figure title added)

Screenshot_2019-06-25 Edit - super test - test - MarketCom Wiki.png

This is a much cleaner solution and a good way to package user macros.

You can start here:

https://developer.atlassian.com/server/confluence/user-macro-module/

That's exactly what I was referring to when I wrote

I don't have to ability to do the kind of plugin development that would "properly" add a button to the toolbar, so I had to resort to JavaScript hacks to make it all happen.

Cleaner, yes, but outside of what I have access to. Because I don't want it in the macro list menu. I wanted an actual button in the editor toolbar, right up there next to bold, italic, etc.

Bill Bailey Community Leader Jun 25, 2019

Ah, but my point is I am stupid and was still able to do it. ;-) You just need to do the one tutorial on plugin development to get your environment set, and the community can help with questions.

Bill Bailey Community Leader Oct 04, 2019

Still don't see how selecting the text, then inserting the macro won't work. Any selected text is transferred to the body of the macro, which is saved as $body. You can then make the needed parameter equal to $body,

0 votes
Bill Bailey Community Leader Jun 25, 2019

This is a case where there is no documentation, and I had never thought about it as it becomes so automatic when I use macros. It is quite simple: all the selected text is written to the variable $body. You need no parameter at all.

(moved reply to correct location above)

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Confluence

What do you think is the most *delightful* Confluence feature? Comment for a prize!

- Create your own custom emoji 🔥 - "Shake for Feedback" on mobile 📱 - An endless supply of GIFs via GIPHY 🤩 Is there anything quite as nice as a pleasant surprise? Comment below with what...

415 views 23 8
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you