Skip to content

Commit

Permalink
ReadMe
Browse files Browse the repository at this point in the history
  • Loading branch information
al5ina5 committed Dec 16, 2020
1 parent ee5460c commit 709ba7e
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 250 deletions.
22 changes: 8 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
# Electron application example
# mpad

This example shows how you can use Next.js inside an Electron application to avoid a lot of configuration. It uses the Next.js router as view and server-render to speed up the initial render of the application.
A multi-use clipboard manager and note pad for macOS.

For development it's going to run an HTTP server and let Next.js handle routing. In production it uses `next export` to pre-generate HTML static files and uses them in your app instead of running an HTTP server.
# Download

**You can find a detailed documentation about how to build Electron apps with Next.js [here](https://leo.im/2017/electron-next)!**
Coming soon.

## How to use

Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:

```bash
npx create-next-app --example with-electron with-electron-app
# or
yarn create next-app --example with-electron with-electron-app
# Development
```

You can create the production app using `npm run dist`.
yarn
yarn start
```
23 changes: 18 additions & 5 deletions main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const fs = require('fs-extra')
const shortid = require('shortid')
const SHA256 = require('crypto-js/sha256')
const keytar = require('keytar')
const CryptoJS = require('crypto-js')

let tray = null
let window = null
Expand All @@ -22,15 +22,28 @@ const path = app.getPath('userData')
app.dock.hide()

app.on('ready', async () => {
await prepareNext('./renderer')

salt = await keytar.getPassword('mpad', 'hash')

if (!salt) {
salt = SHA256(shortid.generate())
salt = CryptoJS.SHA256(shortid.generate())
await keytar.setPassword('mpad', 'hash', salt.toString())
}

var file = await fs.readFile(join(path, '.db')).catch((e) => console.log('No database detected.'))

if (file) {
try {
var crypt = CryptoJS.AES.decrypt(file.toString(), salt)
crypt.toString(CryptoJS.enc.Utf8)
console.log('Decrypted successfully.')
} catch (error) {
console.log(error)
await fs.rename(join(path, '.db'), `${join(path, '.db')}.${Date.now()}`)
console.log('Failed to decrypt. Deleting database.')
}
}

await prepareNext('./renderer')

createTray()
createWindow()
})
Expand Down
9 changes: 2 additions & 7 deletions main/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@ const NetworkSpeed = require('network-speed')
const { join } = require('path')
const CryptoJS = require('crypto-js')

// Since we disabled nodeIntegration we can reintroduce
// needed node functionality here

const salt = process.argv.find((arg) => arg.startsWith('salt:')).split(':')[1]
const dbPath = process.argv.find((arg) => arg.startsWith('dbPath:')).split(':')[1]

console.log(salt)

const encrypt = (message) => {
return CryptoJS.AES.encrypt(JSON.stringify(message), salt).toString()
}
Expand All @@ -33,16 +28,16 @@ const db = low(adapter)

db.defaults({ clipboards: [], notes: '' }).write()

const CopyEvent = (detail) => new CustomEvent('copy', { detail: detail })
const CopyEvent = (detail) => new CustomEvent('clipboardEvent', { detail: detail })
const copyHandler = () => {
const newClip = { id: shortid.generate(), date: Date.now(), content: clipboard.readText() }
if (/^\s*$/.test(newClip.content)) return

const latest = db.get('clipboards').sortBy('date').reverse().take(5).value()
const exists = latest.find((clip) => clip.content === newClip.content)

if (exists) return

console.log('newClip', newClip)
window.dispatchEvent(CopyEvent(newClip))

db.get('clipboards').push(newClip).write()
Expand Down
57 changes: 0 additions & 57 deletions renderer/lib/mstats.js

This file was deleted.

4 changes: 1 addition & 3 deletions renderer/pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import Nav from '../components/Nav'
import '../global.scss'
import { useStats } from '../lib/mstats'

export default function App({ Component, pageProps }) {
const stats = useStats()
return (
<>
<div className='w-full h-screen flex flex-col bg-white text-shadow-lg'>
<Nav />
<div className='relative w-full box-border h-screen p-4 text-gray-600 overflow-auto hide-scroll-bar'>
<Component {...stats} {...pageProps} />
<Component {...pageProps} />
</div>
</div>
</>
Expand Down
14 changes: 9 additions & 5 deletions renderer/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ const ClipboardPage = ({ stats }) => {
useEffect(() => {
const handler = (e) => {
console.log('Detected copy event.')
console.log(e)

console.log(clipboards)
console.log('e', e)
console.log('e.detail', e.detail)

setClipboards([e.detail, ...clipboards])
}

window.addEventListener('copy', handler)
return () => window.removeEventListener('copy', handler)
window.addEventListener('clipboardEvent', handler)
return () => window.removeEventListener('clipboardEvent', handler)
}, [clipboards])

return (
Expand All @@ -52,6 +51,11 @@ const ClipboardPage = ({ stats }) => {
</CopyToClipboard>
)
})}

{!clipboards && <p className='text-xs text-gray-400'>Loading...</p>}
{clipboards?.length <= 0 && (
<p className='text-xs font-mono font-medium text-gray-400'>No clips found. Copy something!</p>
)}
</div>
</>
)
Expand Down
28 changes: 0 additions & 28 deletions renderer/pages/memory.js

This file was deleted.

1 change: 0 additions & 1 deletion renderer/pages/notes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useState, useEffect } from 'react'
import { useStats } from '../lib/mstats'

const { db } = global

Expand Down
16 changes: 0 additions & 16 deletions renderer/pages/settings.js

This file was deleted.

31 changes: 0 additions & 31 deletions renderer/pages/storage.js

This file was deleted.

83 changes: 0 additions & 83 deletions renderer/pages/system.js

This file was deleted.

0 comments on commit 709ba7e

Please sign in to comment.