renamed editor
This commit is contained in:
@@ -30,6 +30,29 @@ pub async fn create_post(
|
|||||||
.join("posts")
|
.join("posts")
|
||||||
.join(format!("{}.md", payload.slug));
|
.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();
|
let mut file_content = String::new();
|
||||||
if let Some(ref summary) = payload.summary {
|
if let Some(ref summary) = payload.summary {
|
||||||
if !summary.trim().is_empty() {
|
if !summary.trim().is_empty() {
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ pub struct PostDetail {
|
|||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct CreatePostRequest {
|
pub struct CreatePostRequest {
|
||||||
pub slug: String,
|
pub slug: String,
|
||||||
|
pub old_slug: Option<String>,
|
||||||
pub summary: Option<String>,
|
pub summary: Option<String>,
|
||||||
pub content: String,
|
pub content: String,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import AdminLayout from '../../layouts/AdminLayout.astro';
|
|||||||
target="_blank"
|
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"
|
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
|
View Post
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
@@ -30,7 +30,7 @@ import AdminLayout from '../../layouts/AdminLayout.astro';
|
|||||||
|
|
||||||
<div class="space-y-6">
|
<div class="space-y-6">
|
||||||
<div>
|
<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
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
id="slug"
|
id="slug"
|
||||||
@@ -137,6 +137,8 @@ import AdminLayout from '../../layouts/AdminLayout.astro';
|
|||||||
const autocomplete = document.getElementById('autocomplete');
|
const autocomplete = document.getElementById('autocomplete');
|
||||||
const autocompleteList = document.getElementById('autocomplete-list');
|
const autocompleteList = document.getElementById('autocomplete-list');
|
||||||
|
|
||||||
|
let originalSlug = "";
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const editor = CodeMirror.fromTextArea(contentInput, {
|
const editor = CodeMirror.fromTextArea(contentInput, {
|
||||||
mode: 'markdown',
|
mode: 'markdown',
|
||||||
@@ -315,11 +317,12 @@ import AdminLayout from '../../layouts/AdminLayout.astro';
|
|||||||
saveBtn?.addEventListener('click', async () => {
|
saveBtn?.addEventListener('click', async () => {
|
||||||
const payload = {
|
const payload = {
|
||||||
slug: slugInput.value,
|
slug: slugInput.value,
|
||||||
|
old_slug: originalSlug || null,
|
||||||
summary: summaryInput.value || null,
|
summary: summaryInput.value || null,
|
||||||
content: editor.getValue()
|
content: editor.getValue()
|
||||||
};
|
};
|
||||||
if (!payload.slug || !payload.content) {
|
if (!payload.slug || !payload.content) {
|
||||||
showAlert('Slug and content are required.', 'error');
|
showAlert('Title and content are required.', 'error');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
@@ -333,13 +336,15 @@ import AdminLayout from '../../layouts/AdminLayout.astro';
|
|||||||
});
|
});
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
showAlert('Post saved!', 'success');
|
showAlert('Post saved!', 'success');
|
||||||
|
originalSlug = payload.slug; // Update for next save
|
||||||
if (viewPostBtn) {
|
if (viewPostBtn) {
|
||||||
viewPostBtn.href = `/posts/${payload.slug}`;
|
viewPostBtn.href = `/posts/${payload.slug}`;
|
||||||
viewPostBtn.classList.remove('hidden');
|
viewPostBtn.classList.remove('hidden');
|
||||||
viewPostBtn.classList.add('inline-flex');
|
viewPostBtn.classList.add('inline-flex');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
showAlert('Error saving post.', 'error');
|
const err = await res.json();
|
||||||
|
showAlert(`Error: ${err.error}`, 'error');
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
showAlert('Failed to connect to server.', 'error');
|
showAlert('Failed to connect to server.', 'error');
|
||||||
@@ -347,15 +352,16 @@ import AdminLayout from '../../layouts/AdminLayout.astro';
|
|||||||
});
|
});
|
||||||
|
|
||||||
delBtn?.addEventListener('click', async () => {
|
delBtn?.addEventListener('click', async () => {
|
||||||
if (confirm(`Delete post "${slugInput.value}" permanently?`)) {
|
const slugToDelete = originalSlug || slugInput.value;
|
||||||
|
if (confirm(`Delete post "${slugToDelete}" permanently?`)) {
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`/api/posts/${encodeURIComponent(slugInput.value)}`, {
|
const res = await fetch(`/api/posts/${encodeURIComponent(slugToDelete)}`, {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
headers: { 'Authorization': `Bearer ${token}` }
|
headers: { 'Authorization': `Bearer ${token}` }
|
||||||
});
|
});
|
||||||
if (res.ok) window.location.href = '/admin';
|
if (res.ok) window.location.href = '/admin';
|
||||||
else showAlert('Error deleting post.', 'error');
|
else showAlert('Error deleting post.', 'error');
|
||||||
} catch (err) {
|
} catch (e) {
|
||||||
showAlert('Connection error.', 'error');
|
showAlert('Connection error.', 'error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -365,7 +371,7 @@ import AdminLayout from '../../layouts/AdminLayout.astro';
|
|||||||
const editSlug = urlParams.get('edit');
|
const editSlug = urlParams.get('edit');
|
||||||
if (editSlug) {
|
if (editSlug) {
|
||||||
slugInput.value = editSlug;
|
slugInput.value = editSlug;
|
||||||
slugInput.disabled = true; // Protect slug on edit
|
originalSlug = editSlug;
|
||||||
delBtn?.classList.remove('hidden');
|
delBtn?.classList.remove('hidden');
|
||||||
|
|
||||||
if (viewPostBtn) {
|
if (viewPostBtn) {
|
||||||
|
|||||||
@@ -76,9 +76,8 @@ import AdminLayout from '../../layouts/AdminLayout.astro';
|
|||||||
div.innerHTML = `
|
div.innerHTML = `
|
||||||
<div>
|
<div>
|
||||||
<h3 class="font-bold text-lavender text-lg">${post.slug}</h3>
|
<h3 class="font-bold text-lavender text-lg">${post.slug}</h3>
|
||||||
<p class="text-xs text-subtext0">/posts/${post.slug}</p>
|
<p class="text-xs text-subtext0" style="color: var(--subtext0) !important;">/posts/${post.slug}</p>
|
||||||
</div>
|
</div> <div class="flex gap-3">
|
||||||
<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">
|
<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>
|
<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>
|
</a>
|
||||||
|
|||||||
Reference in New Issue
Block a user