180 lines
10 KiB
HTML
180 lines
10 KiB
HTML
{% extends "admin/base.html" %}
|
|
{% block admin_content %}
|
|
<div class="admin-panel">
|
|
<h1>{% if post %}Edit post{% else %}New post{% endif %}</h1>
|
|
<form method="post" action="{% if post %}{{ url_for('admin.post_edit', pid=post.id) }}{% else %}{{ url_for('admin.post_new') }}{% endif %}">
|
|
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label for="title">Title *</label>
|
|
<input type="text" id="title" name="title" value="{{ (post or {}).get('title', '') if post else '' }}" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="slug">Slug</label>
|
|
<input type="text" id="slug" name="slug" value="{{ (post or {}).get('slug', '') if post else '' }}" placeholder="auto-generated-from-title">
|
|
<span class="field-hint">URL-friendly version of the title. Leave blank to auto-generate.</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label for="author">Author</label>
|
|
<input type="text" id="author" name="author" value="{{ (post or {}).get('author', 'editor') if post else '' }}">
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="category">Category</label>
|
|
<input type="text" id="category" name="category" value="{{ (post or {}).get('category', 'General') if post else '' }}" list="category-options">
|
|
<datalist id="category-options">
|
|
{% for slug, name in categories %}
|
|
<option value="{{ name }}"></option>
|
|
{% endfor %}
|
|
</datalist>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label for="excerpt">Excerpt</label>
|
|
<input type="text" id="excerpt" name="excerpt" value="{{ (post or {}).get('excerpt', '') if post else '' }}" placeholder="Short summary shown on the homepage">
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="tags">Tags</label>
|
|
<input type="text" id="tags" name="tags" value="{{ (post or {}).get('tags', '') if post else '' }}" placeholder="ai, selfhost, tips">
|
|
<span class="field-hint">Comma separated.</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="body">Body <span class="field-hint">(Rich text editor)</span></label>
|
|
<!-- Hidden field holds the stored HTML. TinyMCE loads it into #body-editor
|
|
on init and writes it back here on submit; the public pipeline passes
|
|
stored HTML straight through (render_markdown/to_html detect it). -->
|
|
<!-- forceescape is REQUIRED: body_html is Markup, so Jinja would emit
|
|
quotes unescaped and the first src="/..." would terminate the value
|
|
attribute, truncating the stored body. -->
|
|
<input type="hidden" id="body" name="body" value="{{ body_html|forceescape }}">
|
|
<div id="body-editor" class="tinymce-editor"></div>
|
|
</div>
|
|
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label for="status">Status</label>
|
|
<select id="status" name="status">
|
|
<option value="published" {% if post and post.status == 'published' %}selected{% endif %}>Published</option>
|
|
<option value="draft" {% if post and post.status == 'draft' %}selected{% endif %}>Draft</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="post_date">Post date</label>
|
|
<input type="date" id="post_date" name="post_date"
|
|
value="{{ post.created_at[:10] if post and post.created_at else request.form.get('post_date', '') }}">
|
|
<span class="field-hint">Shown on the post and used for ordering and archive months. Pick an earlier date to backdate it; leave blank for today.</span>
|
|
</div>
|
|
</div>
|
|
|
|
<p>
|
|
<button type="submit" class="btn btn-primary">Save</button>
|
|
{% if post %}<a class="btn" href="{{ preview_url(post) }}" target="_blank" rel="noreferrer">Preview</a>{% endif %}
|
|
<a class="btn" href="{{ url_for('admin.posts') }}">Cancel</a>
|
|
</p>
|
|
</form>
|
|
</div>
|
|
|
|
<script>
|
|
// TinyMCE: load stored HTML on init, write it back to #body on submit,
|
|
// and upload images to /admin/upload (multipart csrf_token form field).
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
var bodyField = document.getElementById('body');
|
|
console.log('[debug] editor init started; tinymce global defined:', !!(window.tinymce));
|
|
try {
|
|
tinymce.init({
|
|
// Community edition — no license key → no tin.cloud validation, works on any domain.
|
|
// Asset resolution uses TWO TinyMCE mechanisms (see static/tinymce.min.js):
|
|
// * base_url → drives plugins/icons/skins/langs (built as baseURL + "/plugins/..." etc.)
|
|
// * theme_url / model_url / icons_url → each asset's own URL, with a RELATIVE fallback
|
|
// when unset. On this subpath app the fallback would resolve against /admin/ and 404,
|
|
// so every non-base_url asset is given an explicit ABSOLUTE url.
|
|
// js_base (defined in base.html from request.host) makes these match however the
|
|
// app is reached — no hardcoded localhost, so it works on your LAN host too.
|
|
base_url: '{{ js_base }}',
|
|
theme_url: '{{ js_base }}/themes/silver/theme.min.js',
|
|
icons_url: '{{ js_base }}/icons/default/icons.min.js',
|
|
model_url: '{{ js_base }}/models/dom/model.min.js',
|
|
// Community Edition — license_key: 'gpl' agrees to the open-source terms so the bundled
|
|
// LicenseKeyManager plugin takes the 'use_gpl' path and does NOT request its own plugin
|
|
// file (plugins/licensekeymanager/plugin.min.js), which we don't serve and which 404s.
|
|
// NOTE: the config option is 'license_key' (underscore). TinyMCE does NOT read
|
|
// camelCase 'licenseKey', so that value would be silently ignored and the editor would
|
|
// be disabled. Free, works on ANY domain (no tiny.cloud validation, no Setup Partner bot).
|
|
license_key: 'gpl',
|
|
skin: 'oxide', // oxide SKIN provides BOTH the toolbar chrome AND content.min.css.
|
|
// NOTE: do NOT pair silver theme here — the silver theme looks for its own
|
|
// skins/content/silver/content.css (absent) and falls back to skins/content/default/...
|
|
// which 404s. The oxide skin is self-contained, so no separate theme is needed.
|
|
content_css: '/static/js/skins/ui/oxide/content.min.css',
|
|
selector: '#body-editor',
|
|
// NOTE: there is NO `initialData` option in TinyMCE (that's TipTap/
|
|
// ProseMirror). It is silently ignored -> the editor always opened
|
|
// empty, so editing an existing post showed no body. Set it explicitly
|
|
// once the editor instance exists.
|
|
init_instance_callback: function (editor) {
|
|
editor.setContent(bodyField ? bodyField.value : '');
|
|
},
|
|
// Keep stored image URLs as written (/static/uploads/...). Without
|
|
// this TinyMCE rewrites them relative to the CURRENT page (/admin/...),
|
|
// so a body saved on the edit page stored '../../static/uploads/...'.
|
|
convert_urls: false,
|
|
plugins: 'anchor autolink charmap codesample emoticons image link lists media searchreplace table visualblocks wordcount',
|
|
toolbar: 'undo redo | blocks fontfamily fontsize | bold italic underline strikethrough | link image media table | align lineheight | numlist bullist indent outdent | emoticons charmap | removeformat',
|
|
// NOTE: TinyMCE 8 calls this as (blobInfo, progress) and awaits the
|
|
// RETURNED PROMISE, which must resolve to the final URL string (or an
|
|
// object with .url). The old v5 (blobInfo, success, failure) callback
|
|
// form is gone: calling success(url) here hits the progress callback,
|
|
// the promise resolves to undefined, and TinyMCE throws
|
|
// "Cannot read properties of undefined (reading 'url')".
|
|
images_upload_handler: function (blobInfo) {
|
|
var f = blobInfo.blob();
|
|
// NOTE: use the MIME SUBTYPE (index 1), not the category (index 0).
|
|
// 'image/jpeg'.split('/')[0] === 'image' (matches no map key -> falls back to 'png'),
|
|
// which labels a JPEG as .png and fails the server's PNG signature check (400).
|
|
var type = (f.type || '').split('/')[1];
|
|
var ext = { png: 'png', jpg: 'jpg', jpeg: 'jpg', gif: 'gif', webp: 'webp' }[type] || 'png';
|
|
var fd = new FormData();
|
|
fd.append('file', f, 'tiny-image.' + ext);
|
|
var csrf = document.querySelector('[name="csrf_token"]');
|
|
if (csrf) fd.append('csrf_token', csrf.value);
|
|
return fetch('/admin/upload', { method: 'POST', body: fd })
|
|
.then(function (r) {
|
|
if (!r.ok) throw new Error('Upload failed: HTTP ' + r.status);
|
|
return r.json();
|
|
})
|
|
.then(function (d) {
|
|
if (!d || !d.url) throw new Error('Upload response had no url');
|
|
return d.url;
|
|
});
|
|
},
|
|
images_restricted_types: 'image/png,image/jpeg,image/gif,image/webp',
|
|
});
|
|
console.log('[debug] init returned; editors created:', tinymce.get().length, '| body-editor exists:', tinymce.get('body-editor') !== null);
|
|
|
|
// NOTE: bind to the editor's OWN form. document.querySelector('form')
|
|
// returned the logout form in the admin header (first form in the DOM),
|
|
// so this listener never ran for the post form: the editor's content was
|
|
// never copied into #body and every NEW post saved an empty body.
|
|
var form = bodyField ? bodyField.form : null;
|
|
if (form) {
|
|
form.addEventListener('submit', function () {
|
|
// NOTE: tinymce.get() takes an ELEMENT ID, not a CSS selector.
|
|
// tinymce.get('#body-editor') returns null -> .getContent() throws ->
|
|
// the old silent catch left #body empty, so every save stored no body.
|
|
var editor = tinymce.get('body-editor') || tinymce.activeEditor;
|
|
if (editor && bodyField) bodyField.value = editor.getContent();
|
|
});
|
|
}
|
|
} catch (e) {
|
|
console.error('[debug] editor init FAILED:', e);
|
|
}
|
|
});
|
|
</script>
|
|
{% endblock %}
|