summaryrefslogtreecommitdiffstats
path: root/templates/admin/form.html
diff options
context:
space:
mode:
Diffstat (limited to 'templates/admin/form.html')
-rw-r--r--templates/admin/form.html82
1 files changed, 82 insertions, 0 deletions
diff --git a/templates/admin/form.html b/templates/admin/form.html
new file mode 100644
index 0000000..133951f
--- /dev/null
+++ b/templates/admin/form.html
@@ -0,0 +1,82 @@
+{{define "form-content"}}
+<form method="POST" action="{{.Action}}" id="postForm">
+ <label for="title">Title</label>
+ <input type="text" id="title" name="title" value="{{.Post.Title}}" required autofocus>
+
+ {{if not .IsNew}}
+ <label for="slug">Slug</label>
+ <input type="text" id="slug" name="slug" value="{{.Post.Slug}}">
+ {{end}}
+
+ <label for="date">Date</label>
+ <input type="date" id="date" name="date" value="{{.Post.Date}}">
+
+ <label for="tags">Tags</label>
+ <input type="text" id="tags" name="tags" value="{{range $i, $t := .Post.Tags}}{{if $i}}, {{end}}{{$t}}{{end}}"
+ placeholder="tag1, tag2, tag3">
+ <label for="draft">
+ <input type="checkbox" id="draft" name="draft" {{if .Post.Draft}} checked{{end}}>
+ Draft (not published)
+ </label>
+ <button type="submit" class="btn btn-primary">
+ {{if .IsNew}}Create Post{{else}}Save Changes{{end}}
+ </button>
+ <a href="/admin/" class="btn btn-secondary">Cancel</a>
+ <div id="editor" style="border: 1px solid #ccc; border-radius: 4px; min-height: 340px;"></div>
+ <input type="hidden" id="blocks" name="blocks" value="{{.Post.Blocks}}">
+</form>
+<script type="module">
+ import { EditorJS, Header, Paragraph, List, Code, Quote, ImageTool } from 'editorjs-bundle'
+ import ComponentTool from '/assets/admin/lib/component-tool.js'
+ import { qs, on } from 'shared'
+
+ const blocksField = qs('#blocks')
+ const form = qs('#postForm')
+
+ let editorData = { blocks: [] }
+ try {
+ const parsed = JSON.parse(blocksField.value || '[]')
+ if (Array.isArray(parsed)) {
+ editorData = { blocks: parsed }
+ } else if (parsed && typeof parsed === 'object') {
+ editorData = parsed
+ }
+ } catch (e) {
+ console.error('Failed to parse blocks JSON:', e)
+ }
+
+ const editor = new EditorJS({
+ holder: 'editor',
+ tools: {
+ header: Header,
+ paragraph: Paragraph,
+ list: List,
+ code: Code,
+ quote: Quote,
+ image: {
+ class: ImageTool,
+ config: {
+ endpoints: { byFile: '/admin/upload/image' },
+ },
+ },
+ component: ComponentTool,
+ },
+ data: editorData,
+ })
+
+ // Sync editor content back to hidden field before form submission
+ on(form, 'submit', async (e) => {
+ e.preventDefault()
+
+ try {
+ const saved = await editor.save()
+ blocksField.value = JSON.stringify(saved)
+ } catch (err) {
+ console.error('Failed to save editor data:', err)
+ return
+ }
+
+ form.submit()
+ })
+</script>
+{{end}} \ No newline at end of file