Skip to content

Commit ab9d372

Browse files
committed
既存の投稿のタイトルを編集する機能を追加 (#7)
1 parent 4b22265 commit ab9d372

File tree

8 files changed

+80
-19
lines changed

8 files changed

+80
-19
lines changed

package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@
7878
"command": "qiita.makePublic",
7979
"title": "%commands.makePublic.title%",
8080
"category": "Qiita"
81+
},
82+
{
83+
"command": "qiita.editTitle",
84+
"title": "%commands.editTitle.title%",
85+
"category": "Qiita"
8186
}
8287
],
8388
"configuration": [
@@ -109,7 +114,8 @@
109114
{ "command": "qiita.deleteItem", "when": "false" },
110115
{ "command": "qiita.editTags", "when": "false" },
111116
{ "command": "qiita.openItemExternal", "when": "false"},
112-
{ "command": "qiita.makePublic", "when": "false"}
117+
{ "command": "qiita.makePublic", "when": "false"},
118+
{ "command": "qiita.editTitle", "when": "false" }
113119
],
114120
"editor/title": [
115121
{
@@ -155,6 +161,11 @@
155161
"when": "viewItem == qiitaItems",
156162
"group": "1_modification"
157163
},
164+
{
165+
"command": "qiita.editTitle",
166+
"when": "viewItem == qiitaItems",
167+
"group": "1_modification"
168+
},
158169
{
159170
"command": "qiita.deleteItem",
160171
"when": "viewItem == qiitaItems",

package.nls.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
"commands.makePublic.confirm.cancel": "キャンセル",
3232
"commands.makePublic.failure.alredyPublic": "この投稿は既に公開されています。",
3333

34+
"commands.editTitle.title": "タイトルを編集",
35+
"commands.editTitle.success": "タイトルを編集しました",
36+
3437
"quickpicks.tagQuickPick.title": "投稿に登録するタグを入力してください",
3538
"quickpicks.tagQuickPick.placeholder": "例) Rails React Mastodon",
3639
"quickpicks.tagQuickPick.item.description": "{0}件の投稿",

src/commands/deleteItem.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ const localize = nls.loadMessageBundle();
88

99
/**
1010
* コマンド `qiita.deleteItem` のハンドラーで、Qiitaの投稿を削除します。
11-
* @param arg Commandで渡される引数。qiitaItemsビューから発行されるので `item` キーに投稿が入っています。
11+
* @param resource Commandで渡される引数。qiitaItemsビューから発行されるので `item` キーに投稿が入っています。
1212
*/
13-
export async function deleteItem (arg: object & { item: Item }) {
13+
export async function deleteItem (resource: { item: Item }) {
1414
const next = localize(
1515
'commands.deleteItem.confirm.next',
1616
'削除する',
@@ -33,7 +33,7 @@ export async function deleteItem (arg: object & { item: Item }) {
3333
}
3434

3535
try {
36-
await client.deleteItem(arg.item.id);
36+
await client.deleteItem(resource.item.id);
3737

3838
return window.showInformationMessage(localize(
3939
'commands.deleteItem.success',

src/commands/editTags.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ export const updater = async (item: Item, selectedItems: ReadonlyArray<QuickPick
2222

2323
/**
2424
* タグを編集するためのquickPickを作成
25-
* @param arg コマンドから渡される引数
25+
* @param resource コマンドから渡される引数
2626
*/
27-
export async function editTags (arg: object & { item: Item }) {
28-
const { item } = arg;
29-
const taggings = arg.item.tags;
27+
export async function editTags (resource: { item: Item }) {
28+
const { item } = resource;
29+
const taggings = resource.item.tags;
3030

3131
// 既存のタグを取得してselectedItemsとして初期化
3232
const selectedItems = await Promise.all(taggings.map(async (tagging) => {
@@ -36,8 +36,6 @@ export async function editTags (arg: object & { item: Item }) {
3636

3737
const quickPick = tagQuickPickCreator(selectedItems);
3838

39-
quickPick.show();
40-
4139
quickPick.onDidAccept(async () => {
4240
if (!validateTagQuickPick(quickPick)) {
4341
return window.showErrorMessage(localize(
@@ -59,4 +57,6 @@ export async function editTags (arg: object & { item: Item }) {
5957
return handleErrorMessage(error);
6058
}
6159
});
60+
61+
quickPick.show();
6262
}

src/commands/editTitle.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Item } from 'qiita-js-2';
2+
import { window } from 'vscode';
3+
import * as nls from 'vscode-nls';
4+
import { client } from '../client';
5+
import { titleInputBoxCreator, validateTitleInputBox } from '../quickpicks/titleInputBoxCreator';
6+
import { handleErrorMessage } from '../utils/errorHandler';
7+
8+
const localize = nls.loadMessageBundle();
9+
10+
/**
11+
* タイトル編集用のコマンドのハンドラー
12+
* @param コマンドから渡される引数
13+
*/
14+
export function editTitle (resource: { item: Item }) {
15+
const { item } = resource;
16+
const inputBox = titleInputBoxCreator(item.title);
17+
18+
inputBox.onDidAccept(async () => {
19+
if (!validateTitleInputBox(inputBox)) {
20+
return window.showErrorMessage(localize(
21+
'quickpicks.titleInputBox.failure.validationError',
22+
'タイトルは1文字以上255文字以内で入力してください。',
23+
));
24+
}
25+
26+
try {
27+
inputBox.hide();
28+
29+
await client.updateItem(item.id, {
30+
body: item.body,
31+
tags: item.tags,
32+
title: inputBox.value,
33+
});
34+
35+
return window.showInformationMessage(localize(
36+
'commands.editTitle.success',
37+
'タイトルを編集しました',
38+
));
39+
} catch (error) {
40+
return handleErrorMessage(error);
41+
}
42+
});
43+
44+
inputBox.show();
45+
}

src/commands/makePublic.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ const localize = nls.loadMessageBundle();
88

99
/**
1010
* 公開状態を変更するか確認するダイアログを表示して、公開状態を変更します
11-
* @param arg コマンドから渡される引数
11+
* @param resource コマンドから渡される引数
1212
*/
13-
export async function makePublic (arg: object & { item: Item }) {
14-
if (!arg.item.private) {
13+
export async function makePublic (resource: { item: Item }) {
14+
if (!resource.item.private) {
1515
return window.showInformationMessage(localize(
1616
'commands.makePublic.failure.alredyPublic',
1717
'この投稿は既に公開されています。',
@@ -40,10 +40,10 @@ export async function makePublic (arg: object & { item: Item }) {
4040
}
4141

4242
try {
43-
await client.updateItem(arg.item.id, {
44-
body: arg.item.body,
45-
title: arg.item.title,
46-
tags: arg.item.tags,
43+
await client.updateItem(resource.item.id, {
44+
body: resource.item.body,
45+
title: resource.item.title,
46+
tags: resource.item.tags,
4747
private: false,
4848
});
4949

src/commands/openItemExternal.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Item } from 'qiita-js-2';
22
import { commands, Uri } from 'vscode';
33

4-
export function openItemExternal (arg: object & { item: Item }) {
5-
const { url } = arg.item;
4+
export function openItemExternal (resource: { item: Item }) {
5+
const { url } = resource.item;
66
commands.executeCommand('vscode.open', Uri.parse(url));
77
}

src/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as nls from 'vscode-nls';
33
import { compose } from './commands/compose';
44
import { deleteItem } from './commands/deleteItem';
55
import { editTags } from './commands/editTags';
6+
import { editTitle } from './commands/editTitle';
67
import { makePublic } from './commands/makePublic';
78
import { openItem } from './commands/openItem';
89
import { openItemExternal } from './commands/openItemExternal';
@@ -21,6 +22,7 @@ export function activate (context: ExtensionContext) {
2122
commands.registerCommand('qiita.compose', compose),
2223
commands.registerCommand('qiita.openItemExternal', openItemExternal),
2324
commands.registerCommand('qiita.deleteItem', deleteItem),
25+
commands.registerCommand('qiita.editTitle', editTitle),
2426
);
2527
}
2628

0 commit comments

Comments
 (0)