Skip to content

Commit

Permalink
feat(#15): implement view post online
Browse files Browse the repository at this point in the history
  • Loading branch information
laggage committed Dec 30, 2021
1 parent a6c5520 commit 39049ac
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
16 changes: 15 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@
"command": "vscode-cnb.open-workspace",
"title": "打开工作空间",
"category": "Cnblogs"
},
{
"command": "vscode-cnb.view-post-online",
"title": "在线查看博文",
"category": "Cnblogs"
}
],
"configuration": {
Expand Down Expand Up @@ -367,6 +372,10 @@
{
"command": "vscode-cnb.login",
"when": "false"
},
{
"command": "vscode-cnb.view-post-online",
"when": "false"
}
],
"view/item/context": [
Expand Down Expand Up @@ -426,7 +435,12 @@
{
"command": "vscode-cnb.reveal-local-post-file-in-os",
"when": "view == cnblogs-posts-list",
"group": "@3"
"group": "0@1"
},
{
"command": "vscode-cnb.view-post-online",
"when": "view == cnblogs-posts-list",
"group": "0@2"
},
{
"command": "vscode-cnb.delete-selected-post-categories",
Expand Down
2 changes: 2 additions & 0 deletions src/commands/commands-registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { openPostInBlogAdmin } from './open-post-in-blog-admin';
import { openWorkspace } from './open-workspace';
import { setWorkspace } from './set-workspace';
import { revealWorkspaceInOs } from './reveal-workspace-in-os';
import { viewPostOnline } from './view-post-online';

export const registerCommands = () => {
const context = globalState.extensionContext;
Expand Down Expand Up @@ -70,6 +71,7 @@ export const registerCommands = () => {
vscode.commands.registerCommand(`${appName}.open-workspace`, openWorkspace),
vscode.commands.registerCommand(`${appName}.set-workspace`, setWorkspace),
vscode.commands.registerCommand(`${appName}.reveal-workspace-in-os`, revealWorkspaceInOs),
vscode.commands.registerCommand(`${appName}.view-post-online`, viewPostOnline),
];
context?.subscriptions.push(...disposables);
};
5 changes: 4 additions & 1 deletion src/commands/show-local-file-to-post-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { blogPostService } from '../services/blog-post.service';
import { postCategoryService } from '../services/post-category.service';
import { PostFileMapManager } from '../services/post-file-map';
import { searchPostsByTitle } from '../services/search-post-by-title';
import { viewPostOnline } from './view-post-online';

/**
* 本地文件所关联的博文信息
Expand Down Expand Up @@ -54,7 +55,7 @@ export const showLocalFileToPostInfo = async (input: Uri | number): Promise<void
categories = categories.filter(x => post.categoryIds.includes(x.categoryId));
const categoryDesc = categories.length > 0 ? `博文分类: ${categories.map(c => c.title).join(', ')}\n` : '';
const tagsDesc = post.tags?.length ?? 0 > 0 ? `博文标签: ${post.tags?.join(', ')}\n` : '';
const options = ['取消关联'];
const options = ['在线查看博文', '取消关联'];
const postUrl = post.url.startsWith('//') ? `https:${post.url}` : post.url;
const selected = await window.showInformationMessage(
`关联博文 - ${post.title}(Id: ${post.id})`,
Expand All @@ -67,6 +68,8 @@ export const showLocalFileToPostInfo = async (input: Uri | number): Promise<void
...options
);
if (selected === options[0]) {
await viewPostOnline(post);
} else if (selected === options[1]) {
await PostFileMapManager.updateOrCreate(postId, '');
AlertService.info(`博文 ${post.title} 已与 ${filePath} 取消关联`);
}
Expand Down
21 changes: 21 additions & 0 deletions src/commands/view-post-online.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { commands, Uri } from 'vscode';
import { BlogPost } from '../models/blog-post';
import { blogPostService } from '../services/blog-post.service';
import { PostFileMapManager } from '../services/post-file-map';

export const viewPostOnline = async (input: BlogPost | Uri) => {
let post: BlogPost | undefined = input instanceof BlogPost ? input : undefined;
if (input instanceof Uri) {
const postId = PostFileMapManager.getPostId(input.fsPath);
if (postId) {
post = (await blogPostService.fetchPostEditDto(postId)).post;
}
}

if (!post) {
return;
}

const url = post.url.startsWith('//') ? `https:${post.url}` : post.url;
await commands.executeCommand('vscode.open', Uri.parse(url));
};

0 comments on commit 39049ac

Please sign in to comment.