Merge pull request #468 from trheyi/main
[add] editor render, source api
This commit is contained in:
commit
7cebf753fc
8 changed files with 243 additions and 19 deletions
|
|
@ -11,17 +11,31 @@ var dsl = []byte(`
|
|||
"group": "__yao/sui/v1",
|
||||
"paths": [
|
||||
{
|
||||
"path": "/:id/template/find/:template_id",
|
||||
"path": "/:id/template/:template_id",
|
||||
"method": "GET",
|
||||
"process": "sui.Template.Find",
|
||||
"in": ["$param.id", "$param.template_id"],
|
||||
"out": { "status": 200, "type": "application/json" }
|
||||
},{
|
||||
"path": "/:id/template/get",
|
||||
"path": "/:id/template",
|
||||
"method": "GET",
|
||||
"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" }
|
||||
},{
|
||||
"path": "/:id/editor/:kind/source/:template_id/*route",
|
||||
"method": "GET",
|
||||
"process": "sui.Editor.Source",
|
||||
"in": ["$param.id", "$param.template_id", "$param.route", "$param.kind"],
|
||||
"out": { "status": 200, "type": "application/json" }
|
||||
}
|
||||
],
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,9 @@ func init() {
|
|||
process.RegisterGroup("sui", map[string]process.Handler{
|
||||
"template.get": TemplateGet,
|
||||
"template.find": TemplateFind,
|
||||
|
||||
"editor.render": EditorRender,
|
||||
"editor.source": EditorSource,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -17,6 +20,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 +32,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 +42,75 @@ 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 := route(process, 2)
|
||||
query := process.ArgsMap(3, map[string]interface{}{"method": "GET"})
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// EditorSource handle the render page request
|
||||
func EditorSource(process *process.Process) interface{} {
|
||||
process.ValidateArgNums(3)
|
||||
|
||||
sui := get(process)
|
||||
templateID := process.ArgsString(1)
|
||||
route := route(process, 2)
|
||||
kind := process.ArgsString(3)
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
switch kind {
|
||||
|
||||
case "page":
|
||||
return page.EditorPageSource()
|
||||
|
||||
case "style":
|
||||
return page.EditorStyleSource()
|
||||
|
||||
case "script":
|
||||
return page.EditorScriptSource()
|
||||
|
||||
case "data":
|
||||
return page.EditorDataSource()
|
||||
|
||||
default:
|
||||
exception.New("the %s source does not exist", 404, kind).Throw()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// get the sui
|
||||
func get(process *process.Process) core.SUI {
|
||||
sui, has := core.SUIs[process.ArgsString(0)]
|
||||
|
|
@ -45,3 +119,15 @@ func get(process *process.Process) core.SUI {
|
|||
}
|
||||
return sui
|
||||
}
|
||||
|
||||
func route(process *process.Process, i int) string {
|
||||
route := process.ArgsString(i)
|
||||
if route == "" {
|
||||
route = "/index"
|
||||
}
|
||||
|
||||
if route[0] != '/' {
|
||||
route = "/" + route
|
||||
}
|
||||
return route
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,6 +48,67 @@ 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 TestEditorRenderWithQuery(t *testing.T) {
|
||||
load(t)
|
||||
defer clean()
|
||||
|
||||
// test demo
|
||||
p, err := process.Of("sui.editor.render", "demo", "tech-blue", "/index", map[string]interface{}{
|
||||
"method": "POST",
|
||||
})
|
||||
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 TestEditorPageSource(t *testing.T) {
|
||||
load(t)
|
||||
defer clean()
|
||||
|
||||
sources := []string{"page", "script", "style", "data"}
|
||||
for _, source := range sources {
|
||||
p, err := process.Of("sui.editor.source", "demo", "tech-blue", "/index", source)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
res, err := p.Exec()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.IsType(t, core.ResponseSource{}, res)
|
||||
assert.NotEmpty(t, res.(core.ResponseSource).Source)
|
||||
assert.NotEmpty(t, res.(core.ResponseSource).Language)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
|
|
|||
|
|
@ -6,11 +6,8 @@ import (
|
|||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
|
||||
// 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 +16,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 +48,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
|
||||
|
|
@ -71,8 +70,44 @@ func (page *Page) RenderEditor(request *Request) (*ResponseEditor, error) {
|
|||
return res, nil
|
||||
}
|
||||
|
||||
// RenderPreview render for the preview
|
||||
func (page *Page) RenderPreview() {}
|
||||
// EditorPageSource get the editor page source code
|
||||
func (page *Page) EditorPageSource() ResponseSource {
|
||||
return ResponseSource{
|
||||
Source: page.Codes.HTML.Code,
|
||||
Language: "html",
|
||||
}
|
||||
}
|
||||
|
||||
// EditorScriptSource get the editor script source code
|
||||
func (page *Page) EditorScriptSource() ResponseSource {
|
||||
if page.Codes.TS.Code != "" {
|
||||
return ResponseSource{
|
||||
Source: page.Codes.TS.Code,
|
||||
Language: "typescript",
|
||||
}
|
||||
}
|
||||
|
||||
return ResponseSource{
|
||||
Source: page.Codes.JS.Code,
|
||||
Language: "javascript",
|
||||
}
|
||||
}
|
||||
|
||||
// EditorStyleSource get the editor style source code
|
||||
func (page *Page) EditorStyleSource() ResponseSource {
|
||||
return ResponseSource{
|
||||
Source: page.Codes.CSS.Code,
|
||||
Language: "css",
|
||||
}
|
||||
}
|
||||
|
||||
// EditorDataSource get the editor data source code
|
||||
func (page *Page) EditorDataSource() ResponseSource {
|
||||
return ResponseSource{
|
||||
Source: page.Codes.DATA.Code,
|
||||
Language: "json",
|
||||
}
|
||||
}
|
||||
|
||||
// GlobalScripts get the global scripts
|
||||
func (page *Page) GlobalScripts() ([]string, error) {
|
||||
|
|
@ -29,7 +29,11 @@ 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)
|
||||
EditorPageSource() ResponseSource
|
||||
EditorScriptSource() ResponseSource
|
||||
EditorStyleSource() ResponseSource
|
||||
EditorDataSource() ResponseSource
|
||||
|
||||
// Render()
|
||||
|
||||
|
|
|
|||
|
|
@ -70,11 +70,18 @@ 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"`
|
||||
}
|
||||
|
||||
// ResponseSource is the struct for the response
|
||||
type ResponseSource struct {
|
||||
Source string `json:"source,omitempty"`
|
||||
Language string `json:"lang,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