44 lines
1.4 KiB
Go
44 lines
1.4 KiB
Go
package plugin
|
|
|
|
import (
|
|
"io/fs"
|
|
"strings"
|
|
|
|
"git.dev.alexdunmow.com/block/core/blocks"
|
|
)
|
|
|
|
// PluginBlockRegistry wraps a BlockRegistry to auto-prefix block keys with the plugin name.
|
|
type PluginBlockRegistry struct {
|
|
inner blocks.BlockRegistry
|
|
prefix string
|
|
}
|
|
|
|
// NewPluginBlockRegistry creates a block registry that auto-prefixes keys.
|
|
func NewPluginBlockRegistry(inner blocks.BlockRegistry, pluginName string) *PluginBlockRegistry {
|
|
return &PluginBlockRegistry{inner: inner, prefix: pluginName}
|
|
}
|
|
|
|
func (r *PluginBlockRegistry) Register(meta blocks.BlockMeta, fn blocks.BlockFunc) {
|
|
if !strings.Contains(meta.Key, ":") {
|
|
meta.Key = r.prefix + ":" + meta.Key
|
|
}
|
|
meta.Source = r.prefix
|
|
r.inner.Register(meta, fn)
|
|
}
|
|
|
|
func (r *PluginBlockRegistry) RegisterTemplateOverride(templateKey, blockKey string, fn blocks.BlockFunc) {
|
|
r.inner.RegisterTemplateOverrideWithSource(templateKey, blockKey, r.prefix, fn)
|
|
}
|
|
|
|
func (r *PluginBlockRegistry) RegisterTemplateOverrideWithSource(templateKey, blockKey, source string, fn blocks.BlockFunc) {
|
|
r.inner.RegisterTemplateOverrideWithSource(templateKey, blockKey, source, fn)
|
|
}
|
|
|
|
func (r *PluginBlockRegistry) LoadSchemasFromFS(fsys fs.FS) error {
|
|
return r.inner.LoadSchemasFromFSWithPrefix(fsys, r.prefix)
|
|
}
|
|
|
|
func (r *PluginBlockRegistry) LoadSchemasFromFSWithPrefix(fsys fs.FS, prefix string) error {
|
|
return r.inner.LoadSchemasFromFSWithPrefix(fsys, prefix)
|
|
}
|