diff --git a/edit.js b/edit.js index 69a9f62c..1e5d1bb3 100644 --- a/edit.js +++ b/edit.js @@ -3,7 +3,7 @@ var styleId = null; var dirty = {}; // only the actually dirty items here var editors = []; // array of all CodeMirror instances -var isSeparateWindow; // used currrently to determine if the window size/pos should be remembered +var saveSizeOnClose; // direct & reverse mapping of @-moz-document keywords and internal property names var propertyToCss = {urls: "url", urlPrefixes: "url-prefix", domains: "domain", regexps: "regexp"}; @@ -397,13 +397,26 @@ document.addEventListener("wheel", function(event) { } }); -chrome.tabs.query({currentWindow: true}, function(tabs) { - isSeparateWindow = tabs.length == 1; -}); +if (prefs.getPref("openEditInWindow")) { + chrome.tabs.query({currentWindow: true}, function(tabs) { + var windowId = tabs[0].windowId; + if (tabs.length == 1 && window.history.length == 1) { + sessionStorageHash("saveSizeOnClose").set(windowId, true); + saveSizeOnClose = true; + } else { + saveSizeOnClose = sessionStorageHash("saveSizeOnClose").value[windowId]; + } + chrome.tabs.onRemoved.addListener(function(tabId, info) { + if (info.windowId == windowId && info.isWindowClosing) { + sessionStorageHash("saveSizeOnClose").unset(windowId); + } + }); + }); +} window.onbeforeunload = function() { - if (isSeparateWindow) { - prefs.setPref('windowPosition', { + if (saveSizeOnClose) { + prefs.setPref("windowPosition", { left: screenLeft, top: screenTop, width: outerWidth, @@ -876,11 +889,9 @@ function saveComplete(style) { // Go from new style URL to edit style URL if (location.href.indexOf("id=") == -1) { - // give the code above a moment before we kill the page - setTimeout(function() {location.href = "edit.html?id=" + style.id;}, 200); - } else { - updateTitle(); + history.replaceState({}, document.title, "edit.html?id=" + style.id); } + updateTitle(); } function showMozillaFormat() { diff --git a/storage.js b/storage.js index 6e03b152..70a064de 100644 --- a/storage.js +++ b/storage.js @@ -161,6 +161,7 @@ var prefs = { // defaults "openEditInWindow": false, // new editor opens in a own browser window + "windowPosition": {}, // detached window position "show-badge": true, // display text on popup menu icon "disableAll": false, // boss key @@ -171,7 +172,7 @@ var prefs = { "manage.onlyEnabled": false, // display only enabled styles "manage.onlyEdited": false, // display only styles created locally - + "editor.options": null, // CodeMirror.defaults.* "editor.lineWrapping": true, // word wrap "editor.smartIndent": true, // "smart" indent @@ -242,3 +243,17 @@ function getCodeMirrorThemes(callback) { }); }); } + +function sessionStorageHash(name) { + var hash = { + value: {}, + set: function(k, v) { this.value[k] = v; this.updateStorage(); }, + unset: function(k) { delete this.value[k]; this.updateStorage(); }, + updateStorage: function() { + sessionStorage[this.name] = JSON.stringify(this.value); + } + }; + try { hash.value = JSON.parse(sessionStorage[name]); } catch(e) {} + Object.defineProperty(hash, "name", {value: name}); + return hash; +}