How to write Macro code for the edit mode?
Version 4.8 by Vincent Massol on 2024/10/29
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.getId()) #if ($targetSyntaxId == 'annotatedhtml' || $targetSyntaxId == 'annotatedxhtml') ... do something specific here ... #end
- When writing a Java macro
public 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).