Skip to content

Commit

Permalink
Merge branch 'feature/create-and-hide-and-destroy-notes'
Browse files Browse the repository at this point in the history
  • Loading branch information
cuteapple committed Mar 29, 2019
2 parents 87ac637 + 257218f commit 4dac7fd
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 33 deletions.
60 changes: 46 additions & 14 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,70 @@
<meta charset="UTF-8">
<style>
* {
display: none
/*display: none*/
box-sizing: border-box;
margin: 0;
padding: 0;
border: none;
}

#list-of-notes > * {
width: 100%;
border: 3px solid lightblue;
margin: 3px;
}
</style>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<div>list of all notes</div>
<button onclick="new_note()">new note</button>
<div id="list-of-notes">
list of all notes
</div>
<button ondblclick="new_note()">new note</button>

<script>
const { ipcRenderer } = require('electron')
const localforage = require('localforage')
const metastorage = localforage.createInstance({ name: 'meta' })
const notestorage = localforage.createInstance({})
load_notes()
const notestorage = localforage.createInstance({ name: 'notes' })

{
(async () => {
let notes = await load_notes()
//Todo?: use custom element
let noteelements = notes.map(id => {
let header = document.createElement('header')
header.innerText = id
let content = document.createElement('section')
content.innerText = '' + id + id + id;
let article = document.createElement('article')
article.append(header, content)
article.dataset.id = id

article.onclick = () => open_note(article.dataset.id)

return article
})

document.getElementById('list-of-notes').append(...noteelements)
})()
}
async function load_notes() {
let open_notes = await metastorage.getItem('z-order') || []
let all_notes = await notestorage.keys() || []
open_notes.forEach(open_note)

//show hidden notes behind since there is currently no method to open them :P
{
let all_notes = await notestorage.keys() || []
let open_notes_set = new Set(open_notes)
let hidden_notes = all_notes.filter(n => !open_notes_set.has(n))
ipcRenderer.send('open-notes', hidden_notes)
}
return all_notes
}

ipcRenderer.send('open-notes', open_notes)
function open_note(noteid) {
ipcRenderer.send('open-note', noteid)
}

function new_note() {
ipcRenderer.send('open-notes', ['' + Date.now() + Math.random() * 100000])
let id = '' + Date.now() + Math.random() * 100000
open_note(id)
}

ipcRenderer.on('order-changed', (sender, notes_order) => {
Expand Down
40 changes: 24 additions & 16 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@ const { app, BrowserWindow, ipcMain } = require('electron')
/**@type {BrowserWindow} */
let mainWindow

/**@type {Set<BrowserWindow>} */
let notes = new Set()
/**@typedef {string} NoteId*/
/**@type {Map<NoteId,BrowserWindow>} */
let notes = new Map()

ipcMain.addListener('open-notes', (sender, notes) => {
(notes || [1, 2]).forEach(open_note)
})
ipcMain.addListener('open-note', (sender, noteid) => open_note(noteid))
function open_note(id) {
//check if already open
if (notes.has(id)) {
notes.get(id).focus()
console.log(`bring note ${id} to front`)
return
}

console.log('opening note ', id)
let note = new BrowserWindow({
x: 30,
y: 30,
width: 300,
height: 300 + 32,
x: 30 + Math.round(Math.random() * 200),
y: 30 + Math.round(Math.random() * 200),
width: 100 + Math.round(Math.random() * 300),
height: 100 + 32 + Math.round(Math.random() * 300),
transparent: false,
frame: false,
show: false,
Expand All @@ -30,23 +36,25 @@ function open_note(id) {
note.loadFile('note.html')
note.on('focus', () => {
//refersh z-order
notes.delete(note)
notes.add(note)
mainWindow.webContents.send('order-changed', [...notes].map(n => n.noteid))
notes.delete(note.noteid)
notes.set(note.noteid, note)
//Todo: observe change of notes
mainWindow.webContents.send('order-changed', [...notes.keys()])
})
note.on('closed', () => {
notes.delete(note)
if (!notes.size) mainWindow.close()
notes.delete(note.noteid)
//Todo: condition
//if (!notes.size) mainWindow.close() //no need when mainWindow is visible
})
notes.add(note)
notes.set(note.noteid, note)
}

function initialize() {
mainWindow = new BrowserWindow({
//x: -1, y: -1, width: 1, height: 1,
transparent: false,
frame: true,
x: 100, y: 100, height: 300, width: 400,
x: 800, y: 100, height: 300, width: 400,
webPreferences: {
nodeIntegration: true
}
Expand Down
5 changes: 2 additions & 3 deletions note.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@
</section>

<script>
const localforage = require('localforage')
localforage.config({})
const localforage = require('localforage').createInstance({ name: 'notes' })
const { remote } = require('electron')
const id = remote.getCurrentWindow().noteid
/**@type{HTMLTextAreaElement} */
Expand All @@ -131,7 +130,7 @@
}

let memory = await localforage.getItem(id).catch(err => {
console.log(err)
console.warn(`load note ${id} failed: ${err}`)
return {}
})

Expand Down
Empty file added notewindow.js
Empty file.

0 comments on commit 4dac7fd

Please sign in to comment.