[add] editor source api

This commit is contained in:
Max 2023-09-28 10:47:18 +08:00
parent 2f42e13c04
commit 688e06e548
6 changed files with 149 additions and 14 deletions

View file

@ -30,6 +30,12 @@ var dsl = []byte(`
"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" }
}
],
}

View file

@ -12,6 +12,7 @@ func init() {
"template.find": TemplateFind,
"editor.render": EditorRender,
"editor.source": EditorSource,
})
}
@ -47,17 +48,9 @@ func EditorRender(process *process.Process) interface{} {
sui := get(process)
templateID := process.ArgsString(1)
route := process.ArgsString(2)
route := route(process, 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()
@ -79,6 +72,45 @@ func EditorRender(process *process.Process) interface{} {
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)]
@ -87,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
}

View file

@ -67,6 +67,48 @@ func TestEditorRender(t *testing.T) {
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)

View file

@ -6,9 +6,6 @@ import (
"github.com/PuerkitoBio/goquery"
)
// Render render the page
func (page *Page) Render() {}
// EditorRender render HTML for the editor
func (page *Page) EditorRender(request *Request) (*ResponseEditor, error) {
@ -73,8 +70,44 @@ func (page *Page) EditorRender(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) {

View file

@ -30,6 +30,10 @@ type ITemplate interface {
type IPage interface {
Load() error
EditorRender(request *Request) (*ResponseEditor, error)
EditorPageSource() ResponseSource
EditorScriptSource() ResponseSource
EditorStyleSource() ResponseSource
EditorDataSource() ResponseSource
// Render()

View file

@ -78,6 +78,12 @@ type ResponseEditor struct {
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
type SourceCodes struct {
HTML Source `json:"-"`