Skip to content

Commit

Permalink
Merge pull request #463 from trheyi/main
Browse files Browse the repository at this point in the history
[add] Page Builer Component (30%)
  • Loading branch information
trheyi authored Sep 25, 2023
2 parents d61cb8d + 5d6c7d3 commit 3c6fda1
Show file tree
Hide file tree
Showing 12 changed files with 598 additions and 323 deletions.
58 changes: 58 additions & 0 deletions sui/core/component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package core

import (
"fmt"
"strings"

"github.com/evanw/esbuild/pkg/api"
"github.com/yaoapp/gou/runtime/transform"
)

// Compile compile the component
func (component *Component) Compile() (string, error) {

// Typescript is the default language
// Typescript
if component.Codes.TS.Code != "" {
varName := strings.Replace(component.ID, "-", "_", -1)
ts := strings.Replace(component.Codes.TS.Code, "export default", fmt.Sprintf("window.component__%s =", varName), 1)
if component.Codes.HTML.Code != "" && !strings.Contains(component.Codes.TS.Code, "content:") {
html := strings.ReplaceAll(component.Codes.HTML.Code, "`", "\\`")
ts = strings.Replace(ts, "defaults: {", "defaults: {\n components: `"+html+"`,", 1)

}

js, err := transform.TypeScript(ts, api.TransformOptions{
Target: api.ESNext,
MinifyWhitespace: true,
MinifyIdentifiers: true,
MinifySyntax: true,
})

if err != nil {
return "", err
}
component.Compiled = js
return js, nil
}

// Javascript
if component.Codes.JS.Code == "" {
return "", fmt.Errorf("Block %s has no JS code", component.ID)
}

varName := strings.Replace(component.ID, "-", "_", -1)
js := strings.Replace(component.Codes.JS.Code, "export default", fmt.Sprintf("window.component__%s =", varName), 1)
if component.Codes.HTML.Code != "" && !strings.Contains(component.Codes.JS.Code, "content:") {
html := strings.ReplaceAll(component.Codes.HTML.Code, "`", "\\`")
js = strings.Replace(js, "defaults: {", "defaults: {\n components: `"+html+"`,", 1)
}

minified, err := transform.MinifyJS(js)
if err != nil {
return "", err
}

component.Compiled = minified
return minified, nil
}
6 changes: 4 additions & 2 deletions sui/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ type Page struct {

// Component is the struct for the component
type Component struct {
templage *Template
name string
ID string `json:"id"`
Name string `json:"name,omitempty"`
Compiled string `json:"-"`
Codes SourceCodes `json:"-"`
}

// Block is the struct for the block
Expand Down
94 changes: 94 additions & 0 deletions sui/storages/local/block.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,69 @@
package local

import (
"fmt"
"path/filepath"

"github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/sui/core"
)

// Blocks get the blocks
func (tmpl *Template) Blocks() ([]core.IBlock, error) {
path := filepath.Join(tmpl.Root, "__blocks")

blocks := []core.IBlock{}
if exist, _ := tmpl.local.fs.Exists(path); !exist {
return blocks, nil
}

dirs, err := tmpl.local.fs.ReadDir(path, false)
if err != nil {
return nil, err
}

for _, dir := range dirs {
if !tmpl.local.fs.IsDir(dir) {
continue
}

block, err := tmpl.getBlockFrom(dir)
if err != nil {
log.Error("Get block error: %v", err)
continue
}

blocks = append(blocks, block)
}

return blocks, nil
}

// Block get the block
func (tmpl *Template) Block(id string) (core.IBlock, error) {
path := filepath.Join(tmpl.Root, "__blocks", id)
if exist, _ := tmpl.local.fs.Exists(path); !exist {
return nil, fmt.Errorf("Block %s not found", id)
}

block, err := tmpl.getBlockFrom(path)
if err != nil {
return nil, err
}

err = block.Load()
if err != nil {
return nil, err
}

_, err = block.Compile()
if err != nil {
return nil, err
}

return block, nil
}

// Load get the block from the storage
func (block *Block) Load() error {

Expand Down Expand Up @@ -38,3 +98,37 @@ func (block *Block) Load() error {

return nil
}

func (tmpl *Template) getBlockFrom(path string) (core.IBlock, error) {
id := tmpl.getBlockID(path)
return tmpl.getBlock(id)
}

func (tmpl *Template) getBlock(id string) (core.IBlock, error) {

path := filepath.Join(tmpl.Root, "__blocks", id)
if !tmpl.local.fs.IsDir(path) {
return nil, fmt.Errorf("Block %s not found", id)
}

jsFile := filepath.Join("/", id, fmt.Sprintf("%s.js", id))
tsFile := filepath.Join("/", id, fmt.Sprintf("%s.ts", id))
htmlFile := filepath.Join("/", id, fmt.Sprintf("%s.html", id))
block := &Block{
tmpl: tmpl,
Block: &core.Block{
ID: id,
Codes: core.SourceCodes{
HTML: core.Source{File: htmlFile},
JS: core.Source{File: jsFile},
TS: core.Source{File: tsFile},
},
},
}

return block, nil
}

func (tmpl *Template) getBlockID(path string) string {
return filepath.Base(path)
}
83 changes: 83 additions & 0 deletions sui/storages/local/block_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package local

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestTemplateBlocks(t *testing.T) {
tests := prepare(t)
defer clean()

tmpl, err := tests.Demo.GetTemplate("tech-blue")
if err != nil {
t.Fatalf("GetTemplate error: %v", err)
}

blocks, err := tmpl.Blocks()
if err != nil {
t.Fatalf("Blocks error: %v", err)
}

if len(blocks) < 3 {
t.Fatalf("Blocks error: %v", len(blocks))
}

assert.Equal(t, "ColumnsTwo", blocks[0].(*Block).ID)
assert.Equal(t, "/ColumnsTwo/ColumnsTwo.html", blocks[0].(*Block).Codes.HTML.File)
assert.Equal(t, "/ColumnsTwo/ColumnsTwo.js", blocks[0].(*Block).Codes.JS.File)
assert.Equal(t, "/ColumnsTwo/ColumnsTwo.ts", blocks[0].(*Block).Codes.TS.File)

assert.Equal(t, "Hero", blocks[1].(*Block).ID)
assert.Equal(t, "/Hero/Hero.html", blocks[1].(*Block).Codes.HTML.File)
assert.Equal(t, "/Hero/Hero.js", blocks[1].(*Block).Codes.JS.File)
assert.Equal(t, "/Hero/Hero.ts", blocks[1].(*Block).Codes.TS.File)

assert.Equal(t, "Section", blocks[2].(*Block).ID)
assert.Equal(t, "/Section/Section.html", blocks[2].(*Block).Codes.HTML.File)
assert.Equal(t, "/Section/Section.js", blocks[2].(*Block).Codes.JS.File)
assert.Equal(t, "/Section/Section.ts", blocks[2].(*Block).Codes.TS.File)
}

func TestTemplateBlockJS(t *testing.T) {
tests := prepare(t)
defer clean()

tmpl, err := tests.Demo.GetTemplate("tech-blue")
if err != nil {
t.Fatalf("GetTemplate error: %v", err)
}

block, err := tmpl.Block("ColumnsTwo")
if err != nil {
t.Fatalf("Blocks error: %v", err)
}

assert.Equal(t, "ColumnsTwo", block.(*Block).ID)
assert.NotEmpty(t, block.(*Block).Codes.HTML.Code)
assert.NotEmpty(t, block.(*Block).Codes.JS.Code)
assert.Contains(t, block.(*Block).Compiled, "window.block__ColumnsTwo")
assert.Contains(t, block.(*Block).Compiled, `<div class="columns-two-left"`)
}

func TestTemplateBlockTS(t *testing.T) {
tests := prepare(t)
defer clean()

tmpl, err := tests.Demo.GetTemplate("tech-blue")
if err != nil {
t.Fatalf("GetTemplate error: %v", err)
}

block, err := tmpl.Block("Hero")
if err != nil {
t.Fatalf("Blocks error: %v", err)
}

assert.Equal(t, "Hero", block.(*Block).ID)
assert.Empty(t, block.(*Block).Codes.HTML.Code)
assert.NotEmpty(t, block.(*Block).Codes.TS.Code)
assert.Contains(t, block.(*Block).Compiled, "window.block__Hero")
assert.Contains(t, block.(*Block).Compiled, `<div data-gjs-type='Nav'></div>`)
}
Loading

0 comments on commit 3c6fda1

Please sign in to comment.