Woongjae Lee
NHN Dooray - Frontend Team
2woongjae@gmail.com
// 1. ipcMain 이벤트에 바인딩 된 함수를 분리
ipcMain.on('request-login', this._ipcRequestLogin);
ipcMain.on('request-logout', this._ipcRequestLogout);
ipcMain.on('send-message', this._ipcSendMessage);
// 2. log 에 TAG 추가
console.log(TAG, '_btnLoginClicked');
// 3. 모든 함수에 타입 처리
// 1. Dom 이벤트 바인딩과 ipcRenderer 이벤트 바인딩 함수
this._bindDomEvent();
this._bindIpc();
// 2. Dom 이벤트에 바인딩 된 함수를 분리
this._btnLogin.addEventListener('click', this._btnLoginClicked);
this._btnLogout.addEventListener('click', this._btnLogoutClicked);
this._btnSendMessage.addEventListener('click', this._btnSendMessageClicked);
this._btnToggle.addEventListener('click', this._btnToggleClicked);
this._messageDom.addEventListener('keypress', this._messageDomKeypressed);
// 3. ipcRenderer 이벤트에 바인딩 된 함수를 분리
ipcRenderer.on('login-success', this._ipcLoginSuccess);
ipcRenderer.on('login-error', this._ipcLoginError);
ipcRenderer.on('logout-success', this._ipcLogoutSuccess);
ipcRenderer.on('general-message', this._ipcGeneralMessage);
// 4. log 에 TAG 추가
console.log(TAG, '_btnLoginClicked');
// 5. 모든 함수에 타입 처리
ref.child('general').on('value', snapshot => {
if (snapshot) {
const data = null; // snapshot.val();
const messageObjects: MessageObjectType[] = Object.keys(data).map(id => {
const messageObject: MessageObjectType = {
id,
email: data[id].email,
name: data[id].name,
message: data[id].message,
time: data[id].time
};
return messageObject;
});
event.sender.send('general-message', messageObjects);
}
});
ref.child('general').on('value', snapshot => {
if (snapshot) {
const data = snapshot.val();
const messageObjects: MessageObjectType[] = data ? Object.keys(data).map(id => {
const messageObject: MessageObjectType = {
id,
email: data[id].email,
name: data[id].name,
message: data[id].message,
time: data[id].time
};
return messageObject;
}) : [];
event.sender.send('general-message', messageObjects);
}
});
import {app, BrowserWindow, ipcMain, Tray} from 'electron';
const TRAY_ICON_PATH = path.join(__dirname, '../../static/tray.png');
private _ready = (): void => {
console.log(TAG, '_ready');
this._tray = new Tray(TRAY_ICON_PATH);
...
}
import {app, BrowserWindow, ipcMain, Tray, Menu} from 'electron';
private _ready = (): void => {
console.log(TAG, '_ready');
this._tray = new Tray(TRAY_ICON_PATH);
this._tray.setContextMenu(this._getTrayMenu());
}
private _getTrayMenu(): Electron.Menu {
return Menu.buildFromTemplate([
{
label: 'menu',
click: () => {
console.log('menu');
}
}
]);
}
this._win = new BrowserWindow({
width: 500,
minWidth: 500,
maxWidth: 500,
height: 700,
minHeight: 700,
maxHeight: 700,
maximizable: false,
show: false
});
this._win.loadURL(HTML);
this._win.once('ready-to-show', () => {
this._win.show();
});
this._win.on('close', (event) => {
this._win.hide();
event.preventDefault();
});
private _getTrayMenu(): Electron.Menu {
return Menu.buildFromTemplate([
{
label: 'Open',
click: () => {
if (this._win) {
this._win.show();
}
}
},
{
type: 'separator'
},
{
label: 'Exit',
click: () => {
this._app.exit();
}
}
]);
}
this._app.on('quit', this._quit);
private _quit = () => {
console.log(TAG, '_quit');
if (this._tray) {
this._tray.destory();
}
}
private _ready = (): void => {
...
Menu.setApplicationMenu(this._getApplicationMenu());
...
}
private _getApplicationMenu(): Electron.Menu {
return Menu.buildFromTemplate([
{
label: 'SimpleChat',
submenu: [
{
label: 'Open',
click: () => {
this._win.show();
}
},
{
type: 'separator'
},
{
label: 'Quit',
click: () => {
this._app.exit();
}
}
]
}
]);
}
{
label: 'Edit',
submenu: [
{role: 'undo'},
{role: 'redo'},
{type: 'separator'},
{role: 'cut'},
{role: 'copy'},
{role: 'paste'},
{role: 'pasteandmatchstyle'},
{role: 'delete'},
{role: 'selectall'}
]
}
this._input_email.addEventListener('keypress', this._inputEmailKeypressed);
this._input_password.addEventListener('keypress', this._inputPasswordKeypressed);
private _inputEmailKeypressed = (event): void => {
console.log(TAG, '_inputEmailKeypressed');
if (event.keyCode === 13) {
this._input_password.focus();
}
}
private _inputPasswordKeypressed = (event): void => {
console.log(TAG, '_inputPasswordKeypressed');
if (event.keyCode === 13) {
this._requestLogin();
}
}
private _requestLogin = (): void => {
const win = remote.getCurrentWindow();
if (this._input_email.value.length < 4 || !validateEmail(this._input_email.value)) {
dialog.showMessageBox(win, {
message: 'Login Failed',
detail: '메일 주소가 유효하지 않습니다.'
}, () => {
this._input_email.focus();
});
return;
}
if (this._input_password.value.length < 4) {
dialog.showMessageBox(win, {
message: 'Login Failed',
detail: '패스워드가 유효하지 않습니다.'
}, () => {
this._input_password.focus();
});
return;
}
const loginObj: LoginObjectType = {
email: this._input_email.value,
password: this._input_password.value
};
ipcRenderer.send('request-login', loginObj);
}
private _ipcGeneralMessage = (event, arg: MessageObjectType[]): void => {
console.log(TAG, '_ipcGeneralMessage');
const messagesHTML = arg.map(messageObject => {
return `
<div class="box">
<article class="media">
<div class="media-content">
<div class="content">
<p>
<strong>${messageObject.name}</strong> <small>${messageObject.email}</small> <small>${messageObject.time}</small>
<br>
${messageObject.message}
</p>
</div>
</div>
</article>
</div>
`;
}).join('');
const messageContainer = document.querySelector('#message-container') as HTMLDivElement;
messageContainer.innerHTML = messagesHTML;
document.getElementById('chat-section').scrollTop = messageContainer.scrollHeight;
}
ref.child(`profile`).on('value', snapshot => {
if (snapshot) {
const data = snapshot.val();
if (data) {
const isExist = Object.keys(data).filter(id => data[id].email === arg.email);
if (isExist.length > 0) {
this._name = data[isExist[0]].name;
} else {
const name = arg.email.split('@')[0];
ref.child(`profile`).push().set({
email: arg.email,
name,
createAt: new Date().toISOString()
});
this._name = name;
}
} else {
const name = arg.email.split('@')[0];
ref.child(`profile`).push().set({
email: arg.email,
name,
createAt: new Date().toISOString()
});
this._name = name;
}
}
});
private _ipcSendMessage = async (event, arg: string): Promise<void> => {
console.log(TAG, '_ipcSendMessage');
if (this._auth.currentUser) {
const ref = this._database.ref();
ref.child('general').push().set({
email: this._auth.currentUser.email,
name: this._name,
message: arg,
time: new Date().toISOString()
});
}
}
By Woongjae Lee
타입스크립트 한국 유저 그룹 일렉트론 워크샵 201709