Skip to content

Commit 630cd5b

Browse files
committed
refactor: migrate leftovers to Typescript
1 parent 27ed2e8 commit 630cd5b

4 files changed

Lines changed: 172 additions & 160 deletions

File tree

src/extension.js renamed to src/extension.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
const vscode = require("vscode");
2-
const APIReferenceProvider = require("./view/APIReferenceProvider");
3-
const ui5ApiService = require("./core/ui5ApiService");
4-
const dataSource = require("./core/dataSource.js");
5-
const constants = require("./core/constants.js");
6-
const contextMenu = require("./panelFeatures/contextMenu");
7-
const favorites = require("./panelFeatures/favorites");
1+
import * as vscode from "vscode";
2+
import { ApiReferenceProvider } from "./view/APIReferenceProvider";
3+
import * as ui5ApiService from "./core/ui5ApiService";
4+
import * as dataSource from "./core/dataSource";
5+
import * as constants from "./core/constants.js";
6+
import * as contextMenu from "./panelFeatures/contextMenu";
7+
import * as favorites from "./panelFeatures/favorites";
88

9-
function activate(context) {
9+
export function activate(context: vscode.ExtensionContext) {
1010
const templatePaths = getTemplatePaths(context.extensionUri);
1111
const templates = dataSource.readTemplates(templatePaths);
1212
const configuration = vscode.workspace.getConfiguration("UI5ReferencePanel");
13-
const apiViewProvider = new APIReferenceProvider(context.extensionUri, templates);
13+
const apiViewProvider = new ApiReferenceProvider(context.extensionUri, templates);
1414

1515
context.subscriptions.push(
1616
vscode.window.registerWebviewViewProvider("ui5ApiReferenceView", apiViewProvider, {
@@ -30,21 +30,28 @@ function activate(context) {
3030
vscode.commands.registerCommand("vscode-ui5-api-reference.showAPIViewForValue", async () => {
3131
const editor = vscode.window.activeTextEditor;
3232
// Gets the entire string from namespace and control
33-
const input = editor.selection !== "" ? contextMenu.findControl(editor) : editor.selection;
34-
await vscode.commands.executeCommand("workbench.view.extension.ui5ApiReference");
35-
apiViewProvider.triggerSearchCommand(input);
33+
if (editor) {
34+
const input = contextMenu.findControl(editor);
35+
36+
if (input) {
37+
await vscode.commands.executeCommand("workbench.view.extension.ui5ApiReference");
38+
apiViewProvider.triggerSearchCommand(input.toString());
39+
}
40+
}
3641
})
3742
);
3843

3944
context.subscriptions.push(
4045
vscode.commands.registerCommand("vscode-ui5-api-reference.searchAPI", async () => {
4146
const input = await vscode.window.showInputBox();
42-
await vscode.commands.executeCommand("workbench.view.extension.ui5ApiReference");
43-
apiViewProvider.triggerSearchCommand(input);
47+
if (input) {
48+
await vscode.commands.executeCommand("workbench.view.extension.ui5ApiReference");
49+
apiViewProvider.triggerSearchCommand(input);
50+
}
4451
})
4552
);
4653

47-
const apiBaseUrl = configuration.get("apiURL");
54+
const apiBaseUrl = configuration.get("apiURL") as string;
4855
ui5ApiService.setApiBaseURL(apiBaseUrl);
4956

5057
ui5ApiService
@@ -59,14 +66,10 @@ function activate(context) {
5966
favorites.initialize(configuration);
6067
}
6168

62-
function getTemplatePaths(extensionUri) {
69+
function getTemplatePaths(extensionUri: vscode.Uri) {
6370
return {
6471
webview: vscode.Uri.joinPath(extensionUri, "src/view/templates", "webview.html"),
6572
members: vscode.Uri.joinPath(extensionUri, "src/view/templates", "members.html"),
6673
objectAPI: vscode.Uri.joinPath(extensionUri, "src/view/templates", "objectAPI.html"),
6774
};
6875
}
69-
70-
module.exports = {
71-
activate,
72-
};

src/view/APIReferenceCtrl.ts

Lines changed: 59 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ interface SearchState {
2424
interface SearchMessage {
2525
searchInput: string;
2626
}
27-
2827
interface GetDesignApiMessage {
2928
source?: string;
3029
ui5Object: string;
@@ -36,23 +35,23 @@ interface ChangeFavoriteMessage {
3635
}
3736

3837
export class ApiReferenceCtrl {
39-
private _webviewView: vscode.WebviewView;
40-
private _templates: TemplatesContent;
41-
private _globalState: GlobalState;
42-
private _searchState: SearchState;
38+
private webviewView: vscode.WebviewView;
39+
private templates: TemplatesContent;
40+
private globalState: GlobalState;
41+
private searchState: SearchState;
4342

4443
constructor(webviewView: vscode.WebviewView, templates: TemplatesContent) {
45-
this._webviewView = webviewView;
46-
this._templates = templates;
44+
this.webviewView = webviewView;
45+
this.templates = templates;
4746

4847
const configuration = vscode.workspace.getConfiguration("UI5ReferencePanel");
4948

50-
this._globalState = {
49+
this.globalState = {
5150
hitlistObjectsLimit: configuration.get("hitlistSize") as number,
5251
visibleObjectName: null,
5352
};
5453

55-
this._searchState = {
54+
this.searchState = {
5655
previousSearchedObjectName: null,
5756
searchedObjectName: null,
5857
memberSearchString: null,
@@ -61,21 +60,21 @@ export class ApiReferenceCtrl {
6160
}
6261

6362
handleSearch(message: SearchMessage) {
64-
clearTimeout(this._searchState.searchTimeout);
63+
clearTimeout(this.searchState.searchTimeout);
6564
const searchInput = message.searchInput;
6665

6766
if (!searchInput) {
68-
this._webviewView.webview.postMessage({
67+
this.webviewView.webview.postMessage({
6968
type: "emptySearch",
7069
});
7170

72-
if (this._searchState.memberGroupFilter) {
73-
this._searchState.memberGroupFilter = null;
74-
this._searchState.memberSearchString = null;
71+
if (this.searchState.memberGroupFilter) {
72+
this.searchState.memberGroupFilter = null;
73+
this.searchState.memberSearchString = null;
7574

76-
if (this._globalState.visibleObjectName) {
75+
if (this.globalState.visibleObjectName) {
7776
this.handleGetDesignAPIHtml(
78-
{ ui5Object: this._globalState.visibleObjectName }
77+
{ ui5Object: this.globalState.visibleObjectName }
7978
//"oneSearchResult"
8079
);
8180
}
@@ -84,7 +83,7 @@ export class ApiReferenceCtrl {
8483
return;
8584
}
8685

87-
this._searchState.searchTimeout = setTimeout(() => {
86+
this.searchState.searchTimeout = setTimeout(() => {
8887
if (searchInput === "?") {
8988
return;
9089
}
@@ -93,27 +92,25 @@ export class ApiReferenceCtrl {
9392

9493
if (justMembersFiltering) {
9594
// no API shown
96-
if (!this._globalState.visibleObjectName) {
95+
if (!this.globalState.visibleObjectName) {
9796
return;
9897
}
9998

100-
this._searchState.memberGroupFilter = justMembersFiltering[1]
101-
.replace("?", "")
102-
.toLowerCase();
99+
this.searchState.memberGroupFilter = justMembersFiltering[1].replace("?", "").toLowerCase();
103100

104101
const memberSearchPart = justMembersFiltering[2].trim();
105102

106103
if (
107104
memberSearchPart &&
108-
this._searchState.memberGroupFilter !== constants.memberGroupFilter.construct
105+
this.searchState.memberGroupFilter !== constants.memberGroupFilter.construct
109106
) {
110-
this._searchState.memberSearchString = memberSearchPart;
107+
this.searchState.memberSearchString = memberSearchPart;
111108
} else {
112-
this._searchState.memberSearchString = null;
109+
this.searchState.memberSearchString = null;
113110
}
114111

115112
this.handleGetDesignAPIHtml(
116-
{ ui5Object: this._globalState.visibleObjectName }
113+
{ ui5Object: this.globalState.visibleObjectName }
117114
// "oneSearchResult"
118115
);
119116

@@ -123,70 +120,70 @@ export class ApiReferenceCtrl {
123120
const parts = searchInput.split(" ");
124121

125122
if (parts.length === 2 && parts[1]) {
126-
this._searchState.previousSearchedObjectName = this._searchState.searchedObjectName;
127-
this._searchState.searchedObjectName = parts[0];
123+
this.searchState.previousSearchedObjectName = this.searchState.searchedObjectName;
124+
this.searchState.searchedObjectName = parts[0];
128125

129126
// if (parts.length === 2 && parts[1]) {
130-
this._searchState.memberSearchString = parts[1];
131-
const match = this._searchState.memberSearchString.match(/(\?[mpeac])(.*)/i);
127+
this.searchState.memberSearchString = parts[1];
128+
const match = this.searchState.memberSearchString.match(/(\?[mpeac])(.*)/i);
132129

133130
if (match) {
134-
this._searchState.memberGroupFilter = match[1].replace("?", "").toLowerCase();
131+
this.searchState.memberGroupFilter = match[1].replace("?", "").toLowerCase();
135132
const memberSearchPart = match[2].trim();
136133

137134
if (
138135
memberSearchPart &&
139-
this._searchState.memberGroupFilter !== constants.memberGroupFilter.construct
136+
this.searchState.memberGroupFilter !== constants.memberGroupFilter.construct
140137
) {
141-
this._searchState.memberSearchString = memberSearchPart;
138+
this.searchState.memberSearchString = memberSearchPart;
142139
} else {
143-
this._searchState.memberSearchString = null;
140+
this.searchState.memberSearchString = null;
144141
}
145142
} else {
146-
this._searchState.memberGroupFilter = null;
143+
this.searchState.memberGroupFilter = null;
147144
}
148145
} else {
149-
this._searchState.searchedObjectName = searchInput.trim();
146+
this.searchState.searchedObjectName = searchInput.trim();
150147

151-
if (this._searchState.previousSearchedObjectName !== this._searchState.searchedObjectName) {
152-
this._globalState.visibleObjectName = null;
148+
if (this.searchState.previousSearchedObjectName !== this.searchState.searchedObjectName) {
149+
this.globalState.visibleObjectName = null;
153150
}
154151

155-
this._searchState.memberSearchString = null;
156-
this._searchState.memberGroupFilter = null;
152+
this.searchState.memberSearchString = null;
153+
this.searchState.memberGroupFilter = null;
157154
}
158155

159156
//skip single ? sign
160-
if (this._searchState.memberSearchString === "?") {
161-
this._searchState.memberSearchString = null;
157+
if (this.searchState.memberSearchString === "?") {
158+
this.searchState.memberSearchString = null;
162159
}
163160

164161
const foundObjects = ui5APIFinder.findUi5ApiObjects({
165-
name: this._searchState.searchedObjectName,
162+
name: this.searchState.searchedObjectName,
166163
});
167164

168165
const configuration = vscode.workspace.getConfiguration("UI5ReferencePanel");
169-
this._globalState.hitlistObjectsLimit = configuration.get("hitlistSize") as number;
166+
this.globalState.hitlistObjectsLimit = configuration.get("hitlistSize") as number;
170167

171168
if (foundObjects && foundObjects.length > 0) {
172-
if (foundObjects.length > this._globalState.hitlistObjectsLimit) {
173-
this._webviewView.webview.postMessage({
169+
if (foundObjects.length > this.globalState.hitlistObjectsLimit) {
170+
this.webviewView.webview.postMessage({
174171
type: "tooManySearchResults",
175172
notification: `Found ${foundObjects.length} objects. Please narrow your search.`,
176173
});
177174
} else if (
178175
foundObjects.length > 1 &&
179-
foundObjects.length <= this._globalState.hitlistObjectsLimit
176+
foundObjects.length <= this.globalState.hitlistObjectsLimit
180177
) {
181-
this._webviewView.webview.postMessage({
178+
this.webviewView.webview.postMessage({
182179
type: "multipleSearchResults",
183180
result: foundObjects,
184181
});
185182
} else {
186183
this.handleGetDesignAPIHtml({ ui5Object: foundObjects[0].name }); // , "oneSearchResult");
187184
}
188185
} else {
189-
this._webviewView.webview.postMessage({
186+
this.webviewView.webview.postMessage({
190187
type: "noSearchResults",
191188
notification: "Nothing found",
192189
});
@@ -195,28 +192,28 @@ export class ApiReferenceCtrl {
195192
}
196193

197194
handleGetDesignAPIHtml(message: GetDesignApiMessage) {
198-
this._globalState.visibleObjectName = message.ui5Object;
195+
this.globalState.visibleObjectName = message.ui5Object;
199196

200197
if (message.source === "favorite") {
201-
this._searchState.memberGroupFilter = null;
202-
this._searchState.memberSearchString = null;
198+
this.searchState.memberGroupFilter = null;
199+
this.searchState.memberSearchString = null;
203200
}
204201

205202
const designAPIHtml = this.getDesignAPIHtml(message.ui5Object);
206203

207204
if (!designAPIHtml) {
208-
this._webviewView.webview.postMessage({
205+
this.webviewView.webview.postMessage({
209206
type: "designAPINotFound",
210207
notification: "Design API not found",
211208
});
212209
} else {
213210
if (message.source === "hitlist" || message.source === "favorite") {
214-
this._webviewView.webview.postMessage({
211+
this.webviewView.webview.postMessage({
215212
type: "showDesignAPI",
216213
result: designAPIHtml,
217214
});
218215
} else {
219-
this._webviewView.webview.postMessage({
216+
this.webviewView.webview.postMessage({
220217
type: "oneSearchResult",
221218
result: designAPIHtml,
222219
});
@@ -235,7 +232,7 @@ export class ApiReferenceCtrl {
235232
favorites.addFavorite(message.ui5Object);
236233
}
237234

238-
this._webviewView.webview.postMessage({
235+
this.webviewView.webview.postMessage({
239236
type: "updateFavorites",
240237
favorites: favorites.getFavorites(),
241238
});
@@ -248,19 +245,19 @@ export class ApiReferenceCtrl {
248245
return;
249246
}
250247

251-
if (this._searchState.memberSearchString || this._searchState.memberGroupFilter) {
248+
if (this.searchState.memberSearchString || this.searchState.memberGroupFilter) {
252249
designApi = filtering.filterApiMembers(
253250
designApi,
254-
this._searchState.memberSearchString,
255-
this._searchState.memberGroupFilter
251+
this.searchState.memberSearchString,
252+
this.searchState.memberGroupFilter
256253
);
257254
}
258255

259256
return ui5APIFormatter.getFormattedObjectApi(designApi, true, true);
260257
}
261258

262259
triggerSearch(input: string) {
263-
this._webviewView.webview.postMessage({
260+
this.webviewView.webview.postMessage({
264261
type: "triggerSearch",
265262
input,
266263
});
@@ -269,7 +266,7 @@ export class ApiReferenceCtrl {
269266
triggerWebviewResolved() {
270267
const favs = favorites.getFavorites();
271268

272-
this._webviewView.webview.postMessage({
269+
this.webviewView.webview.postMessage({
273270
type: "webviewResolved",
274271
favorites: favs,
275272
});
@@ -281,8 +278,8 @@ export class ApiReferenceCtrl {
281278
if (!designAPI) {
282279
return null;
283280
} else {
284-
return mustache.render(this._templates.objectAPI, designAPI, {
285-
membersTemplate: this._templates.members,
281+
return mustache.render(this.templates.objectAPI, designAPI, {
282+
membersTemplate: this.templates.members,
286283
});
287284
}
288285
}

0 commit comments

Comments
 (0)