Module:MasonryLayout: Difference between revisions
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| Line 276: | Line 276: | ||
end) | end) | ||
-- Calculate total content size and average per column | -- Calculate total content size and average per column | ||
local totalSize = 0 | local totalSize = 0 | ||
for _, card in ipairs( | for _, card in ipairs(sortedCards) do | ||
totalSize = totalSize + (card.estimatedSize or 0) | totalSize = totalSize + (card.estimatedSize or 0) | ||
end | end | ||
| Line 286: | Line 286: | ||
local optimalColumnCount = columnCount | local optimalColumnCount = columnCount | ||
-- If we have very few | -- If we have very few cards or very uneven sizes, consider using fewer columns | ||
if # | if #sortedCards <= 2 then | ||
optimalColumnCount = math.min(# | optimalColumnCount = math.min(#sortedCards, columnCount) | ||
else | else | ||
-- Check if largest card is more than 60% of total content | -- Check if largest card is more than 60% of total content | ||
local largestCard = | local largestCard = sortedCards[1] | ||
if largestCard and largestCard.estimatedSize > (totalSize * 0.6) then | if largestCard and largestCard.estimatedSize > (totalSize * 0.6) then | ||
-- Very unbalanced content - use fewer columns | -- Very unbalanced content - use fewer columns | ||
optimalColumnCount = math.max(2, math.min(columnCount, # | optimalColumnCount = math.max(2, math.min(columnCount, #sortedCards)) | ||
end | end | ||
end | end | ||
-- Greedy placement with balance checking | -- Redistribute columns if we're using fewer | ||
for _, card in ipairs( | if optimalColumnCount < columnCount then | ||
-- Reset columns for optimal count | |||
columns = {} | |||
columnHeights = {} | |||
for i = 1, optimalColumnCount do | |||
columns[i] = {} | |||
columnHeights[i] = 0 | |||
end | |||
-- Add empty columns at the end | |||
for i = optimalColumnCount + 1, columnCount do | |||
columns[i] = {} | |||
columnHeights[i] = 0 | |||
end | |||
end | |||
-- Greedy placement with balance checking | |||
for _, card in ipairs(sortedCards) do | |||
local bestColumn = 1 | local bestColumn = 1 | ||
local bestHeight = columnHeights[1] | local bestHeight = columnHeights[1] | ||
-- Find the best column (shortest among | -- Find the best column (shortest among active columns) | ||
for i = 2, | for i = 2, optimalColumnCount do | ||
if columnHeights[i] < bestHeight then | if columnHeights[i] < bestHeight then | ||
bestColumn = i | bestColumn = i | ||