代码之家  ›  专栏  ›  技术社区  ›  Mohsin Javed Cheema

Atlassian Confluence:宏类型插件,访问页面时会显示弹出窗口

  •  2
  • Mohsin Javed Cheema  · 技术社区  · 12 年前

    非常基本的想法是,当有人访问页面时,我想显示一个弹出窗口。我已经写了一个示例代码。 实际上,我想做的是创建一个Macro类型的插件,它有一个主体,这样我们就可以在其中添加文本、链接和不同的元素,它会在弹出窗口中显示它们。

    这是我能够在访问页面时显示弹出窗口的代码,但当我选择“PLAIN_TEXT”作为“getBodyType()”时。它显示未格式化的文本,当我选择“RICH_text”时,它什么也不显示。请帮忙!

    package com.elixir;
    
    import com.atlassian.confluence.content.render.xhtml.ConversionContext;
    import com.atlassian.confluence.content.render.xhtml.XhtmlException;
    import com.atlassian.confluence.macro.Macro;
    import com.atlassian.confluence.macro.MacroExecutionException;
    import com.atlassian.confluence.xhtml.api.MacroDefinition;
    import com.atlassian.confluence.xhtml.api.MacroDefinitionHandler;
    import com.atlassian.confluence.xhtml.api.XhtmlContent;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    public class MyMacro implements Macro
    {
        private final XhtmlContent xhtmlUtils;
    
        public MyMacro(XhtmlContent xhtmlUtils)
        {
            this.xhtmlUtils = xhtmlUtils;
        }
    
        @Override
        public BodyType getBodyType()
        {
            return BodyType.RICH_TEXT;
        }
    
        @Override
        public OutputType getOutputType()
        {
            return OutputType.BLOCK;
        }
    
        @Override
        public String execute(Map<String, String> parameters, String bodyContent, ConversionContext conversionContext) throws MacroExecutionException
        {
            String body = conversionContext.getEntity().getBodyAsString();
    
            final List<MacroDefinition> macros = new ArrayList<MacroDefinition>();
    
            try
            {
                xhtmlUtils.handleMacroDefinitions(body, conversionContext, new MacroDefinitionHandler()
                {
                    @Override
                    public void handle(MacroDefinition macroDefinition)
                    {
                        macros.add(macroDefinition);
                    }
                });
            }
            catch (XhtmlException e)
            {
                throw new MacroExecutionException(e);
            }
    
            StringBuilder builder = new StringBuilder();
            if (!macros.isEmpty())
            {
                for (MacroDefinition defn : macros) {
                    builder.append("<script>AJS.$(document).ready(function() {var popup2 = AJS.popup({width:400, height:200, id:'my-popup2', closeOnOutsideClick: true});");
                    builder.append("$('#my-popup2').css({padding:4});$('#my-popup2').html(\"").append(defn.getBody()).append("\");");
                    builder.append("popup2.show();});</script>");
    
                }
            }
            else
            {
                builder.append("Body not defined");
            }
    
            return builder.toString();
        }
    }
    
    1 回复  |  直到 12 年前
        1
  •  2
  •   MNRSullivan    12 年前

    我想你需要 hasBody='true' , bodyType='raw' bodyType='rendered' outputType='html' outputType='wiki' .

    以下是如何实现此解决方案的示例: https://answers.atlassian.com/questions/93630/user-macro-module-body-parameter