Skip to content

Commit 27ed2e8

Browse files
committed
refactor: migrate ApiReferenceCtrl to Typescript
1 parent 80b5c7d commit 27ed2e8

7 files changed

Lines changed: 91 additions & 41 deletions

File tree

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"@types/chai": "^4.3.0",
5656
"@types/glob": "^7.1.3",
5757
"@types/mocha": "^8.2.3",
58+
"@types/mustache": "^4.2.1",
5859
"@types/node": "^14.x",
5960
"@types/node-fetch": "^2.6.1",
6061
"@types/sinon": "^10.0.11",

src/core/dataSource.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ export interface TemplatePaths {
88
objectAPI: vscode.Uri;
99
}
1010

11-
export function readTemplates(templatePaths: TemplatePaths) {
11+
export interface TemplatesContent {
12+
webview: string;
13+
members: string;
14+
objectAPI: string;
15+
}
16+
17+
export function readTemplates(templatePaths: TemplatePaths): TemplatesContent {
1218
return {
1319
webview: readFileContent(templatePaths.webview),
1420
members: readFileContent(templatePaths.members),

src/core/ui5ApiService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export function getUi5Objects() {
6464

6565
export function getUi5ObjectDesignApi(
6666
ui5ObjectName: string,
67-
resultApi: ui5Api.LibraryApiSymbol
67+
resultApi?: ui5Api.LibraryApiSymbol
6868
): ui5Api.LibraryApiSymbol | undefined {
6969
const ui5Object = apiIndexNodes[ui5ObjectName];
7070

src/panelFeatures/apiDocsFiltering.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ interface DeleteMarkers {
1111

1212
export function filterApiMembers(
1313
ui5ObjectApi: LibraryApiSymbol,
14-
memberSearchString: string,
15-
memberGroupFilter: string
16-
) {
14+
memberSearchString?: string | null,
15+
memberGroupFilter?: string | null
16+
): LibraryApiSymbol {
1717
const objectApi = JSON.parse(JSON.stringify(ui5ObjectApi));
1818

1919
function filterMembers(key1: string, key2?: string) {
@@ -25,7 +25,7 @@ export function filterApiMembers(
2525
filterable = objectApi[key1];
2626
}
2727

28-
if (filterable) {
28+
if (filterable && memberSearchString) {
2929
return filterable.filter((member: any) => {
3030
if (member.visibility !== "public") {
3131
return false;
Lines changed: 63 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,54 @@
1-
const vscode = require("vscode");
2-
const ui5APIService = require("../core/ui5ApiService.js");
3-
const ui5APIFormatter = require("../objectApi/objectApiFormat.js");
4-
const ui5APIFinder = require("../search/ui5ApiFinder.js");
5-
const constants = require("../core/constants.js");
6-
const favorites = require("../panelFeatures/favorites");
7-
const filtering = require("../panelFeatures/apiDocsFiltering");
8-
const Mustache = require("mustache");
9-
10-
class APIReferenceCtrl {
11-
constructor(webviewView, templates) {
1+
import * as vscode from "vscode";
2+
import * as ui5APIService from "../core/ui5ApiService";
3+
import * as ui5APIFormatter from "../objectApi/objectApiFormat";
4+
import * as ui5APIFinder from "../search/ui5ApiFinder";
5+
import * as constants from "../core/constants";
6+
import * as favorites from "../panelFeatures/favorites";
7+
import * as filtering from "../panelFeatures/apiDocsFiltering";
8+
import * as mustache from "mustache";
9+
import { TemplatesContent } from "../core/dataSource";
10+
11+
interface GlobalState {
12+
hitlistObjectsLimit: number;
13+
visibleObjectName: string | null;
14+
}
15+
16+
interface SearchState {
17+
previousSearchedObjectName: string | null;
18+
searchedObjectName: string | null;
19+
memberSearchString: string | null;
20+
memberGroupFilter: string | null;
21+
searchTimeout?: ReturnType<typeof setTimeout>;
22+
}
23+
24+
interface SearchMessage {
25+
searchInput: string;
26+
}
27+
28+
interface GetDesignApiMessage {
29+
source?: string;
30+
ui5Object: string;
31+
}
32+
33+
interface ChangeFavoriteMessage {
34+
operation: string;
35+
ui5Object: string;
36+
}
37+
38+
export class ApiReferenceCtrl {
39+
private _webviewView: vscode.WebviewView;
40+
private _templates: TemplatesContent;
41+
private _globalState: GlobalState;
42+
private _searchState: SearchState;
43+
44+
constructor(webviewView: vscode.WebviewView, templates: TemplatesContent) {
1245
this._webviewView = webviewView;
1346
this._templates = templates;
1447

1548
const configuration = vscode.workspace.getConfiguration("UI5ReferencePanel");
1649

1750
this._globalState = {
18-
hitlistObjectsLimit: configuration.get("hitlistSize"),
51+
hitlistObjectsLimit: configuration.get("hitlistSize") as number,
1952
visibleObjectName: null,
2053
};
2154

@@ -24,11 +57,10 @@ class APIReferenceCtrl {
2457
searchedObjectName: null,
2558
memberSearchString: null,
2659
memberGroupFilter: null,
27-
searchTimeout: 0,
2860
};
2961
}
3062

31-
handleSearch(message) {
63+
handleSearch(message: SearchMessage) {
3264
clearTimeout(this._searchState.searchTimeout);
3365
const searchInput = message.searchInput;
3466

@@ -43,8 +75,8 @@ class APIReferenceCtrl {
4375

4476
if (this._globalState.visibleObjectName) {
4577
this.handleGetDesignAPIHtml(
46-
{ ui5Object: this._globalState.visibleObjectName },
47-
"oneSearchResult"
78+
{ ui5Object: this._globalState.visibleObjectName }
79+
//"oneSearchResult"
4880
);
4981
}
5082
}
@@ -81,8 +113,8 @@ class APIReferenceCtrl {
81113
}
82114

83115
this.handleGetDesignAPIHtml(
84-
{ ui5Object: this._globalState.visibleObjectName },
85-
"oneSearchResult"
116+
{ ui5Object: this._globalState.visibleObjectName }
117+
// "oneSearchResult"
86118
);
87119

88120
return;
@@ -134,7 +166,7 @@ class APIReferenceCtrl {
134166
});
135167

136168
const configuration = vscode.workspace.getConfiguration("UI5ReferencePanel");
137-
this._globalState.hitlistObjectsLimit = configuration.get("hitlistSize");
169+
this._globalState.hitlistObjectsLimit = configuration.get("hitlistSize") as number;
138170

139171
if (foundObjects && foundObjects.length > 0) {
140172
if (foundObjects.length > this._globalState.hitlistObjectsLimit) {
@@ -151,7 +183,7 @@ class APIReferenceCtrl {
151183
result: foundObjects,
152184
});
153185
} else {
154-
this.handleGetDesignAPIHtml({ ui5Object: foundObjects[0].name }, "oneSearchResult");
186+
this.handleGetDesignAPIHtml({ ui5Object: foundObjects[0].name }); // , "oneSearchResult");
155187
}
156188
} else {
157189
this._webviewView.webview.postMessage({
@@ -162,12 +194,12 @@ class APIReferenceCtrl {
162194
}, 500);
163195
}
164196

165-
handleGetDesignAPIHtml(message) {
197+
handleGetDesignAPIHtml(message: GetDesignApiMessage) {
166198
this._globalState.visibleObjectName = message.ui5Object;
167199

168200
if (message.source === "favorite") {
169-
this._searchState.memberGroupFilter = undefined;
170-
this._searchState.memberSearchString = undefined;
201+
this._searchState.memberGroupFilter = null;
202+
this._searchState.memberSearchString = null;
171203
}
172204

173205
const designAPIHtml = this.getDesignAPIHtml(message.ui5Object);
@@ -192,11 +224,11 @@ class APIReferenceCtrl {
192224
}
193225
}
194226

195-
handleOpenURL(apiDocURL) {
227+
handleOpenURL(apiDocURL: string) {
196228
vscode.env.openExternal(vscode.Uri.parse(apiDocURL));
197229
}
198230

199-
handleChangeFavorite(message) {
231+
handleChangeFavorite(message: ChangeFavoriteMessage) {
200232
if (message.operation === "remove") {
201233
favorites.removeFavorite(message.ui5Object);
202234
} else {
@@ -209,8 +241,8 @@ class APIReferenceCtrl {
209241
});
210242
}
211243

212-
getDesignAPI(ui5ObjectPath) {
213-
let designApi = ui5APIService.getUi5ObjectDesignApi(ui5ObjectPath);
244+
getDesignAPI(ui5ObjectName: string) {
245+
let designApi = ui5APIService.getUi5ObjectDesignApi(ui5ObjectName);
214246

215247
if (!designApi) {
216248
return;
@@ -227,7 +259,7 @@ class APIReferenceCtrl {
227259
return ui5APIFormatter.getFormattedObjectApi(designApi, true, true);
228260
}
229261

230-
triggerSearch(input) {
262+
triggerSearch(input: string) {
231263
this._webviewView.webview.postMessage({
232264
type: "triggerSearch",
233265
input,
@@ -243,17 +275,15 @@ class APIReferenceCtrl {
243275
});
244276
}
245277

246-
getDesignAPIHtml(ui5Object) {
247-
const designAPI = this.getDesignAPI(ui5Object);
278+
getDesignAPIHtml(ui5ObjectName: string) {
279+
const designAPI = this.getDesignAPI(ui5ObjectName);
248280

249281
if (!designAPI) {
250282
return null;
251283
} else {
252-
return Mustache.render(this._templates.objectAPI, designAPI, {
284+
return mustache.render(this._templates.objectAPI, designAPI, {
253285
membersTemplate: this._templates.members,
254286
});
255287
}
256288
}
257289
}
258-
259-
module.exports = APIReferenceCtrl;

src/view/APIReferenceProvider.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const vscode = require("vscode");
2-
const APIReferenceCtrl = require("./APIReferenceCtrl.js");
2+
import { ApiReferenceCtrl } from "./ApiReferenceCtrl";
33
const Mustache = require("mustache");
44

55
class APIReferenceProvider {
@@ -10,7 +10,7 @@ class APIReferenceProvider {
1010

1111
resolveWebviewView(webviewView, context) {
1212
this._view = webviewView;
13-
this._apiReferenceCtrl = new APIReferenceCtrl(webviewView, this._templates);
13+
this._apiReferenceCtrl = new ApiReferenceCtrl(webviewView, this._templates);
1414

1515
webviewView.webview.options = {
1616
enableScripts: true,

0 commit comments

Comments
 (0)