Skip to content

Commit

Permalink
Merge pull request #525 from trheyi/main
Browse files Browse the repository at this point in the history
[add] sui preview render api and fix page build bug
  • Loading branch information
trheyi authored Dec 13, 2023
2 parents f66b5ec + a80393d commit 0318a2e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion sui/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ var dsl = []byte(`
"guard": "query-jwt",
"method": "GET",
"process": "sui.Preview.Render",
"in": ["$param.id", "$param.template_id", "$param.route", "$header.Referer", "$query.r", "$query.t"],
"in": ["$param.id", "$param.template_id", "$param.route", "$header.Referer"],
"out": {"status": 200, "type": "text/html; charset=utf-8"}
},
Expand Down
12 changes: 1 addition & 11 deletions sui/api/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,20 +833,10 @@ func PreviewRender(process *process.Process) interface{} {

process.ValidateArgNums(3)
sui := get(process)
id := process.ArgsString(0)
templateID := process.ArgsString(1)
route := route(process, 2)
referer := process.ArgsString(3, "")

// reqData := process.ArgsString(4)
// timestamp := process.Args[4].(*timestamp.Timestamp)

req := &core.Request{
Method: "GET",
AssetRoot: fmt.Sprintf("/api/__yao/sui/v1/%s/asset/%s/@assets", id, templateID),
Referer: referer,
}

tmpl, err := sui.GetTemplate(templateID)
if err != nil {
exception.New(err.Error(), 500).Throw()
Expand All @@ -858,7 +848,7 @@ func PreviewRender(process *process.Process) interface{} {
}

// Request data
html, err := page.PreviewRender(req)
html, err := page.PreviewRender(referer)
if err != nil {
exception.New(err.Error(), 500).Throw()
}
Expand Down
2 changes: 1 addition & 1 deletion sui/core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ type IPage interface {
EditorStyleSource() SourceData
EditorDataSource() SourceData

PreviewRender(request *Request) (string, error)
PreviewRender(referer string) (string, error)

AssetScript() (*Asset, error)
AssetStyle() (*Asset, error)
Expand Down
29 changes: 22 additions & 7 deletions sui/core/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,31 @@ import (
)

// PreviewRender render HTML for the preview
func (page *Page) PreviewRender(request *Request) (string, error) {
func (page *Page) PreviewRender(referer string) (string, error) {

// get the page config
page.GetConfig()

// Render the page
request := NewRequestMock(page.Config.Mock)
if referer != "" {
request.Referer = referer
}

warnings := []string{}
doc, warnings, err := page.Build(&BuildOption{
SSR: true,
AssetRoot: request.AssetRoot,
SSR: true,
AssetRoot: fmt.Sprintf("/api/__yao/sui/v1/%s/asset/%s/@assets", page.SuiID, page.TemplateID),
KeepPageTag: false,
})

data, _, err := page.Data(request)
if err != nil {
warnings = append(warnings, err.Error())
// Get the data
var data Data = nil
if page.Codes.DATA.Code != "" {
data, err = page.Exec(request)
if err != nil {
warnings = append(warnings, err.Error())
}
}

// Add Frame Height
Expand Down Expand Up @@ -60,7 +74,8 @@ func (page *Page) PreviewRender(request *Request) (string, error) {
return "", err
}

parser := NewTemplateParser(data, nil)
// Parser and render
parser := NewTemplateParser(data, &ParserOption{Preview: true})
html, err = parser.Render(html)
if err != nil {
return "", err
Expand Down
7 changes: 6 additions & 1 deletion sui/storages/local/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ func (tmpl *Template) SyncAssets(option *core.BuildOption) error {
func (page *Page) Build(option *core.BuildOption) error {

if option.AssetRoot == "" {
option.AssetRoot = filepath.Join(page.tmpl.local.DSL.Public.Root, "assets")
root, err := page.tmpl.local.DSL.PublicRoot()
if err != nil {
log.Error("SyncAssets: Get the public root error: %s. use %s", err.Error(), page.tmpl.local.DSL.Public.Root)
root = page.tmpl.local.DSL.Public.Root
}
option.AssetRoot = filepath.Join(root, "assets")
}

log.Trace("Build the page %s AssetRoot: %s", page.Route, option.AssetRoot)
Expand Down
9 changes: 1 addition & 8 deletions sui/storages/local/page_render_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package local

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/yaoapp/yao/sui/core"
)

func TestPageEditorRender(t *testing.T) {
Expand Down Expand Up @@ -60,12 +58,7 @@ func TestPagePreviewRender(t *testing.T) {
t.Fatalf("Page error: %v", err)
}

r := &core.Request{
Method: "GET",
AssetRoot: fmt.Sprintf("/api/__yao/sui/v1/%s/asset/%s/@assets", page.Get().SuiID, page.Get().TemplateID),
}

html, err := page.PreviewRender(r)
html, err := page.PreviewRender("")
if err != nil {
t.Fatalf("PreviewRender error: %v", err)
}
Expand Down

0 comments on commit 0318a2e

Please sign in to comment.