[add] sui editor render with mock request
This commit is contained in:
parent
b0e94e3d73
commit
b0d9631559
9 changed files with 115 additions and 51 deletions
|
|
@ -655,16 +655,7 @@ func EditorRender(process *process.Process) interface{} {
|
||||||
exception.New(err.Error(), 500).Throw()
|
exception.New(err.Error(), 500).Throw()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request data
|
res, err := page.EditorRender()
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exception.New(err.Error(), 500).Throw()
|
exception.New(err.Error(), 500).Throw()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ import (
|
||||||
func (page *Page) Build(option *BuildOption) (*goquery.Document, []string, error) {
|
func (page *Page) Build(option *BuildOption) (*goquery.Document, []string, error) {
|
||||||
|
|
||||||
warnings := []string{}
|
warnings := []string{}
|
||||||
html, err := page.BuildHTML(option.AssetRoot)
|
html, err := page.BuildHTML(option)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
warnings = append(warnings, err.Error())
|
warnings = append(warnings, err.Error())
|
||||||
}
|
}
|
||||||
|
|
@ -23,14 +23,14 @@ func (page *Page) Build(option *BuildOption) (*goquery.Document, []string, error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add Style
|
// Add Style
|
||||||
style, err := page.BuildStyle(option.AssetRoot)
|
style, err := page.BuildStyle(option)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
warnings = append(warnings, err.Error())
|
warnings = append(warnings, err.Error())
|
||||||
}
|
}
|
||||||
doc.Selection.Find("head").AppendHtml(style)
|
doc.Selection.Find("head").AppendHtml(style)
|
||||||
|
|
||||||
// Add Script
|
// Add Script
|
||||||
script, err := page.BuildScript(option.AssetRoot)
|
script, err := page.BuildScript(option)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
warnings = append(warnings, err.Error())
|
warnings = append(warnings, err.Error())
|
||||||
}
|
}
|
||||||
|
|
@ -39,15 +39,18 @@ func (page *Page) Build(option *BuildOption) (*goquery.Document, []string, error
|
||||||
}
|
}
|
||||||
|
|
||||||
// BuildHTML build the html
|
// BuildHTML build the html
|
||||||
func (page *Page) BuildHTML(assetRoot string) (string, error) {
|
func (page *Page) BuildHTML(option *BuildOption) (string, error) {
|
||||||
|
|
||||||
html := string(page.Document)
|
html := string(page.Document)
|
||||||
if page.Codes.HTML.Code != "" {
|
if page.Codes.HTML.Code != "" {
|
||||||
html = strings.Replace(html, "{{ __page }}", page.Codes.HTML.Code, 1)
|
html = strings.Replace(html, "{{ __page }}", page.Codes.HTML.Code, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
code := strings.ReplaceAll(html, "@assets", assetRoot)
|
if !option.IgnoreAssetRoot {
|
||||||
res, err := page.CompileHTML([]byte(code), false)
|
html = strings.ReplaceAll(html, "@assets", option.AssetRoot)
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := page.CompileHTML([]byte(html), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
@ -56,12 +59,16 @@ func (page *Page) BuildHTML(assetRoot string) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// BuildStyle build the style
|
// 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 == "" {
|
if page.Codes.CSS.Code == "" {
|
||||||
return "", nil
|
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)
|
res, err := page.CompileCSS([]byte(code), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|
@ -71,7 +78,7 @@ func (page *Page) BuildStyle(assetRoot string) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// BuildScript build the script
|
// 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 == "" {
|
if page.Codes.JS.Code == "" && page.Codes.TS.Code == "" {
|
||||||
return "", nil
|
return "", nil
|
||||||
|
|
@ -86,7 +93,11 @@ func (page *Page) BuildScript(assetRoot string) (string, error) {
|
||||||
return fmt.Sprintf("<script>\n%s\n</script>\n", res), nil
|
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)
|
res, err := page.CompileJS([]byte(code), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// EditorRender render HTML for the editor
|
// EditorRender render HTML for the editor
|
||||||
func (page *Page) EditorRender(request *Request) (*ResponseEditorRender, error) {
|
func (page *Page) EditorRender() (*ResponseEditorRender, error) {
|
||||||
|
|
||||||
res := &ResponseEditorRender{
|
res := &ResponseEditorRender{
|
||||||
HTML: "",
|
HTML: "",
|
||||||
|
|
@ -32,27 +32,16 @@ func (page *Page) EditorRender(request *Request) (*ResponseEditorRender, error)
|
||||||
}
|
}
|
||||||
res.Styles = append(res.Styles, styles...)
|
res.Styles = append(res.Styles, styles...)
|
||||||
|
|
||||||
// // Page Styles
|
// Render the page
|
||||||
// if page.Codes.CSS.Code != "" {
|
request := NewRequestMock(page.Config.Mock)
|
||||||
// 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 tools
|
// Render tools
|
||||||
// res.Scripts = append(res.Scripts, filepath.Join("@assets", "__render.js"))
|
// res.Scripts = append(res.Scripts, filepath.Join("@assets", "__render.js"))
|
||||||
// res.Styles = append(res.Styles, filepath.Join("@assets", "__render.css"))
|
// res.Styles = append(res.Styles, filepath.Join("@assets", "__render.css"))
|
||||||
|
|
||||||
doc, warnings, err := page.Build(&BuildOption{
|
doc, warnings, err := page.Build(&BuildOption{
|
||||||
SSR: true,
|
SSR: true,
|
||||||
AssetRoot: request.AssetRoot,
|
IgnoreAssetRoot: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -91,7 +80,8 @@ func (res *ResponseEditorRender) Render(data map[string]interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
parser := NewTemplateParser(data, nil)
|
parser := NewTemplateParser(data, &ParserOption{Editor: true})
|
||||||
|
|
||||||
res.HTML, err = parser.Render(res.HTML)
|
res.HTML, err = parser.Render(res.HTML)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@ type IPage interface {
|
||||||
SaveTemp(request *RequestSource) error
|
SaveTemp(request *RequestSource) error
|
||||||
Remove() error
|
Remove() error
|
||||||
|
|
||||||
EditorRender(request *Request) (*ResponseEditorRender, error)
|
EditorRender() (*ResponseEditorRender, error)
|
||||||
EditorPageSource() SourceData
|
EditorPageSource() SourceData
|
||||||
EditorScriptSource() SourceData
|
EditorScriptSource() SourceData
|
||||||
EditorStyleSource() SourceData
|
EditorStyleSource() SourceData
|
||||||
|
|
|
||||||
|
|
@ -13,13 +13,25 @@ func (page *Page) Get() *Page {
|
||||||
|
|
||||||
// GetConfig get the config
|
// GetConfig get the config
|
||||||
func (page *Page) GetConfig() *PageConfig {
|
func (page *Page) GetConfig() *PageConfig {
|
||||||
if page.Config == nil && page.Codes.CONF.Code != "" {
|
|
||||||
|
if page.Config == nil {
|
||||||
|
page.Config = &PageConfig{
|
||||||
|
Mock: &PageMock{Method: "GET"},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if page.Codes.CONF.Code != "" {
|
||||||
var config PageConfig
|
var config PageConfig
|
||||||
err := jsoniter.Unmarshal([]byte(page.Codes.CONF.Code), &config)
|
err := jsoniter.Unmarshal([]byte(page.Codes.CONF.Code), &config)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
page.Config = &config
|
page.Config = &config
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if page.Config.Mock == nil {
|
||||||
|
page.Config.Mock = &PageMock{Method: "GET"}
|
||||||
|
}
|
||||||
|
|
||||||
return page.Config
|
return page.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ type TemplateParser struct {
|
||||||
sequence int // sequence for the rendering
|
sequence int // sequence for the rendering
|
||||||
errors []error // errors
|
errors []error // errors
|
||||||
replace map[*goquery.Selection][]*html.Node // replace nodes
|
replace map[*goquery.Selection][]*html.Node // replace nodes
|
||||||
|
option *ParserOption // parser option
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mapping mapping for the template
|
// Mapping mapping for the template
|
||||||
|
|
@ -26,16 +27,24 @@ type Mapping struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParserOption parser option
|
// ParserOption parser option
|
||||||
type ParserOption struct{}
|
type ParserOption struct {
|
||||||
|
Editor bool `json:"editor,omitempty"`
|
||||||
|
Preview bool `json:"preview,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
// NewTemplateParser create a new template parser
|
// NewTemplateParser create a new template parser
|
||||||
func NewTemplateParser(data Data, option *ParserOption) *TemplateParser {
|
func NewTemplateParser(data Data, option *ParserOption) *TemplateParser {
|
||||||
|
if option == nil {
|
||||||
|
option = &ParserOption{}
|
||||||
|
}
|
||||||
|
|
||||||
return &TemplateParser{
|
return &TemplateParser{
|
||||||
data: data,
|
data: data,
|
||||||
mapping: map[string]Mapping{},
|
mapping: map[string]Mapping{},
|
||||||
sequence: 0,
|
sequence: 0,
|
||||||
errors: []error{},
|
errors: []error{},
|
||||||
replace: map[*goquery.Selection][]*html.Node{},
|
replace: map[*goquery.Selection][]*html.Node{},
|
||||||
|
option: option,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -153,6 +162,12 @@ func (parser *TemplateParser) forStatementNode(sel *goquery.Selection) {
|
||||||
indexVarName := sel.AttrOr("s:for-index", "index")
|
indexVarName := sel.AttrOr("s:for-index", "index")
|
||||||
itemNodes := []*html.Node{}
|
itemNodes := []*html.Node{}
|
||||||
|
|
||||||
|
// Keep the node if the editor is enabled
|
||||||
|
if parser.option.Editor {
|
||||||
|
clone := sel.Clone()
|
||||||
|
itemNodes = append(itemNodes, clone.Nodes...)
|
||||||
|
}
|
||||||
|
|
||||||
for idx, item := range items {
|
for idx, item := range items {
|
||||||
|
|
||||||
// Create a new node
|
// Create a new node
|
||||||
|
|
@ -168,6 +183,10 @@ func (parser *TemplateParser) forStatementNode(sel *goquery.Selection) {
|
||||||
parser.data[itemVarName] = item
|
parser.data[itemVarName] = item
|
||||||
parser.data[indexVarName] = idx
|
parser.data[indexVarName] = idx
|
||||||
|
|
||||||
|
if parser.option.Editor {
|
||||||
|
parser.setSuiAttr(new, "generate", "true")
|
||||||
|
}
|
||||||
|
|
||||||
// Process the new node
|
// Process the new node
|
||||||
for i := range new.Nodes {
|
for i := range new.Nodes {
|
||||||
parser.parseNode(new.Nodes[i])
|
parser.parseNode(new.Nodes[i])
|
||||||
|
|
@ -259,7 +278,23 @@ func (parser *TemplateParser) elseStatementNode(sel *goquery.Selection) ([]*goqu
|
||||||
return elifNodes, elseNode
|
return elifNodes, elseNode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (parser *TemplateParser) setSuiAttr(sel *goquery.Selection, key, value string) *goquery.Selection {
|
||||||
|
key = fmt.Sprintf("data-sui-%s", key)
|
||||||
|
return sel.SetAttr(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (parser *TemplateParser) removeSuiAttr(sel *goquery.Selection, key string) *goquery.Selection {
|
||||||
|
key = fmt.Sprintf("data-sui-%s", key)
|
||||||
|
return sel.RemoveAttr(key)
|
||||||
|
}
|
||||||
|
|
||||||
func (parser *TemplateParser) hide(sel *goquery.Selection) {
|
func (parser *TemplateParser) hide(sel *goquery.Selection) {
|
||||||
|
|
||||||
|
if parser.option.Editor {
|
||||||
|
parser.setSuiAttr(sel, "hide", "true")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
style := sel.AttrOr("style", "")
|
style := sel.AttrOr("style", "")
|
||||||
if strings.Contains(style, "display: none") {
|
if strings.Contains(style, "display: none") {
|
||||||
return
|
return
|
||||||
|
|
@ -274,6 +309,12 @@ func (parser *TemplateParser) hide(sel *goquery.Selection) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *TemplateParser) show(sel *goquery.Selection) {
|
func (parser *TemplateParser) show(sel *goquery.Selection) {
|
||||||
|
|
||||||
|
if parser.option.Editor {
|
||||||
|
parser.removeSuiAttr(sel, "hide")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
style := sel.AttrOr("style", "")
|
style := sel.AttrOr("style", "")
|
||||||
if !strings.Contains(style, "display: none") {
|
if !strings.Contains(style, "display: none") {
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,22 @@ type Cache struct {
|
||||||
// Caches the caches
|
// Caches the caches
|
||||||
var Caches = map[string]*Cache{}
|
var Caches = map[string]*Cache{}
|
||||||
|
|
||||||
|
// NewRequestMock is the constructor for Request.
|
||||||
|
func NewRequestMock(mock *PageMock) *Request {
|
||||||
|
if mock == nil {
|
||||||
|
mock = &PageMock{Method: "GET"}
|
||||||
|
}
|
||||||
|
return &Request{
|
||||||
|
Method: mock.Method,
|
||||||
|
Query: mock.Query,
|
||||||
|
Body: mock.Body,
|
||||||
|
Payload: mock.Payload,
|
||||||
|
Referer: mock.Referer,
|
||||||
|
Headers: mock.Headers,
|
||||||
|
Params: mock.Params,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ExecString get the data
|
// ExecString get the data
|
||||||
func (r *Request) ExecString(data string) (Data, error) {
|
func (r *Request) ExecString(data string) (Data, error) {
|
||||||
var res Data
|
var res Data
|
||||||
|
|
|
||||||
|
|
@ -133,10 +133,11 @@ type MediaSearchResult struct {
|
||||||
|
|
||||||
// BuildOption is the struct for the option option
|
// BuildOption is the struct for the option option
|
||||||
type BuildOption struct {
|
type BuildOption struct {
|
||||||
SSR bool `json:"ssr"`
|
SSR bool `json:"ssr"`
|
||||||
CDN bool `json:"cdn"`
|
CDN bool `json:"cdn"`
|
||||||
UpdateAll bool `json:"update_all"`
|
UpdateAll bool `json:"update_all"`
|
||||||
AssetRoot string `json:"asset_root,omitempty"`
|
AssetRoot string `json:"asset_root,omitempty"`
|
||||||
|
IgnoreAssetRoot bool `json:"ignore_asset_root,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request is the struct for the request
|
// Request is the struct for the request
|
||||||
|
|
@ -202,11 +203,14 @@ type BoardSourceData struct {
|
||||||
|
|
||||||
// PageMock is the struct for the request
|
// PageMock is the struct for the request
|
||||||
type PageMock struct {
|
type PageMock struct {
|
||||||
Method string `json:"method,omitempty"`
|
Method string `json:"method,omitempty"`
|
||||||
Params map[string]string `json:"params,omitempty"`
|
Referer string `json:"referer,omitempty"`
|
||||||
Query map[string][]string `json:"query,omitempty"`
|
Payload map[string]interface{} `json:"payload,omitempty"`
|
||||||
Headers map[string][]string `json:"headers,omitempty"`
|
Query url.Values `json:"query,omitempty"`
|
||||||
Body interface{} `json:"body,omitempty"`
|
Params map[string]string `json:"params,omitempty"`
|
||||||
|
Headers url.Values `json:"headers,omitempty"`
|
||||||
|
Body interface{} `json:"body,omitempty"`
|
||||||
|
Sid string `json:"sid,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// PageConfig is the struct for the page config
|
// PageConfig is the struct for the page config
|
||||||
|
|
|
||||||
|
|
@ -23,8 +23,7 @@ func TestPageEditorRender(t *testing.T) {
|
||||||
t.Fatalf("Page error: %v", err)
|
t.Fatalf("Page error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
r := &core.Request{Method: "GET"}
|
res, err := page.EditorRender()
|
||||||
res, err := page.EditorRender(r)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("EditorRender error: %v", err)
|
t.Fatalf("EditorRender error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue