Skip to content

Commit 383009a

Browse files
authored
Migrate all Javascript code to Typescript (#2)
* Migrate all Javascript code to Typescript * Update function signature * Bump version * Clean code
1 parent 012b319 commit 383009a

File tree

10 files changed

+141
-172
lines changed

10 files changed

+141
-172
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
control-compiled.js
2+
example/main-bundle.js
23
.cache/
34
dist/
45
# Logs

control.d.ts

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

control.js renamed to control.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import { ipcRenderer } from 'electron';
1+
import { WebPreferences, ipcRenderer } from 'electron';
2+
import { type TabID } from './index.js';
23

34
// Used in Renderer process
45

56
/**
67
* Tell browser view to load url
78
* @param {string} url
89
*/
9-
const sendEnterURL = url => ipcRenderer.send('url-enter', url);
10+
const sendEnterURL = (url: string) => ipcRenderer.send('url-enter', url);
1011

1112
/**
1213
* Tell browser view url in address bar changed
1314
* @param {string} url
1415
*/
15-
const sendChangeURL = url => ipcRenderer.send('url-change', url);
16+
const sendChangeURL = (url: string) => ipcRenderer.send('url-change', url);
1617

17-
const sendAct = actName => {
18+
const sendAct = (actName: string) => {
1819
ipcRenderer.send('act', actName);
1920
};
2021

@@ -38,20 +39,20 @@ const sendStop = () => sendAct('stop');
3839
* Tell browser view to close tab
3940
* @param {TabID} id
4041
*/
41-
const sendCloseTab = id => ipcRenderer.send('close-tab', id);
42+
const sendCloseTab = (id: TabID) => ipcRenderer.send('close-tab', id);
4243

4344
/**
4445
* Create a new tab
4546
* @param {string} [url]
4647
* @param {object} [references]
4748
*/
48-
const sendNewTab = (url, references) => ipcRenderer.send('new-tab', url, references);
49+
const sendNewTab = (url?: string, references?: WebPreferences) => ipcRenderer.send('new-tab', url, references);
4950

5051
/**
5152
* Tell browser view to switch to specified tab
5253
* @param {TabID} id
5354
*/
54-
const sendSwitchTab = id => ipcRenderer.send('switch-tab', id);
55+
const sendSwitchTab = (id: TabID) => ipcRenderer.send('switch-tab', id);
5556

5657
export default {
5758
sendEnterURL, // sendEnterURL(url) to load url

example/main.js renamed to example/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { fileURLToPath } from 'url';
55

66
const __dirname = fileURLToPath(new URL('.', import.meta.url));
77

8-
let browser;
8+
let browser: BrowserLikeWindow | null;
99

1010
function createWindow() {
1111
browser = new BrowserLikeWindow({

example/renderer/control.jsx renamed to example/renderer/control.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import React from 'react';
2-
import ReactDOM from 'react-dom';
31
import { createRoot } from 'react-dom/client';
42
import { ipcRenderer } from 'electron';
53
import cx from 'classnames';
6-
import useConnect from '../../useConnect';
7-
import action from '../../control';
4+
import useConnect from '../../useConnect.js';
5+
6+
import action from '../../control.js';
7+
import type { TabID } from '../../index.js';
88

99
const IconLoading = () => (
1010
<svg
@@ -97,12 +97,12 @@ function Control() {
9797

9898
const { url, canGoForward, canGoBack, isLoading } = tabs[activeID] || {};
9999

100-
const onUrlChange = e => {
100+
const onUrlChange = (e: React.ChangeEvent<HTMLInputElement>) => {
101101
// Sync to tab config
102102
const v = e.target.value;
103103
action.sendChangeURL(v);
104104
};
105-
const onPressEnter = e => {
105+
const onPressEnter = (e: React.KeyboardEvent<HTMLInputElement>) => {
106106
if (e.keyCode !== 13) return;
107107
const v = e.currentTarget.value.trim();
108108
if (!v) return;
@@ -113,14 +113,14 @@ function Control() {
113113
}
114114
action.sendEnterURL(href);
115115
};
116-
const close = (e, id) => {
116+
const close = (e: React.MouseEvent<HTMLDivElement, MouseEvent>, id: any) => {
117117
e.stopPropagation();
118118
action.sendCloseTab(id);
119119
};
120120
const newTab = () => {
121121
action.sendNewTab();
122122
};
123-
const switchTab = id => {
123+
const switchTab = (id: TabID) => {
124124
action.sendSwitchTab(id);
125125
};
126126

@@ -184,5 +184,5 @@ function Control() {
184184
}
185185

186186
const container = document.getElementById('app');
187-
const root = createRoot(container);
187+
const root = createRoot(container!);
188188
root.render(<Control />);

index.d.ts

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

0 commit comments

Comments
 (0)