1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00

Versioning works with /edit

This commit is contained in:
Zack Scholl
2016-02-09 18:38:25 -05:00
parent 594360fe7a
commit c15ac624b8
5 changed files with 176 additions and 114 deletions

22
db.go
View File

@ -33,7 +33,7 @@ func Close() {
db.Close() db.Close()
} }
// Data for storing in DB // CowyoData is data for storing in DB
type CowyoData struct { type CowyoData struct {
Title string Title string
CurrentText string CurrentText string
@ -44,14 +44,14 @@ type CowyoData struct {
func hasPassword(title string) (bool, error) { func hasPassword(title string) (bool, error) {
title = strings.ToLower(title) title = strings.ToLower(title)
if !open { if !open {
return false, fmt.Errorf("db must be opened before loading!") return false, fmt.Errorf("db must be opened before loading")
} }
hasPassword := false hasPassword := false
err := db.View(func(tx *bolt.Tx) error { err := db.View(func(tx *bolt.Tx) error {
var err error var err error
b := tx.Bucket([]byte("datas")) b := tx.Bucket([]byte("datas"))
if b == nil { if b == nil {
return fmt.Errorf("db must be opened before loading!") return fmt.Errorf("db must be opened before loading")
} }
k := []byte(title) k := []byte(title)
val := b.Get(k) val := b.Get(k)
@ -77,17 +77,19 @@ func hasPassword(title string) (bool, error) {
return hasPassword, nil return hasPassword, nil
} }
func getCurrentText(title string) string { func getCurrentText(title string, version int) (string, []versionsInfo, bool) {
title = strings.ToLower(title) title = strings.ToLower(title)
var vi []versionsInfo
isCurrent := true
currentText := "" currentText := ""
if !open { if !open {
return currentText return currentText, vi, isCurrent
} }
err := db.View(func(tx *bolt.Tx) error { err := db.View(func(tx *bolt.Tx) error {
var err error var err error
b := tx.Bucket([]byte("datas")) b := tx.Bucket([]byte("datas"))
if b == nil { if b == nil {
return fmt.Errorf("db must be opened before loading!") return fmt.Errorf("db must be opened before loading")
} }
k := []byte(title) k := []byte(title)
val := b.Get(k) val := b.Get(k)
@ -100,12 +102,18 @@ func getCurrentText(title string) string {
return err return err
} }
currentText = p.CurrentText currentText = p.CurrentText
if version > -1 && version < len(p.Diffs) {
// get that version of text instead
currentText = rebuildTextsToDiffN(p, version)
isCurrent = false
}
vi = getImportantVersions(p)
return nil return nil
}) })
if err != nil { if err != nil {
fmt.Printf("Could not get CowyoData: %s", err) fmt.Printf("Could not get CowyoData: %s", err)
} }
return currentText return currentText, vi, isCurrent
} }
func (p *CowyoData) load(title string) error { func (p *CowyoData) load(title string) error {

View File

@ -64,7 +64,7 @@ Options:`)
var q CowyoData var q CowyoData
q.load("SpikySeaSlug2") q.load("SpikySeaSlug2")
rebuildTexts(q) fmt.Println(getImportantVersions(q))
r := gin.Default() r := gin.Default()
r.LoadHTMLGlob(path.Join(RuntimeArgs.SourcePath, "templates/*")) r.LoadHTMLGlob(path.Join(RuntimeArgs.SourcePath, "templates/*"))

View File

@ -34,14 +34,29 @@ func editNote(c *gin.Context) {
if locked { if locked {
c.Redirect(302, "/"+title+"/view") c.Redirect(302, "/"+title+"/view")
} else { } else {
currentText := getCurrentText(title) version := c.DefaultQuery("version", "-1")
versionNum, _ := strconv.Atoi(version)
currentText, versions, currentVersion := getCurrentText(title, versionNum)
numRows := len(strings.Split(currentText, "\n")) + 10 numRows := len(strings.Split(currentText, "\n")) + 10
c.HTML(http.StatusOK, "index.tmpl", gin.H{ if currentVersion {
"Title": title, c.HTML(http.StatusOK, "index.tmpl", gin.H{
"ExternalIP": RuntimeArgs.ExternalIP, "Title": title,
"CurrentText": currentText, "ExternalIP": RuntimeArgs.ExternalIP,
"NumRows": numRows, "CurrentText": currentText,
}) "NumRows": numRows,
"Versions": versions,
})
} else {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"Title": title,
"ExternalIP": RuntimeArgs.ExternalIP,
"CurrentText": currentText,
"NumRows": numRows,
"Versions": versions,
"NoEdit": true,
})
}
} }
} }
} }

View File

@ -1,88 +1,98 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>{{ .Title }}</title> <title>{{ .Title }}</title>
{{ template "header" }} {{ template "header" }}
<script src="/static/js/jquery.autogrowtextarea.min.js"></script> <script src="/static/js/jquery.autogrowtextarea.min.js"></script>
<script src="/static/js/cowyo.js"></script> {{if .NoEdit}} {{else}} <script src="/static/js/cowyo.js"></script> {{end}}
<script>
external_ip = '{{ .ExternalIP }}' <script>
title_name = '{{ .Title }}' external_ip = '{{ .ExternalIP }}'
</script> title_name = '{{ .Title }}'
</script>
<style type="text/css">
textarea { <style type="text/css">
width: 100%; textarea {
margin: 5px 0; width: 100%;
padding: 10px; margin: 5px 0;
border: none; padding: 10px;
overflow: auto; border: none;
outline: none; overflow: auto;
font-size: large; outline: none;
-webkit-box-shadow: none; font-size: large;
-moz-box-shadow: none; -webkit-box-shadow: none;
box-shadow: none; -moz-box-shadow: none;
font-family: Tahoma, sans-serif; box-shadow: none;
} font-family: Tahoma, sans-serif;
body { }
margin: 0;
background: #fff; body {
max-width: 800px; margin: 0;
margin: 0 auto; background: #fff;
} max-width: 800px;
margin: 0 auto;
}
</style> </style>
</head> </head>
<body> <body>
<!-- Fixed navbar --> <!-- Fixed navbar -->
<nav class="navbar navbar-default navbar-fixed-bottom"> <nav class="navbar navbar-default navbar-fixed-bottom">
<div class="container"> <div class="container">
<div class="navbar-header"> <div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span> <span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span> <span class="icon-bar"></span>
<span class="icon-bar"></span> <span class="icon-bar"></span>
<span class="icon-bar"></span> <span class="icon-bar"></span>
</button> </button>
<a class="navbar-brand" href="/">Cowyo</a> <a class="navbar-brand" href="/">Cowyo</a>
</div> </div>
<div id="navbar" class="collapse navbar-collapse"> <div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<li class="active"><a href="/{{ .Title }}">Edit</a></li> <li class="dropdown">
<li><a href="/{{ .Title }}/view">View</a></li> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" class="active">Edit <span class="caret"></span></a>
<li><a href="/{{ .Title }}/list">List</a></li> <ul class="dropdown-menu">
<li><a href="/about/view">About</a></li> <li class="dropdown-header">Previous versions</li>
</ul> <li><a href="/{{ .Title }}?version=0">First</a></li>
</div> {{ range .Versions }}
<!--/.nav-collapse --> <li><a href="/{{ $.Title }}?version={{ .VersionNum }}">{{ .VersionDate }}</a></li>
</div> {{ end }}
</nav> <li><a href="/{{ .Title }}">Current</a></li>
</ul>
<form action='#' id="emit" method="post" name="emit"> </li>
<li><a href="/{{ .Title }}/view">View</a></li>
<div> <li><a href="/{{ .Title }}/list">List</a></li>
<textarea autofocus rows={{ .NumRows }} class='auto_submit_item' id="emit_data" name="emit_data" placeholder="Start typing, it will save automatically. Go to cowyo.com/{{ .Title }} to reload your note. Do not post anything private since anyone with the URL can access this note.">{{ .CurrentText }}</textarea> <li><a href="/about/view">About</a></li>
</div> </ul>
</div>
</form> <!--/.nav-collapse -->
<script> </div>
$(document).ready(function() { </nav>
$("#emit_data").autoGrow();
}); <form action='#' id="emit" method="post" name="emit">
</script>
<div>
<textarea autofocus rows={{ .NumRows }} class='auto_submit_item' id="emit_data" name="emit_data" placeholder="Start typing, it will save automatically. Go to cowyo.com/{{ .Title }} to reload your note. Do not post anything private since anyone with the URL can access this note.">{{ .CurrentText }}</textarea>
</body> </div>
</html> </form>
<script>
$(document).ready(function() {
$("#emit_data").autoGrow();
});
</script>
</body>
</html>

File diff suppressed because one or more lines are too long