Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a page parameter, that will dictate the attachments of which page are being displayed in the attachment picker #454 #458

Merged
merged 9 commits into from
Feb 7, 2025
Merged

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package com.xwiki.macros.viewfile.internal.macro;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.script.ScriptContext;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.xwiki.component.annotation.Component;
import org.xwiki.rendering.block.Block;
import org.xwiki.rendering.macro.MacroExecutionException;
import org.xwiki.rendering.transformation.MacroTransformationContext;
import org.xwiki.script.ScriptContextManager;
import org.xwiki.template.Template;
import org.xwiki.template.TemplateManager;

import com.xwiki.macros.AbstractProMacro;
import com.xwiki.macros.viewfile.macro.ViewFileMacroParameters;

/**
* View File macro: Display a file in different rendering ways.
*
* @version $Id$
* @since 1.27
*/
@Component
@Named("view-file")
@Singleton
public class ViewFileMacro extends AbstractProMacro<ViewFileMacroParameters>
{
@Inject
private TemplateManager templateManager;

@Inject
private ScriptContextManager scriptContextManager;

/**
* Create and initialize the descriptor of the macro.
*/
public ViewFileMacro()
{
super("View file", "Show a file using PDF Viewer Macro or Office Viewer.", ViewFileMacroParameters.class);
trrenty marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public boolean supportsInlineMode()
{
return true;
trrenty marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
protected List<Block> internalExecute(ViewFileMacroParameters parameters, String content,
MacroTransformationContext context) throws MacroExecutionException
{
Template customTemplate = this.templateManager.getTemplate("viewfile/viewFileTemplate.vm");
ScriptContext scriptContext = scriptContextManager.getScriptContext();

scriptContext.setAttribute("params", parameters, ScriptContext.ENGINE_SCOPE);
scriptContext.setAttribute("isInline", context.isInline(), ScriptContext.ENGINE_SCOPE);
try {
return this.templateManager.execute(customTemplate).getChildren();
} catch (Exception e) {
throw new MacroExecutionException(ExceptionUtils.getRootCauseMessage(e));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package com.xwiki.macros.viewfile.macro;

import org.xwiki.model.reference.DocumentReference;
import org.xwiki.properties.annotation.PropertyDescription;
import org.xwiki.properties.annotation.PropertyDisplayHidden;
import org.xwiki.properties.annotation.PropertyDisplayType;
import org.xwiki.properties.annotation.PropertyId;
import org.xwiki.properties.annotation.PropertyName;

/**
* The parameters of the {@link com.xwiki.macros.viewfile.internal.macro.ViewFileMacro}.
*
* @version $Id$
* @since 1.27
*/
public class ViewFileMacroParameters
{
private String name;

private String page;

private ViewFileDisplay display = ViewFileDisplay.thumbnail;

private String width;

private String height;

private String attFilename;

/**
* Enumeration of supported view file display values.
*
* @version $Id$
* @since 1.3
*/
private enum ViewFileDisplay
{
/**
* Thumbnail.
*/
thumbnail,

/**
* Button.
*/
button,

/**
* Full.
*/
full;

/**
* @param name the name of a display type.
* @return the display type corresponding to the name, or {@code null}.
*/
public static ViewFileDisplay forName(String name)
{
for (ViewFileDisplay type : values()) {
if (name.equalsIgnoreCase(type.toString())) {
return type;
}
}

return null;
}
}

/**
* @return the name of the attachment.
*/
public String getName()
{
return name;
}

/**
* Set the attachment name.
*
* @param name the name of the attachment.
*/
@PropertyName("File name")
public void setName(String name)
{
this.name = name;
}

/**
* @return the source document of the attachments.
*/
public String getPage()
{
return page;
}

/**
* Set the source document for the attachments.
*
* @param page the source document of the attachments.
*/
@PropertyDisplayType(DocumentReference.class)
public void setPage(String page)
{
this.page = page;
}

/**
* @return the display type.
*/
public ViewFileDisplay getDisplay()
trrenty marked this conversation as resolved.
Show resolved Hide resolved
{
return display;
}

/**
* Set the display type.
*
* @param display the display type.
*/
public void setDisplay(ViewFileDisplay display)
{
this.display = display;
}

/**
* @return the width of the display window.
*/
public String getWidth()
{
return width;
}

/**
* Set the width of the display window.
*
* @param width the width of the display window.
*/
public void setWidth(String width)
{
this.width = width;
}

/**
* @return the height of the display window.
*/
public String getHeight()
{
return height;
}

/**
* Set the height of the display window.
*
* @param height the height of the display window.
*/
public void setHeight(String height)
{
this.height = height;
}

/**
* @return the attachment filename.
*/
public String getAttFilename()
{
return attFilename;
}

/**
* Alias of File name used for compatibility reasons.
*
* @param attFilename the attachment filename.
*/
@PropertyId("att--filename")
@PropertyName("att--filename")
@PropertyDisplayHidden
@PropertyDescription("Alias of name (here for compatibility reasons)")
public void setAttFilename(String attFilename)
{
this.attFilename = attFilename;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ com.xwiki.macros.showhideif.macro.HideIfMacro
com.xwiki.macros.showhideif.macro.ShowIfMacro
com.xwiki.macros.tab.internal.TabGroupMacro
com.xwiki.macros.tab.internal.TabMacro
com.xwiki.macros.viewfile.internal.macro.ViewFileMacro
Loading