Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 2 additions & 39 deletions lua/wikis/commons/Widget.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---
-- @Liquipedia
-- page=Module:Widget
Expand All @@ -12,21 +12,19 @@
local ErrorDisplay = Lua.import('Module:Error/Display')
local FnUtil = Lua.import('Module:FnUtil')
local Logic = Lua.import('Module:Logic')
local Renderer = Lua.import('Module:Widget/Renderer')
local String = Lua.import('Module:StringUtils')
local Table = Lua.import('Module:Table')

---@class Widget: BaseClass
---@operator call(table): self
---@field context Widget[]
---@field props table<string, any>
local Widget = Class.new(function(self, props)
self.props = Table.deepMerge(Table.deepCopy(self.defaultProps), props)

if not Array.isArray(self.props.children) then
self.props.children = {self.props.children}
end

self.context = {} -- Populated by the parent
end)

Widget.defaultProps = {}
Expand All @@ -46,23 +44,7 @@
---@return string
function Widget:tryMake()
local function renderComponent()
local ret = self:render()
if not Array.isArray(ret) then
ret = {ret}
end
---@cast ret Renderable[]

return table.concat(Array.map(ret, function(val)
if Class.instanceOf(val, Widget) then
---@cast val Widget
val.context = self:_nextContext()
return val:tryMake()
end
if val ~= nil then
return tostring(val)
end
return nil
end))
return Renderer.render(self:render())
end

return Logic.tryOrElseLog(
Expand All @@ -72,31 +54,12 @@
)
end

---@param widget WidgetContext
---@param default any
---@return any
function Widget:useContext(widget, default)
local context = Array.find(self.context, function(node)
return Class.instanceOf(node, widget)
end)
if context then
---@cast context WidgetContext
return context:getValue(default)
end
return default
end

---@param error Error
---@return string
function Widget:getDerivedStateFromError(error)
return tostring(ErrorDisplay.InlineError(error))
end

---@return Widget[]
function Widget:_nextContext()
return {self, unpack(self.context)}
end

---@param error Error
---@return Error
function Widget._updateErrorHeader(error)
Expand Down
31 changes: 0 additions & 31 deletions lua/wikis/commons/Widget/Context.lua

This file was deleted.

5 changes: 4 additions & 1 deletion lua/wikis/commons/Widget/Renderer.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---
-- @Liquipedia
-- page=Module:Widget/Renderer
Expand Down Expand Up @@ -55,7 +55,10 @@

-- Backward Compatibility (with Widgets and mw.html)
if not renderFn then
if vNode.__tostring or vNode._build then
if vNode.tryMake then
---@cast vNode Widget
return Renderer.render(vNode:render(), context)
elseif vNode.__tostring or vNode._build then
return tostring(vNode)
end
mw.log('ERROR! Bad renderable:' .. mw.dumpObject(vNode))
Expand Down
Loading