Skip to content

Commit 57a294d

Browse files
committed
ローカルへのファイル保存に対応
1 parent 7675af9 commit 57a294d

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

package.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,6 @@
137137
"when": "viewItem == qiitaItems",
138138
"group": "navigation"
139139
},
140-
{
141-
"command": "qiita.openItem",
142-
"when": "viewItem == qiitaItems",
143-
"group": "1_modification"
144-
},
145140
{
146141
"command": "qiita.editTags",
147142
"when": "viewItem == qiitaItems",

package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"commands.compose.openInBrowser": "ブラウザで確認",
1212

1313
"commands.openItem.title": "開く",
14+
"commands.openItem.failure.fallback": "ファイルの表示に失敗しました。指定された投稿は存在しない可能性があります。",
1415

1516
"commands.deleteItem.title": "削除",
1617
"commands.deleteItem.confirm.title": "投稿を削除してもよろしいですか?",

src/commands/openItem.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as fs from 'fs';
2+
import { Item } from 'qiita-js-2';
3+
import { Uri, window, workspace } from 'vscode';
4+
import * as nls from 'vscode-nls';
5+
6+
const localize = nls.loadMessageBundle();
7+
8+
export function openItem (storagePath?: string) {
9+
return async (item: Item) => {
10+
console.log(item);
11+
12+
try {
13+
const filePath = `${storagePath}/${item.id}.md`;
14+
const fileUri = `file://${filePath}`;
15+
16+
if (!storagePath) {
17+
return;
18+
}
19+
20+
if (!fs.existsSync(storagePath)) {
21+
fs.mkdirSync(storagePath);
22+
}
23+
24+
fs.writeFileSync(filePath, item.body);
25+
26+
const document = await workspace.openTextDocument(Uri.parse(fileUri));
27+
await window.showTextDocument(document);
28+
} catch (error) {
29+
window.showErrorMessage(localize(
30+
'commands.openItem.failure.fallback',
31+
'ファイルの表示に失敗しました。指定された投稿は存在しない可能性があります。',
32+
));
33+
}
34+
};
35+
}

src/explorer/qiitaItems.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as path from 'path';
21
import { Item } from 'qiita-js-2';
32
import { Command, TreeDataProvider, TreeItem, TreeItemCollapsibleState, Uri } from 'vscode';
43
import { client } from '../client';
@@ -29,7 +28,7 @@ export class QiitaItemProvider implements TreeDataProvider<QiitaItem> {
2928
const command = {
3029
command: 'qiita.openItem',
3130
title: '',
32-
arguments: [ path ],
31+
arguments: [ item ],
3332
};
3433

3534
return new QiitaItem(item, TreeItemCollapsibleState.None, command);

src/extension.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import './polyfills';
1212
nls.config(process.env.VSCODE_NLS_CONFIG as nls.Options)();
1313

1414
export function activate (context: ExtensionContext) {
15-
console.log(context);
1615
window.registerTreeDataProvider('qiitaItems', new QiitaItemProvider());
1716

1817
context.subscriptions.push(

0 commit comments

Comments
 (0)