diff --git a/src/eventPage.js b/src/eventPage.js index 036d97b..ffea30f 100644 --- a/src/eventPage.js +++ b/src/eventPage.js @@ -1,3 +1,11 @@ chrome.browserAction.onClicked.addListener(function(activeTab) { - chrome.tabs.create({ url: chrome.extension.getURL('index.html'), selected: true }); -}); \ No newline at end of file + chrome.tabs.create({ url: chrome.extension.getURL('index.html'), selected: true }); +}); + +// TODO: remove me +// Listen for messsage from tab script +/*chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { + chrome.storage.local.set(request, function() { + console.log('saved', request); + }); +});*/ \ No newline at end of file diff --git a/src/index.html b/src/index.html index e25f9d2..5c678d5 100644 --- a/src/index.html +++ b/src/index.html @@ -251,6 +251,7 @@ + @@ -266,7 +267,17 @@ + + + + + + + + + + © Web Maker    diff --git a/src/manifest.json b/src/manifest.json index 5f2a829..cd2ae13 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -10,6 +10,10 @@ "chrome_url_overrides" : { "newtab": "index.html" }, + "options_ui": { + "page": "options.html", + "chrome_style": true + }, "browser_action": { "default_title": "Web Maker", "default_icon": "icon-16.png" diff --git a/src/options.html b/src/options.html new file mode 100644 index 0000000..8fdee9e --- /dev/null +++ b/src/options.html @@ -0,0 +1,40 @@ + + +Settings - Web Maker + + + +

Settings

+
+ + +
+ +
+ + + + \ No newline at end of file diff --git a/src/options.js b/src/options.js new file mode 100644 index 0000000..77c8f65 --- /dev/null +++ b/src/options.js @@ -0,0 +1,28 @@ +// Restores preferences from chrome.storage. +function restoreOptions() { + chrome.storage.sync.get({ + preserveLastCode: true + }, function(items) { + document.forms.optionsForm.preserveLastCode.checked = items.preserveLastCode; + }); +} + +function saveOptions(e) { + var preserveLastCode = document.forms.optionsForm.preserveLastCode.checked; + + chrome.storage.sync.set({ + preserveLastCode: preserveLastCode + }, function() { + var status = document.getElementById('js-status'); + status.textContent = 'Settings saved.'; + setTimeout(function() { + status.textContent = ''; + }, 750); + }); + + e.preventDefault(); +} + +document.addEventListener('DOMContentLoaded', restoreOptions); +document.forms.optionsForm.addEventListener('submit', + saveOptions); \ No newline at end of file diff --git a/src/script.js b/src/script.js index aa12670..11715de 100644 --- a/src/script.js +++ b/src/script.js @@ -20,6 +20,7 @@ , codepenBtn = $('#js-codepen-btn') , codepenForm = $('#js-codepen-form') , saveHtmlBtn = $('#js-save-html') + , settingsBtn = $('#js-settings-btn') ; editur.cm = {}; @@ -60,21 +61,28 @@ window.saveSetting = function saveSetting(setting, value) { var obj = {}; obj[setting] = value; + // TODO: remove me + // We delegate the saving to background script because, this tab cannot do + // async saving once the tab starts unloading. + // chrome.runtime.sendMessage(obj, function(response) { + // console.log(response); + // }); chrome.storage.local.set(obj, function() { - // alert('Settings saved'); + console.log('saved', request); }); + }; + + function saveCode() { + var code = { + html: editur.cm.html.getValue(), + css: editur.cm.css.getValue(), + js: editur.cm.js.getValue() + }; + saveSetting('code', code); } window.onunload = function () { - // saveSettings(); - }; - - editur.saveContent = function (content) { - window.localStorage.editur = content; - }; - - editur.getLastSavedContent = function () { - return window.localStorage.editur || ""; + saveCode(); }; editur.setPreviewContent = function () { @@ -153,14 +161,6 @@ layoutBtn2.addEventListener('click', function () { saveSetting('layoutMode', 2); toggleLayout(2); return false; }); layoutBtn3.addEventListener('click', function () { saveSetting('layoutMode', 3); toggleLayout(3); return false; }); - chrome.storage.local.get('layoutMode', function localGetCallback(result) { - if (result.layoutMode) { - toggleLayout(result.layoutMode); - } else { - toggleLayout(1); - } - }); - helpBtn.addEventListener('click', function () { helpModal.classList.toggle('is-modal-visible'); return false; @@ -208,7 +208,34 @@ if (typeof e.target.className === 'string' && e.target.className.indexOf('modal-overlay') !== -1) { e.target.previousElementSibling.classList.toggle('is-modal-visible'); } - }) + }); + + settingsBtn.addEventListener('click', function(e) { + if (chrome.runtime.openOptionsPage) { + // New way to open options pages, if supported (Chrome 42+). + chrome.runtime.openOptionsPage(); + } else { + // Fallback. + window.open(chrome.runtime.getURL('options.html')); + } + return false; + }); + + + chrome.storage.local.get({ + layoutMode: 1, + code: '' + }, function localGetCallback(result) { + toggleLayout(result.layoutMode); + if (result.code) { + editur.cm.html.setValue(result.code.html); + editur.cm.css.setValue(result.code.css); + editur.cm.js.setValue(result.code.js); + editur.cm.html.refresh(); + editur.cm.css.refresh(); + editur.cm.js.refresh(); + } + }); } init();