Skip to content

Commit 17c5958

Browse files
authored
Merge pull request #5726 from WoltLab/image-center
Fix centering for images and attachments
2 parents 09400e1 + 9ef0c2a commit 17c5958

File tree

5 files changed

+42
-19
lines changed

5 files changed

+42
-19
lines changed

com.woltlab.wcf/bbcode.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
<usetext>1</usetext>
172172
</attribute>
173173
<attribute name="1">
174-
<validationpattern><![CDATA[^(left|right|none)$]]></validationpattern>
174+
<validationpattern><![CDATA[^(left|right|center|none)$]]></validationpattern>
175175
</attribute>
176176
<attribute name="2">
177177
<validationpattern><![CDATA[^(\d+|true|false)$]]></validationpattern>
@@ -190,7 +190,7 @@
190190
<validationpattern><![CDATA[^(small|medium|large|original)$]]></validationpattern>
191191
</attribute>
192192
<attribute name="2">
193-
<validationpattern><![CDATA[^(left|right|none)$]]></validationpattern>
193+
<validationpattern><![CDATA[^(left|right|center|none)$]]></validationpattern>
194194
</attribute>
195195
<attribute name="3">
196196
<validationpattern><![CDATA[^\d+$]]></validationpattern>

com.woltlab.wcf/templates/mediaBBCodeTag.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{if !$removeLinks|isset}{assign var='removeLinks' value=false}{/if}
2+
{if $float === 'center'}<p class="text-center">{/if}
23
<span class="mediaBBCode{if $float != 'none'} messageFloatObject{$float|ucfirst}{/if}"{if $width !== 'auto'} style="width: {$width}px; display: inline-flex"{/if}>
34
{if $media->isImage}
45
{if $thumbnailSize != 'original'}
@@ -33,3 +34,4 @@
3334
</span>
3435
{/if}
3536
</span>
37+
{if $float === 'center'}</p>{/if}

wcfsetup/install/files/acp/templates/mediaBBCodeTag.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{if !$removeLinks|isset}{assign var='removeLinks' value=false}{/if}
2+
{if $float === 'center'}<p class="text-center">{/if}
23
<span class="mediaBBCode{if $float != 'none'} messageFloatObject{$float|ucfirst}{/if}"{if $width !== 'auto'} style="width: {$width}px; display: inline-flex"{/if}>
34
{if $media->isImage}
45
{if $thumbnailSize != 'original'}
@@ -33,3 +34,4 @@
3334
</span>
3435
{/if}
3536
</span>
37+
{if $float === 'center'}</p>{/if}

wcfsetup/install/files/lib/system/bbcode/AttachmentBBCode.class.php

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private function showImage(Attachment $attachment, string $outputType, array $at
9191
);
9292

9393
if (!$hasParentLink && ($attachment->width > ATTACHMENT_THUMBNAIL_WIDTH || $attachment->height > ATTACHMENT_THUMBNAIL_HEIGHT)) {
94-
return \sprintf(
94+
$result = \sprintf(
9595
<<<'HTML'
9696
<a href="%s" title="%s" class="embeddedAttachmentLink jsImageViewer %s" style="width: %s">
9797
%s
@@ -107,15 +107,22 @@ private function showImage(Attachment $attachment, string $outputType, array $at
107107
$imageElement,
108108
FontAwesomeIcon::fromValues('magnifying-glass')->toHtml(24),
109109
);
110+
} else {
111+
$result = \sprintf(
112+
'<span title="%s" class="%s" style="width: %s; display: inline-flex;">%s</span>',
113+
$title,
114+
$class,
115+
$width,
116+
$imageElement,
117+
);
110118
}
111-
112-
return \sprintf(
113-
'<span title="%s" class="%s" style="width: %s; display: inline-flex;">%s</span>',
114-
$title,
115-
$class,
116-
$width,
117-
$imageElement,
118-
);
119+
if ($alignment === 'center') {
120+
return \sprintf(
121+
'<p class="text-center">%s</p>',
122+
$result,
123+
);
124+
}
125+
return $result;
119126
}
120127

121128
private function showImageAsThumbnail(Attachment $attachment, string $alignment, bool $hasParentLink, string $width): string
@@ -156,7 +163,7 @@ private function showImageAsThumbnail(Attachment $attachment, string $alignment,
156163
);
157164

158165
if (!$hasParentLink && $attachment->hasThumbnail() && $attachment->canDownload()) {
159-
return \sprintf(
166+
$result = \sprintf(
160167
'<a href="%s" title="%s" class="embeddedAttachmentLink jsImageViewer %s" style="width: %s">%s%s</a>',
161168
StringUtil::encodeHTML(LinkHandler::getInstance()->getLink('Attachment', ['object' => $attachment])),
162169
StringUtil::encodeHTML($attachment->filename),
@@ -165,15 +172,23 @@ private function showImageAsThumbnail(Attachment $attachment, string $alignment,
165172
$imageElement,
166173
$enlargeImageControls,
167174
);
175+
} else {
176+
$result = \sprintf(
177+
'<span class="%s" style="width: %s; display: inline-flex">%s%s</span>',
178+
$class,
179+
$width,
180+
$imageElement,
181+
\str_contains($imageClasses, 'embeddedAttachmentLink') ? $enlargeImageControls : '',
182+
);
183+
}
184+
if ($alignment === 'center') {
185+
return \sprintf(
186+
'<p class="text-center">%s</p>',
187+
$result,
188+
);
168189
}
169190

170-
return \sprintf(
171-
'<span class="%s" style="width: %s; display: inline-flex">%s%s</span>',
172-
$class,
173-
$width,
174-
$imageElement,
175-
\str_contains($imageClasses, 'embeddedAttachmentLink') ? $enlargeImageControls : '',
176-
);
191+
return $result;
177192
}
178193

179194
/**

wcfsetup/install/files/lib/system/html/input/node/HtmlInputNodeImg.class.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ protected function handleAttachment(\DOMElement $element, $class)
119119
if ($parent->tagName === "figure") {
120120
if (\preg_match('~\b(?<float>image-style-side-left|image-style-side)\b~', $parent->getAttribute('class'), $matches)) {
121121
$float = ($matches['float'] === 'image-style-side-left') ? 'left' : 'right';
122+
} else {
123+
$float = 'center';
122124
}
123125

124126
$replaceElement = $parent;
@@ -185,6 +187,8 @@ protected function handleMedium(\DOMElement $element, $class)
185187
if ($parent->tagName === "figure") {
186188
if (\preg_match('~\b(?<float>image-style-side-left|image-style-side)\b~', $parent->getAttribute('class'), $matches)) {
187189
$float = ($matches['float'] === 'image-style-side-left') ? 'left' : 'right';
190+
} else {
191+
$float = 'center';
188192
}
189193

190194
$replaceElement = $parent;

0 commit comments

Comments
 (0)