0 Votes

Changes for page AutoGallery

Last modified by Ryan C on 2025/06/17 16:30

From version 3.3
edited by Ryan C
on 2025/06/17 16:11
Change comment: There is no comment for this version
To version 3.6
edited by Ryan C
on 2025/06/17 16:13
Change comment: There is no comment for this version

Summary

Details

XWiki.WikiMacroClass[0]
Macro code
... ... @@ -1,26 +1,29 @@
1 -{{groovy}}
2 -def attachments = doc.getAttachmentList()
3 -def imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp']
1 +import org.xwiki.rendering.listener.reference.ResourceReference
2 +import org.xwiki.rendering.listener.reference.ResourceType
3 +import org.xwiki.rendering.block.ImageBlock
4 +import org.xwiki.rendering.block.GroupBlock
5 +import org.xwiki.rendering.block.SpaceBlock
6 +import org.xwiki.rendering.block.DivBlock
7 +import org.xwiki.rendering.block.WordBlock
4 4  
5 -def images = attachments.findAll { att ->
6 - def ext = att.filename.toLowerCase().tokenize('.').last()
7 - imageExtensions.contains(ext)
9 +def attachments = doc.getAttachmentList().findAll {
10 + it.filename.toLowerCase() ==~ /.*\.(jpg|jpeg|png|gif)$/
8 8  }
9 9  
10 -if (images) {
11 - def html = new StringBuilder()
12 - html << "<div style='display:flex; flex-wrap:wrap; gap:10px;'>"
13 - images.each { att ->
14 - def url = "${xwiki.getURL(doc.fullName, 'download', "filename=${att.filename}")}"
15 - html << "<img src='${url}' style='max-width:200px; height:auto; border:1px solid #ccc;'>"
16 - }
17 - html << "</div>"
13 +def blocks = []
18 18  
19 - def rendered = services.rendering.parse(html.toString(), "xwiki/2.1")
20 - wikimacro.result = rendered?.children ?: []
15 +if (attachments.isEmpty()) {
16 + blocks << new GroupBlock([ new WordBlock("No images available.") ])
21 21  } else {
22 - def fallback = services.rendering.parse("<em>No images found.</em>", "xwiki/2.1")
23 - wikimacro.result = fallback?.children ?: []
18 + def imageBlocks = attachments.collect { att ->
19 + def url = "${doc.getExternalURL('download')}/${att.filename.encodeAsURL()}"
20 + def ref = new ResourceReference(url, ResourceType.URL)
21 + def imgBlock = new ImageBlock(ref, true)
22 + return new DivBlock([imgBlock], [ "style": "margin: 5px; display: inline-block;" ])
23 + }
24 +
25 + blocks << new DivBlock(imageBlocks, [ "style": "display: flex; flex-wrap: wrap; gap: 10px;" ])
24 24  }
25 -{{/groovy}}
26 26  
28 +wikimacro.result = blocks
29 +