Progress toward new controls
This commit is contained in:
@@ -1,19 +1,25 @@
|
||||
'use strict'
|
||||
|
||||
function Theme (_default) {
|
||||
/* global localStorage */
|
||||
/* global FileReader */
|
||||
/* global DOMParser */
|
||||
|
||||
function Theme () {
|
||||
const themer = this
|
||||
|
||||
this.active = _default
|
||||
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.type = 'text/css'
|
||||
|
||||
this.install = function (host = document.body, callback) {
|
||||
this.install = (host = document.body, callback) => {
|
||||
host.appendChild(this.el)
|
||||
this.callback = callback
|
||||
}
|
||||
|
||||
this.start = function () {
|
||||
this.start = () => {
|
||||
this.active = this.default
|
||||
console.log('Theme', 'Starting..')
|
||||
if (isJson(localStorage.theme)) {
|
||||
const storage = JSON.parse(localStorage.theme)
|
||||
@@ -23,13 +29,13 @@ function Theme (_default) {
|
||||
return
|
||||
}
|
||||
}
|
||||
this.load(_default)
|
||||
this.load(this.active)
|
||||
}
|
||||
|
||||
this.load = function (data) {
|
||||
this.load = (data) => {
|
||||
const theme = parse(data)
|
||||
if (!validate(theme)) { console.warn('Theme', 'Not a theme', theme); return }
|
||||
console.log('Theme', `Loaded theme!`)
|
||||
if (!validate(theme)) { 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}; }`
|
||||
localStorage.setItem('theme', JSON.stringify(theme))
|
||||
this.active = theme
|
||||
@@ -38,8 +44,12 @@ function Theme (_default) {
|
||||
}
|
||||
}
|
||||
|
||||
this.reset = function () {
|
||||
this.load(_default)
|
||||
this.reset = () => {
|
||||
this.load(this.default)
|
||||
}
|
||||
|
||||
this.get = (key) => {
|
||||
return this.active[key]
|
||||
}
|
||||
|
||||
function parse (any) {
|
||||
@@ -49,18 +59,18 @@ function Theme (_default) {
|
||||
|
||||
// Drag
|
||||
|
||||
this.drag = function (e) {
|
||||
this.drag = (e) => {
|
||||
e.stopPropagation()
|
||||
e.preventDefault()
|
||||
e.dataTransfer.dropEffect = 'copy'
|
||||
}
|
||||
|
||||
this.drop = function (e) {
|
||||
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) { console.warn('Theme', 'Skipped, not a theme'); 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)
|
||||
@@ -68,10 +78,10 @@ function Theme (_default) {
|
||||
reader.readAsText(file)
|
||||
}
|
||||
|
||||
this.open = function () {
|
||||
this.open = () => {
|
||||
const fs = require('fs')
|
||||
const { dialog, app } = require('electron').remote
|
||||
let paths = dialog.showOpenDialog(app.win, { properties: ['openFile'], filters: [{ name: 'Themes', extensions: ['svg'] }] })
|
||||
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
|
||||
@@ -102,15 +112,15 @@ function Theme (_default) {
|
||||
const svg = new DOMParser().parseFromString(text, 'text/xml')
|
||||
try {
|
||||
return {
|
||||
'background': svg.getElementById('background').getAttribute('fill'),
|
||||
'f_high': svg.getElementById('f_high').getAttribute('fill'),
|
||||
'f_med': svg.getElementById('f_med').getAttribute('fill'),
|
||||
'f_low': svg.getElementById('f_low').getAttribute('fill'),
|
||||
'f_inv': svg.getElementById('f_inv').getAttribute('fill'),
|
||||
'b_high': svg.getElementById('b_high').getAttribute('fill'),
|
||||
'b_med': svg.getElementById('b_med').getAttribute('fill'),
|
||||
'b_low': svg.getElementById('b_low').getAttribute('fill'),
|
||||
'b_inv': svg.getElementById('b_inv').getAttribute('fill')
|
||||
background: svg.getElementById('background').getAttribute('fill'),
|
||||
f_high: svg.getElementById('f_high').getAttribute('fill'),
|
||||
f_med: svg.getElementById('f_med').getAttribute('fill'),
|
||||
f_low: svg.getElementById('f_low').getAttribute('fill'),
|
||||
f_inv: svg.getElementById('f_inv').getAttribute('fill'),
|
||||
b_high: svg.getElementById('b_high').getAttribute('fill'),
|
||||
b_med: svg.getElementById('b_med').getAttribute('fill'),
|
||||
b_low: svg.getElementById('b_low').getAttribute('fill'),
|
||||
b_inv: svg.getElementById('b_inv').getAttribute('fill')
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('Theme', 'Incomplete SVG Theme', err)
|
||||
|
||||
Reference in New Issue
Block a user