How to write Macro code for the edit mode?
Last modified by Josué Tille on 2025/01/06
If you're writing a Macro, and you need to do something specific when the macro is rendered in edit mode, write the following:
- When writing a wiki macro using Velocity#set ($targetSyntaxId = $wikimacro.context.transformationContext.targetSyntax.type.id)
#if ($targetSyntaxId == 'annotatedhtml' || $targetSyntaxId == 'annotatedxhtml')
... do something specific here ...
#end - When writing a Java macropublic class MyMacro extends AbstractMacro<MyMacroParameters>
{
...
public List<Block> execute(MyMacroParameters parameters, String content, MacroTransformationContext context)
{
...
SyntaxType targetSyntaxType = context.getTransformationContext().getTargetSyntax().getType();
if (SyntaxType.ANNOTATED_HTML.equals(targetSyntaxType) || SyntaxType.ANNOTATED_XHTML.equals(targetSyntaxType)) {
... do something specific here ...
}
...
}
}
Note that we check for both annotatedhtml and annotatedxhtml because the WYSIWYG editor generates Annotated HTML content internally, and it used to generate Annotated XHTML in the past (before version 1.60 of the CKEditor Integration Extension).
Other important note, this provided solution only works on the XWiki version which has this issue fixed: https://jira.xwiki.org/browse/XWIKI-22738. So at the time I'm writing this it's >=17.0.0.