Halfway trough editor

This commit is contained in:
Senad Uka
2023-09-09 05:43:31 +02:00
parent a3187bdcc1
commit 1765a150bb
10 changed files with 219 additions and 16 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

9
.idea/cetvorke.iml generated Normal file
View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/cetvorke.iml" filepath="$PROJECT_DIR$/.idea/cetvorke.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@@ -32,7 +32,6 @@ func StartServer(waitgroup *sync.WaitGroup) {
r.HandleFunc("/y/{year}/m/{month}/", monthHandler)
r.HandleFunc("/editor/e/{postID}", editHandler)
r.HandleFunc("/editor/n/", newHandler)
r.HandeeFunc("/editor", editorHandler)
err = http.ListenAndServe(":8018", r)
if err != nil {

View File

@@ -1,16 +1,97 @@
package cethttp
const editorPage = `
<form action="/save" method="POST">
<input type="text" name="title" placeholder="Naslov" />
<input type="hidden" value="{{.PostID}}" />
<div id="editor"></div>
import (
"html/template"
"strings"
"github.com/senaduka/cetvorke/database"
)
const editorTemplate = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>IslamBosna</title>
<style>
html, body {
background-color: #171714;
color: #f9f5c3;
font-family: sans-serif;
justify-content: center;
}
header {
width: 100%;
padding-top: 0.5em;
padding-bottom: 0.5em;
font-family: sans-serif;
font-size: 3em;
background-color: #0f1115;
justify-content: center;
}
header div, content div {
max-width: 800px;
margin: auto;
}
a:link, a:visited {
color: #f9f5c3;
}
header a:link {
color: #f9f5c3;
text-decoration: none;
}
header a:visited {
color: #f9f5c3;
text-decoration: none;
}
h1 {
color: #e6fbfb;
}
#editorcontainer {
background-color: #ffffff;
}
</style>
<link rel="stylesheet" href="/static/pen.css" />
<script src="/static/markdown.js"></script>
<script src="/static/pen.js"></script>
</head>
<body>
<header>
<div>
<a href="/">☙ IslamBosna</a>
</div>
</header>
<content>
<div>
<form method="POST">
<input type="text" name="title" placeholder="Naslov" value="{{.Title}}" />
<input name="ID" type="hidden" value="{{.ID}}" />
<div id="editorcontainer">
<div id="editor">{{.MarkdownContent}}</div>
</div>
</div>
</content>
<footer>
&nbsp;
</footer>
<script>
const editor = new Editor({
el: document.querySelector('#editor'),
height: '500px',
initialEditType: 'wysiwyg',
previewStyle: 'tab'
});
const editor = new Pen('#editor');
</script>
</body>
</html>
`
func editorHtml5Page(post *database.Post) string {
t1 := template.New("editorHtml5Page")
t1, _ = t1.Parse(editorTemplate)
result := new(strings.Builder)
t1.Execute(result, post)
return result.String()
}

View File

@@ -1 +1,65 @@
package cethttp
import (
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/senaduka/cetvorke/database"
)
// editHandler handles the editing of an existing post.
func editHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
postID, err := strconv.Atoi(vars["postID"])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Fetch the post from the database.
post, err := database.GetPost(postID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Check if the request method is POST.
if r.Method == "POST" {
// Get the edited content from the form.
editedContent := r.FormValue("editor")
editedTitle := r.FormValue("title")
// Update the post content.
post.Content = editedContent
post.Title = editedTitle
// Save the edited post to the database.
err := database.UpdatePost(post)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Redirect to the edited post.
http.Redirect(w, r, "/p/"+vars["postID"]+"/"+post.TitleSlug, http.StatusSeeOther)
return
}
// Display the edit form.
response := editorHtml5Page(post)
w.Write([]byte(response))
}
// newHandler handles the creation of a new post.
func newHandler(w http.ResponseWriter, r *http.Request) {
// Create a new empty post in the database.
newPost, err := database.CreateNewPost()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Redirect to the editHandler for the new post.
http.Redirect(w, r, "/editor/e/"+strconv.Itoa(newPost.ID), http.StatusSeeOther)
}

View File

@@ -51,9 +51,6 @@ const html5Template = `
color: #e6fbfb;
}
</style>
<script src="https://uicdn.toast.com/editor/latest/toastui-editor-all.min.js"></script>
<link rel="stylesheet" href="https://uicdn.toast.com/editor/latest/toastui-editor.min.css" />
</head>
<body>

BIN
cetvorke

Binary file not shown.

View File

@@ -3,6 +3,7 @@ package database
import (
"database/sql"
"fmt"
"time"
"github.com/russross/blackfriday/v2"
)
@@ -39,3 +40,33 @@ func GetPost(id int) (*Post, error) {
}
return post, nil
}
func UpdatePost(p *Post) error {
db := GetDB()
_, err := db.Exec("UPDATE posts SET post_title = ?, markdown_content = ?, gemtext_content = ?, title_slug = ?, year = ?, month = ?, day = ? WHERE id = ?", p.Title, p.MarkdownContent, p.GemtextContent, p.TitleSlug, p.Year, p.Month, p.Day, p.ID)
if err != nil {
return err
}
return nil
}
func CreateNewPost() (*Post, error) {
db := GetDB()
day := time.Now().Day()
month := time.Now().Month()
year := time.Now().Year()
result, err := db.Exec("INSERT INTO posts (post_title, markdown_content, gemtext_content, title_slug, year, month, day) VALUES (?, ?, ?, ?, ?, ?, ?)", "Novi Clanak", "", "", "new-post", year, month, day)
if err != nil {
return nil, err
}
postID, err := result.LastInsertId()
if err != nil {
return nil, err
}
post, err := GetPost(int(postID))
if err != nil {
return nil, err
}
return post, nil
}