mirror of
https://github.com/flynx/PortableMag.git
synced 2025-10-29 11:10:08 +00:00
added file reading and writing for node-js...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
893f7a0a77
commit
60f61fa77f
11
index.html
11
index.html
@ -203,6 +203,11 @@ function str2ab(str) {
|
|||||||
function handleFileSelect(evt) {
|
function handleFileSelect(evt) {
|
||||||
var files = evt.target.files; // FileList object
|
var files = evt.target.files; // FileList object
|
||||||
|
|
||||||
|
var type = evt.target.value
|
||||||
|
type = type != null ? type.split('.').pop()
|
||||||
|
: USE_ZIP ? 'zip'
|
||||||
|
: 'json'
|
||||||
|
|
||||||
// Loop through the FileList and render image files as thumbnails.
|
// Loop through the FileList and render image files as thumbnails.
|
||||||
for (var i = 0, f; f = files[i]; i++) {
|
for (var i = 0, f; f = files[i]; i++) {
|
||||||
|
|
||||||
@ -211,11 +216,10 @@ function handleFileSelect(evt) {
|
|||||||
// Closure to capture the file information.
|
// Closure to capture the file information.
|
||||||
reader.onload = (function(theFile) {
|
reader.onload = (function(theFile) {
|
||||||
return function(e) {
|
return function(e) {
|
||||||
console.log('loading...')
|
|
||||||
var raw_data = e.target.result
|
var raw_data = e.target.result
|
||||||
window.JSON_DATA = raw_data
|
window.JSON_DATA = raw_data
|
||||||
|
|
||||||
if(USE_ZIP){
|
if(type == 'zip'){
|
||||||
var zip = new JSZip(raw_data)
|
var zip = new JSZip(raw_data)
|
||||||
console.log('zip loaded...')
|
console.log('zip loaded...')
|
||||||
var json = zip.file('magazine.json').data
|
var json = zip.file('magazine.json').data
|
||||||
@ -223,14 +227,13 @@ function handleFileSelect(evt) {
|
|||||||
var json = raw_data
|
var json = raw_data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
loadJSON($.parseJSON(json), true)
|
loadJSON($.parseJSON(json), true)
|
||||||
console.log('done.')
|
console.log('done.')
|
||||||
};
|
};
|
||||||
})(f);
|
})(f);
|
||||||
|
|
||||||
// Read in the image file as a data URL.
|
// Read in the image file as a data URL.
|
||||||
if(USE_ZIP){
|
if(type == 'zip'){
|
||||||
reader.readAsArrayBuffer(f)
|
reader.readAsArrayBuffer(f)
|
||||||
} else {
|
} else {
|
||||||
reader.readAsBinaryString(f)
|
reader.readAsBinaryString(f)
|
||||||
|
|||||||
@ -80,14 +80,17 @@ var KEYBOARD_CONFIG = {
|
|||||||
// load...
|
// load...
|
||||||
// XXX needs testing...
|
// XXX needs testing...
|
||||||
'ctrl': function(){
|
'ctrl': function(){
|
||||||
showInOverlay('<h1>Open Issue</h1>'+
|
showInOverlay('<h1>Open Issue</h1>'
|
||||||
'<input type="file" id="upload" name="file" multiple onchange="handleFileSelect(event)"/>')
|
+'<input type="file" id="upload" name="file" multiple onchange="handleFileSelect(event)"/>')
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
'S': {
|
'S': {
|
||||||
// save...
|
// save...
|
||||||
// XXX needs testing...
|
// XXX needs testing...
|
||||||
'ctrl': function(){
|
'ctrl': function(){
|
||||||
|
dumpJSONFile()
|
||||||
|
},
|
||||||
|
'ctrl+shift': function(){
|
||||||
showInOverlay('<h1>Save Issue</h1>'+
|
showInOverlay('<h1>Save Issue</h1>'+
|
||||||
'<p>NOTE: this download will not include the actual '+
|
'<p>NOTE: this download will not include the actual '+
|
||||||
'images. at this point, images should be added manually.</p>'+
|
'images. at this point, images should be added manually.</p>'+
|
||||||
|
|||||||
61
magazine.js
61
magazine.js
@ -1070,15 +1070,16 @@ function resetStorageState(title){
|
|||||||
// NOTE: these will only load the data, bookmarks and position are
|
// NOTE: these will only load the data, bookmarks and position are
|
||||||
// ignored...
|
// ignored...
|
||||||
function saveJSONStorage(title){
|
function saveJSONStorage(title){
|
||||||
if(title == null){
|
title = title == null ? getMagazineTitle() : title
|
||||||
title = getMagazineTitle()
|
//var data = $.jStorage.get(title, {})
|
||||||
}
|
var data = localStorage[title]
|
||||||
var data = $.jStorage.get(title, {})
|
data = data == null ? {} : data
|
||||||
$.extend(data, {
|
$.extend(data, {
|
||||||
// XXX do we need to stringify this??
|
// XXX do we need to stringify this??
|
||||||
'magazine-data': buildJSON()
|
'magazine-data': buildJSON()
|
||||||
})
|
})
|
||||||
$.jStorage.set(title, data)
|
//$.jStorage.set(title, data)
|
||||||
|
localStorage[title] = JSON.stringify(data)
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
// load JSON magazine data from storage...
|
// load JSON magazine data from storage...
|
||||||
@ -1086,12 +1087,14 @@ function loadJSONStorage(title){
|
|||||||
if(title == null){
|
if(title == null){
|
||||||
title = getMagazineTitle()
|
title = getMagazineTitle()
|
||||||
}
|
}
|
||||||
var data = $.jStorage.get(title, {})
|
//var data = $.jStorage.get(title, {})
|
||||||
|
var data = localStorage[title]
|
||||||
|
data = data == null ? {} : data
|
||||||
// NOTE: we are caching the data here because the actual structure
|
// NOTE: we are caching the data here because the actual structure
|
||||||
// is persistent and may get overwritten by loadJSON(...)
|
// is persistent and may get overwritten by loadJSON(...)
|
||||||
var bookmarks = data.bookmarks
|
var bookmarks = data.bookmarks
|
||||||
var current_page = data.current_page
|
var current_page = data.current_page
|
||||||
var json = data['magazine-data']
|
var json = JSON.parse(data['magazine-data'])
|
||||||
if(json != null){
|
if(json != null){
|
||||||
loadJSON(json)
|
loadJSON(json)
|
||||||
loadMagazineUserData(current_page, bookmarks)
|
loadMagazineUserData(current_page, bookmarks)
|
||||||
@ -1103,11 +1106,14 @@ function clearJSONStorage(title){
|
|||||||
if(title == null){
|
if(title == null){
|
||||||
title = getMagazineTitle()
|
title = getMagazineTitle()
|
||||||
}
|
}
|
||||||
var data = $.jStorage.get(title, {})
|
//var data = $.jStorage.get(title, {})
|
||||||
|
var data = localStorage[title]
|
||||||
|
data = data == null ? {} : data
|
||||||
var json = data['magazine-data']
|
var json = data['magazine-data']
|
||||||
if(json != null){
|
if(json != null){
|
||||||
delete data['magazine-data']
|
delete data['magazine-data']
|
||||||
$.jStorage.set(title, data)
|
//$.jStorage.set(title, data)
|
||||||
|
localStorage[title] = data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1342,7 +1348,6 @@ function buildJSON(export_bookmarks, export_position){
|
|||||||
// instead.
|
// instead.
|
||||||
function loadJSON(data, load_user_data){
|
function loadJSON(data, load_user_data){
|
||||||
function _build(parent, data){
|
function _build(parent, data){
|
||||||
|
|
||||||
// page...
|
// page...
|
||||||
if(data.type == 'page'){
|
if(data.type == 'page'){
|
||||||
var res = createPage(data.content)
|
var res = createPage(data.content)
|
||||||
@ -1417,6 +1422,38 @@ function loadJSON(data, load_user_data){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/********************************************** JSON serialization ****
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
// XXX this depends on node-webkit...
|
||||||
|
function dumpJSONFile(path, data){
|
||||||
|
path = path == null ? './'+Date.timeStamp()+'-magazine.json' : path
|
||||||
|
data = data == null ? buildJSON() : data
|
||||||
|
|
||||||
|
var fs = require('fs')
|
||||||
|
var fse = require('fs.extra')
|
||||||
|
|
||||||
|
var dirs = path.split(/[\\\/]/)
|
||||||
|
dirs.pop()
|
||||||
|
dirs = dirs.join('/')
|
||||||
|
// build path...
|
||||||
|
if(!fs.existsSync(dirs)){
|
||||||
|
console.log('making:', path)
|
||||||
|
fse.mkdirRecursiveSync(path)
|
||||||
|
}
|
||||||
|
return fs.writeFileSync(path, JSON.stringify(data), encoding='utf8')
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function loadJSONFile(path){
|
||||||
|
return $.getJSON(path)
|
||||||
|
.done(function(data){
|
||||||
|
loadJSON(data)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/***************************************************** constructor ***/
|
/***************************************************** constructor ***/
|
||||||
// These function will construct detached magazine building blocks...
|
// These function will construct detached magazine building blocks...
|
||||||
@ -1469,7 +1506,7 @@ function createPage(data){
|
|||||||
} else {
|
} else {
|
||||||
return page.append($('<div/>')
|
return page.append($('<div/>')
|
||||||
.addClass('content')
|
.addClass('content')
|
||||||
.html(data))
|
.append(jdata))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function createCoverPage(data){
|
function createCoverPage(data){
|
||||||
@ -1550,7 +1587,7 @@ function runMagazineTemplates(){
|
|||||||
// load the data...
|
// load the data...
|
||||||
function loadMagazineData(mag){
|
function loadMagazineData(mag){
|
||||||
removeMagazine()
|
removeMagazine()
|
||||||
mag.appendTo($('.aligner'))
|
mag.appendTo($('.viewer'))
|
||||||
$('.viewer').trigger('magazineDataLoaded')
|
$('.viewer').trigger('magazineDataLoaded')
|
||||||
return mag
|
return mag
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user