Skip to content

Commit e9b15c1

Browse files
committed
feat: implement select and open workspace
1 parent 32c2dc5 commit e9b15c1

File tree

8 files changed

+64
-11
lines changed

8 files changed

+64
-11
lines changed

package.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
"workspaceContains:**",
2727
"onView:cnblogs-authorize",
2828
"onView:cnblogs-account",
29-
"onView:cnblogs-posts-list"
29+
"onView:cnblogs-posts-list",
30+
"onView:vscode-cnb-workspace",
31+
"onCommand:vscode-cnb.open-workspace"
3032
],
3133
"main": "./dist/extension.js",
3234
"contributes": {
@@ -183,7 +185,7 @@
183185
"title": "vscode-cnb",
184186
"properties": {
185187
"vscode-cnb.workspace": {
186-
"default": "",
188+
"default": "~/Documents/Cnblogs",
187189
"scope": "resource",
188190
"type": "string",
189191
"editPresentation": "singlelineText",
@@ -508,9 +510,19 @@
508510
},
509511
{
510512
"view": "vscode-cnb-workspace",
511-
"contents": "[打开工作空间](command:vscode-cnb.open-workspace)",
513+
"contents": "[在vscode中打开工作空间](command:vscode-cnb.open-workspace)",
512514
"when": "!vscode-cnb.isTargetWorkspace"
513515
},
516+
{
517+
"view": "vscode-cnb-workspace",
518+
"contents": "[在访达中打开工作空间](command:vscode-cnb.reveal-workspace-in-os)",
519+
"when": "isMac"
520+
},
521+
{
522+
"view": "vscode-cnb-workspace",
523+
"contents": "[在文件资源器中打开工作空间](command:vscode-cnb.reveal-workspace-in-os)",
524+
"when": "isWindows"
525+
},
514526
{
515527
"view": "vscode-cnb-workspace",
516528
"contents": "[设置工作空间](command:vscode-cnb.set-workspace)"

src/commands/commands-registration.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { openMyAccountSettings } from './open-my-account-settings';
2-
import { openMyBlogManagementBackground } from './open-my-blog-management-background';
2+
import { openMyWebBlogConsole } from './open-my-blog-management-background';
33
import { openMyHomePage } from './open-my-home-page';
44
import { login, logout } from './login';
55
import * as vscode from 'vscode';
@@ -30,6 +30,8 @@ import { deletePostToLocalFileMap } from './posts-list/delete-post-to-local-file
3030
import { renamePost } from './posts-list/rename-post';
3131
import { openPostInBlogAdmin } from './open-post-in-blog-admin';
3232
import { openWorkspace } from './open-workspace';
33+
import { setWorkspace } from './set-workspace';
34+
import { revealWorkspaceInOs } from './reveal-workspace-in-os';
3335

3436
export const registerCommands = () => {
3537
const context = globalState.extensionContext;
@@ -38,10 +40,7 @@ export const registerCommands = () => {
3840
vscode.commands.registerCommand(`${appName}.login`, login),
3941
vscode.commands.registerCommand(`${appName}.open-my-blog`, openMyBlog),
4042
vscode.commands.registerCommand(`${appName}.open-my-home-page`, openMyHomePage),
41-
vscode.commands.registerCommand(
42-
`${appName}.open-my-blog-management-background`,
43-
openMyBlogManagementBackground
44-
),
43+
vscode.commands.registerCommand(`${appName}.open-my-blog-management-background`, openMyWebBlogConsole),
4544
vscode.commands.registerCommand(`${appName}.open-my-account-settings`, openMyAccountSettings),
4645
vscode.commands.registerCommand(`${appName}.logout`, logout),
4746
vscode.commands.registerCommand(`${appName}.refresh-posts-list`, refreshPostsList),
@@ -69,6 +68,8 @@ export const registerCommands = () => {
6968
vscode.commands.registerCommand(`${appName}.rename-post`, renamePost),
7069
vscode.commands.registerCommand(`${appName}.open-post-in-blog-admin`, openPostInBlogAdmin),
7170
vscode.commands.registerCommand(`${appName}.open-workspace`, openWorkspace),
71+
vscode.commands.registerCommand(`${appName}.set-workspace`, setWorkspace),
72+
vscode.commands.registerCommand(`${appName}.reveal-workspace-in-os`, revealWorkspaceInOs),
7273
];
7374
context?.subscriptions.push(...disposables);
7475
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from 'vscode';
22

3-
export const openMyBlogManagementBackground = () => {
3+
export const openMyWebBlogConsole = () => {
44
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse('https://i.cnblogs.com'));
55
};

src/commands/open-workspace.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export const openWorkspace = async () => {
1010
{ modal: true } as MessageOptions,
1111
...options
1212
);
13+
if (!input) {
14+
return;
15+
}
1316
const newWindow = input === options[1];
1417

1518
await commands.executeCommand('vscode.openFolder', uri, newWindow);

src/commands/posts-list/create-local-draft.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const createLocalDraft = async () => {
2323
if (!title) {
2424
return;
2525
}
26-
const workspacePath = Settings.workspaceUri.fsPath;
26+
const { fsPath: workspacePath } = Settings.workspaceUri;
2727
title = ['.md', '.html'].some(ext => title!.endsWith(ext)) ? title : `${title}${title.endsWith('.') ? '' : '.'}md`;
2828
const filePath = path.join(workspacePath, title);
2929
try {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { commands } from 'vscode';
2+
import { Settings } from '../services/settings.service';
3+
4+
export const revealWorkspaceInOs = () => {
5+
commands.executeCommand('revealFileInOS', Settings.workspaceUri);
6+
};

src/commands/set-workspace.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { window } from 'vscode';
2+
import { AlertService } from '../services/alert.service';
3+
import { Settings } from '../services/settings.service';
4+
5+
export const setWorkspace = async () => {
6+
const input = ((await window.showOpenDialog({
7+
title: '选择工作空间',
8+
canSelectFolders: true,
9+
canSelectFiles: false,
10+
canSelectMany: false,
11+
defaultUri: Settings.workspaceUri,
12+
})) ?? [])[0];
13+
14+
if (!input) {
15+
return;
16+
}
17+
18+
Settings.workspaceUri = input;
19+
AlertService.info(`工作空间成功修改为: "${Settings.workspaceUri.fsPath}"`);
20+
};

src/services/settings.service.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { homedir } from 'os';
2+
import * as fs from 'fs';
23
import { Uri, workspace } from 'vscode';
34
import { globalState } from './global-state';
45

@@ -7,10 +8,20 @@ export class Settings {
78
return workspace.getConfiguration(globalState.extensionName);
89
}
910

10-
static get workspaceUri() {
11+
static get workspaceUri(): Uri {
1112
const workspace = this.configuration.get<string>('workspace');
1213
return workspace
1314
? Uri.file(workspace.replace(/^~/, homedir()))
1415
: Uri.joinPath(Uri.file(homedir()), 'Documents', 'Cnblogs');
1516
}
17+
18+
static set workspaceUri(value: Uri) {
19+
if (!value.fsPath || !(value.scheme === 'file')) {
20+
throw Error('Invalid uri');
21+
}
22+
if (!fs.existsSync(value.fsPath)) {
23+
throw Error(`Folder "${value.fsPath}" not exist`);
24+
}
25+
this.configuration.update('workspace', value.fsPath);
26+
}
1627
}

0 commit comments

Comments
 (0)