renamed editor
This commit is contained in:
@@ -30,6 +30,29 @@ pub async fn create_post(
|
||||
.join("posts")
|
||||
.join(format!("{}.md", payload.slug));
|
||||
|
||||
// Handle renaming
|
||||
if let Some(ref old_slug) = payload.old_slug {
|
||||
if old_slug != &payload.slug {
|
||||
let old_file_path = state.data_dir.join("posts").join(format!("{}.md", old_slug));
|
||||
if old_file_path.exists() {
|
||||
// If new path already exists and it's different from old path, error out
|
||||
if file_path.exists() {
|
||||
return Err(AppError::BadRequest(
|
||||
"A post with this new title already exists".to_string(),
|
||||
));
|
||||
}
|
||||
if let Err(e) = fs::rename(&old_file_path, &file_path) {
|
||||
error!("Rename error from {} to {}: {}", old_slug, payload.slug, e);
|
||||
return Err(AppError::Internal(
|
||||
"Rename error".to_string(),
|
||||
Some(e.to_string()),
|
||||
));
|
||||
}
|
||||
info!("Renamed post from {} to {}", old_slug, payload.slug);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut file_content = String::new();
|
||||
if let Some(ref summary) = payload.summary {
|
||||
if !summary.trim().is_empty() {
|
||||
|
||||
@@ -44,6 +44,7 @@ pub struct PostDetail {
|
||||
#[derive(Deserialize)]
|
||||
pub struct CreatePostRequest {
|
||||
pub slug: String,
|
||||
pub old_slug: Option<String>,
|
||||
pub summary: Option<String>,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ import AdminLayout from '../../layouts/AdminLayout.astro';
|
||||
target="_blank"
|
||||
class="hidden bg-blue text-crust font-bold py-3 px-8 rounded-lg hover:bg-sky transition-all transform hover:scale-105 w-full md:w-auto whitespace-nowrap items-center justify-center gap-2"
|
||||
>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/><polyline points="15 3 21 3 21 9"/><line x1="10" x2="14" y2="10" x2="21" y2="3"/></svg>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/><polyline points="15 3 21 3 21 9"/><line x1="10" y1="14" x2="21" y2="3"/></svg>
|
||||
View Post
|
||||
</a>
|
||||
</div>
|
||||
@@ -30,7 +30,7 @@ import AdminLayout from '../../layouts/AdminLayout.astro';
|
||||
|
||||
<div class="space-y-6">
|
||||
<div>
|
||||
<label for="slug" class="block text-sm font-medium text-subtext1 mb-2">Slug (URL endpoint)</label>
|
||||
<label for="slug" class="block text-sm font-medium text-subtext1 mb-2">Post Title (URL identifier)</label>
|
||||
<input
|
||||
type="text"
|
||||
id="slug"
|
||||
@@ -137,6 +137,8 @@ import AdminLayout from '../../layouts/AdminLayout.astro';
|
||||
const autocomplete = document.getElementById('autocomplete');
|
||||
const autocompleteList = document.getElementById('autocomplete-list');
|
||||
|
||||
let originalSlug = "";
|
||||
|
||||
// @ts-ignore
|
||||
const editor = CodeMirror.fromTextArea(contentInput, {
|
||||
mode: 'markdown',
|
||||
@@ -315,11 +317,12 @@ import AdminLayout from '../../layouts/AdminLayout.astro';
|
||||
saveBtn?.addEventListener('click', async () => {
|
||||
const payload = {
|
||||
slug: slugInput.value,
|
||||
old_slug: originalSlug || null,
|
||||
summary: summaryInput.value || null,
|
||||
content: editor.getValue()
|
||||
};
|
||||
if (!payload.slug || !payload.content) {
|
||||
showAlert('Slug and content are required.', 'error');
|
||||
showAlert('Title and content are required.', 'error');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
@@ -333,13 +336,15 @@ import AdminLayout from '../../layouts/AdminLayout.astro';
|
||||
});
|
||||
if (res.ok) {
|
||||
showAlert('Post saved!', 'success');
|
||||
originalSlug = payload.slug; // Update for next save
|
||||
if (viewPostBtn) {
|
||||
viewPostBtn.href = `/posts/${payload.slug}`;
|
||||
viewPostBtn.classList.remove('hidden');
|
||||
viewPostBtn.classList.add('inline-flex');
|
||||
}
|
||||
} else {
|
||||
showAlert('Error saving post.', 'error');
|
||||
const err = await res.json();
|
||||
showAlert(`Error: ${err.error}`, 'error');
|
||||
}
|
||||
} catch (err) {
|
||||
showAlert('Failed to connect to server.', 'error');
|
||||
@@ -347,15 +352,16 @@ import AdminLayout from '../../layouts/AdminLayout.astro';
|
||||
});
|
||||
|
||||
delBtn?.addEventListener('click', async () => {
|
||||
if (confirm(`Delete post "${slugInput.value}" permanently?`)) {
|
||||
const slugToDelete = originalSlug || slugInput.value;
|
||||
if (confirm(`Delete post "${slugToDelete}" permanently?`)) {
|
||||
try {
|
||||
const res = await fetch(`/api/posts/${encodeURIComponent(slugInput.value)}`, {
|
||||
const res = await fetch(`/api/posts/${encodeURIComponent(slugToDelete)}`, {
|
||||
method: 'DELETE',
|
||||
headers: { 'Authorization': `Bearer ${token}` }
|
||||
});
|
||||
if (res.ok) window.location.href = '/admin';
|
||||
else showAlert('Error deleting post.', 'error');
|
||||
} catch (err) {
|
||||
} catch (e) {
|
||||
showAlert('Connection error.', 'error');
|
||||
}
|
||||
}
|
||||
@@ -365,7 +371,7 @@ import AdminLayout from '../../layouts/AdminLayout.astro';
|
||||
const editSlug = urlParams.get('edit');
|
||||
if (editSlug) {
|
||||
slugInput.value = editSlug;
|
||||
slugInput.disabled = true; // Protect slug on edit
|
||||
originalSlug = editSlug;
|
||||
delBtn?.classList.remove('hidden');
|
||||
|
||||
if (viewPostBtn) {
|
||||
|
||||
@@ -76,9 +76,8 @@ import AdminLayout from '../../layouts/AdminLayout.astro';
|
||||
div.innerHTML = `
|
||||
<div>
|
||||
<h3 class="font-bold text-lavender text-lg">${post.slug}</h3>
|
||||
<p class="text-xs text-subtext0">/posts/${post.slug}</p>
|
||||
</div>
|
||||
<div class="flex gap-3">
|
||||
<p class="text-xs text-subtext0" style="color: var(--subtext0) !important;">/posts/${post.slug}</p>
|
||||
</div> <div class="flex gap-3">
|
||||
<a href="/admin/editor?edit=${post.slug}" class="p-2 text-blue hover:bg-blue/10 rounded transition-colors" title="Edit">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17 3a2.85 2.83 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5Z"/><path d="m15 5 4 4"/></svg>
|
||||
</a>
|
||||
|
||||
Reference in New Issue
Block a user