[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()
|
||||
}
|
||||
|
||||
// Request data
|
||||
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()
|
||||
if err != nil {
|
||||
exception.New(err.Error(), 500).Throw()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import (
|
|||
func (page *Page) Build(option *BuildOption) (*goquery.Document, []string, error) {
|
||||
|
||||
warnings := []string{}
|
||||
html, err := page.BuildHTML(option.AssetRoot)
|
||||
html, err := page.BuildHTML(option)
|
||||
if err != nil {
|
||||
warnings = append(warnings, err.Error())
|
||||
}
|
||||
|
|
@ -23,14 +23,14 @@ func (page *Page) Build(option *BuildOption) (*goquery.Document, []string, error
|
|||
}
|
||||
|
||||
// Add Style
|
||||
style, err := page.BuildStyle(option.AssetRoot)
|
||||
style, err := page.BuildStyle(option)
|
||||
if err != nil {
|
||||
warnings = append(warnings, err.Error())
|
||||
}
|
||||
doc.Selection.Find("head").AppendHtml(style)
|
||||
|
||||
// Add Script
|
||||
script, err := page.BuildScript(option.AssetRoot)
|
||||
script, err := page.BuildScript(option)
|
||||
if err != nil {
|
||||
warnings = append(warnings, err.Error())
|
||||
}
|
||||
|
|
@ -39,15 +39,18 @@ func (page *Page) Build(option *BuildOption) (*goquery.Document, []string, error
|
|||
}
|
||||
|
||||
// BuildHTML build the html
|
||||
func (page *Page) BuildHTML(assetRoot string) (string, error) {
|
||||
func (page *Page) BuildHTML(option *BuildOption) (string, error) {
|
||||
|
||||
html := string(page.Document)
|
||||
if page.Codes.HTML.Code != "" {
|
||||
html = strings.Replace(html, "{{ __page }}", page.Codes.HTML.Code, 1)
|
||||
}
|
||||
|
||||
code := strings.ReplaceAll(html, "@assets", assetRoot)
|
||||
res, err := page.CompileHTML([]byte(code), false)
|
||||
if !option.IgnoreAssetRoot {
|
||||
html = strings.ReplaceAll(html, "@assets", option.AssetRoot)
|
||||
}
|
||||
|
||||
res, err := page.CompileHTML([]byte(html), false)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
@ -56,12 +59,16 @@ func (page *Page) BuildHTML(assetRoot string) (string, error) {
|
|||
}
|
||||
|
||||
// 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 == "" {
|
||||
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)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
@ -71,7 +78,7 @@ func (page *Page) BuildStyle(assetRoot string) (string, error) {
|
|||
}
|
||||
|
||||
// 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 == "" {
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import (
|
|||
)
|
||||
|
||||
// EditorRender render HTML for the editor
|
||||
func (page *Page) EditorRender(request *Request) (*ResponseEditorRender, error) {
|
||||
func (page *Page) EditorRender() (*ResponseEditorRender, error) {
|
||||
|
||||
res := &ResponseEditorRender{
|
||||
HTML: "",
|
||||
|
|
@ -32,19 +32,8 @@ func (page *Page) EditorRender(request *Request) (*ResponseEditorRender, error)
|
|||
}
|
||||
res.Styles = append(res.Styles, styles...)
|
||||
|
||||
// // Page Styles
|
||||
// if page.Codes.CSS.Code != "" {
|
||||
// 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 the page
|
||||
request := NewRequestMock(page.Config.Mock)
|
||||
|
||||
// Render tools
|
||||
// res.Scripts = append(res.Scripts, filepath.Join("@assets", "__render.js"))
|
||||
|
|
@ -52,7 +41,7 @@ func (page *Page) EditorRender(request *Request) (*ResponseEditorRender, error)
|
|||
|
||||
doc, warnings, err := page.Build(&BuildOption{
|
||||
SSR: true,
|
||||
AssetRoot: request.AssetRoot,
|
||||
IgnoreAssetRoot: true,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -91,7 +80,8 @@ func (res *ResponseEditorRender) Render(data map[string]interface{}) error {
|
|||
}
|
||||
|
||||
var err error
|
||||
parser := NewTemplateParser(data, nil)
|
||||
parser := NewTemplateParser(data, &ParserOption{Editor: true})
|
||||
|
||||
res.HTML, err = parser.Render(res.HTML)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ type IPage interface {
|
|||
SaveTemp(request *RequestSource) error
|
||||
Remove() error
|
||||
|
||||
EditorRender(request *Request) (*ResponseEditorRender, error)
|
||||
EditorRender() (*ResponseEditorRender, error)
|
||||
EditorPageSource() SourceData
|
||||
EditorScriptSource() SourceData
|
||||
EditorStyleSource() SourceData
|
||||
|
|
|
|||
|
|
@ -13,13 +13,25 @@ func (page *Page) Get() *Page {
|
|||
|
||||
// GetConfig get the config
|
||||
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
|
||||
err := jsoniter.Unmarshal([]byte(page.Codes.CONF.Code), &config)
|
||||
if err == nil {
|
||||
page.Config = &config
|
||||
}
|
||||
}
|
||||
|
||||
if page.Config.Mock == nil {
|
||||
page.Config.Mock = &PageMock{Method: "GET"}
|
||||
}
|
||||
|
||||
return page.Config
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ type TemplateParser struct {
|
|||
sequence int // sequence for the rendering
|
||||
errors []error // errors
|
||||
replace map[*goquery.Selection][]*html.Node // replace nodes
|
||||
option *ParserOption // parser option
|
||||
}
|
||||
|
||||
// Mapping mapping for the template
|
||||
|
|
@ -26,16 +27,24 @@ type Mapping struct {
|
|||
}
|
||||
|
||||
// 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
|
||||
func NewTemplateParser(data Data, option *ParserOption) *TemplateParser {
|
||||
if option == nil {
|
||||
option = &ParserOption{}
|
||||
}
|
||||
|
||||
return &TemplateParser{
|
||||
data: data,
|
||||
mapping: map[string]Mapping{},
|
||||
sequence: 0,
|
||||
errors: []error{},
|
||||
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")
|
||||
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 {
|
||||
|
||||
// Create a new node
|
||||
|
|
@ -168,6 +183,10 @@ func (parser *TemplateParser) forStatementNode(sel *goquery.Selection) {
|
|||
parser.data[itemVarName] = item
|
||||
parser.data[indexVarName] = idx
|
||||
|
||||
if parser.option.Editor {
|
||||
parser.setSuiAttr(new, "generate", "true")
|
||||
}
|
||||
|
||||
// Process the new node
|
||||
for i := range new.Nodes {
|
||||
parser.parseNode(new.Nodes[i])
|
||||
|
|
@ -259,7 +278,23 @@ func (parser *TemplateParser) elseStatementNode(sel *goquery.Selection) ([]*goqu
|
|||
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) {
|
||||
|
||||
if parser.option.Editor {
|
||||
parser.setSuiAttr(sel, "hide", "true")
|
||||
return
|
||||
}
|
||||
|
||||
style := sel.AttrOr("style", "")
|
||||
if strings.Contains(style, "display: none") {
|
||||
return
|
||||
|
|
@ -274,6 +309,12 @@ func (parser *TemplateParser) hide(sel *goquery.Selection) {
|
|||
}
|
||||
|
||||
func (parser *TemplateParser) show(sel *goquery.Selection) {
|
||||
|
||||
if parser.option.Editor {
|
||||
parser.removeSuiAttr(sel, "hide")
|
||||
return
|
||||
}
|
||||
|
||||
style := sel.AttrOr("style", "")
|
||||
if !strings.Contains(style, "display: none") {
|
||||
return
|
||||
|
|
|
|||
|
|
@ -18,6 +18,22 @@ type Cache struct {
|
|||
// Caches the caches
|
||||
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
|
||||
func (r *Request) ExecString(data string) (Data, error) {
|
||||
var res Data
|
||||
|
|
|
|||
|
|
@ -137,6 +137,7 @@ type BuildOption struct {
|
|||
CDN bool `json:"cdn"`
|
||||
UpdateAll bool `json:"update_all"`
|
||||
AssetRoot string `json:"asset_root,omitempty"`
|
||||
IgnoreAssetRoot bool `json:"ignore_asset_root,omitempty"`
|
||||
}
|
||||
|
||||
// Request is the struct for the request
|
||||
|
|
@ -203,10 +204,13 @@ type BoardSourceData struct {
|
|||
// PageMock is the struct for the request
|
||||
type PageMock struct {
|
||||
Method string `json:"method,omitempty"`
|
||||
Referer string `json:"referer,omitempty"`
|
||||
Payload map[string]interface{} `json:"payload,omitempty"`
|
||||
Query url.Values `json:"query,omitempty"`
|
||||
Params map[string]string `json:"params,omitempty"`
|
||||
Query map[string][]string `json:"query,omitempty"`
|
||||
Headers map[string][]string `json:"headers,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
|
||||
|
|
|
|||
|
|
@ -23,8 +23,7 @@ func TestPageEditorRender(t *testing.T) {
|
|||
t.Fatalf("Page error: %v", err)
|
||||
}
|
||||
|
||||
r := &core.Request{Method: "GET"}
|
||||
res, err := page.EditorRender(r)
|
||||
res, err := page.EditorRender()
|
||||
if err != nil {
|
||||
t.Fatalf("EditorRender error: %v", err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue