Files
plugi/extensions/plugi-ext-blog/admin/Views/EditPost.php
T
deepDiverPaul 19e6b1a332 feat(ext-blog): Add rich Markdown editor for blog posts
Replace the plain textarea with a CodeMirror-based editor supporting syntax highlighting, formatting toolbar (bold, italic, lists, code blocks), and a live preview toggle. Update PostContentRenderer to handle new Markdown syntax (blockquotes, fenced code blocks). Add a preview endpoint for rendering content without saving.
2026-12-02 11:09:09 +01:00

192 lines
13 KiB
PHP

<?php
use Plugi\Extensions\Blog\CategoriesRepository;
use Plugi\Extensions\Blog\PostsRepository;
use Plugi\Repositories\UploadRepository;
$postsRepository = new PostsRepository();
$post = [
'title' => '',
'slug' => '',
'excerpt' => '',
'body' => '',
'featured_image_id' => null,
'category_id' => null,
'status' => 'draft',
'published_at' => null,
];
if (isset($_GET['id']) && ctype_digit((string) $_GET['id'])) {
$post = $postsRepository->findWithId($_GET['id']) ?? $post;
}
$isEditing = isset($post['id']);
$publishedAt = $post['published_at'] ? date('Y-m-d\TH:i', strtotime($post['published_at'])) : '';
$categories = (new CategoriesRepository())->getAllForAdmin();
$imageUploads = array_values(array_filter((new UploadRepository())->getAll(), static fn($file): bool => is_string($file->mime_type ?? null) && str_starts_with(strtolower($file->mime_type), 'image/')));
?>
<header class="mb-6 flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
<div class="flex items-center gap-3">
<a href="<?= phpb_url('website_manager', ['tab' => 'ext--blog', 'action' => 'posts']) ?>" class="btn btn-ghost btn-square btn-sm" aria-label="Back to posts" title="Back to posts">
<i class="ph-duotone ph-arrow-left text-xl" aria-hidden="true"></i>
</a>
<div class="space-y-1">
<p class="text-xs font-semibold uppercase tracking-[0.18em] text-primary">Blog editor</p>
<h2 class="text-xl font-bold tracking-tight"><?= $isEditing ? 'Edit post' : 'New post' ?></h2>
<p class="text-sm text-base-content/60">Save as a draft or publish the article.</p>
</div>
</div>
</header>
<form action="/ext--blog/save-post" method="post" class="space-y-6">
<?php phpb_csrf_field(); ?>
<?php if ($isEditing): ?>
<input type="hidden" name="id" value="<?= phpb_e((string) $post['id']) ?>">
<?php endif; ?>
<section class="card border border-base-300 bg-base-100 shadow-sm">
<div class="card-body gap-5 p-4 sm:p-6 lg:p-8">
<div>
<label for="post-title" class="label"><span class="label-text font-medium">Title</span></label>
<input id="post-title" type="text" name="title" maxlength="255" required value="<?= phpb_e($post['title']) ?>" class="input w-full">
</div>
<div>
<label for="post-slug" class="label"><span class="label-text font-medium">Slug</span></label>
<input id="post-slug" type="text" name="slug" maxlength="191" pattern="[a-z0-9]+(-[a-z0-9]+)*" required value="<?= phpb_e($post['slug']) ?>" class="input w-full" aria-describedby="post-slug-help">
<p id="post-slug-help" class="mt-1 text-sm text-base-content/60">Use lowercase letters, numbers, and hyphens.</p>
</div>
<div>
<label for="post-excerpt" class="label"><span class="label-text font-medium">Excerpt</span></label>
<textarea id="post-excerpt" name="excerpt" rows="3" class="textarea w-full"><?= phpb_e($post['excerpt']) ?></textarea>
</div>
<div class="space-y-3">
<div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
<label for="post-body" class="label py-0"><span class="label-text font-medium">Post content</span></label>
<div id="post-body-view-tabs" role="tablist" class="tabs tabs-box hidden w-fit" aria-label="Post body view" hidden>
<button id="post-body-write-tab" type="button" role="tab" class="tab tab-active" aria-controls="post-body-write-panel" aria-selected="true" tabindex="0" data-view="write">Write</button>
<button id="post-body-preview-tab" type="button" role="tab" class="tab" aria-controls="post-body-preview-panel" aria-selected="false" tabindex="-1" data-view="preview">Preview</button>
</div>
</div>
<div id="post-body-write-panel" role="tabpanel" aria-labelledby="post-body-write-tab" tabindex="0" class="space-y-3">
<div class="overflow-hidden rounded-box border border-base-300 bg-base-100 shadow-sm">
<div id="post-body-toolbar" class="hidden flex flex-wrap gap-1 border-b border-base-300 bg-base-200/50 px-3 py-2" role="toolbar" aria-label="Markdown formatting" hidden>
<button type="button" class="btn btn-ghost btn-sm" data-format="bold" aria-label="Bold" title="Bold">Bold</button>
<button type="button" class="btn btn-ghost btn-sm" data-format="italic" aria-label="Italic" title="Italic">Italic</button>
<button type="button" class="btn btn-ghost btn-sm" data-format="heading" aria-label="Heading" title="Heading">Heading</button>
<button type="button" class="btn btn-ghost btn-sm" data-format="link" aria-label="Link" title="Link">Link</button>
<button type="button" class="btn btn-ghost btn-sm" data-format="list" aria-label="Bulleted list" title="Bulleted list">Bullets</button>
<button type="button" class="btn btn-ghost btn-sm" data-format="ordered-list" aria-label="Numbered list" title="Numbered list">Numbered list</button>
<button type="button" class="btn btn-ghost btn-sm" data-format="quote" aria-label="Blockquote" title="Blockquote">Quote</button>
<button type="button" class="btn btn-ghost btn-sm" data-format="horizontal-rule" aria-label="Horizontal rule" title="Horizontal rule">Rule</button>
<button type="button" class="btn btn-ghost btn-sm" data-format="code" aria-label="Inline code" title="Inline code">Code</button>
<button type="button" class="btn btn-ghost btn-sm" data-format="code-block" aria-label="Code block" title="Code block">Code block</button>
</div>
<div id="post-body-editor" class="blog-markdown-editor hidden w-full" hidden></div>
<textarea id="post-body" name="body" rows="16" class="textarea w-full rounded-none border-0 font-mono" aria-describedby="post-body-help"><?= phpb_e($post['body']) ?></textarea>
<p id="post-body-help" class="border-t border-base-300 px-4 py-2 text-sm text-base-content/60">Use Markdown for headings, lists, quotes, rules, code blocks, links, and emphasis.</p>
</div>
<div class="flex flex-col gap-3 sm:flex-row sm:items-end">
<div class="w-full sm:flex-1">
<label for="post-inline-image" class="label"><span class="label-text font-medium">Insert inline image</span></label>
<select id="post-inline-image" class="select w-full" aria-describedby="post-inline-image-help">
<option value="">Choose an image</option>
<?php foreach ($imageUploads as $file): ?>
<?php
$inlineImageUrl = $file->getUrl();
if ($file->original_file !== '') {
$inlineImageUrl = substr($inlineImageUrl, 0, -strlen($file->original_file)) . rawurlencode($file->original_file);
}
?>
<option value="<?= phpb_e($file->public_id) ?>" data-url="<?= phpb_e($inlineImageUrl) ?>" data-alt="<?= phpb_e($file->alt_text ?? '') ?>"><?= phpb_e($file->original_file) ?></option>
<?php endforeach; ?>
</select>
</div>
<button type="button" id="insert-post-inline-image" class="btn btn-outline" aria-label="Insert selected image at the cursor">Insert image</button>
</div>
<p id="post-inline-image-help" class="text-sm text-base-content/60">Alt text comes from the media library; the image Markdown is inserted at the cursor.</p>
</div>
<section id="post-body-preview-panel" role="tabpanel" aria-labelledby="post-body-preview-tab" tabindex="0" class="hidden" hidden>
<div class="min-h-96 overflow-hidden rounded-box border border-base-300 bg-base-100 shadow-sm">
<div class="flex flex-col gap-2 border-b border-base-300 bg-base-200/50 px-4 py-3 sm:flex-row sm:items-center sm:justify-between">
<div>
<h3 id="post-body-preview-heading" class="font-semibold">Live preview</h3>
<p class="text-sm text-base-content/60">Rendered like the published post.</p>
</div>
<p id="post-body-preview-status" class="text-sm text-base-content/60" role="status" aria-live="polite">Waiting for the preview…</p>
</div>
<div id="post-body-preview" class="min-h-80 space-y-4 p-5 sm:p-8" role="region" aria-label="Rendered post preview" aria-busy="false"></div>
</div>
</section>
</div>
<div>
<label for="post-featured-image" class="label"><span class="label-text font-medium">Featured image</span></label>
<select id="post-featured-image" name="featured_image_id" class="select w-full" aria-describedby="post-featured-image-help">
<option value="">No featured image</option>
<?php foreach ($imageUploads as $file): ?>
<option value="<?= phpb_e($file->public_id) ?>" <?= (string) $file->public_id === (string) ($post['featured_image_id'] ?? '') ? 'selected' : '' ?>><?= phpb_e($file->original_file) ?></option>
<?php endforeach; ?>
</select>
<p id="post-featured-image-help" class="mt-1 text-sm text-base-content/60">Choose an image from the existing media library.</p>
</div>
<div class="grid gap-5 sm:grid-cols-2">
<div>
<label for="post-category" class="label"><span class="label-text font-medium">Category</span></label>
<select id="post-category" name="category_id" class="select w-full">
<option value="">No category</option>
<?php foreach ($categories as $category): ?>
<option value="<?= phpb_e((string) $category['id']) ?>" <?= (string) $category['id'] === (string) $post['category_id'] ? 'selected' : '' ?>><?= phpb_e($category['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div>
<label for="post-status" class="label"><span class="label-text font-medium">Status</span></label>
<select id="post-status" name="status" class="select w-full">
<option value="draft" <?= $post['status'] === 'draft' ? 'selected' : '' ?>>Draft</option>
<option value="published" <?= $post['status'] === 'published' ? 'selected' : '' ?>>Published</option>
</select>
</div>
</div>
<div>
<label for="post-published-at" class="label"><span class="label-text font-medium">Publication date</span></label>
<input id="post-published-at" type="datetime-local" name="published_at" value="<?= phpb_e($publishedAt) ?>" class="input w-full" aria-describedby="post-published-at-help">
<p id="post-published-at-help" class="mt-1 text-sm text-base-content/60">Leave blank to use the current time when publishing. Future dates are not supported.</p>
</div>
</div>
</section>
<div class="flex flex-wrap justify-end gap-3">
<a href="<?= phpb_url('website_manager', ['tab' => 'ext--blog', 'action' => 'posts']) ?>" class="btn btn-ghost">Cancel</a>
<button type="submit" class="btn btn-primary">Save post</button>
</div>
</form>
<script>
(function () {
const body = document.getElementById('post-body');
const imageSelect = document.getElementById('post-inline-image');
const insertButton = document.getElementById('insert-post-inline-image');
if (!body || !imageSelect || !insertButton) {
return;
}
insertButton.addEventListener('click', function () {
const option = imageSelect.selectedOptions[0];
if (!option || !option.value) {
return;
}
const altText = option.dataset.alt || option.textContent.trim();
const markdownAlt = altText.replace(/[\r\n]+/g, ' ').replace(/&/g, '&amp;').replace(/\[/g, '&#91;').replace(/\]/g, '&#93;');
const markdown = `![${markdownAlt}](${option.dataset.url})`;
if (window.blogMarkdownEditor && window.blogMarkdownEditor.insertAtCursor(markdown)) {
return;
}
const start = body.selectionStart;
const end = body.selectionEnd;
const prefix = start > 0 && !/\s/.test(body.value[start - 1]) ? ' ' : '';
const suffix = end < body.value.length && !/\s/.test(body.value[end]) ? ' ' : '';
body.setRangeText(prefix + markdown + suffix, start, end, 'end');
body.dispatchEvent(new Event('input', { bubbles: true }));
body.focus();
});
})();
</script>