Browse Source

fix(editor): load article by id for editing drafts

Use admin GET /api/admin/articles/{id} instead of public slug detail,
which only returns PUBLISHED articles, so drafts (and Chinese-title
articles with empty slugs) loaded blank in the editor.
main
Mohan 1 month ago
parent
commit
7e8a713a89
  1. 12
      src/views/admin/ArticleEditorView.vue
  2. 3
      src/views/admin/ArticleListView.vue

12
src/views/admin/ArticleEditorView.vue

@ -1,7 +1,7 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed, onMounted, onBeforeUnmount, watch } from 'vue' import { ref, computed, onMounted, onBeforeUnmount, watch } from 'vue'
import { useRoute, useRouter, onBeforeRouteLeave } from 'vue-router' import { useRoute, useRouter, onBeforeRouteLeave } from 'vue-router'
import { articleAdminApi, articleApi, tagApi, mediaApi } from '@/api/client'
import { articleAdminApi, tagApi, mediaApi } from '@/api/client'
import type { ArticleDetail, Tag } from '@/generated/api' import type { ArticleDetail, Tag } from '@/generated/api'
import QuillEditor from '@/components/QuillEditor.vue' import QuillEditor from '@/components/QuillEditor.vue'
@ -181,12 +181,14 @@ function matchTagIds(detail: ArticleDetail): number[] {
} }
async function loadArticle() { async function loadArticle() {
const slug = route.query.slug as string | undefined
if (!slug) return
const idParam = route.params.id
if (!idParam) return
const id = Number(idParam)
if (Number.isNaN(id)) return
loading.value = true loading.value = true
error.value = null error.value = null
try { try {
const resp = await articleApi.apiArticlesSlugGet(slug)
const resp = await articleAdminApi.apiAdminArticlesIdGet(id)
const detail = resp.data.data const detail = resp.data.data
if (detail) { if (detail) {
title.value = detail.title title.value = detail.title
@ -240,7 +242,7 @@ async function removeCover() {
} }
// ========== ========== // ========== ==========
onBeforeRouteLeave((to, _from, next) => {
onBeforeRouteLeave((_to, _from, next) => {
if (hasUnsavedChanges.value) { if (hasUnsavedChanges.value) {
const ok = window.confirm('您有未保存的内容,确定要离开吗?') const ok = window.confirm('您有未保存的内容,确定要离开吗?')
if (ok) { if (ok) {

3
src/views/admin/ArticleListView.vue

@ -68,8 +68,7 @@ function onPageChange(p: number) {
function onEdit(article: ArticleListItem) { function onEdit(article: ArticleListItem) {
router.push({ router.push({
name: 'AdminArticleEdit', name: 'AdminArticleEdit',
params: { id: String(article.id) },
query: { slug: article.slug }
params: { id: String(article.id) }
}) })
} }

Loading…
Cancel
Save