Code review & cosmetics

This commit is contained in:
tophf 2015-05-05 23:42:38 +03:00
parent a0c5674f6f
commit 539be4ce43
4 changed files with 15 additions and 11 deletions

View File

@ -104,9 +104,9 @@
"message": "An error has occurred using the Stylish database. Would you like to visit a web page with possible solutions?", "message": "An error has occurred using the Stylish database. Would you like to visit a web page with possible solutions?",
"description": "Prompt when a DB error is encountered" "description": "Prompt when a DB error is encountered"
}, },
"default": { "defaultTheme": {
"message": "default", "message": "default",
"description": "Default item in various lists" "description": "Default CodeMirror CSS theme option on the edit style page"
}, },
"deleteStyleLabel": { "deleteStyleLabel": {
"message": "Delete", "message": "Delete",

View File

@ -1,6 +1,10 @@
requestStyles(); requestStyles();
function requestStyles() { function requestStyles() {
// If this is a Stylish page (Edit Style or Manage Styles),
// we'll request the styles directly to minimize delay and flicker,
// unless Chrome still starts up and the background page isn't fully loaded.
// (Note: in this case the function may be invoked again from applyStyles.)
var request = {method: "getStyles", matchUrl: location.href, enabled: true, asHash: true}; var request = {method: "getStyles", matchUrl: location.href, enabled: true, asHash: true};
if (location.href.indexOf(chrome.extension.getURL("")) == 0) { if (location.href.indexOf(chrome.extension.getURL("")) == 0) {
var bg = chrome.extension.getBackgroundPage(); var bg = chrome.extension.getBackgroundPage();

16
edit.js
View File

@ -176,22 +176,22 @@ function initCodeMirror() {
// initialize global editor controls // initialize global editor controls
document.addEventListener("DOMContentLoaded", function() { document.addEventListener("DOMContentLoaded", function() {
function concatOption(html, option) { function optionsHtmlFromArray(options) {
return html + "<option>" + option + "</option>"; return options.map(function(opt) { return "<option>" + opt + "</option>"; }).join("");
} }
var bg = chrome.extension.getBackgroundPage();
var themeControl = document.getElementById("editor.theme"); var themeControl = document.getElementById("editor.theme");
var bg = chrome.extension.getBackgroundPage();
if (bg && bg.codeMirrorThemes) { if (bg && bg.codeMirrorThemes) {
themeControl.innerHTML = bg.codeMirrorThemes.reduce(concatOption, ""); 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
themeControl.innerHTML = concatOption("", theme == "default" ? t(theme) : theme); themeControl.innerHTML = optionsHtmlFromArray([theme == "default" ? t("defaultTheme") : theme]);
getCodeMirrorThemes(function(themes) { getCodeMirrorThemes(function(themes) {
themeControl.innerHTML = themes.reduce(concatOption, ""); 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 = Object.keys(CM.keyMap).sort().reduce(concatOption, ""); document.getElementById("editor.keyMap").innerHTML = optionsHtmlFromArray(Object.keys(CM.keyMap).sort());
var controlPrefs = {}; var controlPrefs = {};
document.querySelectorAll("#options *[data-option][id^='editor.']").forEach(function(option) { document.querySelectorAll("#options *[data-option][id^='editor.']").forEach(function(option) {
controlPrefs[option.id] = CM.defaults[option.dataset.option]; controlPrefs[option.id] = CM.defaults[option.dataset.option];
@ -218,7 +218,7 @@ function acmeEventListener(event) {
case "theme": case "theme":
var themeLink = document.getElementById("cm-theme"); var themeLink = document.getElementById("cm-theme");
// use non-localized "default" internally // use non-localized "default" internally
if (!value || value == "default" || value == t("default")) { if (!value || value == "default" || value == t("defaultTheme")) {
value = "default"; value = "default";
if (prefs.getPref(el.id) != value) { if (prefs.getPref(el.id) != value) {
prefs.setPref(el.id, value); prefs.setPref(el.id, value);

View File

@ -228,7 +228,7 @@ function getCodeMirrorThemes(callback) {
chrome.runtime.getPackageDirectoryEntry(function(rootDir) { chrome.runtime.getPackageDirectoryEntry(function(rootDir) {
rootDir.getDirectory("codemirror/theme", {create: false}, function(themeDir) { rootDir.getDirectory("codemirror/theme", {create: false}, function(themeDir) {
themeDir.createReader().readEntries(function(entries) { themeDir.createReader().readEntries(function(entries) {
var themes = [chrome.i18n.getMessage("default")]; var themes = [chrome.i18n.getMessage("defaultTheme")];
entries entries
.filter(function(entry) { return entry.isFile }) .filter(function(entry) { return entry.isFile })
.sort(function(a, b) { return a.name < b.name ? -1 : 1 }) .sort(function(a, b) { return a.name < b.name ? -1 : 1 })