Fixed issue with path

This commit is contained in:
neauoire 2019-11-08 10:55:29 -05:00
parent 10fc80fb14
commit 8a6ba0fc07
4 changed files with 152 additions and 86 deletions

View File

@ -1,7 +1,9 @@
'use strict' 'use strict'
function Acels () { function Acels (client) {
this.all = {} this.all = {}
this.roles = {}
this.pipe = null
this.install = (host = window) => { this.install = (host = window) => {
host.addEventListener('keydown', this.onKeyDown, false) host.addEventListener('keydown', this.onKeyDown, false)
@ -13,6 +15,10 @@ function Acels () {
this.all[accelerator] = { cat, name, downfn, upfn, accelerator } this.all[accelerator] = { cat, name, downfn, upfn, accelerator }
} }
this.add = (cat, role) => {
this.all[':' + role] = { cat, name: role, role }
}
this.get = (accelerator) => { this.get = (accelerator) => {
return this.all[accelerator] return this.all[accelerator]
} }
@ -27,29 +33,36 @@ function Acels () {
} }
this.convert = (event) => { this.convert = (event) => {
const accelerator = event.key.substr(0, 1).toUpperCase() + event.key.substr(1) const accelerator = event.key === ' ' ? 'Space' : event.key.substr(0, 1).toUpperCase() + event.key.substr(1)
if ((event.ctrlKey || event.metaKey) && event.shiftKey) { if ((event.ctrlKey || event.metaKey) && event.shiftKey) {
return `CmdOrCtrl+Shift+${accelerator}` return `CmdOrCtrl+Shift+${accelerator}`
} }
if (event.shiftKey) { if (event.shiftKey) {
return `Shift+${accelerator}` return `Shift+${accelerator}`
} }
if (event.altKey) {
return `Alt+${accelerator}`
}
if (event.ctrlKey || event.metaKey) { if (event.ctrlKey || event.metaKey) {
return `CmdOrCtrl+${accelerator}` return `CmdOrCtrl+${accelerator}`
} }
return accelerator return accelerator
} }
this.pipe = (obj) => {
this.pipe = obj
}
this.onKeyDown = (e) => { this.onKeyDown = (e) => {
const target = this.get(this.convert(e)) const target = this.get(this.convert(e))
if (!target || !target.downfn) { return } if (!target || !target.downfn) { return this.pipe ? this.pipe.onKeyDown(e) : null }
target.downfn() target.downfn()
e.preventDefault() e.preventDefault()
} }
this.onKeyUp = (e) => { this.onKeyUp = (e) => {
const target = this.get(this.convert(e)) const target = this.get(this.convert(e))
if (!target || !target.upfn) { return } if (!target || !target.upfn) { return this.pipe ? this.pipe.onKeyUp(e) : null }
target.upfn() target.upfn()
e.preventDefault() e.preventDefault()
} }
@ -60,7 +73,7 @@ function Acels () {
for (const cat in cats) { for (const cat in cats) {
text += `\n### ${cat}\n\n` text += `\n### ${cat}\n\n`
for (const item of cats[cat]) { for (const item of cats[cat]) {
text += `- \`${item.accelerator}\`: ${item.info}\n` text += item.accelerator ? `- \`${item.accelerator}\`: ${item.info}\n` : ''
} }
} }
return text.trim() return text.trim()
@ -71,7 +84,7 @@ function Acels () {
let text = '' let text = ''
for (const cat in cats) { for (const cat in cats) {
for (const item of cats[cat]) { for (const item of cats[cat]) {
text += `${cat}: ${item.name} | ${item.accelerator}\n` text += item.accelerator ? `${cat}: ${item.name} | ${item.accelerator}\n` : ''
} }
} }
return text.trim() return text.trim()
@ -87,12 +100,19 @@ function Acels () {
label: name, label: name,
submenu: [ submenu: [
{ label: 'About', click: () => { require('electron').shell.openExternal('https://github.com/hundredrabbits/' + name) } }, { label: 'About', click: () => { require('electron').shell.openExternal('https://github.com/hundredrabbits/' + name) } },
{
label: 'Theme',
submenu: [
{ label: 'Download Themes', click: () => { require('electron').shell.openExternal('https://github.com/hundredrabbits/Themes') } }, { label: 'Download Themes', click: () => { require('electron').shell.openExternal('https://github.com/hundredrabbits/Themes') } },
{ label: 'Open Theme', click: () => { client.theme.open() } },
{ label: 'Reset Theme', click: () => { client.theme.reset() } }
]
},
{ label: 'Fullscreen', accelerator: 'CmdOrCtrl+Enter', click: () => { app.toggleFullscreen() } }, { label: 'Fullscreen', accelerator: 'CmdOrCtrl+Enter', click: () => { app.toggleFullscreen() } },
{ label: 'Hide', accelerator: 'CmdOrCtrl+H', click: () => { app.toggleVisible() } }, { label: 'Hide', accelerator: 'CmdOrCtrl+H', click: () => { app.toggleVisible() } },
{ label: 'Toggle Menubar', accelerator: 'Alt+H', click: () => { app.toggleMenubar() } }, { label: 'Toggle Menubar', accelerator: 'Alt+H', click: () => { app.toggleMenubar() } },
{ label: 'Inspect', accelerator: 'CmdOrCtrl+.', click: () => { app.inspect() } }, { label: 'Inspect', accelerator: 'CmdOrCtrl+.', click: () => { app.inspect() } },
{ label: 'Quit', accelerator: 'CmdOrCtrl+Q', click: () => { app.exit() } } { role: 'quit' }
] ]
}) })

View File

@ -3,7 +3,7 @@
/* global FileReader */ /* global FileReader */
/* global MouseEvent */ /* global MouseEvent */
function Source () { function Source (client) {
this.cache = {} this.cache = {}
this.install = () => { this.install = () => {
@ -24,38 +24,52 @@ function Source () {
input.type = 'file' input.type = 'file'
input.onchange = (e) => { input.onchange = (e) => {
const file = e.target.files[0] const file = e.target.files[0]
if (file.name.indexOf(ext) < 0) { console.warn('Source', 'File is not ' + ext); return } if (file.name.indexOf('.' + ext) < 0) { console.warn('Source', `Skipped ${file.name}`); return }
this.cache = file this.read(file, callback)
this.load(this.cache, callback)
} }
input.click() input.click()
} }
this.load = (ext, callback) => {
console.log('Source', 'Load files..')
const input = document.createElement('input')
input.type = 'file'
input.setAttribute('multiple', 'multiple')
input.onchange = (e) => {
for (const file of e.target.files) {
if (file.name.indexOf('.' + ext) < 0) { console.warn('Source', `Skipped ${file.name}`); return }
this.read(file, this.store)
}
}
input.click()
}
this.store = (file, content) => {
console.info('Source', 'Stored ' + file.name)
this.cache[file.name] = content
}
this.save = (name, content, type = 'text/plain', callback) => { this.save = (name, content, type = 'text/plain', callback) => {
this.saveAs(name, content, type, callback) this.saveAs(name, content, type, callback)
} }
this.saveAs = (name, ext, content, type = 'text/plain', callback) => { this.saveAs = (name, ext, content, type = 'text/plain', callback) => {
console.log('Source', 'Save new file..') console.log('Source', 'Save new file..')
this.download(name, ext, content, type, callback) this.write(name, ext, content, type, callback)
}
this.revert = () => {
} }
// I/O // I/O
this.load = (file, callback) => { this.read = (file, callback) => {
const reader = new FileReader() const reader = new FileReader()
reader.onload = (event) => { reader.onload = (event) => {
const res = event.target.result const res = event.target.result
callback(res) if (callback) { callback(file, res) }
} }
reader.readAsText(file, 'UTF-8') reader.readAsText(file, 'UTF-8')
} }
this.download = (name, ext, content, type, settings = 'charset=utf-8') => { this.write = (name, ext, content, type, settings = 'charset=utf-8') => {
const link = document.createElement('a') const link = document.createElement('a')
link.setAttribute('download', `${name}-${timestamp()}.${ext}`) link.setAttribute('download', `${name}-${timestamp()}.${ext}`)
if (type === 'image/png' || type === 'image/jpeg') { if (type === 'image/png' || type === 'image/jpeg') {
@ -67,6 +81,20 @@ function Source () {
} }
function timestamp (d = new Date(), e = new Date(d)) { function timestamp (d = new Date(), e = new Date(d)) {
return `${arvelie()}-${neralie()}`
}
function arvelie (date = new Date()) {
const start = new Date(date.getFullYear(), 0, 0)
const diff = (date - start) + ((start.getTimezoneOffset() - date.getTimezoneOffset()) * 60 * 1000)
const doty = Math.floor(diff / 86400000) - 1
const y = date.getFullYear().toString().substr(2, 2)
const m = doty === 364 || doty === 365 ? '+' : String.fromCharCode(97 + Math.floor(doty / 14)).toUpperCase()
const d = `${(doty === 365 ? 1 : doty === 366 ? 2 : (doty % 14)) + 1}`.padStart(2, '0')
return `${y}${m}${d}`
}
function neralie (d = new Date(), e = new Date(d)) {
const ms = e - d.setHours(0, 0, 0, 0) const ms = e - d.setHours(0, 0, 0, 0)
return (ms / 8640 / 10000).toFixed(6).substr(2, 6) return (ms / 8640 / 10000).toFixed(6).substr(2, 6)
} }

View File

@ -4,57 +4,83 @@
/* global FileReader */ /* global FileReader */
/* global DOMParser */ /* global DOMParser */
function Theme () { function Theme (client) {
const themer = this
this.default = { background: '#eee', f_high: '#000', f_med: '#999', f_low: '#ccc', f_inv: '#000', b_high: '#000', b_med: '#888', b_low: '#aaa', b_inv: '#ffb545' }
this.active = {}
this.el = document.createElement('style') this.el = document.createElement('style')
this.el.type = 'text/css' this.el.type = 'text/css'
this.install = (host = document.body, callback) => { this.active = {}
this.default = {
background: '#eee',
f_high: '#000',
f_med: '#999',
f_low: '#ccc',
f_inv: '#000',
b_high: '#000',
b_med: '#888',
b_low: '#aaa',
b_inv: '#ffb545'
}
this.install = (host = document.body) => {
window.addEventListener('dragover', this.drag)
window.addEventListener('drop', this.drop)
host.appendChild(this.el) host.appendChild(this.el)
this.callback = callback
} }
this.start = () => { this.start = () => {
this.active = this.default
console.log('Theme', 'Starting..') console.log('Theme', 'Starting..')
if (isJson(localStorage.theme)) { if (isJson(localStorage.theme)) {
const storage = JSON.parse(localStorage.theme) const storage = JSON.parse(localStorage.theme)
if (validate(storage)) { if (isValid(storage)) {
console.log('Theme', 'Loading localStorage..') console.log('Theme', 'Loading theme in localStorage..')
this.load(storage) this.load(storage)
return return
} }
} }
this.load(this.active) this.load(this.default)
}
this.open = () => {
console.log('Theme', 'Open theme..')
const input = document.createElement('input')
input.type = 'file'
input.onchange = (e) => {
this.read(e.target.files[0], this.load)
}
input.click()
} }
this.load = (data) => { this.load = (data) => {
const theme = parse(data) const theme = this.parse(data)
if (!validate(theme)) { return } if (!isValid(theme)) { console.warn('Theme', 'Invalid format'); return }
console.log('Theme', 'Loaded theme!') console.log('Theme', 'Loaded theme!')
this.el.innerHTML = `:root { --background: ${theme.background}; --f_high: ${theme.f_high}; --f_med: ${theme.f_med}; --f_low: ${theme.f_low}; --f_inv: ${theme.f_inv}; --b_high: ${theme.b_high}; --b_med: ${theme.b_med}; --b_low: ${theme.b_low}; --b_inv: ${theme.b_inv}; }` this.el.innerHTML = `:root {
--background: ${theme.background};
--f_high: ${theme.f_high};
--f_med: ${theme.f_med};
--f_low: ${theme.f_low};
--f_inv: ${theme.f_inv};
--b_high: ${theme.b_high};
--b_med: ${theme.b_med};
--b_low: ${theme.b_low};
--b_inv: ${theme.b_inv};
}`
localStorage.setItem('theme', JSON.stringify(theme)) localStorage.setItem('theme', JSON.stringify(theme))
this.active = theme this.active = theme
if (this.callback) {
this.callback()
}
} }
this.reset = () => { this.reset = () => {
this.load(this.default) this.load(this.default)
} }
this.get = (key) => { this.read = (key) => {
return this.active[key] return this.active[key]
} }
function parse (any) { this.parse = (any) => {
if (any && any.background) { return any } else if (any && any.data) { return any.data } else if (any && isJson(any)) { return JSON.parse(any) } else if (any && isHtml(any)) { return extract(any) } if (isValid(any)) { return any }
return null if (isJson(any)) { return JSON.parse(any) }
if (isHtml(any)) { return extract(any) }
} }
// Drag // Drag
@ -67,49 +93,25 @@ function Theme () {
this.drop = (e) => { this.drop = (e) => {
e.preventDefault() e.preventDefault()
e.stopPropagation()
const file = e.dataTransfer.files[0] const file = e.dataTransfer.files[0]
if (!file || !file.name) { console.warn('Theme', 'Unnamed file.'); return } if (file.name.indexOf('.svg') > -1) {
if (file.name.indexOf('.thm') < 0 && file.name.indexOf('.svg') < 0) { return } this.read(file, this.load)
}
e.stopPropagation()
}
this.read = (file, callback) => {
const reader = new FileReader() const reader = new FileReader()
reader.onload = function (e) { reader.onload = (event) => {
themer.load(e.target.result) callback(event.target.result)
} }
reader.readAsText(file) reader.readAsText(file, 'UTF-8')
} }
this.open = () => {
const fs = require('fs')
const { dialog, app } = require('electron').remote
const paths = dialog.showOpenDialog(app.win, { properties: ['openFile'], filters: [{ name: 'Themes', extensions: ['svg'] }] })
if (!paths) { console.log('Nothing to load'); return }
fs.readFile(paths[0], 'utf8', function (err, data) {
if (err) throw err
themer.load(data)
})
}
window.addEventListener('dragover', this.drag)
window.addEventListener('drop', this.drop)
// Helpers // Helpers
function validate (json) { function extract (xml) {
if (!json) { return false } const svg = new DOMParser().parseFromString(xml, 'text/xml')
if (!json.background) { return false }
if (!json.f_high) { return false }
if (!json.f_med) { return false }
if (!json.f_low) { return false }
if (!json.f_inv) { return false }
if (!json.b_high) { return false }
if (!json.b_med) { return false }
if (!json.b_low) { return false }
if (!json.b_inv) { return false }
return true
}
function extract (text) {
const svg = new DOMParser().parseFromString(text, 'text/xml')
try { try {
return { return {
background: svg.getElementById('background').getAttribute('fill'), background: svg.getElementById('background').getAttribute('fill'),
@ -127,6 +129,20 @@ function Theme () {
} }
} }
function isValid (json) {
if (!json) { return false }
if (!json.background) { return false }
if (!json.f_high) { return false }
if (!json.f_med) { return false }
if (!json.f_low) { return false }
if (!json.f_inv) { return false }
if (!json.b_high) { return false }
if (!json.b_med) { return false }
if (!json.b_low) { return false }
if (!json.b_inv) { return false }
return true
}
function isJson (text) { function isJson (text) {
try { JSON.parse(text); return true } catch (error) { return false } try { JSON.parse(text); return true } catch (error) { return false }
} }

View File

@ -14,9 +14,9 @@ function Ronin () {
this.el = document.createElement('div') this.el = document.createElement('div')
this.el.id = 'ronin' this.el.id = 'ronin'
this.acels = new Acels() this.acels = new Acels(this)
this.theme = new Theme() this.theme = new Theme(this)
this.source = new Source() this.source = new Source(this)
this.commander = new Commander(this) this.commander = new Commander(this)
this.surface = new Surface(this) this.surface = new Surface(this)
@ -51,6 +51,7 @@ function Ronin () {
this.acels.set('Project', 'Re-Indent', 'CmdOrCtrl+Shift+I', () => { this.commander.reindent() }) this.acels.set('Project', 'Re-Indent', 'CmdOrCtrl+Shift+I', () => { this.commander.reindent() })
this.acels.set('Project', 'Clean', 'Escape', () => { this.commander.cleanup() }) this.acels.set('Project', 'Clean', 'Escape', () => { this.commander.cleanup() })
this.acels.install(window) this.acels.install(window)
this.acels.pipe(this)
} }
this.start = function () { this.start = function () {
@ -63,7 +64,8 @@ function Ronin () {
this.loop() this.loop()
} }
this.whenOpen = (res) => { this.whenOpen = (file,res) => {
console.log(file,res)
this.commander.load(res) this.commander.load(res)
this.commander.show() this.commander.show()
} }
@ -81,10 +83,6 @@ function Ronin () {
}, '')) }, ''))
} }
this.load = function (content = this.default()) {
}
this.bind = (event, fn) => { this.bind = (event, fn) => {
this.bindings[event] = fn this.bindings[event] = fn
} }
@ -169,12 +167,16 @@ function Ronin () {
const file = e.dataTransfer.files[0] const file = e.dataTransfer.files[0]
if (file.name.indexOf('.lisp') > -1) { if (file.name.indexOf('.lisp') > -1) {
this.source.load(e.dataTransfer.files[0], this.whenOpen) this.source.read(file, this.whenOpen)
this.log('Loaded '+file.name)
} }
if (file.type === 'image/jpeg' || file.type === 'image/png') { if (file.type === 'image/jpeg' || file.type === 'image/png') {
const img = new Image() const img = new Image()
img.onload = () => { img.onload = () => {
this.cache.set(file.name, img.src) this.cache.set(file.name, img.src)
this.commander.injectPath(file.name)
this.log('Loaded '+file.name)
} }
img.src = URL.createObjectURL(file) img.src = URL.createObjectURL(file)
} }