Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
linshiyan1992 committed Aug 23, 2021
0 parents commit 12f0ac0
Show file tree
Hide file tree
Showing 21 changed files with 6,161 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules

out/
16 changes: 16 additions & 0 deletions controller/addEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const {ipcRenderer} = require('electron');

const addForm = document
.querySelector('.todo__add-form')
.addEventListener('submit',function(event){
event.preventDefault();
const item = document.querySelector('.todo__add-input');
ipcRenderer.send('addTodo',item.value);
item.value = '';
})

const closeBtn = document
.querySelector('.icon-tab-close')
.addEventListener('click',function(event){
ipcRenderer.send('manageWindow','closeAdd');
})
27 changes: 27 additions & 0 deletions controller/deleteEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { ipcRenderer } = require("electron")

const fadeOut = item=>{
item.classList.add('todo__item--close');
return new Promise((resolve,reject)=>{
setTimeout(() => {
resolve('success');
}, 500);
})
}

async function deleteitem(event){
const item = this.previousSibling.innerText;
await fadeOut(this.parentElement);
this.parentElement.remove();
ipcRenderer.send('deleteTodo',item);
}

function attachDeleteEvent(item){
const button = item.childNodes[1];
button.addEventListener('click',deleteitem);
}

module.exports = {
deleteitem:deleteitem,
attachDeleteEvent:attachDeleteEvent,
}
69 changes: 69 additions & 0 deletions controller/dragEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const {attachDeleteEvent} = require('./deleteEvent.js');

let dragSrcElement;

function handleDragStart(event){
this.style.opacity='0.4';
dragSrcElement = this;
event.dataTransfer.effectAllowed = 'move';
event.dataTransfer.setData('text/html',this.innerHTML);
return false;
}

function handleDragEnter(event){
this.classList.add('over');
return false;
}

function handleDragOver(event){
if(event.preventDefault){
event.preventDefault();
}
return false;
}

function handleDragLeave(event){
this.classList.remove('over');
return false;
}

function handleDragEnd(event){
this.style.opacity = '';
document.querySelectorAll('.todo__item').forEach(todo=>{
todo.classList.remove('over');
})
return false;
}

function handleDrop(event){
event.stopPropagation();
event.preventDefault();
if(dragSrcElement!==this){
dragSrcElement.innerHTML = this.innerHTML;
this.innerHTML = event.dataTransfer.getData('text/html');
attachDeleteEvent(this);
attachDeleteEvent(dragSrcElement);
}
return false;
}

function attachDragEvent(todo){
todo.addEventListener('dragstart',handleDragStart);
todo.addEventListener('dragenter',handleDragEnter)
todo.addEventListener('dragover',handleDragOver);
todo.addEventListener('dragleave',handleDragLeave);
todo.addEventListener('drop',handleDrop);
todo.addEventListener('dragend',handleDragEnd);
}

module.exports= {
handleDragStart:handleDragStart,
handleDragEnter:handleDragEnter,
handleDragOver:handleDragOver,
handleDragLeave:handleDragLeave,
handleDragEnd:handleDragEnd,
handleDrop:handleDrop,
attachDragEvent:attachDragEvent
}


87 changes: 87 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const{app,BrowserWindow, ipcMain} = require('electron');
const dataStore = require('./model/dataStore.js');
const positionStore = require('./model/position.js');

let todoWindow,addWindow;

const position = new positionStore();

function showTodo(){
todoWindow = new BrowserWindow({
width:600,
height:900,
frame:false,
x:position.x,
y:position.y,
resizable:false,
show:false,
webPreferences:{
contextIsolation:false,
nodeIntegration:true
}
})
todoWindow.webContents.loadFile('./view/todo.html');
}

function addTodo(){
const pos = todoWindow.getPosition();
addWindow = new BrowserWindow({
width:450,
height:300,
x:pos[0]+75,
y:pos[1]+300,
frame:false,
resizable:false,
roundedCorners:true,
webPreferences:{
contextIsolation:false,
nodeIntegration:true
}
})
addWindow.webContents.loadFile('./view/add.html');
}

const todoStore = new dataStore();

app.whenReady()
.then(async()=>{
showTodo();
todoWindow.webContents.once('did-finish-load',(event,message)=>{
todoWindow.webContents.send('todos',todoStore.getTodo());
todoWindow.show();
})
todoWindow.on('close',(event)=>{
const pos = todoWindow.getPosition();
console.log(pos);
position.savePos(pos[0],pos[1]);
})
})



ipcMain.on('deleteTodo',(event,item)=>{
todoStore.deleteTodo(item);
todoWindow.webContents.send('todo-result',`${item} Complete!`);
})

ipcMain.on('addTodo',(event,todo)=>{
todoStore.addTodo(todo);
todoWindow.webContents.send('newTodo',todo);
})

ipcMain.on('addTodoWindow',(event,message)=>{
addTodo();
})

ipcMain.on('manageWindow',(event,message)=>{
if(message==='minimize'){
todoWindow.minimize();
}else if(message==='closeAdd'){
addWindow.destroy();
addWindow = null;
todoWindow.webContents.send('todo-result','Adding complete');
}
else if(message==='close'){
todoWindow.close();
}
})
26 changes: 26 additions & 0 deletions model/dataStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const Store = require('electron-store');

class dataStore extends Store{
constructor(){
super();
this.todo = this.get('todo')||[];
}
saveTodo(){
this.set('todo',this.todo);
return this;
}
getTodo(){
const items = this.get('todo')||[];
return items;
}
addTodo(item){
this.todo.push(item);
return this.saveTodo();
}
deleteTodo(itemToDelete){
this.todo = this.todo.filter(item=>item!==itemToDelete)
return this.saveTodo();
}
}

module.exports=dataStore;
17 changes: 17 additions & 0 deletions model/position.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const Store = require('electron-store');

class positionStore extends Store{
constructor(){
super();
this.x = parseInt(this.get('x'))||0;
this.y = parseInt(this.get('y'))||0;
}
savePos(x,y){
this.x = x;
this.y = y;
this.set('x',x);
this.set('y',y);
}
}

module.exports = positionStore;
Loading

0 comments on commit 12f0ac0

Please sign in to comment.