Updated markdown parser, testing it

This commit is contained in:
Robert Jelic
2025-02-17 09:36:51 +01:00
parent daae1ddb3d
commit cf49f69612
7 changed files with 167 additions and 55 deletions

View File

@@ -75,7 +75,7 @@ function BaseElement:init(props, basalt)
return self
end
--- Post initialization hook
--- Post initialization
--- @return table self The BaseElement instance
function BaseElement:postInit()
if(self._props)then
@@ -174,6 +174,8 @@ function BaseElement:handleEvent(event, ...)
return false
end
--- Returns the base frame of the element
--- @return table BaseFrame The base frame of the element
function BaseElement:getBaseFrame()
if self.parent then
return self.parent:getBaseFrame()
@@ -181,8 +183,26 @@ function BaseElement:getBaseFrame()
return self
end
--- Destroys the element and cleans up all references
--- @usage element:destroy()
function BaseElement:destroy()
-- Remove from parent if exists
if self.parent then
self.parent:removeChild(self)
end
for event in pairs(self._registeredEvents) do
self:listenEvent(event, false)
end
self._values.eventCallbacks = {}
self._props = nil
self._values = nil
self.basalt = nil
self.parent = nil
self.__index = nil
setmetatable(self, nil)
end
--- Requests a render update for this element

View File

@@ -464,4 +464,11 @@ function Container:render()
end
end
function Container:destroy()
for _, child in ipairs(self._values.children) do
child:destroy()
end
VisualElement.destroy(self)
end
return Container

View File

@@ -47,7 +47,7 @@ end
function Table:sortData(columnIndex)
local data = self.get("data")
local direction = self.get("sortDirection")
table.sort(data, function(a, b)
if direction == "asc" then
return a[columnIndex] < b[columnIndex]
@@ -55,7 +55,7 @@ function Table:sortData(columnIndex)
return a[columnIndex] > b[columnIndex]
end
end)
self.set("data", data)
return self
end
@@ -65,7 +65,6 @@ function Table:mouse_click(button, x, y)
local relX, relY = self:getRelativePosition(x, y)
-- Header-Click für Sorting
if relY == 1 then
local currentX = 1
for i, col in ipairs(self.get("columns")) do
@@ -83,7 +82,6 @@ function Table:mouse_click(button, x, y)
end
end
-- Row-Selection (berücksichtigt Scroll-Offset)
if relY > 1 then
local rowIndex = relY - 2 + self.get("scrollOffset")
if rowIndex >= 0 and rowIndex < #self.get("data") then
@@ -98,7 +96,7 @@ function Table:mouse_scroll(direction, x, y)
local data = self.get("data")
local height = self.get("height")
local visibleRows = height - 2
local maxScroll = math.max(0, #data - visibleRows + 1) -- +1 korrigiert den Scroll-Bereich
local maxScroll = math.max(0, #data - visibleRows + 1)
local newOffset = math.min(maxScroll, math.max(0, self.get("scrollOffset") + direction))
self.set("scrollOffset", newOffset)
@@ -125,14 +123,12 @@ function Table:render()
currentX = currentX + col.width
end
-- Angepasste Berechnung der sichtbaren Zeilen
local visibleRows = height - 2 -- Verfügbare Zeilen (minus Header)
local visibleRows = height - 2
for y = 2, height do
local rowIndex = y - 2 + scrollOffset
local rowData = data[rowIndex + 1]
-- Zeile nur rendern wenn es auch Daten dafür gibt
if rowData and (rowIndex + 1) <= #data then -- Korrigierte Bedingung
if rowData and (rowIndex + 1) <= #data then
currentX = 1
local bg = (rowIndex + 1) == selected and self.get("selectedColor") or self.get("background")
@@ -145,33 +141,28 @@ function Table:render()
currentX = currentX + col.width
end
else
-- Leere Zeile füllen
self:blit(1, y, string.rep(" ", self.get("width")),
string.rep(tHex[self.get("foreground")], self.get("width")),
string.rep(tHex[self.get("background")], self.get("width")))
end
end
-- Scrollbar Berechnung überarbeitet
if #data > height - 2 then
local scrollbarHeight = height - 2
local thumbSize = math.max(1, math.floor(scrollbarHeight * (height - 2) / #data))
-- Thumb Position korrigiert
local maxScroll = #data - (height - 2) + 1 -- +1 für korrekte End-Position
local maxScroll = #data - (height - 2) + 1
local scrollPercent = scrollOffset / maxScroll
local thumbPos = 2 + math.floor(scrollPercent * (scrollbarHeight - thumbSize))
if scrollOffset >= maxScroll then
thumbPos = height - thumbSize -- Exakt am Ende
thumbPos = height - thumbSize
end
-- Scrollbar Background
for y = 2, height do
self:blit(self.get("width"), y, "\127", tHex[colors.gray], tHex[colors.gray])
end
-- Thumb zeichnen
for y = thumbPos, math.min(height, thumbPos + thumbSize - 1) do
self:blit(self.get("width"), y, "\127", tHex[colors.white], tHex[colors.white])
end

View File

@@ -2,8 +2,6 @@ local elementManager = require("elementManager")
local BaseElement = elementManager.getElement("BaseElement")
local tHex = require("libraries/colorHex")
---@alias color number
---@class VisualElement : BaseElement
local VisualElement = setmetatable({}, BaseElement)
VisualElement.__index = VisualElement
@@ -62,13 +60,24 @@ VisualElement.defineProperty(VisualElement, "visible", {default = true, type = "
return value
end})
---@combinedProperty position x y
---@combinedProperty position {x y} Position of the element
VisualElement.combineProperties(VisualElement, "position", "x", "y")
---@combinedProperty size width height
---@combinedProperty size {width height} Size of the element
VisualElement.combineProperties(VisualElement, "size", "width", "height")
---@combinedProperty color foreground background
---@combinedProperty color {foreground background} Color of the element
VisualElement.combineProperties(VisualElement, "color", "foreground", "background")
---@event onMouseClick {button number, x number, y number} Fired when the element is clicked
---@event onMouseUp {button number, x number, y number} Fired when the mouse is released
---@event onMouseRelease {button number, x number, y number} Fired when the mouse is released
---@event onMouseDrag {button number, x number, y number} Fired when the mouse is dragged
---@event onFocus {-} Fired when the element is focused
---@event onBlur {-} Fired when the element is blurred
---@event onKey {key number, code number, isRepeat boolean} Fired when a key is pressed
---@event onKeyUp {key number, code number} Fired when a key is released
---@event onChar {char string} Fired when a key is pressed
VisualElement.listenTo(VisualElement, "focus")
VisualElement.listenTo(VisualElement, "blur")
@@ -90,14 +99,7 @@ function VisualElement:init(props, basalt)
self.set("type", "VisualElement")
end
--- Draws a text character/fg/bg at the specified position with a certain size, used in the rendering system
--- @param x number The x position to draw
--- @param y number The y position to draw
--- @param width number The width of the element
--- @param height number The height of the element
--- @param text string The text char to draw
--- @param fg color The foreground color
--- @param bg color The background color
---@protected
function VisualElement:multiBlit(x, y, width, height, text, fg, bg)
x = x + self.get("x") - 1
y = y + self.get("y") - 1
@@ -260,7 +262,6 @@ function VisualElement:setCursor(x, y, blink)
end
--- Renders the element
--- @usage element:render()
function VisualElement:render()
if(not self.get("backgroundEnabled"))then
return