Implemented EasyMDE

This commit is contained in:
Jason Streifling 2024-08-08 21:09:38 +02:00
parent ef1914ee5c
commit 203a1ed147
4 changed files with 67 additions and 86 deletions

View File

@ -1,9 +1,11 @@
package frontend package frontend
import ( import (
"encoding/json"
"fmt" "fmt"
"html/template" "html/template"
"io" "io"
"io/fs"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -462,6 +464,12 @@ func UploadImage(c *b.Config) http.HandlerFunc {
return return
} }
if err = os.MkdirAll(fmt.Sprint(c.PicsDir, "/"), fs.FileMode(0755)); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
img, err := os.Create(absFilepath) img, err := os.Create(absFilepath)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -476,10 +484,9 @@ func UploadImage(c *b.Config) http.HandlerFunc {
return return
} }
alt := strings.Join(nameStrings[0:len(nameStrings)-1], " ") url := fmt.Sprint(c.Domain, "/pics/", filename)
imgMD := fmt.Sprint("![", alt, "](", c.Domain, "/pics/", filename, ")") w.Header().Set("Content-Type", "application/json")
tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html") json.NewEncoder(w).Encode(url)
template.Must(tmpl, err).ExecuteTemplate(w, "editor-images", imgMD)
} }
} }

View File

@ -2,9 +2,6 @@
<h2>Editor</h2> <h2>Editor</h2>
<form id="edit-area"> <form id="edit-area">
{{if eq .Mode 0}}
<button class="btn" hx-post="/preview-article" hx-target="#page-content">Vorschau</button>
<div class="flex flex-col gap-y-1"> <div class="flex flex-col gap-y-1">
<label for="article-title">Titel</label> <label for="article-title">Titel</label>
<input name="article-title" type="text" value="{{.Title}}" /> <input name="article-title" type="text" value="{{.Title}}" />
@ -15,8 +12,9 @@
</div> </div>
<div class="flex flex-col"> <div class="flex flex-col">
<label for="article-content">Artikel</label> <label for="article-content">Artikel</label>
<textarea name="article-content">{{.Content}}</textarea> <textarea id="easyMDE">{{.Content}}</textarea>
</div> </div>
<input id="article-content" name="article-content" type="hidden" />
<div> <div>
<span>Tags</span> <span>Tags</span>
@ -30,63 +28,40 @@
</div> </div>
</div> </div>
<div id="editor-images">
<input class="mb-2" name="article-image" type="file" hx-encoding="multipart/form-data" hx-post="/upload-image"
hx-swap="beforeend" hx-target="#editor-images" />
</div>
<div class="btn-area"> <div class="btn-area">
<input class="action-btn" type="submit" value="Senden" hx-post="/submit-article" hx-target="#page-content" /> <input class="action-btn" type="submit" value="Senden" hx-post="/submit-article" hx-target="#page-content" />
<button class="btn" hx-get="/hub" hx-target="#page-content">Abbrechen</button> <button class="btn" hx-get="/hub" hx-target="#page-content">Abbrechen</button>
</div> </div>
{{else}}
<button class="btn" hx-get="/write-article" hx-target="#page-content">Schreiben</button>
<span>Titel</span>
<div class="bg-white border mb-3 px-2 py-2 rounded-md w-full">
{{.Title}}
</div>
<span>Beschreibung</span>
<div class="bg-white border mb-3 px-2 py-2 rounded-md w-full">
{{.Description}}
</div>
<span>Artikel</span>
<div class="bg-white border mb-3 px-2 py-2 rounded-md w-full">
<div class="prose">
{{.HTMLContent}}
</div>
</div>
{{end}}
</form> </form>
<script> <script>
function copyToClipboard(text) { var easyMDE = new EasyMDE({
event.preventDefault(); // Get-Request verhindern element: document.getElementById('easyMDE'),
hideIcons: ['image'],
imageTexts: {sbInit: ''},
showIcons: ["code", "table", "upload-image"],
uploadImage: true,
var textarea = document.createElement("textarea"); imageUploadFunction: function (file, onSuccess, onError) {
textarea.textContent = text; var formData = new FormData();
document.body.appendChild(textarea); formData.append('article-image', file);
textarea.select(); fetch('/upload-image', {
try { method: 'POST',
document.execCommand('copy'); body: formData
} catch (err) { })
console.warn('Fehler beim Kopieren', err); .then(response => response.json())
} .then(data => {
onSuccess(data);
})
.catch(error => {
onError(error);
});
},
});
document.body.removeChild(textarea); easyMDE.codemirror.on("change", () => {
} document.getElementById('article-content').value = easyMDE.value();
});
</script> </script>
{{end}} {{end}}
{{define "editor-images"}}
{{if gt (len .) 0}}
<div class="border px-2 py-1 rounded-md flex gap-4 justify-between">
<div class="self-center">{{.}}</div>
<button class="bg-slate-50 border my-2 px-3 py-2 rounded-md w-32 hover:bg-slate-100"
onclick="copyToClipboard('{{.}}')">Kopieren</button>
</div>
{{end}}
{{end}}

View File

@ -6,6 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>Orient Editor</title> <title>Orient Editor</title>
<link href="/web/static/css/style.css" rel="stylesheet"> <link href="/web/static/css/style.css" rel="stylesheet">
<link rel="stylesheet" href="https://unpkg.com/easymde/dist/easymde.min.css">
</head> </head>
<body class="flex flex-col justify-between min-h-screen bg-slate-50"> <body class="flex flex-col justify-between min-h-screen bg-slate-50">
@ -25,7 +26,8 @@
</p> </p>
</footer> </footer>
<script src="/web/static/js/htmx.min.js"></script> <script src="https://unpkg.com/htmx.org@2.0.1"></script>
<script src="https://unpkg.com/easymde/dist/easymde.min.js"></script>
</body> </body>
</html> </html>

View File

@ -14,6 +14,7 @@
<label for="article-content">Artikel</label> <label for="article-content">Artikel</label>
<textarea name="article-content" placeholder="Artikel">{{.Article.Content}}</textarea> <textarea name="article-content" placeholder="Artikel">{{.Article.Content}}</textarea>
</div> </div>
<input id="article-content" name="article-content" type="hidden" />
<div> <div>
<span>Tags</span> <span>Tags</span>
@ -28,11 +29,6 @@
</div> </div>
</div> </div>
<div id="editor-images">
<input class="mb-2" name="article-image" type="file" hx-encoding="multipart/form-data" hx-post="/upload-image"
hx-swap="beforeend" hx-target="#editor-images" />
</div>
<div class="btn-area"> <div class="btn-area">
<input class="action-btn" type="submit" value="Senden" hx-post="/resubmit-article/{{.Article.ID}}" <input class="action-btn" type="submit" value="Senden" hx-post="/resubmit-article/{{.Article.ID}}"
hx-target="#page-content" /> hx-target="#page-content" />
@ -41,32 +37,33 @@
</form> </form>
<script> <script>
function copyToClipboard(text) { var easyMDE = new EasyMDE({
event.preventDefault(); // Get-Request verhindern element: document.getElementById('easyMDE'),
hideIcons: ['image'],
imageTexts: {sbInit: ''},
showIcons: ["code", "table", "upload-image"],
uploadImage: true,
var textarea = document.createElement("textarea"); imageUploadFunction: function (file, onSuccess, onError) {
textarea.textContent = text; var formData = new FormData();
document.body.appendChild(textarea); formData.append('article-image', file);
textarea.select(); fetch('/upload-image', {
try { method: 'POST',
document.execCommand('copy'); body: formData
} catch (err) { })
console.warn('Fehler beim Kopieren', err); .then(response => response.json())
} .then(data => {
onSuccess(data);
document.body.removeChild(textarea); })
} .catch(error => {
onError(error);
});
},
});
easyMDE.codemirror.on("change", () => {
document.getElementById('article-content').value = easyMDE.value();
});
</script> </script>
{{end}} {{end}}
{{define "editor-images"}}
{{if gt (len .) 0}}
<div class="border px-2 py-1 rounded-md flex gap-4 justify-between">
<div class="self-center">{{.}}</div>
<button class="bg-slate-50 border my-2 px-3 py-2 rounded-md w-32 hover:bg-slate-100"
onclick="copyToClipboard('{{.}}')">Kopieren</button>
</div>
{{end}}
{{end}}