Skip to content

Commit 39049ac

Browse files
committed
feat(#15): implement view post online
1 parent a6c5520 commit 39049ac

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

package.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@
199199
"command": "vscode-cnb.open-workspace",
200200
"title": "打开工作空间",
201201
"category": "Cnblogs"
202+
},
203+
{
204+
"command": "vscode-cnb.view-post-online",
205+
"title": "在线查看博文",
206+
"category": "Cnblogs"
202207
}
203208
],
204209
"configuration": {
@@ -367,6 +372,10 @@
367372
{
368373
"command": "vscode-cnb.login",
369374
"when": "false"
375+
},
376+
{
377+
"command": "vscode-cnb.view-post-online",
378+
"when": "false"
370379
}
371380
],
372381
"view/item/context": [
@@ -426,7 +435,12 @@
426435
{
427436
"command": "vscode-cnb.reveal-local-post-file-in-os",
428437
"when": "view == cnblogs-posts-list",
429-
"group": "@3"
438+
"group": "0@1"
439+
},
440+
{
441+
"command": "vscode-cnb.view-post-online",
442+
"when": "view == cnblogs-posts-list",
443+
"group": "0@2"
430444
},
431445
{
432446
"command": "vscode-cnb.delete-selected-post-categories",

src/commands/commands-registration.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { openPostInBlogAdmin } from './open-post-in-blog-admin';
3232
import { openWorkspace } from './open-workspace';
3333
import { setWorkspace } from './set-workspace';
3434
import { revealWorkspaceInOs } from './reveal-workspace-in-os';
35+
import { viewPostOnline } from './view-post-online';
3536

3637
export const registerCommands = () => {
3738
const context = globalState.extensionContext;
@@ -70,6 +71,7 @@ export const registerCommands = () => {
7071
vscode.commands.registerCommand(`${appName}.open-workspace`, openWorkspace),
7172
vscode.commands.registerCommand(`${appName}.set-workspace`, setWorkspace),
7273
vscode.commands.registerCommand(`${appName}.reveal-workspace-in-os`, revealWorkspaceInOs),
74+
vscode.commands.registerCommand(`${appName}.view-post-online`, viewPostOnline),
7375
];
7476
context?.subscriptions.push(...disposables);
7577
};

src/commands/show-local-file-to-post-info.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { blogPostService } from '../services/blog-post.service';
55
import { postCategoryService } from '../services/post-category.service';
66
import { PostFileMapManager } from '../services/post-file-map';
77
import { searchPostsByTitle } from '../services/search-post-by-title';
8+
import { viewPostOnline } from './view-post-online';
89

910
/**
1011
* 本地文件所关联的博文信息
@@ -54,7 +55,7 @@ export const showLocalFileToPostInfo = async (input: Uri | number): Promise<void
5455
categories = categories.filter(x => post.categoryIds.includes(x.categoryId));
5556
const categoryDesc = categories.length > 0 ? `博文分类: ${categories.map(c => c.title).join(', ')}\n` : '';
5657
const tagsDesc = post.tags?.length ?? 0 > 0 ? `博文标签: ${post.tags?.join(', ')}\n` : '';
57-
const options = ['取消关联'];
58+
const options = ['在线查看博文', '取消关联'];
5859
const postUrl = post.url.startsWith('//') ? `https:${post.url}` : post.url;
5960
const selected = await window.showInformationMessage(
6061
`关联博文 - ${post.title}(Id: ${post.id})`,
@@ -67,6 +68,8 @@ export const showLocalFileToPostInfo = async (input: Uri | number): Promise<void
6768
...options
6869
);
6970
if (selected === options[0]) {
71+
await viewPostOnline(post);
72+
} else if (selected === options[1]) {
7073
await PostFileMapManager.updateOrCreate(postId, '');
7174
AlertService.info(`博文 ${post.title} 已与 ${filePath} 取消关联`);
7275
}

src/commands/view-post-online.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { commands, Uri } from 'vscode';
2+
import { BlogPost } from '../models/blog-post';
3+
import { blogPostService } from '../services/blog-post.service';
4+
import { PostFileMapManager } from '../services/post-file-map';
5+
6+
export const viewPostOnline = async (input: BlogPost | Uri) => {
7+
let post: BlogPost | undefined = input instanceof BlogPost ? input : undefined;
8+
if (input instanceof Uri) {
9+
const postId = PostFileMapManager.getPostId(input.fsPath);
10+
if (postId) {
11+
post = (await blogPostService.fetchPostEditDto(postId)).post;
12+
}
13+
}
14+
15+
if (!post) {
16+
return;
17+
}
18+
19+
const url = post.url.startsWith('//') ? `https:${post.url}` : post.url;
20+
await commands.executeCommand('vscode.open', Uri.parse(url));
21+
};

0 commit comments

Comments
 (0)