From 9227f976a93a7b2157b1ae9f5d1229c1e6a1c2df Mon Sep 17 00:00:00 2001
From: neauoire <aliceffekt@gmail.com>
Date: Wed, 6 Nov 2019 20:32:09 -0500
Subject: [PATCH] *

---
 desktop/main.js                       |   4 +
 desktop/sources/scripts/dotgrid.js    |  18 ++---
 desktop/sources/scripts/lib/source.js |  49 ++++++++---
 desktop/sources/scripts/lib/theme.js  | 112 +++++++++++++-------------
 4 files changed, 106 insertions(+), 77 deletions(-)

diff --git a/desktop/main.js b/desktop/main.js
index f7d60c4..5fdee91 100644
--- a/desktop/main.js
+++ b/desktop/main.js
@@ -60,6 +60,10 @@ app.toggleFullscreen = function () {
   app.win.setFullScreen(!app.win.isFullScreen())
 }
 
+app.toggleMenubar = function () {
+  app.win.setMenuBarVisibility(!app.win.isMenuBarVisible())
+}
+
 app.toggleVisible = function () {
   if (process.platform === 'win32') {
     if (!app.win.isMinimized()) { app.win.minimize() } else { app.win.restore() }
diff --git a/desktop/sources/scripts/dotgrid.js b/desktop/sources/scripts/dotgrid.js
index 01a13db..880f9db 100644
--- a/desktop/sources/scripts/dotgrid.js
+++ b/desktop/sources/scripts/dotgrid.js
@@ -44,10 +44,10 @@ function Dotgrid () {
     window.addEventListener('drop', this.onDrop)
 
     this.acels.set('File', 'New', 'CmdOrCtrl+N', () => { this.source.new() })
-    this.acels.set('File', 'Save', 'CmdOrCtrl+S', () => { this.source.download('dotgrid','grid' , this.tool.export(), 'text/plain') })
-    this.acels.set('File', 'Export Vector', 'CmdOrCtrl+E', () => { this.source.download('dotgrid','svg', this.manager.toString(), 'image/svg+xml') })
-    this.acels.set('File', 'Export Image', 'CmdOrCtrl+Shift+E', () => { this.manager.toPNG(this.tool.settings.size, (dataUrl) => { this.source.download('dotgrid','png', dataUrl, 'image/png') }) })
     this.acels.set('File', 'Open', 'CmdOrCtrl+O', () => { this.source.open('grid', this.whenOpen) })
+    this.acels.set('File', 'Save', 'CmdOrCtrl+S', () => { this.source.write('dotgrid','grid' , this.tool.export(), 'text/plain') })
+    this.acels.set('File', 'Export Vector', 'CmdOrCtrl+E', () => { this.source.write('dotgrid','svg', this.manager.toString(), 'image/svg+xml') })
+    this.acels.set('File', 'Export Image', 'CmdOrCtrl+Shift+E', () => { this.manager.toPNG(this.tool.settings.size, (dataUrl) => { this.source.download('dotgrid','png', dataUrl, 'image/png') }) })    
     this.acels.set('File', 'Revert', 'CmdOrCtrl+W', () => { this.source.revert() })
     this.acels.set('History', 'Undo', 'CmdOrCtrl+Z', () => { this.history.undo() })
     this.acels.set('History', 'Redo', 'CmdOrCtrl+Shift+Z', () => { this.history.redo() })
@@ -121,7 +121,8 @@ function Dotgrid () {
     this.update()
   }
 
-  this.whenOpen = (data) => {
+  this.whenOpen = (file,data) => {
+    console.log(data)
     this.tool.replace(JSON.parse(data))
   }
 
@@ -130,15 +131,6 @@ function Dotgrid () {
   this.fitSize = () => {
     if (this.requireResize() === false) { return }
     console.log('Dotgrid', `Will resize to: ${printSize(this.getRequiredSize())}`)
-    this.setWindowSize(this.getRequiredSize())
-    this.update()
-  }
-
-  this.setWindowSize = function (size) {
-    console.log('Dotgrid', `Resizing to ${printSize(size)}`)
-    const win = require('electron').remote.getCurrentWindow()
-    win.setSize(size.width, size.height, false)
-    document.title = `Dotgrid — ${size.width}x${size.height}`
     this.update()
   }
 
diff --git a/desktop/sources/scripts/lib/source.js b/desktop/sources/scripts/lib/source.js
index c69f0f8..edf0738 100644
--- a/desktop/sources/scripts/lib/source.js
+++ b/desktop/sources/scripts/lib/source.js
@@ -24,20 +24,38 @@ 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) => {
+  this.saveAs = (name, ext, content, type = 'text/plain', callback) => {
     console.log('Source', 'Save new file..')
-    this.download(name, ext,content, type, callback)
+    this.write(name, ext, content, type, callback)
   }
 
   this.revert = () => {
@@ -46,16 +64,16 @@ function Source () {
 
   // 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') {
@@ -66,8 +84,21 @@ function Source () {
     link.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }))
   }
 
-
   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..5c1d536 100644
--- a/desktop/sources/scripts/lib/theme.js
+++ b/desktop/sources/scripts/lib/theme.js
@@ -5,56 +5,72 @@
 /* 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 = {}
-
   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)) {
+      if (isValid(storage)) {
         console.log('Theme', 'Loading localStorage..')
         this.load(storage)
         return
       }
     }
-    this.load(this.active)
+    this.load(this.default)
   }
 
   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 +83,21 @@ 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 }
+    if (!file || !file.name) { console.warn('Theme', 'Could not read file.'); return }
+    if (file.name.indexOf('.svg') < 0) { console.warn('Theme', 'Not a SVG file.'); return }
     const reader = new FileReader()
-    reader.onload = function (e) {
-      themer.load(e.target.result)
+    reader.onload = (e) => {
+      this.load(e.target.result)
     }
     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)
-    })
-  }
-
-  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 +115,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 }
   }