blockeditor

package module
v0.24.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: 14 Imported by: 5

README

BlockEditor 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.

Status

The status of this project is "work in progress", "multiple changes", "avoid at the minute", "nothing stable".

Introduction

This package allows to build user interfaces based on blocks.

Installation

go get -u github.com/dracory/blockeditor

Screenshots

  • simple sequential blocks
  • nested blocks

Documentation

Index

Constants

View Source
const ACTION = "blockeditor_action"
View Source
const ACTION_BLOCKS_SAVE = "blocks_save"
View Source
const ACTION_BLOCK_ADD = "block_add"
View Source
const ACTION_BLOCK_ADD_MODAL = "block_add_modal"
View Source
const ACTION_BLOCK_DELETE = "block_delete"
View Source
const ACTION_BLOCK_DUPLICATE = "block_duplicate"
View Source
const ACTION_BLOCK_MOVE_DOWN = "block_move_down"
View Source
const ACTION_BLOCK_MOVE_INTO = "block_move_into"
View Source
const ACTION_BLOCK_MOVE_OUT = "block_move_out"
View Source
const ACTION_BLOCK_MOVE_UP = "block_move_up"
View Source
const ACTION_BLOCK_SETTINGS = "block_settings"
View Source
const ACTION_BLOCK_SETTINGS_UPDATE = "block_settings_update"
View Source
const BLOCK_ID = "block_id"
View Source
const BLOCK_TYPE = "block_type"
View Source
const EDITOR_HANDLER_ENDPOINT = "editor_handler_endpoint"
View Source
const EDITOR_ID = "editor_id"
View Source
const EDITOR_NAME = "editor_name"
View Source
const SETTINGS_PREFIX = "blockeditor_settings_"

Variables

This section is empty.

Functions

func ApplyAlignmentParameters

func ApplyAlignmentParameters(block ui.BlockInterface, blockTag *hb.Tag)

func ApplyAnimationParameters

func ApplyAnimationParameters(block ui.BlockInterface, blockTag *hb.Tag)

func ApplyBackgroundParameters

func ApplyBackgroundParameters(block ui.BlockInterface, blockTag *hb.Tag)

func ApplyBorderParameters

func ApplyBorderParameters(block ui.BlockInterface, blockTag *hb.Tag)

func ApplyDisplayParameters

func ApplyDisplayParameters(block ui.BlockInterface, blockTag *hb.Tag)

func ApplyFlexBoxParameters

func ApplyFlexBoxParameters(block ui.BlockInterface, blockTag *hb.Tag)

func ApplyFontParameters

func ApplyFontParameters(block ui.BlockInterface, blockTag *hb.Tag)

func ApplyHTMLParameters

func ApplyHTMLParameters(block ui.BlockInterface, blockTag *hb.Tag)

func ApplyMarginParameters

func ApplyMarginParameters(block ui.BlockInterface, blockTag *hb.Tag)

func ApplyPaddingParameters

func ApplyPaddingParameters(block ui.BlockInterface, blockTag *hb.Tag)

func ApplyPositionParameters

func ApplyPositionParameters(block ui.BlockInterface, blockTag *hb.Tag)

func ApplySizeParameters

func ApplySizeParameters(block ui.BlockInterface, blockTag *hb.Tag)

func ApplyTextParameters

func ApplyTextParameters(block ui.BlockInterface, blockTag *hb.Tag)

func ApplyTransitionParameters

func ApplyTransitionParameters(block ui.BlockInterface, blockTag *hb.Tag)

func FieldGroupEnd

func FieldGroupEnd() form.FieldInterface

func FieldGroupStart

func FieldGroupStart(groupId, groupName string, collapsed bool) form.FieldInterface

func FieldsAlign

func FieldsAlign() []form.FieldInterface

FieldsAlign is a predefined set of alignment fields - Text Align (text_align) - Vertical Align (vertical_align)

Parameters: - none

Returns: - []form.Field - The fields

func FieldsAnimation

func FieldsAnimation() []form.FieldInterface

FieldsAnimation is a predefined set of animation fields

func FieldsBackground

func FieldsBackground() []form.FieldInterface

FieldsBackground is a predefined set of background fields - Background Color (background_color) - Background Image URL (background_image_url) - Background Attachment (background_attachment) - Background Position (background_position) - Background Repeat (background_repeat) - Background Size (background_size)

Parameters: - none

Returns: - []form.Field - The fields

func FieldsBorder

func FieldsBorder() []form.FieldInterface

FieldsBorder is a predefined set of border fields - Border (border)

Parameters: - none

Returns: - []form.Field - The fields

func FieldsDisplay

func FieldsDisplay() []form.FieldInterface

FieldsDisplay is a predefined set of display fields - Display (display) - Visibility (visibility)

Parameters: - none

Returns: - []form.Field - The fields

func FieldsFlexBox

func FieldsFlexBox() []form.FieldInterface

func FieldsFont

func FieldsFont() []form.FieldInterface

FieldsFont is a predefined set of font fields - Font Family (font_family) - Font Size (font_size) - Font Weight (font_weight) - Font Style (font_style)

Parameters: - none

Returns: - []form.Field - The fields

func FieldsHTML

func FieldsHTML() []form.FieldInterface

FieldsHTML is a predefined set of fields common to all HTML elements - HTML ID (html_id) - HTML Class (html_class) - HTML Style (html_style)

Parameters: - none

Returns: - []form.Field - The fields

func FieldsMargin

func FieldsMargin() []form.FieldInterface

FieldsMargin is a predefined set of margin fields - Margin Top (margin_top) - Margin Bottom (margin_bottom) - Margin Left (margin_left) - Margin Right (margin_right)

Parameters: - none

Returns: - []form.Field - The fields

func FieldsPadding

func FieldsPadding() []form.FieldInterface

FieldsPadding is a predefined set of padding fields - Padding Top (padding_top) - Padding Bottom (padding_bottom) - Padding Left (padding_left) - Padding Right (padding_right)

Parameters: - none

Returns: - []form.Field - The fields

func FieldsPositioning

func FieldsPositioning() []form.FieldInterface

FieldsPositioning is a predefined set of positioning fields - Position (position) - Top (top) - Right (right) - Bottom (bottom) - Left (left) - Z-Index (zindex)

Parameters: - none

Returns: - []form.Field - The fields

func FieldsSize

func FieldsSize() []form.FieldInterface

FieldsSize is a predefined set of size fields - Width (width) - Height (height)

Parameters: - none

Returns: - []form.Field - The fields

func FieldsText

func FieldsText() []form.FieldInterface

FieldsText is a predefined set of text fields - Text Color (text_color) - Line Height (line_height)

Parameters: - none

Returns: - []form.Field - The fields

func FieldsTransition

func FieldsTransition() []*form.Field

func Handle

func Handle(w http.ResponseWriter, r *http.Request, blockDefinitions []BlockDefinition) string

Handle handles the block editor

Business logic: - gets the action from the request - gets the editor id, name, content, and handler endpoint from the request - creates a new editor - if an action is provided, executes the action - if no action is provided, renders the editor

Parameters: - w: the http.ResponseWriter - r: the http.Request - blockDefinitions: the block definitions

Returns: - the response string

func NewEditor

func NewEditor(options NewEditorOptions) (*editor, error)

Types

type BlockDefinition

type BlockDefinition struct {
	// Icon is the icon of the block
	Icon hb.TagInterface

	// Type is the type of the block
	Type string

	// Fields are the fields for the paameters of the block which can be edited
	Fields []form.FieldInterface

	// AllowChildren determines if the block can have children
	AllowChildren bool

	// AllowedChildTypes is a list of block types that can be added as children
	AllowedChildTypes []string

	// Wrapper is a function that wraps the block's card in a custom tag
	// suitable for a column that need to apply its width so that the
	// card is displayed with the correct width
	Wrapper func(block ui.BlockInterface) *hb.Tag

	// ToTag is a function that converts a block to a tag to be displayed
	// in the block's card body in the editor
	ToTag func(block ui.BlockInterface) *hb.Tag
}

type FlatBlock

type FlatBlock struct {
	ID         string
	Type       string
	ParentID   string
	Sequence   int
	Parameters map[string]string
}

type FlatTree

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

func NewFlatTree

func NewFlatTree(blocks []ui.BlockInterface) *FlatTree

func (*FlatTree) Add

func (tree *FlatTree) Add(parentID string, flatBlock FlatBlock)

func (*FlatTree) AddBlock

func (tree *FlatTree) AddBlock(parentID string, block ui.BlockInterface)

AddBlock adds a new ui.BlockInterface to the FlatTree

func (*FlatTree) Children

func (tree *FlatTree) Children(parentID string) []FlatBlock

Children returns the children of the FlatBlock with the given parentID

func (*FlatTree) Clone

func (tree *FlatTree) Clone(flatBlock FlatBlock) FlatBlock

Clone creates a shallow clone of a FlatBlock (no children)

This is used to create a clone of a FlatBlock, so that the original FlatBlock is not modified, but we can modify the clone safely

Remember to update the ID, Sequence, and ParentID of the copy with new values

func (*FlatTree) Duplicate

func (tree *FlatTree) Duplicate(blockID string)

Duplicate creates a deep clone of a FlatBlock (with children) and adds it to the tree, under the same parent

Business Logic: - travserses the tree to find all blocks to be duplicated - makes a map with current IDs as keys, newly generated IDs as values - clones each block, and replaces the ID with the new ID - assignes the correct mapped IDs and ParentIDs - adds the cloned blocks to the tree directly (using list) - moves the duplicated block under the block being duplicated

func (*FlatTree) Exists

func (tree *FlatTree) Exists(flatBlockID string) bool

func (*FlatTree) Find

func (tree *FlatTree) Find(flatBlockID string) *FlatBlock

func (*FlatTree) FindNextSibling

func (tree *FlatTree) FindNextSibling(flatBlockID string) *FlatBlock

func (*FlatTree) FindPreviousSibling

func (tree *FlatTree) FindPreviousSibling(flatBlockID string) *FlatBlock

func (*FlatTree) List

func (tree *FlatTree) List() []FlatBlock

func (*FlatTree) MoveDown

func (tree *FlatTree) MoveDown(flatBlockID string)

func (*FlatTree) MoveToParent

func (tree *FlatTree) MoveToParent(flatBlockID, parentID string)

func (*FlatTree) MoveToPosition

func (tree *FlatTree) MoveToPosition(flatBlockID, parentID string, position int)

func (*FlatTree) MoveUp

func (tree *FlatTree) MoveUp(flatBlockID string)

func (*FlatTree) Parent

func (tree *FlatTree) Parent(flatBlockID string) *FlatBlock

func (*FlatTree) RecalculateSequences

func (tree *FlatTree) RecalculateSequences(blockID string)

func (*FlatTree) Remove

func (tree *FlatTree) Remove(flatBlockID string)

Remove removes the block with the given id

Buisiness Logic: - checks if the block exists, if not, do nothing - removes the block from the list - recalculates the sequences of the parent's children

func (*FlatTree) RemoveOrphans

func (tree *FlatTree) RemoveOrphans()

RemoveOrphans removes all orphaned blocks that have no parent

Buisiness Logic: - finds and creates a new list without orphaned blocks - non orphaned blocks are the ones that have a parent or root blocks - updates the list with the new list

Parameters: - none

Returns: - none

func (*FlatTree) ToBlocks

func (tree *FlatTree) ToBlocks() []ui.BlockInterface

func (*FlatTree) Traverse

func (tree *FlatTree) Traverse(blockID string) []FlatBlock

func (*FlatTree) Update

func (tree *FlatTree) Update(flatBlock FlatBlock)

type NewEditorOptions

type NewEditorOptions struct {
	ID               string
	Name             string
	Value            string
	HandleEndpoint   string
	BlockDefinitions []BlockDefinition
}

Jump to

Keyboard shortcuts

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