Skip to content

Commit

Permalink
Merge pull request #507 from trheyi/main
Browse files Browse the repository at this point in the history
[add] sui editor render with mock request and global data support
  • Loading branch information
trheyi authored Nov 24, 2023
2 parents ecd67e9 + 26b03fe commit 5ab5361
Show file tree
Hide file tree
Showing 15 changed files with 360 additions and 79 deletions.
11 changes: 1 addition & 10 deletions sui/api/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,16 +655,7 @@ func EditorRender(process *process.Process) interface{} {
exception.New(err.Error(), 500).Throw()
}

// Request data
urlQuery := url.Values{}
if process.NumOfArgs() > 3 {
if v, ok := process.Args[3].(url.Values); ok {
urlQuery = v
}
}

req := &core.Request{Method: "GET", Query: urlQuery}
res, err := page.EditorRender(req)
res, err := page.EditorRender()
if err != nil {
exception.New(err.Error(), 500).Throw()
}
Expand Down
17 changes: 16 additions & 1 deletion sui/api/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,20 @@ func (r *Request) Render() (string, int, error) {
dataSel.Remove()
}

globalDataText := ""
globalDataSel := doc.Find("script[name=global]")
if globalDataSel != nil && globalDataSel.Length() > 0 {
globalDataText = globalDataSel.Text()
globalDataSel.Remove()
}

html, err := doc.Html()
if err != nil {
return "", 500, fmt.Errorf("parse error, please re-complie the page %s", err.Error())
}

// Save to The Cache
c = core.SetCache(r.File, html, dataText)
c = core.SetCache(r.File, html, dataText, globalDataText)
log.Trace("The page %s is cached", r.File)
}

Expand All @@ -88,6 +95,14 @@ func (r *Request) Render() (string, int, error) {
}
}

if c.Global != "" {
global, err := r.Request.ExecString(c.Global)
if err != nil {
return "", 500, fmt.Errorf("global data error, please re-complie the page %s", err.Error())
}
data["$global"] = global
}

parser := core.NewTemplateParser(data, nil)
html, err := parser.Render(c.HTML)
if err != nil {
Expand Down
31 changes: 21 additions & 10 deletions sui/core/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func (page *Page) Build(option *BuildOption) (*goquery.Document, []string, error) {

warnings := []string{}
html, err := page.BuildHTML(option.AssetRoot)
html, err := page.BuildHTML(option)
if err != nil {
warnings = append(warnings, err.Error())
}
Expand All @@ -23,14 +23,14 @@ func (page *Page) Build(option *BuildOption) (*goquery.Document, []string, error
}

// Add Style
style, err := page.BuildStyle(option.AssetRoot)
style, err := page.BuildStyle(option)
if err != nil {
warnings = append(warnings, err.Error())
}
doc.Selection.Find("head").AppendHtml(style)

// Add Script
script, err := page.BuildScript(option.AssetRoot)
script, err := page.BuildScript(option)
if err != nil {
warnings = append(warnings, err.Error())
}
Expand All @@ -39,15 +39,18 @@ func (page *Page) Build(option *BuildOption) (*goquery.Document, []string, error
}

// BuildHTML build the html
func (page *Page) BuildHTML(assetRoot string) (string, error) {
func (page *Page) BuildHTML(option *BuildOption) (string, error) {

html := string(page.Document)
if page.Codes.HTML.Code != "" {
html = strings.Replace(html, "{{ __page }}", page.Codes.HTML.Code, 1)
}

code := strings.ReplaceAll(html, "@assets", assetRoot)
res, err := page.CompileHTML([]byte(code), false)
if !option.IgnoreAssetRoot {
html = strings.ReplaceAll(html, "@assets", option.AssetRoot)
}

res, err := page.CompileHTML([]byte(html), false)
if err != nil {
return "", err
}
Expand All @@ -56,12 +59,16 @@ func (page *Page) BuildHTML(assetRoot string) (string, error) {
}

// BuildStyle build the style
func (page *Page) BuildStyle(assetRoot string) (string, error) {
func (page *Page) BuildStyle(option *BuildOption) (string, error) {
if page.Codes.CSS.Code == "" {
return "", nil
}

code := strings.ReplaceAll(page.Codes.CSS.Code, "@assets", assetRoot)
code := page.Codes.CSS.Code
if !option.IgnoreAssetRoot {
code = strings.ReplaceAll(page.Codes.CSS.Code, "@assets", option.AssetRoot)
}

res, err := page.CompileCSS([]byte(code), false)
if err != nil {
return "", err
Expand All @@ -71,7 +78,7 @@ func (page *Page) BuildStyle(assetRoot string) (string, error) {
}

// BuildScript build the script
func (page *Page) BuildScript(assetRoot string) (string, error) {
func (page *Page) BuildScript(option *BuildOption) (string, error) {

if page.Codes.JS.Code == "" && page.Codes.TS.Code == "" {
return "", nil
Expand All @@ -86,7 +93,11 @@ func (page *Page) BuildScript(assetRoot string) (string, error) {
return fmt.Sprintf("<script>\n%s\n</script>\n", res), nil
}

code := strings.ReplaceAll(page.Codes.JS.Code, "@assets", assetRoot)
code := page.Codes.JS.Code
if !option.IgnoreAssetRoot {
code = strings.ReplaceAll(page.Codes.JS.Code, "@assets", option.AssetRoot)
}

res, err := page.CompileJS([]byte(code), false)
if err != nil {
return "", err
Expand Down
19 changes: 12 additions & 7 deletions sui/core/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@ func (page *Page) Compile(option *BuildOption) (string, error) {
}
}

// Page Data
if page.Codes.DATA.Code != "" {
doc.Find("body").AppendHtml("\n\n" + `<script name="data" type="json">` + "\n" +
page.Codes.DATA.Code +
"\n</script>\n\n",
)
}

// // add the route data
// doc.Find("body").AppendHtml(`<script name="route" type="json">` + "\n" +
// fmt.Sprintf(
// `{"sui": "%s", "template": "%s", "route": "%s"}`,
// page.SuiID, page.TemplateID, page.Route,
// ) +
// "\n</script>\n")
// Page Global Data
if page.GlobalData != nil && len(page.GlobalData) > 0 {
doc.Find("body").AppendHtml("\n\n" + `<script name="global" type="json">` + "\n" +
string(page.GlobalData) +
"\n</script>\n\n",
)
}

// Replace the document
page.Config = page.GetConfig()
page.ReplaceDocument(doc)

html, err := doc.Html()
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions sui/core/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/antonmedv/expr/ast"
"github.com/antonmedv/expr/vm"
"github.com/yaoapp/gou/process"
"github.com/yaoapp/kun/log"
)

var stmtRe = regexp.MustCompile(`\{\{([^}]+)\}\}`)
Expand Down Expand Up @@ -56,6 +57,20 @@ func (data Data) ExecString(stmt string) (string, error) {
return fmt.Sprintf("%v", res), nil
}

// Replace replace the statement
func (data Data) Replace(value string) (string, bool) {
hasStmt := false
res := stmtRe.ReplaceAllStringFunc(value, func(stmt string) string {
hasStmt = true
res, err := data.ExecString(stmt)
if err != nil {
log.Warn("Replace %s: %s", stmt, err)
}
return res
})
return res, hasStmt
}

func _process(args ...any) (interface{}, error) {

if len(args) < 1 {
Expand Down
32 changes: 15 additions & 17 deletions sui/core/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

// EditorRender render HTML for the editor
func (page *Page) EditorRender(request *Request) (*ResponseEditorRender, error) {
func (page *Page) EditorRender() (*ResponseEditorRender, error) {

res := &ResponseEditorRender{
HTML: "",
Expand All @@ -32,27 +32,16 @@ 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 the page
request := NewRequestMock(page.Config.Mock)

// 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,
SSR: true,
IgnoreAssetRoot: true,
})

if err != nil {
Expand All @@ -77,6 +66,13 @@ func (page *Page) EditorRender(request *Request) (*ResponseEditorRender, error)
}

res.Render(data)

// Set the title
res.Config.Rendered = &PageConfigRendered{
Title: page.RenderTitle(data),
Link: page.Link(request),
}

return res, nil
}

Expand All @@ -91,7 +87,8 @@ func (res *ResponseEditorRender) Render(data map[string]interface{}) error {
}

var err error
parser := NewTemplateParser(data, nil)
parser := NewTemplateParser(data, &ParserOption{Editor: true})

res.HTML, err = parser.Render(res.HTML)
if err != nil {
return err
Expand All @@ -102,6 +99,7 @@ func (res *ResponseEditorRender) Render(data map[string]interface{}) error {
res.Warnings = append(res.Warnings, err.Error())
}
}

return nil
}

Expand Down
3 changes: 2 additions & 1 deletion sui/core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type SUI interface {
WithSid(sid string)
PublicRootMatcher() *Matcher
GetPublic() *Public
PublicRootWithSid(sid string) (string, error)
}

// ITemplate is the interface for the ITemplate
Expand Down Expand Up @@ -70,7 +71,7 @@ type IPage interface {
SaveTemp(request *RequestSource) error
Remove() error

EditorRender(request *Request) (*ResponseEditorRender, error)
EditorRender() (*ResponseEditorRender, error)
EditorPageSource() SourceData
EditorScriptSource() SourceData
EditorStyleSource() SourceData
Expand Down
Loading

0 comments on commit 5ab5361

Please sign in to comment.