Browse Source

Minor refactoring

simov 8 years ago
parent
commit
8c4ded2108
3 changed files with 26 additions and 25 deletions
  1. 5 2
      background/background.js
  2. 17 18
      content/content.js
  3. 4 5
      content/popup.js

+ 5 - 2
background/background.js

@@ -150,8 +150,11 @@ chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
     sendMessage({message: 'reload'})
   }
   else if (req.message === 'defaults') {
-    chrome.storage.sync.set(
-      {compiler: md.defaults, content: {toc: false, scroll: true}, theme: 'github', raw: false}, sendResponse)
+    chrome.storage.sync.set({
+      compiler: md.defaults,
+      content: {toc: false, scroll: true},
+      theme: 'github', raw: false
+    }, sendResponse)
     sendMessage({message: 'reload'})
   }
   else if (req.message === 'theme') {

+ 17 - 18
content/content.js

@@ -3,12 +3,11 @@ var $ = document.querySelector.bind(document)
 
 var state = {
   theme: window['theme'] || '',
-  html: '',
-  markdown: '',
   raw: window['raw'] || false,
   content: window['content'] || {},
-  toc: '',
-  getURL: () => chrome.runtime.getURL('/themes/' + state.theme + '.css')
+  html: '',
+  markdown: '',
+  toc: ''
 }
 
 chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
@@ -79,21 +78,21 @@ function mount () {
         dom.push(m('pre#markdown', {oncreate: oncreate.markdown}, state.markdown))
         $('body').classList.remove('_toc-left', '_toc-right')
       }
-      if (state.theme && !state.raw) {
-        dom.push(m('link#theme [rel="stylesheet"] [type="text/css"]', {
-          href: state.getURL()
-        }))
-      }
-      if (state.html && !state.raw) {
-        dom.push(
-          m('#html', {oncreate: oncreate.html,
+      else {
+        if (state.theme) {
+          dom.push(m('link#theme [rel="stylesheet"] [type="text/css"]', {
+            href: chrome.runtime.getURL('/themes/' + state.theme + '.css')
+          }))
+        }
+        if (state.html) {
+          dom.push(m('#html', {oncreate: oncreate.html,
             class: /github(-dark)?/.test(state.theme) ? 'markdown-body' : 'markdown-theme'},
-            m.trust(state.html))
-        )
-        if (state.content.toc && state.toc) {
-          dom.push(m.trust(state.toc))
-          // TODO: should be configurable
-          $('body').classList.add('_toc-left')
+            m.trust(state.html)
+          ))
+          if (state.content.toc && state.toc) {
+            dom.push(m.trust(state.toc))
+            $('body').classList.add('_toc-left')
+          }
         }
       }
 

+ 4 - 5
content/popup.js

@@ -42,8 +42,7 @@ var events = {
     state.raw = !state.raw
     chrome.runtime.sendMessage({
       message: 'raw',
-      raw: state.raw,
-      theme: state.theme
+      raw: state.raw
     })
   },
 
@@ -68,7 +67,7 @@ var description = {
     pedantic: 'Don\'t fix any of the original markdown\nbugs or poor behavior',
     sanitize: 'Ignore any HTML\nthat has been input',
     smartLists: 'Use smarter list behavior\nthan the original markdown',
-    smartypants: 'Use "smart" typograhic punctuation\nfor things like quotes and dashes'
+    smartypants: 'Use "smart" typographic punctuation\nfor things like quotes and dashes'
   },
   content: {
     scroll: 'Remember scroll position',
@@ -89,6 +88,8 @@ function init (res) {
   m.redraw()
 }
 
+chrome.runtime.sendMessage({message: 'settings'}, init)
+
 function oncreate (vnode) {
   componentHandler.upgradeElements(vnode.dom)
 }
@@ -98,8 +99,6 @@ var onupdate = (tab, key) => (vnode) => {
   }
 }
 
-chrome.runtime.sendMessage({message: 'settings'}, init)
-
 m.mount(document.querySelector('body'), {
   view: (vnode) =>
     m('#popup', [