snippets

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: MPL-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package snippets manages the pre-written HTML blocks editors can insert into rich regions from the in-place editor's palette. Snippets come from two places: the host application's config (per-customer components, versioned with the code) and the database (created by admins in the admin UI). Once inserted, a snippet is ordinary region content — edited in place, sanitized on save, and styled entirely by the host site's CSS.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("snippets: not found")

ErrNotFound is returned when no snippet matches the query.

Functions

This section is empty.

Types

type Snippet

type Snippet struct {
	ID        int64
	Name      string
	Group     string
	HTML      string
	Settings  map[string]string
	CreatedAt time.Time
	UpdatedAt time.Time
}

Snippet is one insertable block. ID is set only for database-stored snippets; config-registered ones have ID zero.

A snippet with a non-nil Settings map is a *section preset*: instead of a block dropped into existing content, it is a starting point for a whole section — the editor offers it only in the "Add a section" chooser, and applies the settings (background, width, height, vertical alignment) to the new section along with the HTML. Keys and values are the section-settings vocabulary: "bg" and "width" name curated SectionStyles option keys, "height" is "50"/"75"/"100", "valign" is "center"/"bottom", "bgcolor" is #rrggbb, "bgimage" is a URL, and "bgposition" anchors that image as a pair of percentages across and down, e.g. "50% 20%" ("50% 50%" is centered, and is the default). Unknown keys or invalid values fall back to the defaults, same as the section settings dialog. Presets come from config or from the admin snippets UI (which offers the curated settings; the free-form bgcolor/bgimage/bgposition are config-only). Group names the category the editor's drawer files the snippet under: the drawer offers a category dropdown when any loaded snippet carries one. Grouping is config-only (like render.EditorStyle.Group) — admin- created snippets and ungrouped config snippets appear under "Custom".

func DefaultSectionPresets

func DefaultSectionPresets() []Snippet

DefaultSectionPresets is the Tailwind-first default library of section presets — snippets with Settings, offered as one-click starting points in the "Add a section" chooser. Two markup strategies, chosen per preset: markup that should adapt when the editor later changes the section background skips not-prose and leans on prose/prose-invert for its colors (Hero, CTA, FAQ); grid layouts that Typography would restyle use not-prose with explicit slate colors and suit light backgrounds (Feature grid, Stats, Testimonials). Like DefaultSnippets, every class must be safelisted in the site's Tailwind build (see the README).

func DefaultSnippets

func DefaultSnippets() []Snippet

DefaultSnippets is the Tailwind-first default library, used when the host does not configure its own. The markup avoids elements the editor sanitizer strips (no SVG, no scripts) and uses `not-prose` so Tailwind Typography doesn't restyle component internals. Like the editor styles, every class here must be safelisted in the site's Tailwind build (see the README). The cms-snippet marker class on each snippet's root makes the block manageable in the in-place editor: a dotted outline while editing, and a floating drag-handle/trash chrome when clicked. Like cms-btn (which marks a link as an editable button), it carries no CSS of its own.

func LibrarySectionPresets added in v0.9.3

func LibrarySectionPresets() []Snippet

LibrarySectionPresets returns the imported section presets. Like DefaultSectionPresets, each carries Settings so the editor offers it in the "Add a section" chooser. All use not-prose with explicit slate colors, so they suit light backgrounds.

func LibrarySnippets added in v0.9.3

func LibrarySnippets() []Snippet

LibrarySnippets returns the imported inline blocks. They ship with the defaults when Config.Snippets is nil.

type Store

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

Store reads and writes admin-created snippets in Postgres.

func NewStore

func NewStore(db *sqldb.DB) *Store

NewStore returns a Store backed by db.

func (*Store) All

func (s *Store) All(ctx context.Context) ([]Snippet, error)

All returns every stored snippet, ordered by name.

func (*Store) Count

func (s *Store) Count(ctx context.Context) (int, error)

Count returns how many stored snippets exist — the admin adds the host-registered ones and shows the total beside its Snippets nav entry.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, id int64) error

Delete removes a stored snippet. Content already inserted into pages is unaffected — inserted snippets are plain region HTML.

func (*Store) GetByID

func (s *Store) GetByID(ctx context.Context, id int64) (*Snippet, error)

GetByID returns one stored snippet, or ErrNotFound.

func (*Store) Insert

func (s *Store) Insert(ctx context.Context, sn *Snippet) (int64, error)

Insert stores a new snippet and returns its id. A nil Settings map is stored as NULL (plain block); a non-nil one makes it a section preset.

func (*Store) Update

func (s *Store) Update(ctx context.Context, sn *Snippet) error

Update saves a stored snippet's name, HTML, and settings.

Jump to

Keyboard shortcuts

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