Skip to content

Commit

Permalink
the first cmit is the depest
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed Mar 7, 2019
0 parents commit 94c6a4c
Show file tree
Hide file tree
Showing 8 changed files with 2,768 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
node_modules/
dist/
42 changes: 42 additions & 0 deletions add-note.html
@@ -0,0 +1,42 @@
<!doctype html>

<style>
html, body {
margin: 0;
overflow: hidden;
}

form {
display: flex;
}

input {
flex: 1;
font-size: 72px;
padding: 5px;
color: #333;
font-family: avenir next;
outline: none;
}
</style>

<body hidden>
<form id="form">
<input id="input" autofocus>
<submit hidden></submit>
</form>

<script>
let electron = require("electron")

document.getElementById("form").addEventListener("submit", event => {
electron.ipcRenderer.send(
"add-note",
document.getElementById("input").value
)

event.preventDefault()
})

document.body.removeAttribute("hidden")
</script>
Binary file added icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
85 changes: 85 additions & 0 deletions main.js
@@ -0,0 +1,85 @@
let {
app,
globalShortcut,
BrowserWindow,
ipcMain
} = require("electron")

let fs = require("fs")
let os = require("os")

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

let notesFile = os.homedir() + "/notes"

function show ({width, height, name}) {
window.hide()
window.setSize(width, height)
window.center()
window.loadFile(`${name}.html`)
setTimeout(() => {
window.show()
}, 200)
}

function showAddNote () {
show({
width: 800,
height: 100,
name: "add-note"
})
}

function showNotes () {
show({
width: 800,
height: 800,
name: "notes"
})
}

app.on("ready", () => {
window = new BrowserWindow({
width: 800,
height: 100,
frame: false,
resizable: false,
show: false
})

window.webContents.on("before-input-event", (event, input) => {
if (input.key == "Enter" || input.key == "Escape") {
window.hide()
}
})

ipcMain.on("add-note", (event, note) => {
fs.appendFileSync(notesFile, note + "\n")
window.hide()
})

window.on("close", event => {
event.preventDefault()
window.hide()
})

globalShortcut.register(
"Command+Alt+Space",
showAddNote
)

globalShortcut.register(
"Command+Control+Alt+Space",
showNotes
)

app.on("will-quit", () => {
// Unregister all shortcuts.
globalShortcut.unregisterAll()
})

app.on("window-all-closed", Function.prototype)
})
93 changes: 93 additions & 0 deletions notes.html
@@ -0,0 +1,93 @@
<style>
html, body {
margin: 0;
}
body {
font-family: avenir next;
font-size: 36px;
color: #222;
padding: 1em;
}
ul {
list-style-type: none;
padding: 0;
line-height: 1.5;
}
[type="checkbox"] {
vertical-align: middle;
margin-right: 10px;
}
:checked + label {
text-decoration: line-through;
color: #2222;
}
</style>
<body hidden>
<main id="main" role="main">
<ul id="ul">

</ul>
</main>
<script>
let main = document.getElementById("main")

let fs = require("fs")
let os = require("os")
let electron = require("electron")
let notesFile = os.homedir() + "/notes"

let notes = fs.readFileSync(notesFile, "utf-8")
.split("\n")
.filter(Boolean)

function deleteNotes (checkies) {
let filtered = notes.filter((note, index) => {
return !checkies[index].checked
})

fs.writeFileSync(notesFile, filtered.join("\n") + "\n")
}

function toggleNote (event) {
let {index} = event.target
let checky = event.target.querySelector("[type=\"checkbox\"]")
checky.checked = !checky.checked
}

function render (notes) {
let ul = document.createElement("ul")
ul.id = "ul"
if (notes.length) {
notes.forEach((note, index) => {
let li = document.createElement("li")
let checky = document.createElement("input")
checky.type = "checkbox"
checky.id = `checky-${index}`
let label = document.createElement("label")
label.setAttribute("for", checky.id)
label.textContent = note
li.prepend(checky)
li.append(label)
li.index = index
li.addEventListener("click", toggleNote)
ul.appendChild(li)
})
}
let old = document.getElementById("ul")
main.replaceChild(ul, old)
}

let askToBeClosed = () =>
electron.ipcRenderer("please-close-notes")

window.addEventListener("beforeunload", event => {
event.preventDefault()
deleteNotes(document.querySelectorAll("[type=\"checkbox\"]"))
askToBeClosed()
return true
})

render(notes)

document.body.removeAttribute("hidden")
</script>

0 comments on commit 94c6a4c

Please sign in to comment.