diff --git a/sui/core/interfaces.go b/sui/core/interfaces.go index 511eb251..7c0561c7 100644 --- a/sui/core/interfaces.go +++ b/sui/core/interfaces.go @@ -2,20 +2,18 @@ package core // ITemplate is the interface for the ITemplate type ITemplate interface { - Get() error - Save() error - Pages() ([]IPage, error) - Blocks() ([]IBlock, error) - Components() ([]IComponent, error) - Page(route string) (IPage, error) + + Blocks() ([]IBlock, error) Block(name string) (IBlock, error) + + Components() ([]IComponent, error) Component(name string) (IComponent, error) - Styles() []string - Locales() []string - Themes() []string + Assets() []string + Locales() []SelectOption + Themes() []SelectOption } // IPage is the interface for the page diff --git a/sui/core/types.go b/sui/core/types.go index b1e641a4..8f01cce1 100644 --- a/sui/core/types.go +++ b/sui/core/types.go @@ -41,11 +41,24 @@ type Block struct { // Template is the struct for the template type Template struct { - Version int `json:"version"` // Yao Builder version - ID string `json:"id"` - Name string `json:"name"` - Descrption string `json:"description"` - Screenshots []string `json:"screenshots"` + Version int `json:"version"` // Yao Builder version + ID string `json:"id"` + Name string `json:"name"` + Descrption string `json:"description"` + Screenshots []string `json:"screenshots"` + Themes []SelectOption `json:"themes"` +} + +// Theme is the struct for the theme +type Theme struct { + ID string `json:"id"` + Name string `json:"name,omitempty"` +} + +// SelectOption is the struct for the select option +type SelectOption struct { + Label string `json:"label"` + Value string `json:"value"` } // SourceCodes is the struct for the page codes diff --git a/sui/storages/local/local.go b/sui/storages/local/local.go index 591de96d..e593d6d4 100644 --- a/sui/storages/local/local.go +++ b/sui/storages/local/local.go @@ -113,6 +113,7 @@ func (local *Local) getTemplate(id string, path string) (*Template, error) { Name: strings.ToUpper(id), Version: 1, Screenshots: []string{}, + Themes: []core.SelectOption{}, }} // load the template.json diff --git a/sui/storages/local/template.go b/sui/storages/local/template.go index b8323a98..bf5f5d7a 100644 --- a/sui/storages/local/template.go +++ b/sui/storages/local/template.go @@ -1,26 +1,44 @@ package local -// Get get the template -func (tmpl *Template) Get() error { - return nil -} +import ( + "path/filepath" -// Save save the template -func (tmpl *Template) Save() error { - return nil -} + "github.com/yaoapp/yao/sui/core" + "golang.org/x/text/language" +) -// Styles get the global styles -func (tmpl *Template) Styles() []string { +// Assets get the assets treelist +func (tmpl *Template) Assets() []string { return nil } // Locales get the global locales -func (tmpl *Template) Locales() []string { - return nil +func (tmpl *Template) Locales() []core.SelectOption { + + supportLocales := []core.SelectOption{} + path := filepath.Join(tmpl.Root, "__locales") + if !tmpl.local.fs.IsDir(path) { + return nil + } + + dirs, err := tmpl.local.fs.ReadDir(path, false) + if err != nil { + return nil + } + + for _, dir := range dirs { + locale := filepath.Base(dir) + label := language.Make(locale).String() + supportLocales = append(supportLocales, core.SelectOption{ + Value: locale, + Label: label, + }) + } + + return supportLocales } // Themes get the global themes -func (tmpl *Template) Themes() []string { - return nil +func (tmpl *Template) Themes() []core.SelectOption { + return tmpl.Template.Themes } diff --git a/sui/storages/local/template_test.go b/sui/storages/local/template_test.go index 469c3dc0..0432452e 100644 --- a/sui/storages/local/template_test.go +++ b/sui/storages/local/template_test.go @@ -1 +1,51 @@ package local + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestTemplateThemes(t *testing.T) { + tests := prepare(t) + defer clean() + + tmpl, err := tests.Demo.GetTemplate("tech-blue") + if err != nil { + t.Fatalf("GetTemplate error: %v", err) + } + + themes := tmpl.Themes() + if len(themes) < 2 { + t.Fatalf("Themes error: %v", len(themes)) + } + + assert.Equal(t, "dark", themes[0].Value) + assert.Equal(t, "暗色主题", themes[0].Label) + assert.Equal(t, "light", themes[1].Value) + assert.Equal(t, "明亮主题", themes[1].Label) +} + +func TestTemplateLocales(t *testing.T) { + tests := prepare(t) + defer clean() + + tmpl, err := tests.Demo.GetTemplate("tech-blue") + if err != nil { + t.Fatalf("GetTemplate error: %v", err) + } + + locales := tmpl.Locales() + if len(locales) < 3 { + t.Fatalf("Locales error: %v", len(locales)) + } + + assert.Equal(t, "ar", locales[0].Label) + assert.Equal(t, "ar", locales[0].Value) + + assert.Equal(t, "zh-CN", locales[1].Label) + assert.Equal(t, "zh-cn", locales[1].Value) + + assert.Equal(t, "zh-TW", locales[2].Label) + assert.Equal(t, "zh-tw", locales[2].Value) +}