From dfc294ad86a1ff03f0287a09d1ea9360a30d7ef1 Mon Sep 17 00:00:00 2001 From: kor Date: Thu, 9 Aug 2018 03:49:00 +1200 Subject: [PATCH] Update add. Messy but working correctly. --- docs/index.html | 3 --- docs/logic/add.js | 14 +++++++------- docs/logic/main.js | 26 ++++++++++++++++++++++---- docs/logic/wrap.js | 4 ++-- docs/logic/write.js | 23 ----------------------- foo.txt | 1 - main.js | 38 +++++++++++++++++++++++++++++++++----- 7 files changed, 64 insertions(+), 45 deletions(-) delete mode 100644 docs/logic/write.js delete mode 100644 foo.txt diff --git a/docs/index.html b/docs/index.html index 75fc2da..0c4beb1 100644 --- a/docs/index.html +++ b/docs/index.html @@ -19,12 +19,10 @@ - -
@@ -35,7 +33,6 @@ \ No newline at end of file diff --git a/docs/logic/add.js b/docs/logic/add.js index f306bea..1e5429a 100644 --- a/docs/logic/add.js +++ b/docs/logic/add.js @@ -1,6 +1,4 @@ const { ipcRenderer } = nodeRequire('electron'); -// const mainProcess = remote.nodeRequire('./main.js'); -//const mainProcess = nodeRequire('./main.js'); function Add() { @@ -95,7 +93,10 @@ function Add() () => { console.log('called write'); - ipcRenderer.send('write', '
'+this.display.innerHTML); + let content = this.display.innerHTML.replace(/\s?()\s?/g, "\r\n"); // replace line breaks + content = content.replace(/ /g, ' '); // replace tabs/spaces + // var content = content.replace(/\u00a0/g, ' '); + ipcRenderer.send('write', "\r\n" + "\r\n" + content); }); for (var i = 0; i < this.keys.length; i++) @@ -164,15 +165,16 @@ function Add() { if (parent.elementList[parent.keys[i]].elem.value != '') { - value += parent.elementList[parent.keys[i]].elem.value.toUpperCase() + '
' + value += parent.elementList[parent.keys[i]].elem.value.toUpperCase(); } else { - value += 'TITLE
'; + value += 'TITLE'; } } else if (parent.elementList[parent.keys[i]].elem.value != '') { + value += '
'; value += '  '; value += parent.elementList[parent.keys[i]].desc.toUpperCase() + ' : '; @@ -207,8 +209,6 @@ function Add() { value += parent.elementList[parent.keys[i]].elem.value.toLowerCase(); } - - value += '
'; } } parent.display.innerHTML = value; diff --git a/docs/logic/main.js b/docs/logic/main.js index d508db0..7e20ece 100644 --- a/docs/logic/main.js +++ b/docs/logic/main.js @@ -6,23 +6,41 @@ function Main() this.write = null; this.queryPrev = ''; this.queryCur = ''; + var parent = this; this.install = function() { - this.db = new Wrap(DATABASE); - this.db.install(); + let dbLoaded = false; + const FILELOCATION = 'content/data.ndtl'; + var client = new XMLHttpRequest(); + client.open('GET', FILELOCATION); + client.onreadystatechange = function() + { + if (!dbLoaded && client.responseText.trim() != '') + { + dbLoaded = true; + parent.setup(client.responseText); + } + } + client.send(); + } + + this.setup = function(data) + { + this.db = new Wrap(); + this.db.install(data); this.view = new View(); this.view.install(); this.add = new Add(); this.add.install(); - this.write = new Write(); - this.write.install(); var escape = document.getElementById("escape"); escape.onclick = function() { main.load(main.queryPrev); } + + this.start(); } this.start = function() diff --git a/docs/logic/wrap.js b/docs/logic/wrap.js index 86f72e0..4ee3409 100644 --- a/docs/logic/wrap.js +++ b/docs/logic/wrap.js @@ -3,9 +3,9 @@ function Wrap() this.database = null; this.keys = null; - this.install = function() + this.install = function(data) { - this.database = new Indental(DATABASE).parse(); + this.database = new Indental(data).parse(); this.keys = Object.keys(this.database); this.process(); } diff --git a/docs/logic/write.js b/docs/logic/write.js deleted file mode 100644 index 2c85da7..0000000 --- a/docs/logic/write.js +++ /dev/null @@ -1,23 +0,0 @@ - -function Write() -{ - - this.install = function() - { - } - - this.insert = function(data) - { - let position = 5; // get insert position - - let foo = fs.openSync('foo.txt','r+'); - let buf = new Buffer("hello"); - fs.writeSync(foo, buf, 0, buf.length, position); - fs.close(foo); - } -} - -// fs.appendFile('mynewfile1.txt', 'Hello content!', function (err) { -// if (err) throw err; -// console.log('Saved!'); -// }); \ No newline at end of file diff --git a/foo.txt b/foo.txt deleted file mode 100644 index e2e107a..0000000 --- a/foo.txt +++ /dev/null @@ -1 +0,0 @@ -123456789 \ No newline at end of file diff --git a/main.js b/main.js index 6285a48..8a8f9f8 100644 --- a/main.js +++ b/main.js @@ -4,6 +4,7 @@ const url = require('url') const shell = require('electron').shell; const fs = require('fs'); const { ipcMain } = require('electron'); +const FILELOCATION = 'docs/content/data.ndtl'; let is_shown = true; @@ -94,10 +95,37 @@ app.on('activate', () => { ipcMain.on('write', (event, arg) => { - console.log('Write called! Entry = ' + arg); + // var fileLength = fs.statSync(FILELOCATION)['size']; + // console.log('Write called! Length = ' + fileLength); + // console.log('Write called! Entry = ' + arg); - let foo = fs.openSync('foo.txt','r+'); - let buf = new Buffer(arg); - fs.writeSync(foo, buf, 0, buf.length, 5); - fs.close(foo); + // const stream = fs.createReadStream(FILELOCATION, { encoding: 'utf8' }); + // stream.on('data', data => { + // header = data.split(/\n/)[0]; + // stream.destroy(); + // }); + // stream.on('close', () => { + // console.timeEnd(label); + // resolve(); + // }); + + // fs.readFile(FILELOCATION, "utf8", + // function(err, data) + // { + // console.log(data); + // var position = data.lastIndexOf('`'); + // console.log('Write called! position = ' + position); + + // let fileStream = fs.openSync(FILELOCATION, 'r+'); + // // let buf = new Buffer(arg); + // let buf = new Buffer('_____HERE_____'); + // fs.writeSync(fileStream, buf, 0, buf.length, position); + // fs.close(fileStream); + // } + // ); + + fs.appendFile(FILELOCATION, arg, function (err) { + if (err) throw err; + console.log('Saved!'); + }); }); \ No newline at end of file