Implementing source.load()

This commit is contained in:
Devine Lu Linvega
2019-04-22 10:25:31 +09:00
parent 2b8ddbf764
commit f72547b634
7 changed files with 25 additions and 19 deletions

View File

@@ -16,10 +16,17 @@ function Source (dotgrid) {
fs.readFile(paths[0], 'utf-8', (err, data) => {
if (err) { alert('An error ocurred reading the file :' + err.message); return }
dotgrid.tool.replace(JSON.parse(data.toString().trim()))
this.load(paths[0], data)
})
}
this.load = function (path, data) {
if (!path || isJson(data) === false) { return }
const parsed = JSON.parse(`${data}`)
console.log(path)
dotgrid.tool.replace(parsed)
}
this.save = function () {
if (dotgrid.tool.length() < 1) { console.warn('Nothing to save'); return }
dotgrid.manager.toGRID(grab)
@@ -41,4 +48,6 @@ function Source (dotgrid) {
link.setAttribute('download', name)
link.dispatchEvent(new MouseEvent(`click`, { bubbles: true, cancelable: true, view: window }))
}
function isJson (text) { try { JSON.parse(text); return true } catch (error) { return false } }
}