Fix slug
This commit is contained in:
parent
2513e19b37
commit
c615791090
1 changed files with 20 additions and 7 deletions
|
|
@ -12,6 +12,7 @@
|
||||||
postId?: number
|
postId?: number
|
||||||
initialData?: {
|
initialData?: {
|
||||||
title?: string
|
title?: string
|
||||||
|
slug?: string
|
||||||
content?: JSONContent
|
content?: JSONContent
|
||||||
gallery?: Media[]
|
gallery?: Media[]
|
||||||
status: 'draft' | 'published'
|
status: 'draft' | 'published'
|
||||||
|
|
@ -29,6 +30,7 @@
|
||||||
|
|
||||||
// Form data
|
// Form data
|
||||||
let title = $state(initialData?.title || '')
|
let title = $state(initialData?.title || '')
|
||||||
|
let slug = $state(initialData?.slug || '')
|
||||||
let content = $state<JSONContent>({ type: 'doc', content: [] })
|
let content = $state<JSONContent>({ type: 'doc', content: [] })
|
||||||
let gallery = $state<Media[]>([])
|
let gallery = $state<Media[]>([])
|
||||||
let tags = $state(initialData?.tags?.join(', ') || '')
|
let tags = $state(initialData?.tags?.join(', ') || '')
|
||||||
|
|
@ -36,6 +38,16 @@
|
||||||
// Editor ref
|
// Editor ref
|
||||||
let editorRef: any
|
let editorRef: any
|
||||||
|
|
||||||
|
// Auto-generate slug from title
|
||||||
|
$effect(() => {
|
||||||
|
if (title && !slug) {
|
||||||
|
slug = title
|
||||||
|
.toLowerCase()
|
||||||
|
.replace(/[^a-z0-9]+/g, '-')
|
||||||
|
.replace(/^-+|-+$/g, '')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// Initialize data for edit mode
|
// Initialize data for edit mode
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
if (initialData && mode === 'edit') {
|
if (initialData && mode === 'edit') {
|
||||||
|
|
@ -114,7 +126,7 @@
|
||||||
try {
|
try {
|
||||||
const postData = {
|
const postData = {
|
||||||
title: title.trim(),
|
title: title.trim(),
|
||||||
slug: generateSlug(title),
|
slug: slug,
|
||||||
postType: 'album',
|
postType: 'album',
|
||||||
status: newStatus,
|
status: newStatus,
|
||||||
content,
|
content,
|
||||||
|
|
@ -155,12 +167,6 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateSlug(title: string): string {
|
|
||||||
return title
|
|
||||||
.toLowerCase()
|
|
||||||
.replace(/[^a-z0-9]+/g, '-')
|
|
||||||
.replace(/^-+|-+$/g, '')
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleCancel() {
|
function handleCancel() {
|
||||||
if (hasChanges() && !confirm('Are you sure you want to cancel? Your changes will be lost.')) {
|
if (hasChanges() && !confirm('Are you sure you want to cancel? Your changes will be lost.')) {
|
||||||
|
|
@ -242,6 +248,13 @@
|
||||||
required={true}
|
required={true}
|
||||||
error={title.trim().length === 0 ? 'Title is required' : undefined}
|
error={title.trim().length === 0 ? 'Title is required' : undefined}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<Input
|
||||||
|
label="Slug"
|
||||||
|
bind:value={slug}
|
||||||
|
placeholder="album-url-slug"
|
||||||
|
helpText="URL-friendly version of the title"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-section">
|
<div class="form-section">
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue