From 43c87cb79b264f72d0e70a7dec92c719d4bb7859 Mon Sep 17 00:00:00 2001 From: Rikard Blixt Date: Mon, 14 Oct 2024 15:18:00 +0200 Subject: [PATCH] hopefully fix customizable --- components/widget/widget.lua | 76 ++++++----------------- components/widget/widget_customizable.lua | 13 +--- 2 files changed, 23 insertions(+), 66 deletions(-) diff --git a/components/widget/widget.lua b/components/widget/widget.lua index 2e4fca7a4de..ac69819f9b0 100644 --- a/components/widget/widget.lua +++ b/components/widget/widget.lua @@ -18,11 +18,13 @@ local Table = require('Module:Table') ---@field children (Widget|Html|string|number)[] @deprecated ---@field context Widget[] ---@field props table ----@field makeChildren? fun(self:Widget, injector: WidgetInjector?): Widget[]? +---@field injector WidgetInjector? local Widget = Class.new(function(self, props) self.props = Table.copy(props) or {} self.props.children = self.props.children or {} self.context = {} -- Populated by the parent + ---@deprecated To be removed when CustomizableWidget is removed + self.injector = nil -- Populated by the parent end) ---Asserts the existence of a value and copies it @@ -37,44 +39,28 @@ function Widget:render() error('A Widget must override the render() function!') end ----@deprecated ----@param children string[] ----@return string|nil -function Widget:make(children) - error('A Widget must override the make() function!') -end - ---@param injector WidgetInjector? ---@return string function Widget:tryMake(injector) - local renderComponent - if self.render == Widget.render then - -- Widget v1 backwards compability - renderComponent = function() - local processedChildren = self:tryChildren(injector) - local ret = self:make(processedChildren) - return ret ~= nil and ret or '' + local renderComponent = function() + self.injector = injector + local ret = self:render() + if not Array.isArray(ret) then + ret = {ret} end - else - renderComponent = function() - local ret = self:render() - if not Array.isArray(ret) then - ret = {ret} - end - ---@cast ret (string|Widget|Html|nil)[] - return Array.reduce(ret, function(acc, val) - if Class.instanceOf(val, Widget) then - ---@cast val Widget - val.context = self:_nextContext() - return acc .. val:tryMake(injector) - end - if val ~= nil then - return acc .. tostring(val) - end - return acc - end, '') - end + ---@cast ret (string|Widget|Html|nil)[] + return Array.reduce(ret, function(acc, val) + if Class.instanceOf(val, Widget) then + ---@cast val Widget + val.context = self:_nextContext() + return acc .. val:tryMake(injector) + end + if val ~= nil then + return acc .. tostring(val) + end + return acc + end, '') end return Logic.tryOrElseLog( @@ -84,28 +70,6 @@ function Widget:tryMake(injector) ) end ----@deprecated ----@param injector WidgetInjector? ----@return string[] -function Widget:tryChildren(injector) - local children = self.props.children - if self.makeChildren then - children = self:makeChildren(injector) or {} - end - return Array.map(children, function(child) - if Class.instanceOf(child, Widget) then - ---@cast child Widget - return Logic.tryOrElseLog( - function() return child:tryMake(injector) end, - FnUtil.curry(self.getDerivedStateFromError, self), - Widget._updateErrorHeader - ) - end - ---@cast child -Widget - return tostring(child) - end) -end - ---@param widget WidgetContext ---@param default any ---@return any diff --git a/components/widget/widget_customizable.lua b/components/widget/widget_customizable.lua index 9e0dfe7c86b..d1a406b6d99 100644 --- a/components/widget/widget_customizable.lua +++ b/components/widget/widget_customizable.lua @@ -21,19 +21,12 @@ local Customizable = Class.new( end ) ----@param children string[] ----@return string -function Customizable:make(children) - return table.concat(children) -end - ----@param injector WidgetInjector? ---@return Widget[]? -function Customizable:makeChildren(injector) - if injector == nil then +function Customizable:render() + if self.injector == nil then return self.props.children end - return injector:parse(self.id, self.props.children) + return self.injector:parse(self.id, self.props.children) end return Customizable