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 (only for regular cards)
     -- Calculate total content size and average per column
     local totalSize = 0
     local totalSize = 0
     for _, card in ipairs(regularCards) do
     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 regular cards or very uneven sizes, consider using fewer columns
     -- If we have very few cards or very uneven sizes, consider using fewer columns
     if #regularCards <= 2 then
     if #sortedCards <= 2 then
         optimalColumnCount = math.min(#regularCards + 2, columnCount) -- Account for special cards
         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 = regularCards[1]
         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, #regularCards + 1))
             optimalColumnCount = math.max(2, math.min(columnCount, #sortedCards))
         end
         end
     end
     end
      
      
     -- Greedy placement with balance checking for regular cards only
    -- Redistribute columns if we're using fewer
     for _, card in ipairs(regularCards) do
    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 all columns)
         -- Find the best column (shortest among active columns)
         for i = 2, columnCount do
         for i = 2, optimalColumnCount do
             if columnHeights[i] < bestHeight then
             if columnHeights[i] < bestHeight then
                 bestColumn = i
                 bestColumn = i