Files
plainleaf/libraries/Library/Std/APIs/Widget.md
T
Zef Hemel 26d9d5bdf0 Restructure standard library and improve documentation
Refactor Library/Std into subfolders for APIs, Infrastructure, Editor,
Widgets, etc. Add more documentation and descriptions. Update references
throughout website and docs.
2025-10-01 11:22:31 +02:00

1.2 KiB

#meta/api

This implements the widget API. For a Lua Directive to be rendered as a widget, you need to return it via widget.new.

-- priority: 50

-- Widget APIs
widget = {}

-- Container for widgets
widgets = {}

-- Widget schema for validation
local widgetSchema = {
  type = "object",
  properties = {
    markdown = { type = "string"},
    html = {
      anyOf = {
        -- HTMLElement
        { type = "object"},
        -- Plain HTML code
        { type = "string" }
      }
    },
    cssClasses = {
      type = "array",
      items = { type = "string" },
    },
    display = {
      type = "string",
      enum = {"block", "inline"}
    },
    events = {
      type = "object",
      additionalProperties = true
    }
  }
}

-- Creates a widget
function widget.new(spec)
  -- Validate spec
  local validationResult = jsonschema.validateObject(widgetSchema, spec)
  if validationResult then
    error(validationResult)
  end
  -- Mark as a widget
  spec._isWidget = true
  return spec
end

-- Convenience function for HTML widgets
function widget.html(html)
  return widget.new {
    html = html
  }
end

-- Convenience function for markdown widgets
function widget.markdown(markdown)
  return widget.new {
    markdown = markdown
  }
end