Skip to content

Commit

Permalink
Merge branch 'test/remember-things'
Browse files Browse the repository at this point in the history
  • Loading branch information
cuteapple committed Mar 27, 2019
2 parents 6560b8a + 99ae158 commit 90c8e7c
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 44 deletions.
34 changes: 24 additions & 10 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
* {
display:none
}
</style>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<div>list of all notes</div>
<script>
const { ipcRenderer } = require('electron')
const localforage = require('localforage')
localforage.config({})
localforage.keys().then(notes => {
ipcRenderer.send('open-notes', notes)
})
</script>
</body>
</html>
62 changes: 33 additions & 29 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
const { app, BrowserWindow } = require('electron')
require('electron-debug')();
const { app, BrowserWindow, ipcMain } = require('electron')
require('electron-debug')()

/**@type {BrowserWindow} */
let mainWindow

/**@type {Set<BrowserWindow>} */
let notes = new Set()

ipcMain.addListener('open-notes', (sender, notes) => {
console.log(notes, [1, 2], notes || [1, 2]);
(notes || [1, 2]).forEach(open_note)
})
function open_note(id) {
console.log('opening note ', id)
let note = new BrowserWindow({
x: 30,
y: 30,
width: 300,
height: 300 + 32,
transparent: false,
frame: false,
show: false,
backgroundColor: '#f0f',
parent: mainWindow,
webPreferences: {
nodeIntegration: true
}
})
note.noteid = id
note.loadFile('note.html')

note.on('closed', () => {
notes.delete(note)
if (!notes.size) mainWindow.close()
})
notes.add(note)
}

function initialize() {

Expand All @@ -16,36 +45,11 @@ function initialize() {
frame: false,
x: 100, y: 100, height: 100, width: 100,
webPreferences: {
nodeIntegration: false
nodeIntegration: true
}
})
mainWindow.setIgnoreMouseEvents(true)


for (let i of [1, 2]) {
let note = new BrowserWindow({
x: 30 + i * 100,
y: 30 + i * 100,
width: 300,
height: 300 + 32,
transparent: false,
frame: false,
backgroundColor: '#f0f',
parent: mainWindow,
webPreferences: {
nodeIntegration: true
}
})
note.noteid = i
note.loadFile('note.html')

note.on('closed', () => {
notes.delete(note)
if (!notes.size) mainWindow.close()
})
notes.add(note)
}

mainWindow.loadFile('index.html')
mainWindow.on('closed', function () {
mainWindow = null
})
Expand Down
35 changes: 31 additions & 4 deletions note.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
}

#content > textarea {
/*white-space: pre;*/
width: 100%;
height: 100%;
resize: none;
Expand Down Expand Up @@ -114,20 +115,46 @@
</section>

<script>
const localforage = require('localforage')
localforage.config({})
const { remote } = require('electron')
const id = remote.getCurrentWindow().noteid
/**@type{HTMLTextAreaElement} */
const textContentElement = document.querySelector('#content textarea')

load()
console.log(`note ${id} opened`)

function load() {
textContentElement.value = localStorage[id] || ''
async function load() {
const default_config = {
bounds: remote.getCurrentWindow().getBounds(),
content: '',
}

let memory = await localforage.getItem(id).catch(err => {
console.log(err)
return {}
})

let config = { ...default_config, ...memory }

console.log('default_config', default_config)
console.log('memory', memory)
console.log('config', config)

textContentElement.value = config.content
remote.getCurrentWindow().setBounds(config.bounds)
remote.getCurrentWindow().show()
console.log(`note ${id} opened`)
}

remote.getCurrentWindow().addListener('moved', save)
remote.getCurrentWindow().addListener('resize', save)

function save() {
localStorage[id] = textContentElement.value
localforage.setItem(id, {
bounds: remote.getCurrentWindow().getBounds(),
content: textContentElement.value
})
console.log(`note ${id} saved`)
}

Expand Down
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"electron": "^4.0.7"
},
"dependencies": {
"electron-debug": "^2.1.0"
"electron-debug": "^2.1.0",
"localforage": "^1.7.3"
}
}

0 comments on commit 90c8e7c

Please sign in to comment.