diff --git a/components/infobox/commons/infobox_widget_header.lua b/components/infobox/commons/infobox_widget_header.lua index 4497a2a2cf2..577afe46231 100644 --- a/components/infobox/commons/infobox_widget_header.lua +++ b/components/infobox/commons/infobox_widget_header.lua @@ -7,7 +7,10 @@ -- local Class = require('Module:Class') +local Image = require('Module:Image') +local Logic = require('Module:Logic') local Lua = require('Module:Lua') +local String = require('Module:StringUtils') local Widget = Lua.import('Module:Infobox/Widget') @@ -92,49 +95,40 @@ end ---@param size number|string|nil ---@return Html? function Header:_image(fileName, fileNameDark, default, defaultDark, size) - if (fileName == nil or fileName == '') and (default == nil or default == '') then + local imageName = fileName or default + local imageDarkname = fileNameDark or fileName or defaultDark or default + + if String.isEmpty(imageName) and String.isEmpty(imageDarkname) then return nil end - local imageName = fileName or default - ---@cast imageName -nil - local infoboxImage = Header:_makeSizedImage(imageName, fileName, size, 'lightmode') + if imageName == imageDarkname then + imageDarkname = nil + end - imageName = fileNameDark or fileName or defaultDark or default - ---@cast imageName -nil - local infoboxImageDark = Header:_makeSizedImage(imageName, fileNameDark or fileName, size, 'darkmode') + local infoboxImage = Header:_makeSizedImage(imageName, size, 'lightmode') + local infoboxImageDark = Header:_makeSizedImage(imageDarkname, size, 'darkmode') return mw.html.create('div'):node(infoboxImage):node(infoboxImageDark) end ----@param imageName string ----@param fileName string? +---@param imageName string? ---@param size number|string|nil ----@param mode string ----@return Html -function Header:_makeSizedImage(imageName, fileName, size, mode) - local infoboxImage = mw.html.create('div'):addClass('infobox-image ' .. mode) +---@param mode string? +---@return Html? +function Header:_makeSizedImage(imageName, size, mode) + if String.isEmpty(imageName) then + return + end - -- Number (interpret as pixels) - size = size or '' - if tonumber(size) then - size = tonumber(size) .. 'px' + local infoboxImage = mw.html.create('div'):addClass('infobox-image'):addClass(mode) + if Logic.isNumeric(size) then infoboxImage:addClass('infobox-fixed-size-image') - -- Percentage (interpret as scaling) - elseif size:find('%%') then - local scale = size:gsub('%%', '') - local scaleNumber = tonumber(scale) - if scaleNumber then - size = 'frameless|upright=' .. (scaleNumber / 100) - infoboxImage:addClass('infobox-fixed-size-image') - end - -- Default else - size = '600px' + size = '600x1000px' end - local fullFileName = '[[File:' .. imageName .. '|center|' .. size .. ']]' - infoboxImage:wikitext(fullFileName) + infoboxImage:wikitext(Image.display(imageName, nil, {size = size})) return infoboxImage end diff --git a/stylesheets/commons/Infobox.less b/stylesheets/commons/Infobox.less index f78f9bd8399..728f3a107cd 100644 --- a/stylesheets/commons/Infobox.less +++ b/stylesheets/commons/Infobox.less @@ -125,6 +125,7 @@ Author(s): FO-nTTaX } .fo-nttax-infobox > div > div.infobox-image { + text-align: center; padding: 0; width: 100%; }