From f06bbb244794f021225125d9cc4e00bf98face13 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 22 Nov 2023 10:37:46 +0800 Subject: [PATCH] [add] sui preview and editor page render --- sui/core/editor.go | 115 +++++++++++-------------- sui/core/preview.go | 13 ++- sui/core/render.go | 5 +- sui/core/render_test.go | 2 +- sui/storages/local/page_render_test.go | 6 +- 5 files changed, 68 insertions(+), 73 deletions(-) diff --git a/sui/core/editor.go b/sui/core/editor.go index c9d41615a6..b905642bf6 100644 --- a/sui/core/editor.go +++ b/sui/core/editor.go @@ -1,15 +1,12 @@ package core import ( - "path/filepath" - "github.com/PuerkitoBio/goquery" ) // EditorRender render HTML for the editor func (page *Page) EditorRender(request *Request) (*ResponseEditorRender, error) { - html := page.Codes.HTML.Code res := &ResponseEditorRender{ HTML: "", CSS: page.Codes.CSS.Code, @@ -35,87 +32,77 @@ func (page *Page) EditorRender(request *Request) (*ResponseEditorRender, error) } res.Styles = append(res.Styles, styles...) - // Page Styles - if page.Codes.CSS.Code != "" { - res.Styles = append(res.Styles, filepath.Join("@pages", page.Route, page.Name+".css")) - } + // // Page Styles + // if page.Codes.CSS.Code != "" { + // res.Styles = append(res.Styles, filepath.Join("@pages", page.Route, page.Name+".css")) + // } - // Render the HTML with the data - // Page Scripts - if page.Codes.JS.Code != "" { - res.Scripts = append(res.Scripts, filepath.Join("@pages", page.Route, page.Name+".js")) - } - if page.Codes.TS.Code != "" { - res.Scripts = append(res.Scripts, filepath.Join("@pages", page.Route, page.Name+".ts")) - } + // // Render the HTML with the data + // // Page Scripts + // if page.Codes.JS.Code != "" { + // res.Scripts = append(res.Scripts, filepath.Join("@pages", page.Route, page.Name+".js")) + // } + // if page.Codes.TS.Code != "" { + // res.Scripts = append(res.Scripts, filepath.Join("@pages", page.Route, page.Name+".ts")) + // } // Render tools - res.Scripts = append(res.Scripts, filepath.Join("@assets", "__render.js")) - res.Styles = append(res.Styles, filepath.Join("@assets", "__render.css")) - - // doc, _, err := page.Build(&BuildOption{ - // SSR: true, - // AssetRoot: request.AssetRoot, - // }) - - // doc.Selection.Find("body").AppendHtml(` - // - // `) - - // html, err = doc.Html() - // if err != nil { - // return nil, err - // } + // res.Scripts = append(res.Scripts, filepath.Join("@assets", "__render.js")) + // res.Styles = append(res.Styles, filepath.Join("@assets", "__render.css")) - // fmt.Println(html) + doc, warnings, err := page.Build(&BuildOption{ + SSR: true, + AssetRoot: request.AssetRoot, + }) - data, setting, err := page.Data(request) if err != nil { res.Warnings = append(res.Warnings, err.Error()) } - res.Setting = setting - if data == nil { - res.HTML = html - return res, nil + if warnings != nil { + res.Warnings = append(res.Warnings, warnings...) } - if html != "" { - html, err := page.Render(html, data, res.Warnings) + res.HTML, err = doc.Html() + if err != nil { + return nil, err + } + + var data Data = nil + if page.Codes.DATA.Code != "" { + data, err = page.Exec(request) if err != nil { res.Warnings = append(res.Warnings, err.Error()) } - res.HTML = html } + res.Render(data) return res, nil } // Render render for the html -func (page *Page) Render(html string, data map[string]interface{}, warnings []string) (string, error) { - return html, nil +func (res *ResponseEditorRender) Render(data map[string]interface{}) error { + if res.HTML == "" { + return nil + } + + if data == nil || len(data) == 0 { + return nil + } + + var err error + parser := NewTemplateParser(data, nil) + res.HTML, err = parser.Render(res.HTML) + if err != nil { + return err + } + + if len(parser.errors) > 0 { + for _, err := range parser.errors { + res.Warnings = append(res.Warnings, err.Error()) + } + } + return nil } // EditorPageSource get the editor page source code diff --git a/sui/core/preview.go b/sui/core/preview.go index 7172deaf24..c457e5457c 100644 --- a/sui/core/preview.go +++ b/sui/core/preview.go @@ -60,10 +60,17 @@ func (page *Page) PreviewRender(request *Request) (string, error) { return "", err } - html, err = page.Render(html, data, warnings) + parser := NewTemplateParser(data, nil) + html, err = parser.Render(html) if err != nil { - warnings = append(warnings, err.Error()) + return "", err } - return doc.Html() + // Warnings should be added after rendering + if len(parser.errors) > 0 { + for _, err := range parser.errors { + warnings = append(warnings, err.Error()) + } + } + return html, nil } diff --git a/sui/core/render.go b/sui/core/render.go index 22b94987a5..be38d2a0f1 100644 --- a/sui/core/render.go +++ b/sui/core/render.go @@ -25,8 +25,11 @@ type Mapping struct { Value interface{} `json:"value,omitempty"` } +// ParserOption parser option +type ParserOption struct{} + // NewTemplateParser create a new template parser -func NewTemplateParser(data Data) *TemplateParser { +func NewTemplateParser(data Data, option *ParserOption) *TemplateParser { return &TemplateParser{ data: data, mapping: map[string]Mapping{}, diff --git a/sui/core/render_test.go b/sui/core/render_test.go index caf0a4986f..61279cecd5 100644 --- a/sui/core/render_test.go +++ b/sui/core/render_test.go @@ -23,7 +23,7 @@ func TestRender(t *testing.T) { } assert.NotEmpty(t, data) - parser := NewTemplateParser(data) + parser := NewTemplateParser(data, nil) html, err := parser.Render(page.Codes.HTML.Code) if err != nil { t.Fatalf("Render error: %v", err) diff --git a/sui/storages/local/page_render_test.go b/sui/storages/local/page_render_test.go index 37ed808550..8ba566af74 100644 --- a/sui/storages/local/page_render_test.go +++ b/sui/storages/local/page_render_test.go @@ -33,19 +33,17 @@ func TestPageEditorRender(t *testing.T) { assert.NotEmpty(t, res.CSS) assert.NotEmpty(t, res.Scripts) assert.NotEmpty(t, res.Styles) - assert.Equal(t, 5, len(res.Scripts)) - assert.Equal(t, 6, len(res.Styles)) + assert.Equal(t, 3, len(res.Scripts)) + assert.Equal(t, 4, len(res.Styles)) assert.Equal(t, "@assets/libs/tiny-slider/min/tiny-slider.js", res.Scripts[0]) assert.Equal(t, "@assets/libs/feather-icons/feather.min.js", res.Scripts[1]) assert.Equal(t, "@assets/js/plugins.init.js", res.Scripts[2]) - assert.Equal(t, "@pages/index/index.js", res.Scripts[3]) assert.Equal(t, "@assets/libs/tiny-slider/tiny-slider.css", res.Styles[0]) assert.Equal(t, "@assets/libs/@iconscout/unicons/css/line.css", res.Styles[1]) assert.Equal(t, "@assets/libs/@mdi/font/css/materialdesignicons.min.css", res.Styles[2]) assert.Equal(t, "@assets/css/tailwind.css", res.Styles[3]) - assert.Equal(t, "@pages/index/index.css", res.Styles[4]) } func TestPagePreviewRender(t *testing.T) {