Skip to content

Commit 969f906

Browse files
committed
fix: post delete not works
1 parent b602ec4 commit 969f906

5 files changed

Lines changed: 86 additions & 30 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"vue-tsc": "^2.2.0"
3434
},
3535
"config": {
36-
"base": "/data/",
36+
"base": "http://192.168.1.1/.www/blog/data/",
3737
"post_dir": "post/",
3838
"page_dir": "page/",
3939
"static_dir": "static/",

src/admin/driver.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ export class RemoteFile{
1818
return res;
1919
}
2020

21+
static async __delete(f: string){
22+
this.__check_enabled();
23+
const url = `${CONFIG.davroot}${CONFIG.davbase ?? config.base}${f}`;
24+
const res = await fetch(url, {
25+
method: 'DELETE'
26+
});
27+
if(!res.ok) throw new Error(`Failed to delete ${url}: ${res.statusText}`);
28+
return res;
29+
}
30+
2131
static async __put(f: string, data: XMLHttpRequestBodyInit){
2232
this.__check_enabled();
2333
const url = `${CONFIG.davroot}${CONFIG.davbase ?? config.base}${f}`;
@@ -96,4 +106,17 @@ ${contents}
96106
// repack
97107
const str = await exportIndex();
98108
await RemoteFile.__put(config.index, str);
109+
}
110+
111+
export async function delete_post(post: Post) {
112+
post.del();
113+
const rpath = config.post_dir + post.info.name + '.md';
114+
const ctx = await RemoteFile.__get(rpath).then(r => r.text());
115+
await RemoteFile.__delete(rpath);
116+
117+
// repack
118+
const str = await exportIndex();
119+
await RemoteFile.__put(config.index, str);
120+
121+
console.warn('Post deleted:', post.info, '\ndeleted content:\n', ctx);
99122
}

src/admin/postlist.vue

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,33 @@
22
<div class="post-list-container" tabindex="1" @blur="activePost = null">
33
<!-- 筛选控件 -->
44
<div class="filter-controls">
5-
<div class="filter-row" style="width: 20%;">
6-
<Autofill
7-
v-model="searchQuery"
8-
:options="posts.map(p => p.title)"
9-
placeholder="搜索文章标题..."
10-
@select="searchQuery = $event as string"
11-
/>
12-
</div>
13-
14-
<div class="filter-row">
15-
<Autofill
16-
v-model="selectedCategory"
17-
:options="['所有分类', ...allCategories]"
18-
placeholder="选择分类"
19-
@select="selectedCategory = $event == '所有分类' ? null : $event as string"
20-
/>
21-
</div>
22-
23-
<div class="filter-row">
24-
<Autofill
25-
v-model="tagQuery"
26-
:options="allTags"
27-
placeholder="选择标签"
28-
@select="selectedTags.includes($event as string) || selectedTags.push($event as string); tagQuery = ''"
29-
/>
5+
<div class="filter-col">
6+
<div class="filter-row" style="width: 20%;">
7+
<Autofill
8+
v-model="searchQuery"
9+
:options="posts.map(p => p.title)"
10+
placeholder="搜索文章标题..."
11+
@select="searchQuery = $event as string"
12+
/>
13+
</div>
14+
15+
<div class="filter-row">
16+
<Autofill
17+
v-model="selectedCategory"
18+
:options="['所有分类', ...allCategories]"
19+
placeholder="选择分类"
20+
@select="selectedCategory = $event == '所有分类' ? null : $event as string"
21+
/>
22+
</div>
23+
24+
<div class="filter-row">
25+
<Autofill
26+
v-model="tagQuery"
27+
:options="allTags"
28+
placeholder="选择标签"
29+
@select="selectedTags.includes($event as string) || selectedTags.push($event as string); tagQuery = ''"
30+
/>
31+
</div>
3032
</div>
3133

3234
<div v-if="selectedTags.length" class="selected-tags">
@@ -52,15 +54,15 @@
5254
<div class="post-header">
5355
<h3 class="post-title">{{ post.title }}</h3>
5456
<div class="post-meta">
55-
<button class="delete-btn" @click.stop="handleDelete(post.name)">删除</button>
57+
<button class="delete-btn" @click.stop="handleDelete(post.name, $event)">删除</button>
5658
<span class="post-date">{{ formatDate(post.created) }}</span>
5759
<span v-if="post.category" class="post-category">{{ post.category }}</span>
5860
</div>
5961
</div>
6062

6163
<transition name="slide-fade">
6264
<div v-show="activePost === post.name" class="post-details">
63-
<p v-if="post.outline" class="post-outline">{{ post.outline }}</p>
65+
<p v-if="post.outline" class="post-outline">{{ post.outline }} ...</p>
6466
<div v-if="post.tags && post.tags.length" class="post-tags">
6567
<span v-for="tag in post.tags" :key="tag" class="tag">{{ tag }}</span>
6668
</div>
@@ -155,9 +157,16 @@
155157
}
156158
})
157159
158-
const handleDelete = (postName: string) => {
160+
const handleDelete = (postName: string, ev: Event) => {
159161
if(confirm('确定要删除这篇文章吗?')) {
160162
emit('delete', postName)
163+
164+
// remove element
165+
let el = ev.target as HTMLElement;
166+
while(!el.classList.contains('post-item')){
167+
el = el.parentElement as HTMLElement;
168+
}
169+
el.remove();
161170
}
162171
}
163172
@@ -191,6 +200,9 @@
191200
padding: 1.5rem;
192201
background-color: #f8f9fa;
193202
border-radius: 0.8rem;
203+
}
204+
205+
.filter-controls .filter-col {
194206
display: flex;
195207
gap: 1.2rem;
196208
}

src/admin/posts.vue

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { Post } from '../utils/post';
44
import Postlist from './postlist.vue';
55
import { useRouter } from 'vue-router';
6+
import { delete_post } from './driver';
67
78
const $route = useRouter();
89
function goto(post: IPost){
@@ -13,12 +14,23 @@
1314
}
1415
});
1516
}
17+
18+
function delPost(postName: string){
19+
const post = Post.get(postName);
20+
if(!post) throw new Error('Post not found');
21+
delete_post(post).catch(e => {
22+
console.error(e);
23+
alert('删除失败')
24+
});
25+
}
1626
</script>
1727

1828
<template>
1929
<div class="posts-wrapper">
2030
<h1>文章列表</h1>
21-
<Postlist :posts="Post.get_all().array.map(i => markRaw(i.info))" @navigate="goto" />
31+
<Postlist :posts="Post.get_all().array.map(i => markRaw(i.info))" @navigate="goto"
32+
@delete="delPost"
33+
/>
2234
</div>
2335
</template>
2436

src/utils/post.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,15 @@ export class Post{
268268
this.$post.modified = Date.now();
269269
}
270270
}
271+
272+
del(){
273+
if(!this.$create){
274+
const idx = cache.post.findIndex(post => post.name === this.$post.name);
275+
if(idx >= 0) cache.post.splice(idx, 1);
276+
}
277+
this.$create = false;
278+
console.log('Post deleted:', this.$post.name);
279+
}
271280
}
272281

273282
// 初始化post

0 commit comments

Comments
 (0)