diff --git a/build.sh b/build.sh new file mode 100644 index 0000000000000000000000000000000000000000..0fa3ccd71f3b4b30cc3918ed385bb5de47138d3c --- /dev/null +++ b/build.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +if ! hash electron-packager 2>/dev/null; then + RED='\033[0;31m' + NC='\033[0m' + echo "${RED}Error${NC}: you need to npm install electron-packager. Aborting." + exit 1 +fi + +if [ "$#" -ne 2 ]; then + echo -e "Usage: ./script/build.sh " + echo -e " platform: darwin, linux, win32" + echo -e " arch: ia32, x64" + exit 1 +fi + +PLATFORM=$1 +ARCH=$2 + +echo "Start packaging for $PLATFORM $ARCH." + +if [ $PLATFORM = "linux" ]; then + APP_NAME="electronic-wechat" +else + APP_NAME="Electronic WeChat" +fi + +ignore_list="dist|scripts|\.idea|.*\.md|.*\.yml|node_modules/nodejieba" + +electron-packager . "${APP_NAME}" --platform=$PLATFORM --arch=$ARCH --electronVersion=1.4.15 --app-version=1.4.0 --asar --icon=assets/icon.icns --overwrite --out=./dist --ignore=${ignore_list} + +if [ $? -eq 0 ]; then + echo -e "$(tput setaf 2)Packaging for $PLATFORM $ARCH succeeded.$(tput sgr0)\n" +fi + +if [ $PLATFORM = "darwin" ]; then + ditto -rsrcFork ./dist/Electronic\ WeChat-darwin-x64/Electronic\ WeChat.app /Applications/Electronic\ WeChat.app + echo "$(tput setaf 3)App copied to /Applications. You can open Electronic WeChat there or from Spotlight.$(tput sgr0)" +fi diff --git a/index.html b/index.html index 9b94287f8ca5522b07e9799f4c5f6d31074b05c6..03648af54644eaf3a4b34ea8d8fb66843278e6df 100755 --- a/index.html +++ b/index.html @@ -5,10 +5,6 @@ Dead simple markdown file reader - Use menu to open a file - -
diff --git a/main.js b/main.js index ea43579089a23ba299eee1b33806e1c2dc370a5f..252632120ad0b53a65136025b3bc438853206119 100755 --- a/main.js +++ b/main.js @@ -1,4 +1,23 @@ -const { app, BrowserWindow, Menu, dialog, ipcMain } = require('electron'); +const { app, BrowserWindow, Menu, dialog, ipcMain } = require('electron') +const fs = require('fs') + +/** + * Grab the file path and open it + * @param {array} filePaths the file path from the fileObj + * @return {promise} return a promise + */ +function readFile(filePaths) { + return new Promise((resolver, rejecter) => { + // we don't care about the rest of the selected file only the first one for now + fs.readFile(filePaths[0], {encoding: 'utf8'}, function(err, data) { + if (err) { + return rejecter(err) + } + resolver(data) + }) + }) +} + app.whenReady().then(function() { const mainWindow = new BrowserWindow({ @@ -18,7 +37,6 @@ app.whenReady().then(function() { label:'Open File', accelerator: 'CmdOrCtrl+O', click() { - // ipcMain.on('open-file-dialog', function (event) { dialog.showOpenDialog({ properties: ['openFile'], filters: [ @@ -27,14 +45,18 @@ app.whenReady().then(function() { }) .then(function(fileObj) { console.log('got file?', fileObj) - mainWindow.webContents.send('FILE_OPEN', fileObj.filePaths); + if (!fileObj.canceled) { + readFile(fileObj.filePaths) + .then(md => { + mainWindow.webContents.send('FILE_OPEN', md) + }) + } }) .catch(function(err) { console.error(err) }) } }, - // {label:'CoinMarketCap'}, { label:'Exit', accelerator: 'CmdOrCtrl+x', @@ -45,25 +67,10 @@ app.whenReady().then(function() { ] } ]) - Menu.setApplicationMenu(menu); + Menu.setApplicationMenu(menu) - ipcMain.on('open-file-dialog', function (event) { - console.log('got the message', event) - }) - - mainWindow.webContents.on('did-finish-load', () => { - console.log('----> did-finish-load') - mainWindow.webContents.send('ping', 'whoooooooh!') - }) - - - - - mainWindow.webContents.openDevTools() // mainWindow.maximize(); mainWindow.loadURL(`file://${__dirname}/index.html`) mainWindow.show(); - - -}); +}) diff --git a/package-lock.json b/package-lock.json index 72ae40fdcd3e462cc933e273656fad98fa5f64a2..2bf05f1bbff774ad6c73a271e94944b996e89213 100755 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "desktop-exp", - "version": "1.0.0", + "version": "0.0.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -42,6 +42,21 @@ "integrity": "sha512-yzBInQFhdY8kaZmqoL2+3U5dSTMrKaYcb561VU+lDzAYvqt+2lojvBEy+hmpSNuXnPTx7m9+04CzWYOUqWME2A==", "dev": true }, + "argparse": { + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", + "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", + "requires": { + "sprintf-js": "~1.0.2" + }, + "dependencies": { + "sprintf-js": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", + "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=" + } + } + }, "boolean": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/boolean/-/boolean-3.0.1.tgz", @@ -229,6 +244,11 @@ "once": "^1.4.0" } }, + "entities": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/entities/-/entities-2.0.3.tgz", + "integrity": "sha512-MyoZ0jgnLvB2X3Lg5HqpFmn1kybDiIfEQmKzTb5apr51Rb+T3KdmMiqa70T+bhGnyv7bQ6WMj2QMHpGMmlrUYQ==" + }, "env-paths": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/env-paths/-/env-paths-2.2.0.tgz", @@ -427,6 +447,14 @@ "json-buffer": "3.0.0" } }, + "linkify-it": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/linkify-it/-/linkify-it-3.0.2.tgz", + "integrity": "sha512-gDBO4aHNZS6coiZCKVhSNh43F9ioIL4JwRjLZPkoLIY4yZFwg264Y5lu2x6rb1Js42Gh6Yqm2f6L2AJcnkzinQ==", + "requires": { + "uc.micro": "^1.0.1" + } + }, "lodash": { "version": "4.17.15", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", @@ -440,6 +468,18 @@ "integrity": "sha512-G2Lj61tXDnVFFOi8VZds+SoQjtQC3dgokKdDG2mTm1tx4m50NUHBOZSBwQQHyy0V12A0JTG4icfZQH+xPyh8VA==", "dev": true }, + "markdown-it": { + "version": "11.0.1", + "resolved": "https://registry.npmjs.org/markdown-it/-/markdown-it-11.0.1.tgz", + "integrity": "sha512-aU1TzmBKcWNNYvH9pjq6u92BML+Hz3h5S/QpfTFwiQF852pLT+9qHsrhM9JYipkOXZxGn+sGH8oyJE9FD9WezQ==", + "requires": { + "argparse": "^1.0.7", + "entities": "~2.0.0", + "linkify-it": "^3.0.1", + "mdurl": "^1.0.1", + "uc.micro": "^1.0.5" + } + }, "matcher": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/matcher/-/matcher-3.0.0.tgz", @@ -450,6 +490,11 @@ "escape-string-regexp": "^4.0.0" } }, + "mdurl": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/mdurl/-/mdurl-1.0.1.tgz", + "integrity": "sha1-/oWy7HWlkDfyrf7BAP1sYBdhFS4=" + }, "mimic-response": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-1.0.1.tgz", @@ -702,6 +747,11 @@ "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=", "dev": true }, + "uc.micro": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/uc.micro/-/uc.micro-1.0.6.tgz", + "integrity": "sha512-8Y75pvTYkLJW2hWQHXxoqRgV7qb9B+9vFEtidML+7koHUFapnVJAZ6cKs+Qjz5Aw3aZWHMC6u0wJE3At+nSGwA==" + }, "universalify": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz", diff --git a/package.json b/package.json index b4e382e22983b4d623dd6dae30529f094ae9a856..4291cd40a737f95967f89c44fa90e799e280d20e 100755 --- a/package.json +++ b/package.json @@ -1,16 +1,21 @@ { - "name": "desktop-exp", - "version": "1.0.0", - "description": "", + "name": "dead-simple-markdown-reader", + "version": "0.0.1", + "description": "A dead simple Markdown file reader for Raspberry pi / ARM computers", "main": "main.js", "scripts": { "start": "./node_modules/electron/cli.js .", - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "build": "./build.sh linux armv7l" }, - "author": "", + "author": "to1source ", "license": "ISC", "devDependencies": { "electron": "^9.0.4", + "electron-packager": "^15.1.0", "electron-utils": "^1.0.9" + }, + "dependencies": { + "markdown-it": "^11.0.1" } } diff --git a/render.js b/render.js index 0b0c15cc1e69ae8d2e06771232f0655f6a6931b4..f0f6a21b0972e10b126ac537cebd57c34af42fe1 100644 --- a/render.js +++ b/render.js @@ -1,33 +1,19 @@ // taking all the code out from he index.html +// try to com with the main using ipc +const { ipcRenderer } = window.require('electron') +const MarkdownIt = window.require('markdown-it') +const md = new MarkdownIt() -// using the file input button +// @TODO add a store to remember the last open file -document.getElementById('rendered-area').innerHTML = 'wtf' - -document.getElementById('btnLoadFile').addEventListener("click", function(){ - document.getElementById('fileId').click(); -}); -document.getElementById('fileId').addEventListener('change', function(e){ - //use the file here - var files = e.target.files; - var f = files[0]; { - var reader = new FileReader(); - var name = f.name; - reader.onload = function (e) { - console.log(e.target.result); - }; - reader.readAsBinaryString(f); - } -}); +const target = document.getElementById('rendered-area') -// try to com with the main using ipc -const { ipcRenderer } = window.require('electron') -ipcRenderer.on('ping', (event, message) => { - console.log(event) - alert('wtf????') - document.getElementById('rendered-area').innerHTML = message -}) +target.innerHTML = 'Use the menu to open a file' + ipcRenderer.on('FILE_OPEN', (event, args) => { console.log('got FILE_OPEN', event, args) + let result = md.render(args) + + target.innerHTML = result }) \ No newline at end of file