restore editor window size when reopened via Ctrl-Shift-T

This commit is contained in:
tophf 2017-04-17 21:06:00 +03:00
parent 0d6f7e0a4b
commit 021f50015b
2 changed files with 46 additions and 35 deletions

View File

@ -1,6 +1,14 @@
<html id="stylus"> <html id="stylus">
<head> <head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<script src="dom.js"></script>
<script src="messaging.js"></script>
<script src="prefs.js"></script>
<script src="localization.js"></script>
<script src="apply.js"></script>
<script src="edit.js"></script>
<script src="codemirror/lib/codemirror.js"></script> <script src="codemirror/lib/codemirror.js"></script>
<link rel="stylesheet" href="codemirror/lib/codemirror.css"> <link rel="stylesheet" href="codemirror/lib/codemirror.css">
<script src="codemirror/mode/css/css.js"></script> <script src="codemirror/mode/css/css.js"></script>
@ -644,14 +652,8 @@
<template data-id="regexpTestPartial"> <template data-id="regexpTestPartial">
<a target="_blank" href="https://github.com/stylish-userstyles/stylish/wiki/Applying-styles-to-specific-sites#advanced-matching-with-regular-expressions"><svg class="svg-icon info"><use xlink:href="#svg-icon-help"/></svg></a> <a target="_blank" href="https://github.com/stylish-userstyles/stylish/wiki/Applying-styles-to-specific-sites#advanced-matching-with-regular-expressions"><svg class="svg-icon info"><use xlink:href="#svg-icon-help"/></svg></a>
</template> </template>
<script src="dom.js"></script>
<script src="messaging.js"></script>
<script src="prefs.js"></script>
<script src="localization.js"></script>
<script src="apply.js"></script>
<script src="edit.js"></script>
</head> </head>
<body id="stylus-edit"> <body id="stylus-edit">
<div id="header"> <div id="header">
<h1 id="heading">&nbsp;</h1> <!-- nbsp allocates the actual height which prevents page shift --> <h1 id="heading">&nbsp;</h1> <!-- nbsp allocates the actual height which prevents page shift -->

65
edit.js
View File

@ -37,6 +37,16 @@ Array.prototype.rotate = function(amount) { // negative amount == rotate left
Object.defineProperty(Array.prototype, "last", {get: function() { return this[this.length - 1]; }}); Object.defineProperty(Array.prototype, "last", {get: function() { return this[this.length - 1]; }});
// preload the theme so that CodeMirror can calculate its metrics in DOMContentLoaded->setupLivePrefs()
new MutationObserver((mutations, observer) => {
const themeElement = document.getElementById("cm-theme");
if (themeElement) {
themeElement.href = prefs.get("editor.theme") == "default" ? ""
: "codemirror/theme/" + prefs.get("editor.theme") + ".css";
observer.disconnect();
}
}).observe(document, {subtree: true, childList: true});
// reroute handling to nearest editor when keypress resolves to one of these commands // reroute handling to nearest editor when keypress resolves to one of these commands
var hotkeyRerouter = { var hotkeyRerouter = {
commands: { commands: {
@ -239,38 +249,32 @@ function initCodeMirror() {
return this.display.wrapper.parentNode; return this.display.wrapper.parentNode;
}; };
// preload the theme so that CodeMirror can calculate its metrics in DOMContentLoaded->setupLivePrefs()
var theme = prefs.get("editor.theme");
document.getElementById("cm-theme").href = theme == "default" ? "" : "codemirror/theme/" + theme + ".css";
// initialize global editor controls // initialize global editor controls
document.addEventListener("DOMContentLoaded", function() { function optionsHtmlFromArray(options) {
function optionsHtmlFromArray(options) { return options.map(function(opt) { return "<option>" + opt + "</option>"; }).join("");
return options.map(function(opt) { return "<option>" + opt + "</option>"; }).join(""); }
} var themeControl = document.getElementById("editor.theme");
var themeControl = document.getElementById("editor.theme"); if (BG && BG.codeMirrorThemes) {
if (BG && BG.codeMirrorThemes) { themeControl.innerHTML = optionsHtmlFromArray(BG.codeMirrorThemes);
themeControl.innerHTML = optionsHtmlFromArray(BG.codeMirrorThemes); } else {
} else { // Chrome is starting up and shows our edit.html, but the background page isn't loaded yet
// Chrome is starting up and shows our edit.html, but the background page isn't loaded yet const theme = prefs.get("editor.theme");
themeControl.innerHTML = optionsHtmlFromArray([theme == "default" ? t("defaultTheme") : theme]); themeControl.innerHTML = optionsHtmlFromArray([theme == "default" ? t("defaultTheme") : theme]);
BG.getCodeMirrorThemes().then(themes => { BG.getCodeMirrorThemes().then(themes => {
BG.codeMirrorThemes = themes; BG.codeMirrorThemes = themes;
themeControl.innerHTML = optionsHtmlFromArray(themes); themeControl.innerHTML = optionsHtmlFromArray(themes);
themeControl.selectedIndex = Math.max(0, themes.indexOf(theme)); themeControl.selectedIndex = Math.max(0, themes.indexOf(theme));
}); });
} }
document.getElementById("editor.keyMap").innerHTML = optionsHtmlFromArray(Object.keys(CM.keyMap).sort()); document.getElementById("editor.keyMap").innerHTML = optionsHtmlFromArray(Object.keys(CM.keyMap).sort());
document.getElementById("options").addEventListener("change", acmeEventListener, false); document.getElementById("options").addEventListener("change", acmeEventListener, false);
setupLivePrefs( setupLivePrefs(
document.querySelectorAll("#options *[data-option][id^='editor.']") document.querySelectorAll("#options *[data-option][id^='editor.']")
.map(function(option) { return option.id }) .map(function(option) { return option.id })
); );
});
hotkeyRerouter.setState(true); hotkeyRerouter.setState(true);
} }
initCodeMirror();
function acmeEventListener(event) { function acmeEventListener(event) {
var el = event.target; var el = event.target;
@ -416,6 +420,10 @@ document.addEventListener("wheel", function(event) {
chrome.tabs.query({currentWindow: true}, function(tabs) { chrome.tabs.query({currentWindow: true}, function(tabs) {
var windowId = tabs[0].windowId; var windowId = tabs[0].windowId;
if (prefs.get("openEditInWindow")) { if (prefs.get("openEditInWindow")) {
if (sessionStorage.saveSizeOnClose && 'left' in prefs.get('windowPosition', {})) {
// window was reopened via Ctrl-Shift-T etc.
chrome.windows.update(windowId, prefs.get('windowPosition'));
}
if (tabs.length == 1 && window.history.length == 1) { if (tabs.length == 1 && window.history.length == 1) {
chrome.windows.getAll(function(windows) { chrome.windows.getAll(function(windows) {
if (windows.length > 1) { if (windows.length > 1) {
@ -1111,6 +1119,7 @@ function beautify(event) {
document.addEventListener("DOMContentLoaded", init); document.addEventListener("DOMContentLoaded", init);
function init() { function init() {
initCodeMirror();
var params = getParams(); var params = getParams();
if (!params.id) { // match should be 2 - one for the whole thing, one for the parentheses if (!params.id) { // match should be 2 - one for the whole thing, one for the parentheses
// This is an add // This is an add