2

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

这是我在访问页面时能够显示弹出窗口的代码,但是当我选择“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();
    }
}
4

1 回答 1

2

我认为你需要hasBody='true', bodyType='raw'or bodyType='rendered', and outputType='html'or outputType='wiki'

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

于 2013-03-18T13:53:29.427 回答