From 63bdbb58249203d6b203d4d484529c2053675208 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Sat, 4 Nov 2017 15:30:44 -0600 Subject: [PATCH] Add -debounce for setting the debounce time --- handlers.go | 8 +++++++- main.go | 7 ++++++- templates/index.tmpl | 6 +++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/handlers.go b/handlers.go index 2c836ff..b93cb9c 100755 --- a/handlers.go +++ b/handlers.go @@ -19,8 +19,9 @@ import ( var customCSS []byte var defaultLock string +var debounceTime int -func serve(host, port, crt_path, key_path string, TLS bool, cssFile string, defaultPage string, defaultPassword string) { +func serve(host, port, crt_path, key_path string, TLS bool, cssFile string, defaultPage string, defaultPassword string, debounce int) { gin.SetMode(gin.ReleaseMode) router := gin.Default() store := sessions.NewCookieStore([]byte("secret")) @@ -63,11 +64,15 @@ func serve(host, port, crt_path, key_path string, TLS bool, cssFile string, defa fmt.Printf("Loaded CSS file, %d bytes\n", len(customCSS)) } + // lock all pages automatically if defaultPassword != "" { fmt.Println("running with locked pages") defaultLock = HashPassword(defaultPassword) } + // set the debounce time + debounceTime = debounce + if TLS { http.ListenAndServeTLS(host+":"+port, crt_path, key_path, router) } else { @@ -334,6 +339,7 @@ func handlePageRequest(c *gin.Context) { "RecentlyEdited": getRecentlyEdited(page, c), "IsPublished": p.IsPublished, "CustomCSS": len(customCSS) > 0, + "Debounce": debounceTime, }) } diff --git a/main.go b/main.go index b884339..1702bad 100755 --- a/main.go +++ b/main.go @@ -38,7 +38,7 @@ func main() { } else { fmt.Printf("\nRunning cowyo server (version %s) at http://%s:%s\n\n", version, host, c.GlobalString("port")) } - serve(c.GlobalString("host"), c.GlobalString("port"), c.GlobalString("cert"), c.GlobalString("key"), TLS, c.GlobalString("css"), c.GlobalString("default-page"), c.GlobalString("lock")) + serve(c.GlobalString("host"), c.GlobalString("port"), c.GlobalString("cert"), c.GlobalString("key"), TLS, c.GlobalString("css"), c.GlobalString("default-page"), c.GlobalString("lock"), c.GlobalInt("debounce")) return nil } app.Flags = []cli.Flag{ @@ -87,6 +87,11 @@ func main() { Value: "", Usage: "password to lock editing all files (default: all pages unlocked)", }, + cli.IntFlag{ + Name: "debounce", + Value: 500, + Usage: "debounce time for saving data, in milliseconds", + }, cli.BoolFlag{ Name: "debug, d", Usage: "turn on debugging", diff --git a/templates/index.tmpl b/templates/index.tmpl index 287f324..448ba95 100755 --- a/templates/index.tmpl +++ b/templates/index.tmpl @@ -176,7 +176,7 @@ body#pad textarea { $('#saveEditButton').removeClass() $('#saveEditButton').text("Saving") upload(); - }, 500)); + }, {{ .Debounce }})); function upload() { $.ajax({ @@ -531,10 +531,10 @@ body#pad textarea { View