Merge pull request #474 from trheyi/main

[add] page builder editor render api
This commit is contained in:
Max 2023-10-05 11:12:28 -05:00 committed by GitHub
commit 40af69d89f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 8 deletions

View file

@ -114,13 +114,25 @@ var dsl = []byte(`
"path": "/:id/editor/render/:template_id/*route", "path": "/:id/editor/render/:template_id/*route",
"method": "GET", "method": "GET",
"process": "sui.Editor.Render", "process": "sui.Editor.Render",
"in": ["$param.id", "$param.template_id", "$param.route"], "in": ["$param.id", "$param.template_id", "$param.route", ":query"],
"out": { "status": 200, "type": "application/json" }
},{
"path": "/:id/editor/render/:template_id/*route",
"method": "POST",
"process": "sui.Editor.RenderAfterSaveTemp",
"in": ["$param.id", "$param.template_id", "$param.route", ":context", ":query"],
"out": { "status": 200, "type": "application/json" } "out": { "status": 200, "type": "application/json" }
},{ },{
"path": "/:id/editor/:kind/source/:template_id/*route", "path": "/:id/editor/:kind/source/:template_id/*route",
"method": "GET", "method": "GET",
"process": "sui.Editor.Source", "process": "sui.Editor.Source",
"in": ["$param.id", "$param.template_id", "$param.route", "$param.kind"], "in": ["$param.id", "$param.template_id", "$param.route", "$param.kind", ":query"],
"out": { "status": 200, "type": "application/json" }
},{
"path": "/:id/editor/:kind/source/:template_id/*route",
"method": "POST",
"process": "sui.Editor.SourceAfterSaveTemp",
"in": ["$param.id", "$param.template_id", "$param.route", ":context", "$param.kind", ":query"],
"out": { "status": 200, "type": "application/json" } "out": { "status": 200, "type": "application/json" }
}, },

View file

@ -1,6 +1,8 @@
package api package api
import ( import (
"fmt"
"net/url"
"path/filepath" "path/filepath"
"strings" "strings"
@ -35,8 +37,10 @@ func init() {
"page.exist": PageExist, "page.exist": PageExist,
"page.asset": PageAsset, "page.asset": PageAsset,
"editor.render": EditorRender, "editor.render": EditorRender,
"editor.source": EditorSource, "editor.source": EditorSource,
"editor.renderaftersavetemp": EditorRenderAfterSaveTemp,
"editor.sourceaftersavetemp": EditorSourceAfterSaveTemp,
}) })
} }
@ -439,7 +443,6 @@ func EditorRender(process *process.Process) interface{} {
sui := get(process) sui := get(process)
templateID := process.ArgsString(1) templateID := process.ArgsString(1)
route := route(process, 2) route := route(process, 2)
query := process.ArgsMap(3, map[string]interface{}{"method": "GET"})
tmpl, err := sui.GetTemplate(templateID) tmpl, err := sui.GetTemplate(templateID)
if err != nil { if err != nil {
@ -452,8 +455,14 @@ func EditorRender(process *process.Process) interface{} {
} }
// Request data // Request data
req := &core.Request{Method: query["method"].(string)} 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(req)
if err != nil { if err != nil {
exception.New(err.Error(), 500).Throw() exception.New(err.Error(), 500).Throw()
@ -462,6 +471,16 @@ func EditorRender(process *process.Process) interface{} {
return res return res
} }
// EditorRenderAfterSaveTemp handle the render page request
func EditorRenderAfterSaveTemp(process *process.Process) interface{} {
process.ValidateArgNums(5)
PageSaveTemp(process)
args := append([]interface{}{}, process.Args[:3]...)
args = append(args, process.Args[4:]...)
process.Args = args
return EditorRender(process)
}
// EditorSource handle the render page request // EditorSource handle the render page request
func EditorSource(process *process.Process) interface{} { func EditorSource(process *process.Process) interface{} {
process.ValidateArgNums(3) process.ValidateArgNums(3)
@ -501,6 +520,17 @@ func EditorSource(process *process.Process) interface{} {
} }
} }
// EditorSourceAfterSaveTemp handle the render page request
func EditorSourceAfterSaveTemp(process *process.Process) interface{} {
process.ValidateArgNums(5)
PageSaveTemp(process)
args := append([]interface{}{}, process.Args[:3]...)
args = append(args, process.Args[4:]...)
process.Args = args
return EditorSource(process)
}
// get the sui // get the sui
func get(process *process.Process) core.SUI { func get(process *process.Process) core.SUI {
sui, has := core.SUIs[process.ArgsString(0)] sui, has := core.SUIs[process.ArgsString(0)]
@ -534,9 +564,9 @@ func getSource(process *process.Process) (*core.RequestSource, error) {
case *gin.Context: case *gin.Context:
source := core.RequestSource{UID: v.GetHeader("Yao-Builder-Uid")} source := core.RequestSource{UID: v.GetHeader("Yao-Builder-Uid")}
err := v.Bind(&source) err := v.ShouldBind(&source)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("Bind: %s", err.Error())
} }
return &source, nil return &source, nil