Implemented file save/load, fixes #39

This commit is contained in:
Devine Lu Linvega
2019-07-14 09:52:38 +09:00
parent 851c2bdac2
commit 335feca28f
7 changed files with 202 additions and 40 deletions

View File

@@ -11,9 +11,6 @@ function Commander (ronin) {
host.appendChild(this.el)
this._input.addEventListener('input', this.onInput)
window.addEventListener('dragover', this.drag)
window.addEventListener('drop', this.drop)
}
this.start = function () {
@@ -24,8 +21,9 @@ function Commander (ronin) {
}
this.run = function (txt = this._input.value) {
if (txt.indexOf('$') > -1) { console.log('Contains $'); return }
if (txt.indexOf('$') > -1) { ronin.log('Present: $'); return }
console.log('========')
ronin.surface.maximize()
const inter = new Lisp(txt, ronin.library)
inter.toPixels()
}
@@ -126,12 +124,10 @@ function Commander (ronin) {
// Display
this.show = function () {
console.log('show')
this.el.className = ''
}
this.hide = function () {
console.log('hide')
this.el.className = 'hidden'
}
@@ -142,30 +138,4 @@ function Commander (ronin) {
this.hide()
}
}
// Events
this.drag = (e) => {
e.stopPropagation()
e.preventDefault()
e.dataTransfer.dropEffect = 'copy'
}
this.drop = (e) => {
e.preventDefault()
e.stopPropagation()
const file = e.dataTransfer.files[0]
if (!file || !file.name) { console.warn('File', 'Not a valid file.'); return }
if (file.name.indexOf('.lisp') > -1) {
const reader = new FileReader()
reader.onload = (e) => {
this.load(e.target.result)
this.show()
}
reader.readAsText(file)
} else if (file.path) {
this.injectPath(file.path)
this.show()
}
}
}

View File

@@ -15,6 +15,8 @@ function Ronin () {
this.el.id = 'ronin'
this.theme = new Theme(defaultTheme)
this.source = new Source(this)
this.commander = new Commander(this)
this.surface = new Surface(this)
this.library = new Library(this)
@@ -28,10 +30,14 @@ function Ronin () {
this.el.appendChild(this._wrapper)
host.appendChild(this.el)
this.theme.install()
window.addEventListener('dragover', this.drag)
window.addEventListener('drop', this.drop)
}
this.start = function () {
this.theme.start()
this.source.start()
this.commander.start()
this.surface.start()
@@ -49,4 +55,26 @@ function Ronin () {
this.load = function (content = this.default()) {
}
// Events
this.drag = (e) => {
e.stopPropagation()
e.preventDefault()
e.dataTransfer.dropEffect = 'copy'
}
this.drop = (e) => {
e.preventDefault()
e.stopPropagation()
const file = e.dataTransfer.files[0]
if (!file || !file.name) { console.warn('File', 'Not a valid file.'); return }
const path = file.path ? file.path : file.name
if (path.indexOf('.lisp') > -1) {
this.source.read(path)
this.commander.show()
} else if (file.path) {
this.commander.injectPath(file.path)
this.commander.show()
}
}
}

View File

@@ -0,0 +1,156 @@
'use strict'
function Source (ronin) {
const fs = require('fs')
const path = require('path')
const { dialog, app } = require('electron').remote
this.path = null
this.start = function () {
this.new()
}
this.new = function () {
console.log('Source', 'Make a new file..')
this.path = null
}
this.open = function () {
console.log('Source', 'Open a file..')
let paths = dialog.showOpenDialog(app.win, { properties: ['openFile'], filters: [{ name: 'Ronin Lisp', extensions: ['lisp'] }] })
if (!paths) { console.log('Nothing to load'); return }
this.read(paths[0])
}
this.save = function (quitAfter = false) {
console.log('Source', 'Save..', this.path)
if (this.path) {
this.write(this.path, this.generate(), quitAfter)
} else {
this.saveAs(quitAfter)
}
}
this.saveAs = function (quitAfter = false) {
console.log('Source', 'Save a file as..')
dialog.showSaveDialog((loc) => {
if (loc === undefined) { return }
if (loc.indexOf('.lisp') < 0) { loc += '.lisp' }
this.write(loc, this.generate(), quitAfter)
this.path = loc
})
}
this.revert = function () {
if (!this.path) { return }
console.log('Source', 'Revert a file..')
this.read(this.path)
}
// I/O
this.write = function (loc, data = this.generate(), quitAfter = false) {
console.log('Source', 'Writing ' + loc)
fs.writeFileSync(loc, data)
if (quitAfter === true) {
app.exit()
}
}
this.read = function (loc = this.path) {
if (!loc) { return }
if (!fs.existsSync(loc)) { console.warn('Source', 'File does not exist: ' + loc); return }
console.log('Source', 'Reading ' + loc)
this.path = loc
this.load(fs.readFileSync(this.path, 'utf8'))
}
this.run = function () {
ronin.commander.run()
}
this.load = function (data) {
ronin.commander._input.value = data
}
this.quit = function () {
if (this.hasChanges() === true) {
this.verify()
} else {
app.exit()
}
}
this.verify = function () {
let response = dialog.showMessageBox(app.win, {
type: 'question',
buttons: ['Cancel', 'Discard', 'Save'],
title: 'Confirm',
message: 'Unsaved data will be lost. Would you like to save your changes before leaving?',
icon: path.join(__dirname, '../icon.png')
})
if (response === 2) {
this.save(true)
} else if (response === 1) {
app.exit()
}
}
this.hasChanges = function () {
console.log('Source', 'Looking for changes..')
if (!this.path) {
console.log('Source', 'File is unsaved..')
if (ronin.commander._input.value.length() > 2) {
console.log('Source', `File is not empty.`)
return true
}
} else {
if (fs.existsSync(this.path)) {
console.log('Source', 'Comparing with last saved copy..')
const diff = isDifferent(fs.readFileSync(this.path, 'utf8'), this.generate())
if (diff === true) {
console.log('Source', 'File has been changed.')
return true
}
} else {
console.log('Source', 'File does not exist.')
return true
}
}
}
// Converters
this.generate = function (str = ronin.commander._input.value) {
return `${str}`
}
this.locate = function (name) {
if (!this.path) { return }
const loc = path.join(this.folder(), name)
return fs.existsSync(loc) ? loc : null
}
// Etc
this.name = function () {
return this.path ? path.basename(this.path, '.lisp') : null
}
this.folder = function () {
return this.path ? path.dirname(this.path) : null
}
this.toString = function () {
return this.path ? this.name() : 'unsaved'
}
function isDifferent (a, b) {
return a.trim() !== b.trim()
}
function clean (s) {
return s
}
}

View File

@@ -119,6 +119,8 @@ function Surface (ronin) {
if (ronin.commander._input.value === '') {
this.maximize()
}
const f = this.getFrame()
ronin.log(`resize ${f.w}x${f.h}`)
}
this.getFrame = function () {