ui

package module
v0.17.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 1, 2025 License: AGPL-3.0 Imports: 5 Imported by: 3

README

UI Open in Gitpod

Tests Status Go Report Card PkgGoDev

License

This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0). You can find a copy of the license at https://www.gnu.org/licenses/agpl-3.0.en.html

For commercial use, please use my contact page to obtain a commercial license.

Introduction

This package allows to build user interfaces based on blocks.

The block based user interface design approach breaks down complex user interfaces into smaller, self-contained blocks or modules.

These blocks can be arranged and rearranged to create different layouts and user experiences.

Benefits of Block-Based User Interfaces

  • Faster Development: Pre-built blocks streamline the UI creation process, reducing development time. Developers can focus on the logic and functionality rather than the nitty-gritty of UI design.

  • Reduced Technical Barrier: No need for in-depth knowledge of complex UI frameworks or languages. Visual nature makes it accessible to a wider range of users, including non-programmers.

  • Consistency and Standardization: Enforces consistency in UI design and layout across different parts of the application. Promotes reusability of UI components.

  • Flexibility: Can be adapted to various UI paradigms (e.g., web, mobile, desktop) by customizing the available blocks. Offers the potential for creating complex and dynamic UIs.

Disadvantages of Block-Based User Interfaces

Before starting on this path beware of the following:

  • Limited Customization: While blocks provide flexibility, they may not always meet specific design requirements. May not be suitable for highly customized or unique UIs.

  • Steeper Learning Curve: While easier than traditional UI development, there's still a learning curve to understand the block library and its capabilities.

  • Performance Overhead: In some cases, block-based UIs might introduce performance overhead due to the underlying framework and interpretation of blocks.

  • Vendor Lock-in: Reliance on a specific block-based framework can limit future options and migration possibilities.

Overall, block-based user interfaces offer a promising approach to streamline UI development, especially for teams with diverse skill sets. However, their suitability depends on the specific project requirements and the trade-offs between speed, flexibility, and customization.

Installation

go get -u github.com/dracory/ui

Usage

import (
  "github.com/dracory/ui"
)

Example

  • Creating a Document with a Page and Two Paragraphs:
paragraph1 := NewBlock()
paragraph1.SetID("paragraph1")
paragraph1.SetType("paragraph")
paragraph1.SetParameter("content", "Hello, world!")

paragraph2 := NewBlock()
paragraph2.SetID("paragraph2")
paragraph2.SetType("paragraph")
paragraph2.SetParameter("content", "Goodbye, world!")

page := NewBlock()
page.SetID("page1")
page.SetType("page")
page.AddChild(paragraph1)
page.AddChild(paragraph2)

document := NewBlock()
document.SetID("document1")
document.SetType("document")
document.AddChild(page)
  • To JSON:
documentAsJson := document.ToJson()
print(documentAsJson)

// or
documentAsJson := ui.BlockToJson(document)
print(documentAsJson)
  • To Map:
documentAsMap := document.ToMap()
print(documentAsMap)

// or
documentAsMap := ui.BlockToMap(document)
print(documentAsMap)

Create a Block

  • Using the NewBlock function
block := ui.NewBlock()
block.SetID("block1")
block.SetType("type1")
block.SetParameter("parameter1", "value1")
block.SetParameter("parameter2", "value2")
  • Using the NewBlockBuilder function
block := ui.NewBlockBuilder().
    WithID("block1").
    WithType("type1").
    WithParameters(map[string]string{
      "parameter1": "value1",
      "parameter2": "value2",
    }).
    Build()
  • Using the NewBlockFromMap function
block := ui.NewBlockFromMap(map[string]any{}{
  "id": "block1",
  "type": "type1",
  "parameters": map[string]string{}{
    "parameter1": "value1",
    "parameter2": "value2",
  },
  "children": []BlockInterface{},
})
  • Using the NewBlockFromJson function
block, err := ui.NewBlockFromJson(`{
  "id":"block1",
  "type":"type1",
  "parameters":{"parameter1":"value1","parameter2":"value2"},
  "children":[]}`)
if err != nil {
  panic(err)
}

Marshal and Unmarshal to/from JSON

  • To JSON
documentAsJson := document.ToJson()
  • From JSON
documentFromJson, err := ui.NewBlockFromJson(documentAsJson)

if err != nil {
  panic(err)
}

Convert to/fom Map

  • To Map
documentAsMap := document.ToMap()
  • From Map
documentFromMap := ui.NewBlockFromMap(documentAsMap)

Working with List of Blocks

  • Marshal Blocks to JSON
blocksJson := MarshalBlocksToJson(blocks)
  • Unmarshal Blocks from JSON
blocks := UnmarshalBlocksFromJson(blocksJson)
  • Convert Map to Blocks
blocks := ConvertMapToBlocks([]map[string]any{
  {
    "id": "block1",
    "type": "type1",
    "parameters": map[string]string{
      "parameter1": "value1",
      "parameter2": "value2",
    },
    "children": []BlockInterface{},
  },
})
  • Convert Blocks to Slice of Maps
blockMaps := ConvertBlocksToMap(blocks)

Documentation

Overview

block_validator.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertBlocksToMap

func ConvertBlocksToMap(blocks []BlockInterface) []map[string]any

func MarshalBlocksToJson

func MarshalBlocksToJson(blocks []BlockInterface) (string, error)

Types

type Block

type Block struct {
	// contains filtered or unexported fields
}

func (*Block) AddChild

func (b *Block) AddChild(child BlockInterface)

func (*Block) AddChildren

func (b *Block) AddChildren(children []BlockInterface)

func (*Block) Children

func (b *Block) Children() []BlockInterface

func (*Block) HasParameter

func (b *Block) HasParameter(key string) bool

func (*Block) ID

func (b *Block) ID() string

func (*Block) Parameter

func (b *Block) Parameter(key string) string

func (*Block) Parameters

func (b *Block) Parameters() map[string]string

func (*Block) SetChildren

func (b *Block) SetChildren(children []BlockInterface)

func (*Block) SetID

func (b *Block) SetID(id string)

func (*Block) SetParameter

func (b *Block) SetParameter(key, value string)

func (*Block) SetParameters

func (b *Block) SetParameters(parameters map[string]string)

func (*Block) SetType

func (b *Block) SetType(blockType string)

func (*Block) ToJson

func (b *Block) ToJson() (string, error)

func (*Block) ToJsonObject

func (b *Block) ToJsonObject() blockJsonObject

func (*Block) ToJsonPretty

func (b *Block) ToJsonPretty() (string, error)

func (*Block) ToMap

func (b *Block) ToMap() map[string]interface{}

func (*Block) Type

func (b *Block) Type() string

type BlockBuilderInterface

type BlockBuilderInterface interface {
	WithID(string) BlockBuilderInterface
	WithType(string) BlockBuilderInterface
	WithParameters(map[string]string) BlockBuilderInterface
	WithChildren([]BlockInterface) BlockBuilderInterface
	Build() BlockInterface
}

func NewBlockBuilder

func NewBlockBuilder() BlockBuilderInterface

type BlockInterface

func ConvertMapToBlock

func ConvertMapToBlock(blockMap map[string]any) (BlockInterface, error)

ConvertMapToBlock converts a map to a block

The map must represent a valid block (have parameters like id, and type), otherwise an error will be returned

Parameters: - blockMap - a map[string]any to convert to a block

Returns: - BlockInterface - a block - error - if the map[string]any is not a valid block

func ConvertMapToBlocks

func ConvertMapToBlocks(blocks []map[string]any) []BlockInterface

func NewBlock

func NewBlock() BlockInterface

NewBlock returns a new block instance, and sets the default ID

func NewBlockFromJson

func NewBlockFromJson(blockJson string) (BlockInterface, error)

func NewBlockFromMap

func NewBlockFromMap(m map[string]any) BlockInterface

BlockFromMap creates a block from a map

func UnmarshalJsonToBlocks

func UnmarshalJsonToBlocks(blocksJson string) ([]BlockInterface, error)

type BlockValidator

type BlockValidator struct {
	// contains filtered or unexported fields
}

BlockValidator is a thread-safe registry of block validators

func NewBlockValidator

func NewBlockValidator() *BlockValidator

NewBlockValidator creates a new BlockValidator

func (*BlockValidator) Add

func (v *BlockValidator) Add(blockType string, validator Validator)

Add registers a validator for a block type

func (*BlockValidator) Validate

func (v *BlockValidator) Validate(block BlockInterface) error

Validate validates a block using its registered validator

type ChildrenInterface

type ChildrenInterface interface {
	Children() []BlockInterface
	SetChildren([]BlockInterface)
	AddChild(BlockInterface)
	AddChildren([]BlockInterface)
}

type IDInterface

type IDInterface interface {
	ID() string
	SetID(string)
}

type ParametersInterface

type ParametersInterface interface {
	HasParameter(key string) bool
	Parameter(key string) string
	SetParameter(key string, value string)
	Parameters() map[string]string
	SetParameters(map[string]string)
}

type ToHTMLInterface

type ToHTMLInterface interface {
	ToHTML() string
}

type ToJsonInterface

type ToJsonInterface interface {
	ToJson() (string, error)
}

type ToJsonObjectInterface

type ToJsonObjectInterface interface {
	ToJsonObject() blockJsonObject
}

type ToJsonPrettyInterface

type ToJsonPrettyInterface interface {
	ToJsonPretty() (string, error)
}

type ToMapInterface

type ToMapInterface interface {
	ToMap() map[string]interface{}
}

type TypeInterface

type TypeInterface interface {
	Type() string
	SetType(string)
}

type Validator

type Validator func(block BlockInterface) error

Validator validates a block

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL