Skip to content

Commit f0fc336

Browse files
committed
WIP: エクスプローラーのモデルとアイテムのモデルを統合
1 parent c66505c commit f0fc336

File tree

10 files changed

+96
-100
lines changed

10 files changed

+96
-100
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"core-js": "^2.5.7",
2828
"mocha": "^5.2.0",
2929
"npm-run-all": "^4.1.3",
30-
"qiita-js-2": "^1.3.0",
30+
"qiita-js-2": "^1.3.1",
3131
"tslint": "^5.8.0",
3232
"typescript": "^3.0.3",
3333
"vscode": "^1.1.6",

src/commands/compose.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { qiitaItemsProvider } from '../explorers/qiitaItems';
66
import { tagQuickPickCreator, validateTagQuickPick } from '../quickpicks/tagQuickPick';
77
import { titleInputBoxCreator, validateTitleInputBox } from '../quickpicks/titleInputBox';
88
import { privateLabel, visibilityQuickPickCreator } from '../quickpicks/visibilityQuickPick';
9+
import { itemsStore } from '../stores/itemsStore';
910
import { handleErrorMessage } from '../utils/errorHandler';
1011
import { getFilenameFromPath } from '../utils/getFilenameFromPath';
1112

@@ -84,6 +85,7 @@ export async function compose (resource?: Uri) {
8485

8586
try {
8687
const item = await client.createItem(options);
88+
await itemsStore.refreshItems();
8789
qiitaItemsProvider.refresh();
8890

8991
const openInBrowser = localize(

src/commands/deleteItem.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { window } from 'vscode';
33
import * as nls from 'vscode-nls';
44
import { client } from '../client';
55
import { qiitaItemsProvider } from '../explorers/qiitaItems';
6+
import { itemsStore } from '../stores/itemsStore';
67
import { handleErrorMessage } from '../utils/errorHandler';
78

89
const localize = nls.loadMessageBundle();
@@ -35,6 +36,7 @@ export async function deleteItem (resource: { item: Item }) {
3536

3637
try {
3738
await client.deleteItem(resource.item.id);
39+
await itemsStore.refreshItems();
3840
qiitaItemsProvider.refresh();
3941

4042
return window.showInformationMessage(localize(

src/commands/editTags.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as nls from 'vscode-nls';
44
import { client } from '../client';
55
import { qiitaItemsProvider } from '../explorers/qiitaItems';
66
import { makeQuickPickItemFromTag, tagQuickPickCreator, validateTagQuickPick } from '../quickpicks/tagQuickPick';
7+
import { itemsStore } from '../stores/itemsStore';
78
import { handleErrorMessage } from '../utils/errorHandler';
89

910
const localize = nls.loadMessageBundle();
@@ -48,6 +49,7 @@ export async function editTags (resource: { item: Item }) {
4849
try {
4950
quickPick.hide();
5051
await updater(item, quickPick.selectedItems);
52+
await itemsStore.refreshItems();
5153
qiitaItemsProvider.refresh();
5254

5355
return window.showInformationMessage(localize(

src/commands/expandItems.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ import { qiitaItemsProvider } from '../explorers/qiitaItems';
22
import { itemsStore } from '../stores/itemsStore';
33

44
export async function expandItems () {
5-
if (!itemsStore.done) {
6-
await itemsStore.expandItems();
7-
qiitaItemsProvider.refresh();
5+
try {
6+
if (!itemsStore.done) {
7+
await itemsStore.expandItems();
8+
qiitaItemsProvider.refresh();
9+
}
10+
} catch (e) {
11+
console.error(e);
812
}
913
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { TreeItem, TreeItemCollapsibleState } from 'vscode';
2+
import * as nls from 'vscode-nls';
3+
4+
const localize = nls.loadMessageBundle();
5+
6+
export class ExpandItems extends TreeItem {
7+
/**
8+
* 「さらに読み込む」ボタンのためのアイテム
9+
* @param collapsibleState アイテムが折り畳まれているかの状態
10+
*/
11+
constructor (public readonly collapsibleState: TreeItemCollapsibleState) {
12+
super(localize(
13+
'commands.expandItems.title',
14+
'さらに読み込む...',
15+
), collapsibleState);
16+
}
17+
18+
public command = {
19+
command: 'qiita.expandItems',
20+
title: localize('commands.expandItems.title', 'さらに読み込む...'),
21+
arguments: [],
22+
};
23+
24+
public contextValue = 'qiitaItems';
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Item } from 'qiita-js-2';
2+
import { Command, TreeItem, TreeItemCollapsibleState, Uri } from 'vscode';
3+
4+
export class QiitaItem extends TreeItem {
5+
/**
6+
* "Qiitaの投稿" ビューに表示されるアイテム
7+
* @param item Qiitaの投稿
8+
* @param collapsibleState アイテムが折り畳まれているかの状態
9+
* @param command クリック時に発火するコマンド
10+
*/
11+
constructor (
12+
public readonly item: Item,
13+
public readonly collapsibleState: TreeItemCollapsibleState,
14+
public readonly command: Command,
15+
) {
16+
super(item.title, collapsibleState);
17+
}
18+
19+
public resourceUri = Uri.parse('file:///text.md'); // Hack: アイコンをMarkdownのものに
20+
public contextValue = 'qiitaItems';
21+
}

src/explorers/qiitaItems.ts

Lines changed: 33 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,30 @@
11
import { Item } from 'qiita-js-2';
2-
import {
3-
Command,
4-
Event,
5-
EventEmitter,
6-
TreeDataProvider,
7-
TreeItem,
8-
TreeItemCollapsibleState,
9-
Uri,
10-
} from 'vscode';
2+
import { Event, EventEmitter, TreeDataProvider, TreeItem, TreeItemCollapsibleState } from 'vscode';
113
import * as nls from 'vscode-nls';
12-
import { itemsStore } from '../stores/itemsStore';
4+
import { client } from '../client';
5+
import '../polyfills';
6+
import { ExpandItems } from './models/expandItemsNode';
7+
import { QiitaItem } from './models/qiitaItemsNode';
138

149
const localize = nls.loadMessageBundle();
1510

11+
class QiitaItemsProvider implements TreeDataProvider<TreeItem> {
12+
private _onDidChangeTreeData: EventEmitter<TreeItem|undefined> = new EventEmitter<TreeItem|undefined>();
13+
public readonly onDidChangeTreeData: Event<TreeItem|undefined> = this._onDidChangeTreeData.event;
1614

17-
export class QiitaItem extends TreeItem {
18-
/**
19-
* "Qiitaの投稿" ビューに表示されるアイテム
20-
* @param item Qiitaの投稿
21-
* @param collapsibleState アイテムが折り畳まれているかの状態
22-
* @param command クリック時に発火するコマンド
23-
*/
24-
constructor (
25-
public readonly item: Item,
26-
public readonly collapsibleState: TreeItemCollapsibleState,
27-
public readonly command: Command,
28-
) {
29-
super(item.title, collapsibleState);
30-
}
15+
/** 取得した投稿 */
16+
protected items: Item[] = [];
3117

32-
public resourceUri = Uri.parse('file:///text.md'); // Hack: アイコンをMarkdownのものに
33-
public contextValue = 'qiitaItems';
34-
}
18+
/** 全件取得したかどうか */
19+
protected done = false;
3520

36-
37-
export class ExpandItems extends TreeItem {
38-
/**
39-
* 「さらに読み込む」ボタンのためのアイテム
40-
* @param collapsibleState アイテムが折り畳まれているかの状態
41-
*/
42-
constructor (public readonly collapsibleState: TreeItemCollapsibleState) {
43-
super(localize(
44-
'commands.expandItems.title',
45-
'さらに読み込む...',
46-
), collapsibleState);
47-
}
48-
49-
public command = {
50-
command: 'qiita.expandItems',
51-
title: localize('commands.expandItems.title', 'さらに読み込む...'),
52-
arguments: [],
53-
};
54-
55-
public contextValue = 'qiitaItems';
56-
}
57-
58-
59-
class QiitaItemsProvider implements TreeDataProvider<QiitaItem|ExpandItems> {
60-
private _onDidChangeTreeData: EventEmitter<QiitaItem|ExpandItems|undefined> = new EventEmitter<QiitaItem|ExpandItems|undefined>();
61-
public readonly onDidChangeTreeData: Event<QiitaItem|ExpandItems|undefined> = this._onDidChangeTreeData.event;
21+
/** 自分の投稿の配列を返すイテラブル */
22+
protected itemsIterable = client.fetchMyItems({ page: 1, per_page: 60 });
6223

6324
/**
6425
* ツリーデータを更新
6526
*/
6627
public async refresh () {
67-
await itemsStore.refreshItems();
6828
this._onDidChangeTreeData.fire();
6929
}
7030

@@ -82,7 +42,7 @@ class QiitaItemsProvider implements TreeDataProvider<QiitaItem|ExpandItems> {
8242
* @param element 取得するelement
8343
*/
8444
public async getChildren (): Promise<(QiitaItem|ExpandItems)[]> {
85-
if (itemsStore.items.size === 0) {
45+
if (!itemsStore.items || !itemsStore.items.length) {
8646
await itemsStore.refreshItems();
8747
}
8848

@@ -105,6 +65,24 @@ class QiitaItemsProvider implements TreeDataProvider<QiitaItem|ExpandItems> {
10565

10666
return children;
10767
}
68+
69+
/**
70+
* イテラブルを初期化して最初のページを再取得
71+
*/
72+
public async refreshItems () {
73+
const { value: items, done } = await this.itemsIterable.next('reset');
74+
this.items = items;
75+
this.done = done;
76+
}
77+
78+
/**
79+
* イテラブルのnextを呼び出し
80+
*/
81+
public async expandItems () {
82+
const { value: items, done } = await this.itemsIterable.next();
83+
this.items.concat(items);
84+
this.done = done;
85+
}
10886
}
10987

11088
export const qiitaItemsProvider = new QiitaItemsProvider();

src/stores/itemsStore.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,9 +1618,9 @@ punycode@^1.4.1:
16181618
version "1.4.1"
16191619
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e"
16201620

1621-
qiita-js-2@^1.3.0:
1622-
version "1.3.0"
1623-
resolved "https://registry.yarnpkg.com/qiita-js-2/-/qiita-js-2-1.3.0.tgz#b975b4599db894b697ad0432082ce54086a57688"
1621+
qiita-js-2@^1.3.1:
1622+
version "1.3.1"
1623+
resolved "https://registry.yarnpkg.com/qiita-js-2/-/qiita-js-2-1.3.1.tgz#9d407cde3b5dd48c6f5ee1e9ccd1d841e6c45412"
16241624
dependencies:
16251625
axios "^0.18.0"
16261626
http-link-header "^0.8.0"

0 commit comments

Comments
 (0)