[add] sui preview and editor page render
This commit is contained in:
parent
90f05a6ad9
commit
f06bbb2447
5 changed files with 70 additions and 75 deletions
|
|
@ -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"))
|
||||
}
|
||||
|
||||
// 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
|
||||
// // Page Styles
|
||||
// if page.Codes.CSS.Code != "" {
|
||||
// res.Styles = append(res.Styles, filepath.Join("@pages", page.Route, page.Name+".css"))
|
||||
// }
|
||||
|
||||
// fmt.Println(html)
|
||||
// // 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, 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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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{},
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue