diff --git a/desktop/sources/scripts/lib/acels.js b/desktop/sources/scripts/lib/acels.js index 17d9c9a..40320bc 100644 --- a/desktop/sources/scripts/lib/acels.js +++ b/desktop/sources/scripts/lib/acels.js @@ -1,7 +1,9 @@ 'use strict' -function Acels () { +function Acels (client) { this.all = {} + this.roles = {} + this.pipe = null this.install = (host = window) => { host.addEventListener('keydown', this.onKeyDown, false) @@ -13,6 +15,10 @@ function Acels () { this.all[accelerator] = { cat, name, downfn, upfn, accelerator } } + this.add = (cat, role) => { + this.all[':' + role] = { cat, name: role, role } + } + this.get = (accelerator) => { return this.all[accelerator] } @@ -27,29 +33,36 @@ function Acels () { } 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) { return `CmdOrCtrl+Shift+${accelerator}` } if (event.shiftKey) { return `Shift+${accelerator}` } + if (event.altKey) { + return `Alt+${accelerator}` + } if (event.ctrlKey || event.metaKey) { return `CmdOrCtrl+${accelerator}` } return accelerator } + this.pipe = (obj) => { + this.pipe = obj + } + this.onKeyDown = (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() e.preventDefault() } this.onKeyUp = (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() e.preventDefault() } @@ -60,7 +73,7 @@ function Acels () { for (const cat in cats) { text += `\n### ${cat}\n\n` for (const item of cats[cat]) { - text += `- \`${item.accelerator}\`: ${item.info}\n` + text += item.accelerator ? `- \`${item.accelerator}\`: ${item.info}\n` : '' } } return text.trim() @@ -71,7 +84,7 @@ function Acels () { let text = '' for (const cat in cats) { 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() @@ -87,12 +100,19 @@ function Acels () { label: name, submenu: [ { label: 'About', click: () => { require('electron').shell.openExternal('https://github.com/hundredrabbits/' + name) } }, - { label: 'Download Themes', click: () => { require('electron').shell.openExternal('https://github.com/hundredrabbits/Themes') } }, + { + label: 'Theme', + submenu: [ + { 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: 'Hide', accelerator: 'CmdOrCtrl+H', click: () => { app.toggleVisible() } }, { label: 'Toggle Menubar', accelerator: 'Alt+H', click: () => { app.toggleMenubar() } }, { label: 'Inspect', accelerator: 'CmdOrCtrl+.', click: () => { app.inspect() } }, - { label: 'Quit', accelerator: 'CmdOrCtrl+Q', click: () => { app.exit() } } + { role: 'quit' } ] }) diff --git a/desktop/sources/scripts/lib/source.js b/desktop/sources/scripts/lib/source.js index d058150..f651b2e 100644 --- a/desktop/sources/scripts/lib/source.js +++ b/desktop/sources/scripts/lib/source.js @@ -3,7 +3,7 @@ /* global FileReader */ /* global MouseEvent */ -function Source () { +function Source (client) { this.cache = {} this.install = () => { @@ -24,38 +24,52 @@ function Source () { input.type = 'file' input.onchange = (e) => { const file = e.target.files[0] - if (file.name.indexOf(ext) < 0) { console.warn('Source', 'File is not ' + ext); return } - this.cache = file - this.load(this.cache, callback) + if (file.name.indexOf('.' + ext) < 0) { console.warn('Source', `Skipped ${file.name}`); return } + this.read(file, callback) } 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.saveAs(name, content, type, callback) } this.saveAs = (name, ext, content, type = 'text/plain', callback) => { console.log('Source', 'Save new file..') - this.download(name, ext, content, type, callback) - } - - this.revert = () => { - + this.write(name, ext, content, type, callback) } // I/O - this.load = (file, callback) => { + this.read = (file, callback) => { const reader = new FileReader() reader.onload = (event) => { const res = event.target.result - callback(res) + if (callback) { callback(file, res) } } 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') link.setAttribute('download', `${name}-${timestamp()}.${ext}`) if (type === 'image/png' || type === 'image/jpeg') { @@ -67,6 +81,20 @@ function Source () { } 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) return (ms / 8640 / 10000).toFixed(6).substr(2, 6) } diff --git a/desktop/sources/scripts/lib/theme.js b/desktop/sources/scripts/lib/theme.js index 94061ed..8856e47 100644 --- a/desktop/sources/scripts/lib/theme.js +++ b/desktop/sources/scripts/lib/theme.js @@ -4,57 +4,83 @@ /* global FileReader */ /* global DOMParser */ -function Theme () { - 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 = {} - +function Theme (client) { this.el = document.createElement('style') 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) - this.callback = callback } this.start = () => { - this.active = this.default console.log('Theme', 'Starting..') if (isJson(localStorage.theme)) { const storage = JSON.parse(localStorage.theme) - if (validate(storage)) { - console.log('Theme', 'Loading localStorage..') + if (isValid(storage)) { + console.log('Theme', 'Loading theme in localStorage..') this.load(storage) 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) => { - const theme = parse(data) - if (!validate(theme)) { return } + const theme = this.parse(data) + if (!isValid(theme)) { console.warn('Theme', 'Invalid format'); return } 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)) this.active = theme - if (this.callback) { - this.callback() - } } this.reset = () => { this.load(this.default) } - this.get = (key) => { + this.read = (key) => { return this.active[key] } - function 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) } - return null + this.parse = (any) => { + if (isValid(any)) { return any } + if (isJson(any)) { return JSON.parse(any) } + if (isHtml(any)) { return extract(any) } } // Drag @@ -67,49 +93,25 @@ function Theme () { this.drop = (e) => { e.preventDefault() - e.stopPropagation() const file = e.dataTransfer.files[0] - if (!file || !file.name) { console.warn('Theme', 'Unnamed file.'); return } - if (file.name.indexOf('.thm') < 0 && file.name.indexOf('.svg') < 0) { return } - const reader = new FileReader() - reader.onload = function (e) { - themer.load(e.target.result) + if (file.name.indexOf('.svg') > -1) { + this.read(file, this.load) } - reader.readAsText(file) + e.stopPropagation() } - 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) - }) + this.read = (file, callback) => { + const reader = new FileReader() + reader.onload = (event) => { + callback(event.target.result) + } + reader.readAsText(file, 'UTF-8') } - window.addEventListener('dragover', this.drag) - window.addEventListener('drop', this.drop) - // Helpers - function validate (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 extract (text) { - const svg = new DOMParser().parseFromString(text, 'text/xml') + function extract (xml) { + const svg = new DOMParser().parseFromString(xml, 'text/xml') try { return { 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) { try { JSON.parse(text); return true } catch (error) { return false } } diff --git a/desktop/sources/scripts/ronin.js b/desktop/sources/scripts/ronin.js index 33ee184..49bce1e 100644 --- a/desktop/sources/scripts/ronin.js +++ b/desktop/sources/scripts/ronin.js @@ -14,9 +14,9 @@ function Ronin () { this.el = document.createElement('div') this.el.id = 'ronin' - this.acels = new Acels() - this.theme = new Theme() - this.source = new Source() + this.acels = new Acels(this) + this.theme = new Theme(this) + this.source = new Source(this) this.commander = new Commander(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', 'Clean', 'Escape', () => { this.commander.cleanup() }) this.acels.install(window) + this.acels.pipe(this) } this.start = function () { @@ -63,7 +64,8 @@ function Ronin () { this.loop() } - this.whenOpen = (res) => { + this.whenOpen = (file,res) => { + console.log(file,res) this.commander.load(res) this.commander.show() } @@ -81,10 +83,6 @@ function Ronin () { }, '')) } - this.load = function (content = this.default()) { - - } - this.bind = (event, fn) => { this.bindings[event] = fn } @@ -169,12 +167,16 @@ function Ronin () { const file = e.dataTransfer.files[0] 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') { const img = new Image() img.onload = () => { this.cache.set(file.name, img.src) + this.commander.injectPath(file.name) + this.log('Loaded '+file.name) } img.src = URL.createObjectURL(file) }