diff --git a/frames/exportRoute.lua b/frames/exportRoute.lua index 55db4f8..f18bb07 100644 --- a/frames/exportRoute.lua +++ b/frames/exportRoute.lua @@ -18,10 +18,15 @@ function AprRC.export:Show() dropdown:SetFullWidth(true) local routeList = {} local defaultIndex = nil + local selectedRouteName = '' for index, route in ipairs(AprRCData.Routes) do + if index == 1 then + selectedRouteName = route.name + end routeList[index] = route.name if AprRCData.CurrentRoute and route.name == AprRCData.CurrentRoute.name then defaultIndex = index + selectedRouteName = route.name end end dropdown:SetList(routeList) @@ -49,6 +54,20 @@ function AprRC.export:Show() end) frame:AddChild(btnExportELT) + local btnSave = AceGUI:Create("Button") + btnSave:SetText("Save Route") + btnSave:SetWidth(200) + btnSave:SetCallback("OnClick", function() + local routeText = editbox:GetText() + local newStepRouteTable = AprRC:stringToTable(routeText) + local newRoute = { name = selectedRouteName, steps = newStepRouteTable } + AprRC:UpdateRouteByName(selectedRouteName, newRoute) + if AprRCData.CurrentRoute.name == selectedRouteName then + AprRCData.CurrentRoute = newRoute + end + end) + frame:AddChild(btnSave) + -- local btnDelete = AceGUI:Create("Button") -- btnDelete:SetText("Delete this route") -- btnDelete:SetWidth(200) @@ -68,9 +87,9 @@ function AprRC.export:Show() dropdown:SetCallback("OnValueChanged", function(widget, event, index) local selectedRoute = AprRCData.Routes[index] - -- defaultIndex = index if selectedRoute then editbox:SetText(AprRC:RouteToString(selectedRoute.steps)) + selectedRouteName = selectedRoute.name end end) end diff --git a/helper/Utils.lua b/helper/Utils.lua index 550f9ce..7c7a9e7 100644 --- a/helper/Utils.lua +++ b/helper/Utils.lua @@ -88,7 +88,12 @@ local function qpartTableToString(tbl, level) local itemIndent = string.rep(" ", level + 1) for k, v in pairs(tbl) do - local keyStr = k .. " = " + local keyStr = '' + if k == "Button" then + keyStr = '["' .. tostring(k) .. '"] = ' + else + keyStr = "[" .. k .. "] = " + end if type(v) == "table" then str = str .. itemIndent .. keyStr .. AprRC:RouteToString(v, level + 1) .. ",\n" else @@ -120,7 +125,7 @@ function AprRC:RouteToString(tbl, level) str = str .. itemIndent .. keyStr .. "{}" .. ",\n" else local valueStr - if k == "Qpart" or k == "Fillers" then + if k == "Qpart" or k == "Fillers" or k == "Button" then valueStr = qpartTableToString(v, level + 1) else valueStr = self:RouteToString(v, level + 1) @@ -136,3 +141,19 @@ function AprRC:RouteToString(tbl, level) str = str .. indent .. "}" return str end + +function AprRC:stringToTable(str) + local cleanedStr = str:gsub("[%s\n\r\t]+", "") + + local func, err = loadstring("return " .. cleanedStr) + if not func then + AprRC:Debug("Error when converting the string to a table", err) + else + local success, tableResult = pcall(func) + if success then + return tableResult + else + AprRC:Debug("Error when executing the string converted to a table", tableResult) + end + end +end