diff options
| author | ivar <i@oiee.no> | 2026-04-07 00:23:24 +0200 |
|---|---|---|
| committer | ivar <i@oiee.no> | 2026-04-07 00:23:24 +0200 |
| commit | 85920b8c7a2696115d1f77c046f48f6f00d639f1 (patch) | |
| tree | 14ed2043796eadd6ed5b0a95c55e38e48713d638 /templates/admin/form.html | |
| download | iblog-85920b8c7a2696115d1f77c046f48f6f00d639f1.tar.xz iblog-85920b8c7a2696115d1f77c046f48f6f00d639f1.zip | |
Init
Diffstat (limited to 'templates/admin/form.html')
| -rw-r--r-- | templates/admin/form.html | 82 |
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 |
