Skip to content

Commit

Permalink
refactor(widget): simplify setup of html tags widgets (#4902)
Browse files Browse the repository at this point in the history
* refactor(widget): simplify setup of html tags widgets

* remove center

* fix merge conflict incorerct resovle
  • Loading branch information
Rathoz authored Oct 18, 2024
1 parent 3240d3a commit 17bdf90
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 334 deletions.
28 changes: 0 additions & 28 deletions components/widget/html/widget_html_abbr.lua

This file was deleted.

70 changes: 59 additions & 11 deletions components/widget/html/widget_html_all.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,66 @@

local Widgets = {}

local Array = require('Module:Array')
local Class = require('Module:Class')
local Lua = require('Module:Lua')
local String = require('Module:StringUtils')

Widgets.Abbr = Lua.import('Module:Widget/Html/Abbr')
Widgets.Center = Lua.import('Module:Widget/Html/Center')
Widgets.Div = Lua.import('Module:Widget/Html/Div')
Widgets.Fragment = Lua.import('Module:Widget/Html/Fragment')
Widgets.Li = Lua.import('Module:Widget/Html/Li')
Widgets.Span = Lua.import('Module:Widget/Html/Span')
Widgets.Table = Lua.import('Module:Widget/Html/Table')
Widgets.Td = Lua.import('Module:Widget/Html/Td')
Widgets.Th = Lua.import('Module:Widget/Html/Th')
Widgets.Tr = Lua.import('Module:Widget/Html/Tr')
Widgets.Ul = Lua.import('Module:Widget/Html/Ul')
local Widget = Lua.import('Module:Widget')

---@param tag? string
---@return WidgetHtml
local function createHtmlTag(tag)
---@class WidgetHtml: Widget
---@operator call(table): WidgetHtml
local Html = Class.new(Widget)
Html.defaultProps = {
classes = {},
css = {},
attributes = {},
}

---@return Html
function Html:render()
local htmlNode = mw.html.create(tag)

htmlNode:addClass(String.nilIfEmpty(table.concat(self.props.classes, ' ')))
htmlNode:css(self.props.css)
htmlNode:attr(self.props.attributes)

Array.forEach(self.props.children, function(child)
if Class.instanceOf(child, Widget) then
---@cast child Widget
child.context = self:_nextContext()
htmlNode:node(child:tryMake())
else
---@cast child -Widget
---@diagnostic disable-next-line: undefined-field
if type(child) == 'table' and not child._build then
mw.log('ERROR! Bad child input:' .. mw.dumpObject(self.props.children))
-- Erroring here to make it easier to debug
-- Otherwise it will fail when the html is built
error('Table passed to HtmlBase:renderAs() without _build method')
end
htmlNode:node(child)
end
end)
return htmlNode
end

return Html
end

Widgets.Abbr = createHtmlTag('abbr')
Widgets.Center = createHtmlTag('center')
Widgets.Div = createHtmlTag('div')
Widgets.Fragment = createHtmlTag()
Widgets.Li = createHtmlTag('li')
Widgets.Span = createHtmlTag('span')
Widgets.Table = createHtmlTag('table')
Widgets.Td = createHtmlTag('td')
Widgets.Th = createHtmlTag('th')
Widgets.Tr = createHtmlTag('tr')
Widgets.Ul = createHtmlTag('ul')

return Widgets
59 changes: 0 additions & 59 deletions components/widget/html/widget_html_base.lua

This file was deleted.

23 changes: 0 additions & 23 deletions components/widget/html/widget_html_center.lua

This file was deleted.

23 changes: 0 additions & 23 deletions components/widget/html/widget_html_div.lua

This file was deleted.

23 changes: 0 additions & 23 deletions components/widget/html/widget_html_fragment.lua

This file was deleted.

23 changes: 0 additions & 23 deletions components/widget/html/widget_html_li.lua

This file was deleted.

23 changes: 0 additions & 23 deletions components/widget/html/widget_html_span.lua

This file was deleted.

23 changes: 0 additions & 23 deletions components/widget/html/widget_html_table.lua

This file was deleted.

23 changes: 0 additions & 23 deletions components/widget/html/widget_html_td.lua

This file was deleted.

23 changes: 0 additions & 23 deletions components/widget/html/widget_html_th.lua

This file was deleted.

23 changes: 0 additions & 23 deletions components/widget/html/widget_html_tr.lua

This file was deleted.

Loading

0 comments on commit 17bdf90

Please sign in to comment.