Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[add] sui preview and editor page render #501

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 51 additions & 64 deletions sui/core/editor.go
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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(`
// <script>
// console.log("setIframeHeight window.onload: setIframeHeight");
// function setIframeHeight(height) {
// window.parent.postMessage(
// {
// messageType: "setIframeHeight",
// iframeHeight: height,
// },
// "` + request.Referer + `"
// );
// }

// window.onload = function () {
// console.log("window.onload: setIframeHeight");
// const contentHeight = document.documentElement.scrollHeight;
// console.log("window.onload: setIframeHeight", contentHeight);
// try {
// setIframeHeight(contentHeight + "px");
// } catch (err) {
// console.log(` + "`" + `setIframeHeight error: ${err}` + "`" + `);
// }
// };
// </script>
// `)

// 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
Expand Down
13 changes: 10 additions & 3 deletions sui/core/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 4 additions & 1 deletion sui/core/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down
2 changes: 1 addition & 1 deletion sui/core/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions sui/storages/local/page_render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down