[add] editor render API
This commit is contained in:
parent
dabc8a0841
commit
2f42e13c04
8 changed files with 101 additions and 12 deletions
|
|
@ -22,6 +22,14 @@ var dsl = []byte(`
|
|||
"process": "sui.Template.Get",
|
||||
"in": ["$param.id"],
|
||||
"out": { "status": 200, "type": "application/json" }
|
||||
},
|
||||
|
||||
{
|
||||
"path": "/:id/editor/render/:template_id/*route",
|
||||
"method": "GET",
|
||||
"process": "sui.Editor.Render",
|
||||
"in": ["$param.id", "$param.template_id", "$param.route"],
|
||||
"out": { "status": 200, "type": "application/json" }
|
||||
}
|
||||
],
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ func init() {
|
|||
process.RegisterGroup("sui", map[string]process.Handler{
|
||||
"template.get": TemplateGet,
|
||||
"template.find": TemplateFind,
|
||||
|
||||
"editor.render": EditorRender,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -17,6 +19,7 @@ func init() {
|
|||
// Process sui.<ID>.templates
|
||||
func TemplateGet(process *process.Process) interface{} {
|
||||
process.ValidateArgNums(1)
|
||||
|
||||
sui := get(process)
|
||||
templates, err := sui.GetTemplates()
|
||||
if err != nil {
|
||||
|
|
@ -28,6 +31,7 @@ func TemplateGet(process *process.Process) interface{} {
|
|||
// TemplateFind handle the find Template request
|
||||
func TemplateFind(process *process.Process) interface{} {
|
||||
process.ValidateArgNums(2)
|
||||
|
||||
sui := get(process)
|
||||
template, err := sui.GetTemplate(process.ArgsString(1))
|
||||
if err != nil {
|
||||
|
|
@ -37,6 +41,44 @@ func TemplateFind(process *process.Process) interface{} {
|
|||
return template
|
||||
}
|
||||
|
||||
// EditorRender handle the render page request
|
||||
func EditorRender(process *process.Process) interface{} {
|
||||
process.ValidateArgNums(3)
|
||||
|
||||
sui := get(process)
|
||||
templateID := process.ArgsString(1)
|
||||
route := process.ArgsString(2)
|
||||
query := process.ArgsMap(3, map[string]interface{}{"method": "GET"})
|
||||
|
||||
if route == "" {
|
||||
route = "/index"
|
||||
}
|
||||
|
||||
if route[0] != '/' {
|
||||
route = "/" + route
|
||||
}
|
||||
|
||||
tmpl, err := sui.GetTemplate(templateID)
|
||||
if err != nil {
|
||||
exception.New(err.Error(), 500).Throw()
|
||||
}
|
||||
|
||||
page, err := tmpl.Page(route)
|
||||
if err != nil {
|
||||
exception.New(err.Error(), 500).Throw()
|
||||
}
|
||||
|
||||
// Request data
|
||||
req := &core.Request{Method: query["method"].(string)}
|
||||
|
||||
res, err := page.EditorRender(req)
|
||||
if err != nil {
|
||||
exception.New(err.Error(), 500).Throw()
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
// get the sui
|
||||
func get(process *process.Process) core.SUI {
|
||||
sui, has := core.SUIs[process.ArgsString(0)]
|
||||
|
|
|
|||
|
|
@ -48,6 +48,25 @@ func TestTemplateFind(t *testing.T) {
|
|||
assert.Equal(t, "tech-blue", res.(*local.Template).ID)
|
||||
}
|
||||
|
||||
func TestEditorRender(t *testing.T) {
|
||||
load(t)
|
||||
defer clean()
|
||||
|
||||
// test demo
|
||||
p, err := process.Of("sui.editor.render", "demo", "tech-blue", "/index")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
res, err := p.Exec()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.IsType(t, &core.ResponseEditor{}, res)
|
||||
assert.NotEmpty(t, res.(*core.ResponseEditor).HTML)
|
||||
}
|
||||
|
||||
func load(t *testing.T) {
|
||||
prepare(t)
|
||||
err := Load(config.Conf)
|
||||
|
|
|
|||
|
|
@ -1,8 +1,25 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
// Data get the data
|
||||
func (page *Page) Data(request *Request) (map[string]interface{}, error) {
|
||||
return nil, nil
|
||||
func (page *Page) Data(request *Request) (map[string]interface{}, map[string]interface{}, error) {
|
||||
|
||||
setting := map[string]interface{}{
|
||||
"title": strings.ToUpper(page.Name),
|
||||
}
|
||||
|
||||
if page.Codes.DATA.Code != "" {
|
||||
err := jsoniter.UnmarshalFromString(page.Codes.DATA.Code, &setting)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
return nil, setting, nil
|
||||
}
|
||||
|
||||
// RenderHTML render for the html
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ type ITemplate interface {
|
|||
// IPage is the interface for the page
|
||||
type IPage interface {
|
||||
Load() error
|
||||
RenderEditor(request *Request) (*ResponseEditor, error)
|
||||
EditorRender(request *Request) (*ResponseEditor, error)
|
||||
|
||||
// Render()
|
||||
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@ import (
|
|||
// Render render the page
|
||||
func (page *Page) Render() {}
|
||||
|
||||
// RenderEditor render for the editor
|
||||
func (page *Page) RenderEditor(request *Request) (*ResponseEditor, error) {
|
||||
// EditorRender render HTML for the editor
|
||||
func (page *Page) EditorRender(request *Request) (*ResponseEditor, error) {
|
||||
|
||||
html := page.Codes.HTML.Code
|
||||
res := &ResponseEditor{
|
||||
|
|
@ -19,6 +19,7 @@ func (page *Page) RenderEditor(request *Request) (*ResponseEditor, error) {
|
|||
Scripts: []string{},
|
||||
Styles: []string{},
|
||||
Warnings: []string{},
|
||||
Setting: map[string]interface{}{},
|
||||
}
|
||||
|
||||
// Get The scripts and styles
|
||||
|
|
@ -50,11 +51,12 @@ func (page *Page) RenderEditor(request *Request) (*ResponseEditor, error) {
|
|||
res.Scripts = append(res.Scripts, filepath.Join("@pages", page.Route, page.Name+".ts"))
|
||||
}
|
||||
|
||||
data, err := page.Data(request)
|
||||
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
|
||||
|
|
|
|||
|
|
@ -70,11 +70,12 @@ type Request struct {
|
|||
|
||||
// ResponseEditor is the struct for the response
|
||||
type ResponseEditor struct {
|
||||
HTML string `json:"html,omitempty"`
|
||||
CSS string `json:"css,omitempty"`
|
||||
Scripts []string `json:"scripts,omitempty"`
|
||||
Styles []string `json:"styles,omitempty"`
|
||||
Warnings []string `json:"warnings,omitempty"`
|
||||
HTML string `json:"html,omitempty"`
|
||||
CSS string `json:"css,omitempty"`
|
||||
Scripts []string `json:"scripts,omitempty"`
|
||||
Styles []string `json:"styles,omitempty"`
|
||||
Setting map[string]interface{} `json:"setting,omitempty"`
|
||||
Warnings []string `json:"warnings,omitempty"`
|
||||
}
|
||||
|
||||
// SourceCodes is the struct for the page codes
|
||||
|
|
|
|||
|
|
@ -131,7 +131,7 @@ func TestPageRenderEditor(t *testing.T) {
|
|||
}
|
||||
|
||||
r := &core.Request{Method: "GET"}
|
||||
res, err := page.RenderEditor(r)
|
||||
res, err := page.EditorRender(r)
|
||||
if err != nil {
|
||||
t.Fatalf("RenderEditor error: %v", err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue