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

View File

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

View File

@ -34,14 +34,29 @@ func editNote(c *gin.Context) {
if locked {
c.Redirect(302, "/"+title+"/view")
} 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
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"Title": title,
"ExternalIP": RuntimeArgs.ExternalIP,
"CurrentText": currentText,
"NumRows": numRows,
})
if currentVersion {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"Title": title,
"ExternalIP": RuntimeArgs.ExternalIP,
"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>
<html>
<head>
<title>{{ .Title }}</title>
{{ template "header" }}
<script src="/static/js/jquery.autogrowtextarea.min.js"></script>
<script src="/static/js/cowyo.js"></script>
<script>
external_ip = '{{ .ExternalIP }}'
title_name = '{{ .Title }}'
</script>
<style type="text/css">
textarea {
width: 100%;
margin: 5px 0;
padding: 10px;
border: none;
overflow: auto;
outline: none;
font-size: large;
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
font-family: Tahoma, sans-serif;
}
body {
margin: 0;
background: #fff;
max-width: 800px;
margin: 0 auto;
}
</style>
</head>
<body>
<!-- Fixed navbar -->
<nav class="navbar navbar-default navbar-fixed-bottom">
<div class="container">
<div class="navbar-header">
<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="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Cowyo</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="/{{ .Title }}">Edit</a></li>
<li><a href="/{{ .Title }}/view">View</a></li>
<li><a href="/{{ .Title }}/list">List</a></li>
<li><a href="/about/view">About</a></li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</nav>
<form action='#' id="emit" method="post" name="emit">
<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>
</div>
</form>
<script>
$(document).ready(function() {
$("#emit_data").autoGrow();
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>{{ .Title }}</title>
{{ template "header" }}
<script src="/static/js/jquery.autogrowtextarea.min.js"></script>
{{if .NoEdit}} {{else}} <script src="/static/js/cowyo.js"></script> {{end}}
<script>
external_ip = '{{ .ExternalIP }}'
title_name = '{{ .Title }}'
</script>
<style type="text/css">
textarea {
width: 100%;
margin: 5px 0;
padding: 10px;
border: none;
overflow: auto;
outline: none;
font-size: large;
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
font-family: Tahoma, sans-serif;
}
body {
margin: 0;
background: #fff;
max-width: 800px;
margin: 0 auto;
}
</style>
</head>
<body>
<!-- Fixed navbar -->
<nav class="navbar navbar-default navbar-fixed-bottom">
<div class="container">
<div class="navbar-header">
<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="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Cowyo</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" class="active">Edit <span class="caret"></span></a>
<ul class="dropdown-menu">
<li class="dropdown-header">Previous versions</li>
<li><a href="/{{ .Title }}?version=0">First</a></li>
{{ range .Versions }}
<li><a href="/{{ $.Title }}?version={{ .VersionNum }}">{{ .VersionDate }}</a></li>
{{ end }}
<li><a href="/{{ .Title }}">Current</a></li>
</ul>
</li>
<li><a href="/{{ .Title }}/view">View</a></li>
<li><a href="/{{ .Title }}/list">List</a></li>
<li><a href="/about/view">About</a></li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</nav>
<form action='#' id="emit" method="post" name="emit">
<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>
</div>
</form>
<script>
$(document).ready(function() {
$("#emit_data").autoGrow();
});
</script>
</body>
</html>

File diff suppressed because one or more lines are too long