2017-03-15 12:41:39 +00:00
|
|
|
/* globals stringAsRegExp */
|
2015-03-14 11:51:41 +00:00
|
|
|
"use strict";
|
|
|
|
|
2015-01-30 17:05:06 +00:00
|
|
|
var styleId = null;
|
2015-03-21 13:24:45 +00:00
|
|
|
var dirty = {}; // only the actually dirty items here
|
|
|
|
var editors = []; // array of all CodeMirror instances
|
2015-05-27 19:02:36 +00:00
|
|
|
var saveSizeOnClose;
|
2015-06-10 12:55:32 +00:00
|
|
|
var useHistoryBack; // use browser history back when "back to manage" is clicked
|
2015-01-30 17:05:06 +00:00
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
// direct & reverse mapping of @-moz-document keywords and internal property names
|
|
|
|
var propertyToCss = {urls: "url", urlPrefixes: "url-prefix", domains: "domain", regexps: "regexp"};
|
|
|
|
var CssToProperty = {"url": "urls", "url-prefix": "urlPrefixes", "domain": "domains", "regexp": "regexps"};
|
|
|
|
|
|
|
|
// make querySelectorAll enumeration code readable
|
2015-10-05 11:12:24 +00:00
|
|
|
["forEach", "some", "indexOf", "map"].forEach(function(method) {
|
2015-03-21 13:24:45 +00:00
|
|
|
NodeList.prototype[method]= Array.prototype[method];
|
|
|
|
});
|
|
|
|
|
2015-07-26 02:24:18 +00:00
|
|
|
// Chrome pre-34
|
|
|
|
Element.prototype.matches = Element.prototype.matches || Element.prototype.webkitMatchesSelector;
|
|
|
|
|
2015-07-16 19:31:03 +00:00
|
|
|
// Chrome pre-41 polyfill
|
|
|
|
Element.prototype.closest = Element.prototype.closest || function(selector) {
|
|
|
|
for (var e = this; e && !e.matches(selector); e = e.parentElement) {}
|
|
|
|
return e;
|
|
|
|
};
|
|
|
|
|
2015-07-11 14:35:04 +00:00
|
|
|
Array.prototype.rotate = function(amount) { // negative amount == rotate left
|
|
|
|
var r = this.slice(-amount, this.length);
|
|
|
|
Array.prototype.push.apply(r, this.slice(0, this.length - r.length));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-07-13 17:44:46 +00:00
|
|
|
Object.defineProperty(Array.prototype, "last", {get: function() { return this[this.length - 1]; }});
|
|
|
|
|
2015-04-23 01:53:30 +00:00
|
|
|
// reroute handling to nearest editor when keypress resolves to one of these commands
|
2015-08-01 15:16:40 +00:00
|
|
|
var hotkeyRerouter = {
|
|
|
|
commands: {
|
|
|
|
save: true, jumpToLine: true, nextEditor: true, prevEditor: true,
|
|
|
|
find: true, findNext: true, findPrev: true, replace: true, replaceAll: true
|
|
|
|
},
|
|
|
|
setState: function(enable) {
|
|
|
|
setTimeout(function() {
|
|
|
|
document[(enable ? "add" : "remove") + "EventListener"]("keydown", hotkeyRerouter.eventHandler);
|
|
|
|
}, 0);
|
|
|
|
},
|
|
|
|
eventHandler: function(event) {
|
|
|
|
var keyName = CodeMirror.keyName(event);
|
|
|
|
if ("handled" == CodeMirror.lookupKey(keyName, CodeMirror.getOption("keyMap"), handleCommand)
|
|
|
|
|| "handled" == CodeMirror.lookupKey(keyName, CodeMirror.defaults.extraKeys, handleCommand)) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
function handleCommand(command) {
|
|
|
|
if (hotkeyRerouter.commands[command] === true) {
|
|
|
|
CodeMirror.commands[command](getEditorInSight(event.target));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-23 01:53:30 +00:00
|
|
|
};
|
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
function onChange(event) {
|
2015-03-14 11:51:41 +00:00
|
|
|
var node = event.target;
|
2015-03-21 13:24:45 +00:00
|
|
|
if ("savedValue" in node) {
|
|
|
|
var currentValue = "checkbox" === node.type ? node.checked : node.value;
|
|
|
|
setCleanItem(node, node.savedValue === currentValue);
|
|
|
|
} else {
|
|
|
|
// the manually added section's applies-to is dirty only when the value is non-empty
|
|
|
|
setCleanItem(node, node.localName != "input" || !node.value.trim());
|
|
|
|
delete node.savedValue; // only valid when actually saved
|
2015-03-14 11:51:41 +00:00
|
|
|
}
|
2015-03-21 13:24:45 +00:00
|
|
|
updateTitle();
|
|
|
|
}
|
2015-03-14 11:51:41 +00:00
|
|
|
|
2015-03-15 19:41:36 +00:00
|
|
|
// Set .dirty on stylesheet contributors that have changed
|
2015-03-21 13:24:45 +00:00
|
|
|
function setDirtyClass(node, isDirty) {
|
|
|
|
node.classList.toggle("dirty", isDirty);
|
2015-03-15 19:41:36 +00:00
|
|
|
}
|
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
function setCleanItem(node, isClean) {
|
|
|
|
if (!node.id) {
|
|
|
|
node.id = Date.now().toString(32).substr(-6);
|
|
|
|
}
|
2015-03-15 19:41:36 +00:00
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
if (isClean) {
|
|
|
|
delete dirty[node.id];
|
2016-02-02 06:18:23 +00:00
|
|
|
// code sections have .CodeMirror property
|
|
|
|
if (node.CodeMirror) {
|
|
|
|
node.savedValue = node.CodeMirror.changeGeneration();
|
2015-04-08 18:24:26 +00:00
|
|
|
} else {
|
|
|
|
node.savedValue = "checkbox" === node.type ? node.checked : node.value;
|
|
|
|
}
|
2015-03-21 13:24:45 +00:00
|
|
|
} else {
|
|
|
|
dirty[node.id] = true;
|
|
|
|
}
|
2015-03-16 20:47:56 +00:00
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
setDirtyClass(node, !isClean);
|
|
|
|
}
|
2015-03-16 20:47:56 +00:00
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
function isCleanGlobal() {
|
|
|
|
var clean = Object.keys(dirty).length == 0;
|
|
|
|
setDirtyClass(document.body, !clean);
|
2017-01-30 19:29:12 +00:00
|
|
|
var saveBtn = document.getElementById("save-button")
|
|
|
|
if (clean){
|
|
|
|
//saveBtn.removeAttribute('disabled');
|
|
|
|
}else{
|
|
|
|
//saveBtn.setAttribute('disabled', true);
|
|
|
|
}
|
2015-03-16 20:47:56 +00:00
|
|
|
return clean;
|
2015-03-15 19:41:36 +00:00
|
|
|
}
|
2015-03-14 11:51:41 +00:00
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
function setCleanGlobal() {
|
|
|
|
document.querySelectorAll("#header, #sections > div").forEach(setCleanSection);
|
|
|
|
dirty = {}; // forget the dirty applies-to ids from a deleted section after the style was saved
|
|
|
|
}
|
2015-03-15 19:41:36 +00:00
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
function setCleanSection(section) {
|
|
|
|
section.querySelectorAll(".style-contributor").forEach(function(node) { setCleanItem(node, true) });
|
2015-03-15 19:41:36 +00:00
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
// #header section has no codemirror
|
2016-02-02 06:18:23 +00:00
|
|
|
var cm = section.CodeMirror;
|
2015-04-08 18:24:26 +00:00
|
|
|
if (cm) {
|
2015-03-21 13:24:45 +00:00
|
|
|
section.savedValue = cm.changeGeneration();
|
|
|
|
indicateCodeChange(cm);
|
|
|
|
}
|
2015-03-15 19:41:36 +00:00
|
|
|
}
|
2015-03-14 11:51:41 +00:00
|
|
|
|
2015-03-08 06:21:43 +00:00
|
|
|
function initCodeMirror() {
|
|
|
|
var CM = CodeMirror;
|
2015-04-23 01:53:30 +00:00
|
|
|
var isWindowsOS = navigator.appVersion.indexOf("Windows") > 0;
|
|
|
|
|
2015-03-08 06:21:43 +00:00
|
|
|
// default option values
|
2017-03-15 11:58:59 +00:00
|
|
|
Object.assign(CM.defaults, {
|
2015-02-09 04:02:08 +00:00
|
|
|
mode: 'css',
|
|
|
|
lineNumbers: true,
|
|
|
|
lineWrapping: true,
|
2015-02-25 04:05:00 +00:00
|
|
|
foldGutter: true,
|
|
|
|
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter", "CodeMirror-lint-markers"],
|
|
|
|
matchBrackets: true,
|
2017-03-19 03:17:23 +00:00
|
|
|
highlightSelectionMatches: {showToken: /[#.\-\w]/, annotateScrollbar: true},
|
2015-10-05 11:27:17 +00:00
|
|
|
lint: {getAnnotations: CodeMirror.lint.css, delay: prefs.get("editor.lintDelay")},
|
|
|
|
lintReportDelay: prefs.get("editor.lintReportDelay"),
|
2015-05-27 18:01:05 +00:00
|
|
|
styleActiveLine: true,
|
2015-04-11 10:28:25 +00:00
|
|
|
theme: "default",
|
2015-10-05 11:27:17 +00:00
|
|
|
keyMap: prefs.get("editor.keyMap"),
|
2015-04-23 01:53:30 +00:00
|
|
|
extraKeys: { // independent of current keyMap
|
|
|
|
"Alt-PageDown": "nextEditor",
|
|
|
|
"Alt-PageUp": "prevEditor"
|
|
|
|
}
|
2015-10-05 11:27:17 +00:00
|
|
|
}, prefs.get("editor.options"));
|
2015-03-08 06:21:43 +00:00
|
|
|
|
|
|
|
// additional commands
|
2015-04-23 01:53:30 +00:00
|
|
|
CM.commands.jumpToLine = jumpToLine;
|
|
|
|
CM.commands.nextEditor = function(cm) { nextPrevEditor(cm, 1) };
|
|
|
|
CM.commands.prevEditor = function(cm) { nextPrevEditor(cm, -1) };
|
|
|
|
CM.commands.save = save;
|
2015-05-27 18:02:23 +00:00
|
|
|
CM.commands.blockComment = function(cm) {
|
|
|
|
cm.blockComment(cm.getCursor("from"), cm.getCursor("to"), {fullLines: false});
|
|
|
|
};
|
2015-04-23 01:53:30 +00:00
|
|
|
|
|
|
|
// "basic" keymap only has basic keys by design, so we skip it
|
|
|
|
|
2015-05-13 20:49:18 +00:00
|
|
|
var extraKeysCommands = {};
|
2015-10-05 11:12:24 +00:00
|
|
|
Object.keys(CM.defaults.extraKeys).forEach(function(key) {
|
|
|
|
extraKeysCommands[CM.defaults.extraKeys[key]] = true;
|
|
|
|
});
|
2015-05-13 20:49:18 +00:00
|
|
|
if (!extraKeysCommands.jumpToLine) {
|
|
|
|
CM.keyMap.sublime["Ctrl-G"] = "jumpToLine";
|
|
|
|
CM.keyMap.emacsy["Ctrl-G"] = "jumpToLine";
|
|
|
|
CM.keyMap.pcDefault["Ctrl-J"] = "jumpToLine";
|
|
|
|
CM.keyMap.macDefault["Cmd-J"] = "jumpToLine";
|
|
|
|
}
|
|
|
|
if (!extraKeysCommands.autocomplete) {
|
|
|
|
CM.keyMap.pcDefault["Ctrl-Space"] = "autocomplete"; // will be used by "sublime" on PC via fallthrough
|
|
|
|
CM.keyMap.macDefault["Alt-Space"] = "autocomplete"; // OSX uses Ctrl-Space and Cmd-Space for something else
|
|
|
|
CM.keyMap.emacsy["Alt-/"] = "autocomplete"; // copied from "emacs" keymap
|
|
|
|
// "vim" and "emacs" define their own autocomplete hotkeys
|
|
|
|
}
|
2015-05-27 18:02:23 +00:00
|
|
|
if (!extraKeysCommands.blockComment) {
|
|
|
|
CM.keyMap.sublime["Shift-Ctrl-/"] = "blockComment";
|
|
|
|
}
|
2015-04-23 01:53:30 +00:00
|
|
|
|
|
|
|
if (isWindowsOS) {
|
|
|
|
// "pcDefault" keymap on Windows should have F3/Shift-F3
|
2015-05-13 20:49:18 +00:00
|
|
|
if (!extraKeysCommands.findNext) {
|
|
|
|
CM.keyMap.pcDefault["F3"] = "findNext";
|
|
|
|
}
|
|
|
|
if (!extraKeysCommands.findPrev) {
|
|
|
|
CM.keyMap.pcDefault["Shift-F3"] = "findPrev";
|
|
|
|
}
|
2015-04-23 01:53:30 +00:00
|
|
|
|
|
|
|
// try to remap non-interceptable Ctrl-(Shift-)N/T/W hotkeys
|
|
|
|
["N", "T", "W"].forEach(function(char) {
|
|
|
|
[{from: "Ctrl-", to: ["Alt-", "Ctrl-Alt-"]},
|
|
|
|
{from: "Shift-Ctrl-", to: ["Ctrl-Alt-", "Shift-Ctrl-Alt-"]} // Note: modifier order in CM is S-C-A
|
|
|
|
].forEach(function(remap) {
|
|
|
|
var oldKey = remap.from + char;
|
|
|
|
Object.keys(CM.keyMap).forEach(function(keyMapName) {
|
|
|
|
var keyMap = CM.keyMap[keyMapName];
|
|
|
|
var command = keyMap[oldKey];
|
|
|
|
if (!command) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
remap.to.some(function(newMod) {
|
|
|
|
var newKey = newMod + char;
|
|
|
|
if (!(newKey in keyMap)) {
|
|
|
|
delete keyMap[oldKey];
|
|
|
|
keyMap[newKey] = command;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2015-03-14 03:47:43 +00:00
|
|
|
|
2015-03-08 06:21:43 +00:00
|
|
|
// user option values
|
|
|
|
CM.getOption = function (o) {
|
|
|
|
return CodeMirror.defaults[o];
|
2017-01-30 19:29:12 +00:00
|
|
|
};
|
2015-03-08 06:21:43 +00:00
|
|
|
CM.setOption = function (o, v) {
|
|
|
|
CodeMirror.defaults[o] = v;
|
|
|
|
editors.forEach(function(editor) {
|
|
|
|
editor.setOption(o, v);
|
|
|
|
});
|
2017-01-30 19:29:12 +00:00
|
|
|
};
|
2015-03-08 06:21:43 +00:00
|
|
|
|
2016-02-02 06:18:23 +00:00
|
|
|
CM.prototype.getSection = function() {
|
|
|
|
return this.display.wrapper.parentNode;
|
2017-01-30 19:29:12 +00:00
|
|
|
};
|
2016-02-02 06:18:23 +00:00
|
|
|
|
2015-10-05 11:27:17 +00:00
|
|
|
// preload the theme so that CodeMirror can calculate its metrics in DOMContentLoaded->setupLivePrefs()
|
|
|
|
var theme = prefs.get("editor.theme");
|
2015-05-05 15:21:45 +00:00
|
|
|
document.getElementById("cm-theme").href = theme == "default" ? "" : "codemirror/theme/" + theme + ".css";
|
2015-03-08 06:21:43 +00:00
|
|
|
|
2015-04-11 10:28:25 +00:00
|
|
|
// initialize global editor controls
|
|
|
|
document.addEventListener("DOMContentLoaded", function() {
|
2015-05-05 20:42:38 +00:00
|
|
|
function optionsHtmlFromArray(options) {
|
|
|
|
return options.map(function(opt) { return "<option>" + opt + "</option>"; }).join("");
|
2015-04-11 10:28:25 +00:00
|
|
|
}
|
2015-05-05 15:21:45 +00:00
|
|
|
var themeControl = document.getElementById("editor.theme");
|
2015-05-05 20:42:38 +00:00
|
|
|
var bg = chrome.extension.getBackgroundPage();
|
2015-05-05 15:21:45 +00:00
|
|
|
if (bg && bg.codeMirrorThemes) {
|
2015-05-05 20:42:38 +00:00
|
|
|
themeControl.innerHTML = optionsHtmlFromArray(bg.codeMirrorThemes);
|
2015-05-05 15:21:45 +00:00
|
|
|
} else {
|
|
|
|
// Chrome is starting up and shows our edit.html, but the background page isn't loaded yet
|
2015-05-05 20:42:38 +00:00
|
|
|
themeControl.innerHTML = optionsHtmlFromArray([theme == "default" ? t("defaultTheme") : theme]);
|
2015-05-05 15:21:45 +00:00
|
|
|
getCodeMirrorThemes(function(themes) {
|
2015-05-05 20:42:38 +00:00
|
|
|
themeControl.innerHTML = optionsHtmlFromArray(themes);
|
2015-05-05 15:21:45 +00:00
|
|
|
themeControl.selectedIndex = Math.max(0, themes.indexOf(theme));
|
|
|
|
});
|
|
|
|
}
|
2015-05-05 20:42:38 +00:00
|
|
|
document.getElementById("editor.keyMap").innerHTML = optionsHtmlFromArray(Object.keys(CM.keyMap).sort());
|
2015-04-11 10:28:25 +00:00
|
|
|
document.getElementById("options").addEventListener("change", acmeEventListener, false);
|
2015-10-05 11:27:17 +00:00
|
|
|
setupLivePrefs(
|
2015-10-05 11:12:24 +00:00
|
|
|
document.querySelectorAll("#options *[data-option][id^='editor.']")
|
|
|
|
.map(function(option) { return option.id })
|
|
|
|
);
|
2015-03-08 06:21:43 +00:00
|
|
|
});
|
2015-08-01 15:16:40 +00:00
|
|
|
|
|
|
|
hotkeyRerouter.setState(true);
|
2015-03-08 06:21:43 +00:00
|
|
|
}
|
|
|
|
initCodeMirror();
|
|
|
|
|
|
|
|
function acmeEventListener(event) {
|
2015-04-11 10:28:25 +00:00
|
|
|
var el = event.target;
|
|
|
|
var option = el.dataset.option;
|
|
|
|
//console.log("acmeEventListener heard %s on %s", event.type, el.id);
|
|
|
|
if (!option) {
|
|
|
|
console.error("acmeEventListener: no 'cm_option' %O", el);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var value = el.type == "checkbox" ? el.checked : el.value;
|
|
|
|
switch (option) {
|
|
|
|
case "tabSize":
|
|
|
|
CodeMirror.setOption("indentUnit", value);
|
|
|
|
break;
|
|
|
|
case "theme":
|
|
|
|
var themeLink = document.getElementById("cm-theme");
|
|
|
|
// use non-localized "default" internally
|
2015-05-05 20:42:38 +00:00
|
|
|
if (!value || value == "default" || value == t("defaultTheme")) {
|
2015-04-11 10:28:25 +00:00
|
|
|
value = "default";
|
2015-10-05 11:27:17 +00:00
|
|
|
if (prefs.get(el.id) != value) {
|
|
|
|
prefs.set(el.id, value);
|
2015-04-11 10:28:25 +00:00
|
|
|
}
|
|
|
|
themeLink.href = "";
|
|
|
|
el.selectedIndex = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
var url = chrome.extension.getURL("codemirror/theme/" + value + ".css");
|
|
|
|
if (themeLink.href == url) { // preloaded in initCodeMirror()
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// avoid flicker: wait for the second stylesheet to load, then apply the theme
|
|
|
|
document.head.insertAdjacentHTML("beforeend",
|
|
|
|
'<link id="cm-theme2" rel="stylesheet" href="' + url + '">');
|
|
|
|
(function() {
|
|
|
|
setTimeout(function() {
|
|
|
|
CodeMirror.setOption(option, value);
|
|
|
|
themeLink.remove();
|
|
|
|
document.getElementById("cm-theme2").id = "cm-theme";
|
|
|
|
}, 100);
|
|
|
|
})();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CodeMirror.setOption(option, value);
|
2015-03-08 06:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// replace given textarea with the CodeMirror editor
|
2015-03-21 13:24:45 +00:00
|
|
|
function setupCodeMirror(textarea, index) {
|
2017-03-20 09:30:19 +00:00
|
|
|
var cm = CodeMirror.fromTextArea(textarea, {lint: null});
|
2015-04-23 01:53:30 +00:00
|
|
|
|
2015-03-03 00:58:04 +00:00
|
|
|
cm.on("change", indicateCodeChange);
|
2015-08-01 15:16:40 +00:00
|
|
|
cm.on("blur", function(cm) {
|
|
|
|
editors.lastActive = cm;
|
|
|
|
hotkeyRerouter.setState(true);
|
2015-10-27 22:37:19 +00:00
|
|
|
setTimeout(function() {
|
|
|
|
var cm = editors.lastActive;
|
|
|
|
var childFocused = cm.display.wrapper.contains(document.activeElement);
|
|
|
|
cm.display.wrapper.classList.toggle("CodeMirror-active", childFocused);
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
cm.on("focus", function() {
|
|
|
|
hotkeyRerouter.setState(false);
|
|
|
|
cm.display.wrapper.classList.add("CodeMirror-active");
|
2015-08-01 15:16:40 +00:00
|
|
|
});
|
2015-03-10 21:48:42 +00:00
|
|
|
|
2015-03-25 13:08:28 +00:00
|
|
|
var resizeGrip = cm.display.wrapper.appendChild(document.createElement("div"));
|
|
|
|
resizeGrip.className = "resize-grip";
|
|
|
|
resizeGrip.addEventListener("mousedown", function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var cm = e.target.parentNode.CodeMirror;
|
|
|
|
var minHeight = cm.defaultTextHeight()
|
|
|
|
+ cm.display.lineDiv.offsetParent.offsetTop /* .CodeMirror-lines padding */
|
|
|
|
+ cm.display.wrapper.offsetHeight - cm.display.wrapper.scrollHeight /* borders */;
|
|
|
|
function resize(e) {
|
|
|
|
cm.setSize(null, Math.max(minHeight, cm.display.wrapper.scrollHeight + e.movementY));
|
|
|
|
}
|
|
|
|
document.addEventListener("mousemove", resize);
|
|
|
|
document.addEventListener("mouseup", function resizeStop() {
|
|
|
|
document.removeEventListener("mouseup", resizeStop);
|
|
|
|
document.removeEventListener("mousemove", resize);
|
|
|
|
});
|
|
|
|
});
|
2015-03-26 08:15:40 +00:00
|
|
|
// resizeGrip has enough space when scrollbars.horiz is visible
|
|
|
|
if (cm.display.scrollbars.horiz.style.display != "") {
|
|
|
|
cm.display.scrollbars.vert.style.marginBottom = "0";
|
|
|
|
}
|
|
|
|
// resizeGrip space adjustment in case a long line was entered/deleted by a user
|
|
|
|
new MutationObserver(function(mutations) {
|
|
|
|
var hScrollbar = mutations[0].target;
|
|
|
|
var hScrollbarVisible = hScrollbar.style.display != "";
|
|
|
|
var vScrollbar = hScrollbar.parentNode.CodeMirror.display.scrollbars.vert;
|
|
|
|
vScrollbar.style.marginBottom = hScrollbarVisible ? "0" : "";
|
|
|
|
}).observe(cm.display.scrollbars.horiz, {
|
|
|
|
attributes: true,
|
|
|
|
attributeFilter: ["style"]
|
|
|
|
});
|
2015-03-25 13:08:28 +00:00
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
editors.splice(index || editors.length, 0, cm);
|
|
|
|
return cm;
|
|
|
|
}
|
|
|
|
|
|
|
|
function indicateCodeChange(cm) {
|
2016-02-02 06:18:23 +00:00
|
|
|
var section = cm.getSection();
|
2015-03-21 13:24:45 +00:00
|
|
|
setCleanItem(section, cm.isClean(section.savedValue));
|
|
|
|
updateTitle();
|
2015-05-22 12:28:05 +00:00
|
|
|
updateLintReport(cm);
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
2015-07-16 19:31:03 +00:00
|
|
|
function getSectionForChild(e) {
|
|
|
|
return e.closest("#sections > div");
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSections() {
|
|
|
|
return document.querySelectorAll("#sections > div");
|
2015-04-08 18:24:26 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 11:43:43 +00:00
|
|
|
// remind Chrome to repaint a previously invisible editor box by toggling any element's transform
|
|
|
|
// this bug is present in some versions of Chrome (v37-40 or something)
|
|
|
|
document.addEventListener("scroll", function(event) {
|
|
|
|
var style = document.getElementById("name").style;
|
|
|
|
style.webkitTransform = style.webkitTransform ? "" : "scale(1)";
|
|
|
|
});
|
|
|
|
|
2015-04-26 20:03:39 +00:00
|
|
|
// Shift-Ctrl-Wheel scrolls entire page even when mouse is over a code editor
|
|
|
|
document.addEventListener("wheel", function(event) {
|
|
|
|
if (event.shiftKey && event.ctrlKey && !event.altKey && !event.metaKey) {
|
|
|
|
// Chrome scrolls horizontally when Shift is pressed but on some PCs this might be different
|
|
|
|
window.scrollBy(0, event.deltaX || event.deltaY);
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-06-10 12:55:32 +00:00
|
|
|
chrome.tabs.query({currentWindow: true}, function(tabs) {
|
|
|
|
var windowId = tabs[0].windowId;
|
2015-10-05 11:27:17 +00:00
|
|
|
if (prefs.get("openEditInWindow")) {
|
2015-05-27 19:02:36 +00:00
|
|
|
if (tabs.length == 1 && window.history.length == 1) {
|
2015-06-15 15:56:48 +00:00
|
|
|
chrome.windows.getAll(function(windows) {
|
|
|
|
if (windows.length > 1) {
|
|
|
|
sessionStorageHash("saveSizeOnClose").set(windowId, true);
|
|
|
|
saveSizeOnClose = true;
|
|
|
|
}
|
|
|
|
});
|
2015-05-27 19:02:36 +00:00
|
|
|
} else {
|
|
|
|
saveSizeOnClose = sessionStorageHash("saveSizeOnClose").value[windowId];
|
|
|
|
}
|
2015-06-10 12:55:32 +00:00
|
|
|
}
|
|
|
|
chrome.tabs.onRemoved.addListener(function(tabId, info) {
|
|
|
|
sessionStorageHash("manageStylesHistory").unset(tabId);
|
|
|
|
if (info.windowId == windowId && info.isWindowClosing) {
|
|
|
|
sessionStorageHash("saveSizeOnClose").unset(windowId);
|
|
|
|
}
|
2015-05-27 19:02:36 +00:00
|
|
|
});
|
2015-06-10 12:55:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
getActiveTab(function(tab) {
|
|
|
|
useHistoryBack = sessionStorageHash("manageStylesHistory").value[tab.id] == location.href;
|
|
|
|
});
|
|
|
|
|
|
|
|
function goBackToManage(event) {
|
|
|
|
if (useHistoryBack) {
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
history.back();
|
|
|
|
}
|
2015-05-27 19:02:36 +00:00
|
|
|
}
|
2015-03-13 22:26:26 +00:00
|
|
|
|
2015-01-30 17:05:06 +00:00
|
|
|
window.onbeforeunload = function() {
|
2015-05-27 19:02:36 +00:00
|
|
|
if (saveSizeOnClose) {
|
2015-10-05 11:27:17 +00:00
|
|
|
prefs.set("windowPosition", {
|
2015-03-13 22:26:26 +00:00
|
|
|
left: screenLeft,
|
|
|
|
top: screenTop,
|
|
|
|
width: outerWidth,
|
|
|
|
height: outerHeight
|
|
|
|
});
|
|
|
|
}
|
2015-03-14 11:51:41 +00:00
|
|
|
document.activeElement.blur();
|
2015-08-02 15:11:15 +00:00
|
|
|
if (isCleanGlobal()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
updateLintReport(null, 0);
|
|
|
|
return confirm(t('styleChangesNotSaved'));
|
2017-01-30 19:29:12 +00:00
|
|
|
};
|
2015-03-03 00:58:04 +00:00
|
|
|
|
2015-01-30 17:05:06 +00:00
|
|
|
function addAppliesTo(list, name, value) {
|
|
|
|
var showingEverything = list.querySelector(".applies-to-everything") != null;
|
|
|
|
// blow away "Everything" if it's there
|
|
|
|
if (showingEverything) {
|
|
|
|
list.removeChild(list.firstChild);
|
|
|
|
}
|
|
|
|
var e;
|
|
|
|
if (name && value) {
|
2015-07-11 14:35:04 +00:00
|
|
|
e = template.appliesTo.cloneNode(true);
|
2015-01-30 17:05:06 +00:00
|
|
|
e.querySelector("[name=applies-type]").value = name;
|
|
|
|
e.querySelector("[name=applies-value]").value = value;
|
|
|
|
e.querySelector(".remove-applies-to").addEventListener("click", removeAppliesTo, false);
|
|
|
|
} else if (showingEverything || list.hasChildNodes()) {
|
2015-07-11 14:35:04 +00:00
|
|
|
e = template.appliesTo.cloneNode(true);
|
2015-01-30 17:05:06 +00:00
|
|
|
if (list.hasChildNodes()) {
|
|
|
|
e.querySelector("[name=applies-type]").value = list.querySelector("li:last-child [name='applies-type']").value;
|
|
|
|
}
|
|
|
|
e.querySelector(".remove-applies-to").addEventListener("click", removeAppliesTo, false);
|
|
|
|
} else {
|
2015-07-11 14:35:04 +00:00
|
|
|
e = template.appliesToEverything.cloneNode(true);
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
e.querySelector(".add-applies-to").addEventListener("click", function() {addAppliesTo(this.parentNode.parentNode)}, false);
|
|
|
|
list.appendChild(e);
|
|
|
|
}
|
|
|
|
|
2015-03-12 10:17:59 +00:00
|
|
|
function addSection(event, section) {
|
2015-07-11 14:35:04 +00:00
|
|
|
var div = template.section.cloneNode(true);
|
2015-01-30 17:05:06 +00:00
|
|
|
div.querySelector(".applies-to-help").addEventListener("click", showAppliesToHelp, false);
|
|
|
|
div.querySelector(".remove-section").addEventListener("click", removeSection, false);
|
2015-03-12 10:17:59 +00:00
|
|
|
div.querySelector(".add-section").addEventListener("click", addSection, false);
|
2015-06-23 16:24:53 +00:00
|
|
|
div.querySelector(".beautify-section").addEventListener("click", beautify);
|
2015-01-30 17:05:06 +00:00
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
var codeElement = div.querySelector(".code");
|
2015-01-30 17:05:06 +00:00
|
|
|
var appliesTo = div.querySelector(".applies-to-list");
|
2015-03-21 13:24:45 +00:00
|
|
|
var appliesToAdded = false;
|
2015-01-30 17:05:06 +00:00
|
|
|
|
|
|
|
if (section) {
|
|
|
|
codeElement.value = section.code;
|
2015-03-21 13:24:45 +00:00
|
|
|
for (var i in propertyToCss) {
|
|
|
|
if (section[i]) {
|
|
|
|
section[i].forEach(function(url) {
|
|
|
|
addAppliesTo(appliesTo, propertyToCss[i], url);
|
|
|
|
appliesToAdded = true;
|
|
|
|
});
|
|
|
|
}
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
2015-03-21 13:24:45 +00:00
|
|
|
}
|
|
|
|
if (!appliesToAdded) {
|
2015-01-30 17:05:06 +00:00
|
|
|
addAppliesTo(appliesTo);
|
|
|
|
}
|
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
appliesTo.addEventListener("change", onChange);
|
|
|
|
appliesTo.addEventListener("input", onChange);
|
|
|
|
|
2015-01-30 17:05:06 +00:00
|
|
|
var sections = document.getElementById("sections");
|
2015-03-21 13:24:45 +00:00
|
|
|
if (event) {
|
2016-02-02 06:18:23 +00:00
|
|
|
var clickedSection = getSectionForChild(event.target);
|
2015-03-21 13:24:45 +00:00
|
|
|
sections.insertBefore(div, clickedSection.nextElementSibling);
|
2015-07-16 19:31:03 +00:00
|
|
|
var newIndex = getSections().indexOf(clickedSection) + 1;
|
2015-04-22 20:47:29 +00:00
|
|
|
var cm = setupCodeMirror(codeElement, newIndex);
|
|
|
|
makeSectionVisible(cm);
|
|
|
|
cm.focus()
|
2015-05-22 12:28:05 +00:00
|
|
|
renderLintReport();
|
2015-03-12 10:17:59 +00:00
|
|
|
} else {
|
|
|
|
sections.appendChild(div);
|
2016-02-02 06:18:23 +00:00
|
|
|
var cm = setupCodeMirror(codeElement);
|
2015-03-12 10:17:59 +00:00
|
|
|
}
|
2015-03-21 13:24:45 +00:00
|
|
|
|
2016-02-02 06:18:23 +00:00
|
|
|
div.CodeMirror = cm;
|
2015-03-21 13:24:45 +00:00
|
|
|
setCleanSection(div);
|
2015-05-24 23:03:10 +00:00
|
|
|
return div;
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeAppliesTo(event) {
|
2015-03-14 11:51:41 +00:00
|
|
|
var appliesTo = event.target.parentNode,
|
|
|
|
appliesToList = appliesTo.parentNode;
|
2015-03-21 13:24:45 +00:00
|
|
|
removeAreaAndSetDirty(appliesTo);
|
2015-01-30 17:05:06 +00:00
|
|
|
if (!appliesToList.hasChildNodes()) {
|
2015-03-21 13:24:45 +00:00
|
|
|
addAppliesTo(appliesToList);
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeSection(event) {
|
2016-02-02 06:18:23 +00:00
|
|
|
var section = getSectionForChild(event.target);
|
|
|
|
var cm = section.CodeMirror;
|
2015-03-21 13:24:45 +00:00
|
|
|
removeAreaAndSetDirty(section);
|
|
|
|
editors.splice(editors.indexOf(cm), 1);
|
2015-05-22 12:28:05 +00:00
|
|
|
renderLintReport();
|
2015-03-21 13:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeAreaAndSetDirty(area) {
|
2017-01-30 19:29:12 +00:00
|
|
|
var contributors = area.querySelectorAll('.style-contributor');
|
|
|
|
if(!contributors.length){
|
|
|
|
setCleanItem(area, false);
|
|
|
|
}
|
|
|
|
contributors.some(function(node) {
|
2015-03-21 13:24:45 +00:00
|
|
|
if (node.savedValue) {
|
|
|
|
// it's a saved section, so make it dirty and stop the enumeration
|
|
|
|
setCleanItem(area, false);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// it's an empty section, so undirty the applies-to items,
|
|
|
|
// otherwise orphaned ids would keep the style dirty
|
|
|
|
setCleanItem(node, true);
|
|
|
|
}
|
2015-03-14 11:51:41 +00:00
|
|
|
});
|
2015-03-21 13:24:45 +00:00
|
|
|
updateTitle();
|
|
|
|
area.parentNode.removeChild(area);
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 20:07:35 +00:00
|
|
|
function makeSectionVisible(cm) {
|
2016-02-02 06:18:23 +00:00
|
|
|
var section = cm.getSection();
|
2015-03-14 20:07:35 +00:00
|
|
|
var bounds = section.getBoundingClientRect();
|
|
|
|
if ((bounds.bottom > window.innerHeight && bounds.top > 0) || (bounds.top < 0 && bounds.bottom < window.innerHeight)) {
|
|
|
|
if (bounds.top < 0) {
|
|
|
|
window.scrollBy(0, bounds.top - 1);
|
|
|
|
} else {
|
|
|
|
window.scrollBy(0, bounds.bottom - window.innerHeight + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-07 00:08:04 +00:00
|
|
|
function setupGlobalSearch() {
|
|
|
|
var originalCommand = {
|
|
|
|
find: CodeMirror.commands.find,
|
|
|
|
findNext: CodeMirror.commands.findNext,
|
2015-07-11 14:35:04 +00:00
|
|
|
findPrev: CodeMirror.commands.findPrev,
|
|
|
|
replace: CodeMirror.commands.replace
|
2017-01-30 19:29:12 +00:00
|
|
|
};
|
2015-07-11 14:35:04 +00:00
|
|
|
var originalOpenDialog = CodeMirror.prototype.openDialog;
|
|
|
|
var originalOpenConfirm = CodeMirror.prototype.openConfirm;
|
2015-03-07 00:08:04 +00:00
|
|
|
|
2015-04-22 20:50:18 +00:00
|
|
|
var curState; // cm.state.search for last used 'find'
|
|
|
|
|
2015-03-07 00:08:04 +00:00
|
|
|
function shouldIgnoreCase(query) { // treat all-lowercase non-regexp queries as case-insensitive
|
|
|
|
return typeof query == "string" && query == query.toLowerCase();
|
|
|
|
}
|
|
|
|
|
2015-04-22 20:50:18 +00:00
|
|
|
function updateState(cm, newState) {
|
|
|
|
if (!newState) {
|
|
|
|
if (cm.state.search) {
|
|
|
|
return cm.state.search;
|
|
|
|
}
|
2015-09-01 07:54:56 +00:00
|
|
|
if (!curState) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-04-22 20:50:18 +00:00
|
|
|
newState = curState;
|
|
|
|
}
|
|
|
|
cm.state.search = {
|
|
|
|
query: newState.query,
|
|
|
|
overlay: newState.overlay,
|
|
|
|
annotate: cm.showMatchesOnScrollbar(newState.query, shouldIgnoreCase(newState.query))
|
|
|
|
}
|
|
|
|
cm.addOverlay(newState.overlay);
|
|
|
|
return cm.state.search;
|
|
|
|
}
|
|
|
|
|
2015-07-11 14:35:04 +00:00
|
|
|
// temporarily overrides the original openDialog with the provided template's innerHTML
|
|
|
|
function customizeOpenDialog(cm, template, callback) {
|
|
|
|
cm.openDialog = function(tmpl, cb, opt) {
|
|
|
|
// invoke 'callback' and bind 'this' to the original callback
|
|
|
|
originalOpenDialog.call(cm, template.innerHTML, callback.bind(cb), opt);
|
|
|
|
};
|
|
|
|
setTimeout(function() { cm.openDialog = originalOpenDialog; }, 0);
|
2015-10-29 18:20:05 +00:00
|
|
|
refocusMinidialog(cm);
|
2015-07-11 14:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function focusClosestCM(activeCM) {
|
2015-05-09 20:40:49 +00:00
|
|
|
editors.lastActive = activeCM;
|
|
|
|
var cm = getEditorInSight();
|
|
|
|
if (cm != activeCM) {
|
|
|
|
cm.focus();
|
|
|
|
}
|
2015-07-11 14:35:04 +00:00
|
|
|
return cm;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function find(activeCM) {
|
|
|
|
activeCM = focusClosestCM(activeCM);
|
|
|
|
customizeOpenDialog(activeCM, template.find, function(query) {
|
|
|
|
this(query);
|
|
|
|
curState = activeCM.state.search;
|
|
|
|
if (editors.length == 1 || !curState.query) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
editors.forEach(function(cm) {
|
|
|
|
if (cm != activeCM) {
|
|
|
|
cm.execCommand("clearSearch");
|
|
|
|
updateState(cm, curState);
|
2015-03-07 00:08:04 +00:00
|
|
|
}
|
2015-07-11 14:35:04 +00:00
|
|
|
});
|
|
|
|
if (CodeMirror.cmpPos(curState.posFrom, curState.posTo) == 0) {
|
|
|
|
findNext(activeCM);
|
|
|
|
}
|
|
|
|
});
|
2015-03-07 00:08:04 +00:00
|
|
|
originalCommand.find(activeCM);
|
|
|
|
}
|
|
|
|
|
|
|
|
function findNext(activeCM, reverse) {
|
2015-04-22 20:50:18 +00:00
|
|
|
var state = updateState(activeCM);
|
|
|
|
if (!state || !state.query) {
|
2015-03-07 00:08:04 +00:00
|
|
|
find(activeCM);
|
|
|
|
return;
|
|
|
|
}
|
2015-04-22 20:50:18 +00:00
|
|
|
var pos = activeCM.getCursor(reverse ? "from" : "to");
|
|
|
|
activeCM.setSelection(activeCM.getCursor()); // clear the selection, don't move the cursor
|
2015-03-07 00:08:04 +00:00
|
|
|
|
2015-05-10 00:33:06 +00:00
|
|
|
var rxQuery = typeof state.query == "object"
|
|
|
|
? state.query : stringAsRegExp(state.query, shouldIgnoreCase(state.query) ? "i" : "");
|
|
|
|
|
|
|
|
if (document.activeElement && document.activeElement.name == "applies-value"
|
|
|
|
&& searchAppliesTo(activeCM)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-07 00:08:04 +00:00
|
|
|
for (var i=0, cm=activeCM; i < editors.length; i++) {
|
2015-04-22 20:50:18 +00:00
|
|
|
state = updateState(cm);
|
2015-05-10 00:33:06 +00:00
|
|
|
if (!cm.hasFocus()) {
|
2015-03-07 00:08:04 +00:00
|
|
|
pos = reverse ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(0, 0);
|
|
|
|
}
|
|
|
|
var searchCursor = cm.getSearchCursor(state.query, pos, shouldIgnoreCase(state.query));
|
2015-03-21 02:53:41 +00:00
|
|
|
if (searchCursor.find(reverse)) {
|
2015-03-07 00:08:04 +00:00
|
|
|
if (editors.length > 1) {
|
2015-03-14 20:07:35 +00:00
|
|
|
makeSectionVisible(cm);
|
2015-03-07 00:08:04 +00:00
|
|
|
cm.focus();
|
|
|
|
}
|
|
|
|
// speedup the original findNext
|
|
|
|
state.posFrom = reverse ? searchCursor.to() : searchCursor.from();
|
|
|
|
state.posTo = CodeMirror.Pos(state.posFrom.line, state.posFrom.ch);
|
|
|
|
originalCommand[reverse ? "findPrev" : "findNext"](cm);
|
|
|
|
return;
|
2015-05-10 00:33:06 +00:00
|
|
|
} else if (!reverse && searchAppliesTo(cm)) {
|
|
|
|
return;
|
2015-03-07 00:08:04 +00:00
|
|
|
}
|
|
|
|
cm = editors[(editors.indexOf(cm) + (reverse ? -1 + editors.length : 1)) % editors.length];
|
2015-05-10 00:33:06 +00:00
|
|
|
if (reverse && searchAppliesTo(cm)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-07 00:08:04 +00:00
|
|
|
}
|
|
|
|
// nothing found so far, so call the original search with wrap-around
|
|
|
|
originalCommand[reverse ? "findPrev" : "findNext"](activeCM);
|
2015-05-10 00:33:06 +00:00
|
|
|
|
|
|
|
function searchAppliesTo(cm) {
|
2016-02-02 06:18:23 +00:00
|
|
|
var inputs = [].slice.call(cm.getSection().querySelectorAll(".applies-value"));
|
2015-05-10 00:33:06 +00:00
|
|
|
if (reverse) {
|
|
|
|
inputs = inputs.reverse();
|
|
|
|
}
|
|
|
|
inputs.splice(0, inputs.indexOf(document.activeElement) + 1);
|
|
|
|
return inputs.some(function(input) {
|
|
|
|
var match = rxQuery.exec(input.value);
|
|
|
|
if (match) {
|
|
|
|
input.focus();
|
|
|
|
var end = match.index + match[0].length;
|
|
|
|
// scroll selected part into view in long inputs,
|
|
|
|
// works only outside of current event handlers chain, hence timeout=0
|
|
|
|
setTimeout(function() {
|
|
|
|
input.setSelectionRange(end, end);
|
|
|
|
input.setSelectionRange(match.index, end)
|
|
|
|
}, 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-03-07 00:08:04 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 07:11:18 +00:00
|
|
|
function findPrev(cm) {
|
|
|
|
findNext(cm, true);
|
|
|
|
}
|
|
|
|
|
2015-07-11 14:35:04 +00:00
|
|
|
function replace(activeCM, all) {
|
|
|
|
var queue, query, replacement;
|
|
|
|
activeCM = focusClosestCM(activeCM);
|
|
|
|
customizeOpenDialog(activeCM, template[all ? "replaceAll" : "replace"], function(txt) {
|
|
|
|
query = txt;
|
|
|
|
customizeOpenDialog(activeCM, template.replaceWith, function(txt) {
|
|
|
|
replacement = txt;
|
|
|
|
queue = editors.rotate(-editors.indexOf(activeCM));
|
|
|
|
all ? editors.forEach(doReplace) : doReplace();
|
|
|
|
});
|
|
|
|
this(query);
|
|
|
|
});
|
|
|
|
originalCommand.replace(activeCM, all);
|
|
|
|
|
|
|
|
function doReplace() {
|
|
|
|
var cm = queue.shift();
|
|
|
|
if (!cm) {
|
|
|
|
if (!all) {
|
|
|
|
editors.lastActive.focus();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// hide the first two dialogs (replace, replaceWith)
|
|
|
|
cm.openDialog = function(tmpl, callback, opt) {
|
|
|
|
cm.openDialog = function(tmpl, callback, opt) {
|
|
|
|
cm.openDialog = originalOpenDialog;
|
|
|
|
if (all) {
|
|
|
|
callback(replacement);
|
|
|
|
} else {
|
|
|
|
doConfirm(cm);
|
|
|
|
callback(replacement);
|
|
|
|
if (!cm.getWrapperElement().querySelector(".CodeMirror-dialog")) {
|
|
|
|
// no dialog == nothing found in the current CM, move to the next
|
|
|
|
doReplace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
callback(query);
|
|
|
|
};
|
|
|
|
originalCommand.replace(cm, all);
|
|
|
|
}
|
|
|
|
function doConfirm(cm) {
|
|
|
|
var wrapAround = false;
|
|
|
|
var origPos = cm.getCursor();
|
|
|
|
cm.openConfirm = function overrideConfirm(tmpl, callbacks, opt) {
|
|
|
|
var ovrCallbacks = callbacks.map(function(callback) {
|
|
|
|
return function() {
|
|
|
|
makeSectionVisible(cm);
|
|
|
|
cm.openConfirm = overrideConfirm;
|
|
|
|
setTimeout(function() { cm.openConfirm = originalOpenConfirm; }, 0);
|
|
|
|
|
|
|
|
var pos = cm.getCursor();
|
|
|
|
callback();
|
|
|
|
var cmp = CodeMirror.cmpPos(cm.getCursor(), pos);
|
|
|
|
wrapAround |= cmp <= 0;
|
|
|
|
|
|
|
|
var dlg = cm.getWrapperElement().querySelector(".CodeMirror-dialog");
|
|
|
|
if (!dlg || cmp == 0 || wrapAround && CodeMirror.cmpPos(cm.getCursor(), origPos) >= 0) {
|
|
|
|
if (dlg) {
|
|
|
|
dlg.remove();
|
|
|
|
}
|
|
|
|
doReplace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
originalOpenConfirm.call(cm, template.replaceConfirm.innerHTML, ovrCallbacks, opt);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function replaceAll(cm) {
|
|
|
|
replace(cm, true);
|
|
|
|
}
|
|
|
|
|
2015-03-07 00:08:04 +00:00
|
|
|
CodeMirror.commands.find = find;
|
2015-03-26 07:11:18 +00:00
|
|
|
CodeMirror.commands.findNext = findNext;
|
|
|
|
CodeMirror.commands.findPrev = findPrev;
|
2015-07-11 14:35:04 +00:00
|
|
|
CodeMirror.commands.replace = replace;
|
|
|
|
CodeMirror.commands.replaceAll = replaceAll;
|
2015-03-07 00:08:04 +00:00
|
|
|
}
|
|
|
|
|
2015-03-08 06:21:43 +00:00
|
|
|
function jumpToLine(cm) {
|
|
|
|
var cur = cm.getCursor();
|
2015-10-29 18:20:05 +00:00
|
|
|
refocusMinidialog(cm);
|
2015-07-11 14:35:04 +00:00
|
|
|
cm.openDialog(template.jumpToLine.innerHTML, function(str) {
|
2015-03-08 06:21:43 +00:00
|
|
|
var m = str.match(/^\s*(\d+)(?:\s*:\s*(\d+))?\s*$/);
|
|
|
|
if (m) {
|
|
|
|
cm.setCursor(m[1] - 1, m[2] ? m[2] - 1 : cur.ch);
|
|
|
|
}
|
|
|
|
}, {value: cur.line+1});
|
|
|
|
}
|
|
|
|
|
2015-10-29 18:20:05 +00:00
|
|
|
function refocusMinidialog(cm) {
|
2016-02-02 06:18:23 +00:00
|
|
|
var section = cm.getSection();
|
2015-10-29 18:20:05 +00:00
|
|
|
if (!section.querySelector(".CodeMirror-dialog")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// close the currently opened minidialog
|
|
|
|
cm.focus();
|
|
|
|
// make sure to focus the input in newly opened minidialog
|
|
|
|
setTimeout(function() {
|
|
|
|
section.querySelector(".CodeMirror-dialog").focus();
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
2015-04-23 01:53:30 +00:00
|
|
|
function nextPrevEditor(cm, direction) {
|
2015-03-21 13:24:45 +00:00
|
|
|
cm = editors[(editors.indexOf(cm) + direction + editors.length) % editors.length];
|
2015-03-14 20:07:35 +00:00
|
|
|
makeSectionVisible(cm);
|
|
|
|
cm.focus();
|
2015-03-08 06:21:43 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 01:53:30 +00:00
|
|
|
function getEditorInSight(nearbyElement) {
|
|
|
|
// priority: 1. associated CM for applies-to element 2. last active if visible 3. first visible
|
|
|
|
var cm;
|
|
|
|
if (nearbyElement && nearbyElement.className.indexOf("applies-") >= 0) {
|
2016-02-02 06:18:23 +00:00
|
|
|
cm = getSectionForChild(nearbyElement).CodeMirror;
|
2015-04-23 01:53:30 +00:00
|
|
|
} else {
|
|
|
|
cm = editors.lastActive;
|
|
|
|
}
|
|
|
|
if (!cm || offscreenDistance(cm) > 0) {
|
|
|
|
var sorted = editors
|
|
|
|
.map(function(cm, index) { return {cm: cm, distance: offscreenDistance(cm), index: index} })
|
|
|
|
.sort(function(a, b) { return a.distance - b.distance || a.index - b.index });
|
|
|
|
cm = sorted[0].cm;
|
|
|
|
if (sorted[0].distance > 0) {
|
|
|
|
makeSectionVisible(cm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cm;
|
|
|
|
|
|
|
|
function offscreenDistance(cm) {
|
|
|
|
var LINES_VISIBLE = 2; // closest editor should have at least # lines visible
|
2016-02-02 06:18:23 +00:00
|
|
|
var bounds = cm.getSection().getBoundingClientRect();
|
2015-04-23 01:53:30 +00:00
|
|
|
if (bounds.top < 0) {
|
|
|
|
return -bounds.top;
|
|
|
|
} else if (bounds.top < window.innerHeight - cm.defaultTextHeight() * LINES_VISIBLE) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return bounds.top - bounds.height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-02 15:11:15 +00:00
|
|
|
function updateLintReport(cm, delay) {
|
|
|
|
if (delay == 0) {
|
|
|
|
// immediately show pending csslint messages in onbeforeunload and save
|
2017-03-20 09:30:19 +00:00
|
|
|
update(cm);
|
2015-08-02 15:11:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (delay > 0) {
|
2017-03-20 09:30:19 +00:00
|
|
|
setTimeout(cm => { cm.performLint(); update(cm) }, delay, cm);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var state = cm.state.lint;
|
|
|
|
if (!state) {
|
2015-08-02 15:11:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-08-03 16:34:47 +00:00
|
|
|
// user is editing right now: postpone updating the report for the new issues (default: 500ms lint + 4500ms)
|
|
|
|
// or update it as soon as possible (default: 500ms lint + 100ms) in case an existing issue was just fixed
|
2015-05-22 12:28:05 +00:00
|
|
|
clearTimeout(state.reportTimeout);
|
2017-03-20 09:30:19 +00:00
|
|
|
state.reportTimeout = setTimeout(update, state.options.delay + 100, cm);
|
2015-09-13 09:41:19 +00:00
|
|
|
state.postponeNewIssues = delay == undefined || delay == null;
|
2015-08-03 16:34:47 +00:00
|
|
|
|
2017-03-20 09:30:19 +00:00
|
|
|
function update(cm) {
|
|
|
|
var scope = cm ? [cm] : editors;
|
2015-08-02 15:11:15 +00:00
|
|
|
var changed = false;
|
2015-08-03 16:34:47 +00:00
|
|
|
var fixedOldIssues = false;
|
2015-08-02 15:11:15 +00:00
|
|
|
scope.forEach(function(cm) {
|
2015-09-13 09:41:19 +00:00
|
|
|
var state = cm.state.lint;
|
|
|
|
var oldMarkers = state.markedLast || {};
|
2015-08-03 16:34:47 +00:00
|
|
|
var newMarkers = {};
|
2015-09-13 09:41:19 +00:00
|
|
|
var html = state.marked.length == 0 ? "" : "<tbody>" +
|
|
|
|
state.marked.map(function(mark) {
|
2015-08-02 15:11:15 +00:00
|
|
|
var info = mark.__annotation;
|
2015-09-13 09:41:19 +00:00
|
|
|
var isActiveLine = info.from.line == cm.getCursor().line;
|
|
|
|
var pos = isActiveLine ? "cursor" : (info.from.line + "," + info.from.ch);
|
2015-08-07 14:11:56 +00:00
|
|
|
var message = escapeHtml(info.message.replace(/ at line \d.+$/, ""));
|
|
|
|
if (message.length > 100) {
|
|
|
|
message = message.substr(0, 100) + "...";
|
|
|
|
}
|
2015-09-13 09:41:19 +00:00
|
|
|
if (isActiveLine || oldMarkers[pos] == message) {
|
|
|
|
delete oldMarkers[pos];
|
|
|
|
}
|
|
|
|
newMarkers[pos] = message;
|
2015-08-02 15:11:15 +00:00
|
|
|
return "<tr class='" + info.severity + "'>" +
|
|
|
|
"<td role='severity' class='CodeMirror-lint-marker-" + info.severity + "'>" +
|
|
|
|
info.severity + "</td>" +
|
|
|
|
"<td role='line'>" + (info.from.line+1) + "</td>" +
|
|
|
|
"<td role='sep'>:</td>" +
|
|
|
|
"<td role='col'>" + (info.from.ch+1) + "</td>" +
|
2015-08-07 14:11:56 +00:00
|
|
|
"<td role='message'>" + message + "</td></tr>";
|
2015-08-02 15:11:15 +00:00
|
|
|
}).join("") + "</tbody>";
|
2015-09-13 09:41:19 +00:00
|
|
|
state.markedLast = newMarkers;
|
|
|
|
fixedOldIssues |= state.reportDisplayed && Object.keys(oldMarkers).length > 0;
|
|
|
|
if (state.html != html) {
|
|
|
|
state.html = html;
|
2015-08-02 15:11:15 +00:00
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (changed) {
|
2015-08-03 16:34:47 +00:00
|
|
|
clearTimeout(state ? state.renderTimeout : undefined);
|
2015-09-13 09:41:19 +00:00
|
|
|
if (!state || !state.postponeNewIssues || fixedOldIssues) {
|
2015-08-03 16:34:47 +00:00
|
|
|
renderLintReport(true);
|
|
|
|
} else {
|
|
|
|
state.renderTimeout = setTimeout(function() {
|
|
|
|
renderLintReport(true);
|
|
|
|
}, CodeMirror.defaults.lintReportDelay);
|
|
|
|
}
|
2015-05-22 12:28:05 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-07 14:11:56 +00:00
|
|
|
function escapeHtml(html) {
|
|
|
|
var chars = {"&": "&", "<": "<", ">": ">", '"': '"', "'": ''', "/": '/'};
|
|
|
|
return html.replace(/[&<>"'\/]/g, function(char) { return chars[char] });
|
|
|
|
}
|
2015-05-22 12:28:05 +00:00
|
|
|
}
|
|
|
|
|
2015-09-13 09:41:19 +00:00
|
|
|
function renderLintReport(someBlockChanged) {
|
2015-05-22 12:28:05 +00:00
|
|
|
var container = document.getElementById("lint");
|
|
|
|
var content = container.children[1];
|
|
|
|
var label = t("sectionCode");
|
|
|
|
var newContent = content.cloneNode(false);
|
2016-01-20 18:02:10 +00:00
|
|
|
var issueCount = 0;
|
2015-05-22 12:28:05 +00:00
|
|
|
editors.forEach(function(cm, index) {
|
2017-03-20 09:30:19 +00:00
|
|
|
if (cm.state.lint && cm.state.lint.html) {
|
2015-05-22 12:28:05 +00:00
|
|
|
var newBlock = newContent.appendChild(document.createElement("table"));
|
|
|
|
var html = "<caption>" + label + " " + (index+1) + "</caption>" + cm.state.lint.html;
|
|
|
|
newBlock.innerHTML = html;
|
|
|
|
newBlock.cm = cm;
|
2016-01-20 18:02:10 +00:00
|
|
|
issueCount += newBlock.rows.length;
|
2015-09-13 09:41:19 +00:00
|
|
|
|
|
|
|
var block = content.children[newContent.children.length - 1];
|
2015-09-21 11:59:13 +00:00
|
|
|
var blockChanged = !block || cm != block.cm || html != block.innerHTML;
|
2015-09-13 09:41:19 +00:00
|
|
|
someBlockChanged |= blockChanged;
|
|
|
|
cm.state.lint.reportDisplayed = blockChanged;
|
2015-05-22 12:28:05 +00:00
|
|
|
}
|
|
|
|
});
|
2015-09-13 09:41:19 +00:00
|
|
|
if (someBlockChanged || newContent.children.length != content.children.length) {
|
2016-01-20 18:02:10 +00:00
|
|
|
document.getElementById('issue-count').textContent = issueCount;
|
2015-05-22 12:28:05 +00:00
|
|
|
container.replaceChild(newContent, content);
|
|
|
|
container.style.display = newContent.children.length ? "block" : "none";
|
|
|
|
resizeLintReport(null, newContent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function resizeLintReport(event, content) {
|
|
|
|
content = content || document.getElementById("lint").children[1];
|
|
|
|
if (content.children.length) {
|
2016-01-20 18:02:10 +00:00
|
|
|
var bounds = content.getBoundingClientRect();
|
|
|
|
var newMaxHeight = bounds.bottom <= innerHeight ? '' : (innerHeight - bounds.top) + "px";
|
2015-05-22 12:28:05 +00:00
|
|
|
if (newMaxHeight != content.style.maxHeight) {
|
|
|
|
content.style.maxHeight = newMaxHeight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function gotoLintIssue(event) {
|
2015-07-16 19:31:03 +00:00
|
|
|
var issue = event.target.closest("tr");
|
2015-05-22 12:28:05 +00:00
|
|
|
if (!issue) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-16 19:31:03 +00:00
|
|
|
var block = issue.closest("table");
|
2015-05-22 12:28:05 +00:00
|
|
|
makeSectionVisible(block.cm);
|
|
|
|
block.cm.focus();
|
|
|
|
block.cm.setSelection({
|
|
|
|
line: parseInt(issue.querySelector("td[role='line']").textContent) - 1,
|
|
|
|
ch: parseInt(issue.querySelector("td[role='col']").textContent) - 1
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-01 15:09:31 +00:00
|
|
|
function toggleLintReport() {
|
|
|
|
document.getElementById("lint").classList.toggle("collapsed");
|
|
|
|
}
|
|
|
|
|
2015-06-23 16:24:53 +00:00
|
|
|
function beautify(event) {
|
|
|
|
if (exports.css_beautify) { // thanks to csslint's definition of 'exports'
|
|
|
|
doBeautify();
|
|
|
|
} else {
|
|
|
|
var script = document.head.appendChild(document.createElement("script"));
|
|
|
|
script.src = "beautify/beautify-css.js";
|
|
|
|
script.onload = doBeautify;
|
|
|
|
}
|
|
|
|
function doBeautify() {
|
2015-10-05 11:27:17 +00:00
|
|
|
var tabs = prefs.get("editor.indentWithTabs");
|
|
|
|
var options = prefs.get("editor.beautify");
|
|
|
|
options.indent_size = tabs ? 1 : prefs.get("editor.tabSize");
|
2015-06-23 16:24:53 +00:00
|
|
|
options.indent_char = tabs ? "\t" : " ";
|
|
|
|
|
2015-07-16 19:31:03 +00:00
|
|
|
var section = getSectionForChild(event.target);
|
2016-02-02 06:18:23 +00:00
|
|
|
var scope = section ? [section.CodeMirror] : editors;
|
2015-06-23 16:24:53 +00:00
|
|
|
|
|
|
|
showHelp(t("styleBeautify"), "<div class='beautify-options'>" +
|
|
|
|
optionHtml(".selector1,", "selector_separator_newline") +
|
|
|
|
optionHtml(".selector2,", "newline_before_open_brace") +
|
|
|
|
optionHtml("{", "newline_after_open_brace") +
|
|
|
|
optionHtml("border: none;", "newline_between_properties", true) +
|
|
|
|
optionHtml("display: block;", "newline_before_close_brace", true) +
|
|
|
|
optionHtml("}", "newline_between_rules") +
|
2015-06-22 19:03:28 +00:00
|
|
|
"</div>" +
|
|
|
|
"<div><button role='undo'></button></div>");
|
|
|
|
|
|
|
|
var undoButton = document.querySelector("#help-popup button[role='undo']");
|
|
|
|
undoButton.textContent = t(scope.length == 1 ? "undo" : "undoGlobal");
|
|
|
|
undoButton.addEventListener("click", function() {
|
2015-07-04 06:36:56 +00:00
|
|
|
var undoable = false;
|
|
|
|
scope.forEach(function(cm) {
|
|
|
|
if (cm.beautifyChange && cm.beautifyChange[cm.changeGeneration()]) {
|
|
|
|
delete cm.beautifyChange[cm.changeGeneration()];
|
|
|
|
cm.undo();
|
|
|
|
undoable |= cm.beautifyChange[cm.changeGeneration()];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
undoButton.disabled = !undoable;
|
|
|
|
});
|
|
|
|
|
|
|
|
scope.forEach(function(cm) {
|
|
|
|
setTimeout(function() {
|
|
|
|
var text = cm.getValue();
|
|
|
|
var newText = exports.css_beautify(text, options);
|
|
|
|
if (newText != text) {
|
|
|
|
if (!cm.beautifyChange || !cm.beautifyChange[cm.changeGeneration()]) {
|
|
|
|
// clear the list if last change wasn't a css-beautify
|
|
|
|
cm.beautifyChange = {};
|
|
|
|
}
|
|
|
|
cm.setValue(newText);
|
|
|
|
cm.beautifyChange[cm.changeGeneration()] = true;
|
|
|
|
undoButton.disabled = false;
|
|
|
|
}
|
|
|
|
}, 0);
|
2015-06-22 19:03:28 +00:00
|
|
|
});
|
2015-06-23 16:24:53 +00:00
|
|
|
|
|
|
|
document.querySelector(".beautify-options").addEventListener("change", function(event) {
|
|
|
|
var value = event.target.selectedIndex > 0;
|
|
|
|
options[event.target.dataset.option] = value;
|
2015-10-05 11:27:17 +00:00
|
|
|
prefs.set("editor.beautify", options);
|
2015-06-23 16:24:53 +00:00
|
|
|
event.target.parentNode.setAttribute("newline", value.toString());
|
|
|
|
doBeautify();
|
|
|
|
});
|
|
|
|
|
|
|
|
function optionHtml(label, optionName, indent) {
|
|
|
|
var value = options[optionName];
|
|
|
|
return "<div newline='" + value.toString() + "'>" +
|
|
|
|
"<span" + (indent ? " indent" : "") + ">" + label + "</span>" +
|
|
|
|
"<select data-option='" + optionName + "'>" +
|
|
|
|
"<option" + (value ? "" : " selected") + "> </option>" +
|
|
|
|
"<option" + (value ? " selected" : "") + ">\\n</option>" +
|
|
|
|
"</select></div>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-20 04:59:51 +00:00
|
|
|
document.addEventListener("DOMContentLoaded", init);
|
2015-01-30 17:05:06 +00:00
|
|
|
|
|
|
|
function init() {
|
2015-01-30 17:28:05 +00:00
|
|
|
var params = getParams();
|
|
|
|
if (!params.id) { // match should be 2 - one for the whole thing, one for the parentheses
|
2015-01-30 17:05:06 +00:00
|
|
|
// This is an add
|
2017-03-20 04:59:51 +00:00
|
|
|
tE("heading", "addStyleTitle");
|
2015-01-30 17:28:05 +00:00
|
|
|
var section = {code: ""}
|
2015-03-21 13:24:45 +00:00
|
|
|
for (var i in CssToProperty) {
|
|
|
|
if (params[i]) {
|
|
|
|
section[CssToProperty[i]] = [params[i]];
|
|
|
|
}
|
2015-01-30 17:28:05 +00:00
|
|
|
}
|
2017-03-20 04:59:51 +00:00
|
|
|
onload = () => {
|
|
|
|
onload = null;
|
|
|
|
addSection(null, section);
|
|
|
|
// default to enabled
|
|
|
|
document.getElementById("enabled").checked = true
|
|
|
|
initHooks();
|
|
|
|
};
|
2015-01-30 17:05:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// This is an edit
|
2015-07-19 16:49:17 +00:00
|
|
|
tE("heading", "editStyleHeading", null, false);
|
Improve style caching, cache requests too, add code:false mode
Previously, when a cache was invalidated and every tab/iframe issued a getStyles request, we previous needlessly accessed IndexedDB for each of these requests. It happened because 1) the global cachedStyles was created only at the end of the async DB-reading, 2) and each style record is retrieved asynchronously so the single threaded JS engine interleaved all these operations. It could easily span a few seconds when many tabs are open and you have like 100 styles.
Now, in getStyles: all requests issued while cachedStyles is being populated are queued and invoked at the end.
Now, in filterStyles: all requests are cached using the request's options combined in a string as a key. It also helps on each navigation because we monitor page loading process at different stages: before, when committed, history traversal, requesting applicable styles by a content script. Icon badge update also may issue a copy of the just issued request by one of the navigation listeners.
Now, the caches are invalidated smartly: style add/update/delete/toggle only purges filtering cache, and modifies style cache in-place without re-reading the entire IndexedDB.
Now, code:false mode for manage page that only needs style meta. It reduces the transferred message size 10-100 times thus reducing the overhead caused by to internal JSON-fication in the extensions API.
Also fast&direct getStylesSafe for own pages; code cosmetics
2017-03-17 22:50:35 +00:00
|
|
|
getStylesSafe({id: params.id}).then(styles => {
|
2017-03-20 01:58:55 +00:00
|
|
|
const style = styles[0];
|
|
|
|
styleId = style.id;
|
2017-03-20 04:59:51 +00:00
|
|
|
setStyleMeta(style);
|
|
|
|
onload = () => {
|
|
|
|
onload = null;
|
|
|
|
initWithStyle({style});
|
|
|
|
};
|
|
|
|
if (document.readyState != 'loading') {
|
|
|
|
onload();
|
|
|
|
}
|
Improve style caching, cache requests too, add code:false mode
Previously, when a cache was invalidated and every tab/iframe issued a getStyles request, we previous needlessly accessed IndexedDB for each of these requests. It happened because 1) the global cachedStyles was created only at the end of the async DB-reading, 2) and each style record is retrieved asynchronously so the single threaded JS engine interleaved all these operations. It could easily span a few seconds when many tabs are open and you have like 100 styles.
Now, in getStyles: all requests issued while cachedStyles is being populated are queued and invoked at the end.
Now, in filterStyles: all requests are cached using the request's options combined in a string as a key. It also helps on each navigation because we monitor page loading process at different stages: before, when committed, history traversal, requesting applicable styles by a content script. Icon badge update also may issue a copy of the just issued request by one of the navigation listeners.
Now, the caches are invalidated smartly: style add/update/delete/toggle only purges filtering cache, and modifies style cache in-place without re-reading the entire IndexedDB.
Now, code:false mode for manage page that only needs style meta. It reduces the transferred message size 10-100 times thus reducing the overhead caused by to internal JSON-fication in the extensions API.
Also fast&direct getStylesSafe for own pages; code cosmetics
2017-03-17 22:50:35 +00:00
|
|
|
});
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 04:59:51 +00:00
|
|
|
function setStyleMeta(style) {
|
2015-01-30 17:05:06 +00:00
|
|
|
document.getElementById("name").value = style.name;
|
2016-03-07 02:27:17 +00:00
|
|
|
document.getElementById("enabled").checked = style.enabled;
|
2015-05-15 16:46:21 +00:00
|
|
|
document.getElementById("url").href = style.url;
|
2017-03-20 04:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function initWithStyle({style, codeIsUpdated}) {
|
|
|
|
setStyleMeta(style);
|
2017-03-20 01:58:55 +00:00
|
|
|
|
|
|
|
if (codeIsUpdated === false) {
|
|
|
|
setCleanGlobal();
|
|
|
|
updateTitle();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-30 17:05:06 +00:00
|
|
|
// if this was done in response to an update, we need to clear existing sections
|
2015-07-16 19:31:03 +00:00
|
|
|
getSections().forEach(function(div) { div.remove(); });
|
Improve style caching, cache requests too, add code:false mode
Previously, when a cache was invalidated and every tab/iframe issued a getStyles request, we previous needlessly accessed IndexedDB for each of these requests. It happened because 1) the global cachedStyles was created only at the end of the async DB-reading, 2) and each style record is retrieved asynchronously so the single threaded JS engine interleaved all these operations. It could easily span a few seconds when many tabs are open and you have like 100 styles.
Now, in getStyles: all requests issued while cachedStyles is being populated are queued and invoked at the end.
Now, in filterStyles: all requests are cached using the request's options combined in a string as a key. It also helps on each navigation because we monitor page loading process at different stages: before, when committed, history traversal, requesting applicable styles by a content script. Icon badge update also may issue a copy of the just issued request by one of the navigation listeners.
Now, the caches are invalidated smartly: style add/update/delete/toggle only purges filtering cache, and modifies style cache in-place without re-reading the entire IndexedDB.
Now, code:false mode for manage page that only needs style meta. It reduces the transferred message size 10-100 times thus reducing the overhead caused by to internal JSON-fication in the extensions API.
Also fast&direct getStylesSafe for own pages; code cosmetics
2017-03-17 22:50:35 +00:00
|
|
|
var queue = style.sections.length ? style.sections.slice() : [{code: ""}];
|
2015-06-22 18:18:33 +00:00
|
|
|
var queueStart = new Date().getTime();
|
2015-07-29 17:45:54 +00:00
|
|
|
// after 100ms the sections will be added asynchronously
|
|
|
|
while (new Date().getTime() - queueStart <= 100 && queue.length) {
|
2015-08-02 15:11:15 +00:00
|
|
|
add();
|
2015-06-22 18:18:33 +00:00
|
|
|
}
|
2015-08-02 15:11:15 +00:00
|
|
|
(function processQueue() {
|
|
|
|
if (queue.length) {
|
|
|
|
add();
|
|
|
|
setTimeout(processQueue, 0);
|
|
|
|
}
|
|
|
|
})();
|
2015-03-21 13:24:45 +00:00
|
|
|
initHooks();
|
2015-08-02 15:11:15 +00:00
|
|
|
|
|
|
|
function add() {
|
|
|
|
var sectionDiv = addSection(null, queue.shift());
|
|
|
|
maximizeCodeHeight(sectionDiv, !queue.length);
|
2017-03-20 09:30:19 +00:00
|
|
|
const cm = sectionDiv.CodeMirror;
|
|
|
|
setTimeout(() => {
|
|
|
|
cm.setOption('lint', CodeMirror.defaults.lint);
|
|
|
|
updateLintReport(cm, 0);
|
|
|
|
}, prefs.get("editor.lintDelay"));
|
2015-08-02 15:11:15 +00:00
|
|
|
}
|
2015-03-21 13:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function initHooks() {
|
|
|
|
document.querySelectorAll("#header .style-contributor").forEach(function(node) {
|
|
|
|
node.addEventListener("change", onChange);
|
|
|
|
node.addEventListener("input", onChange);
|
|
|
|
});
|
2015-04-11 10:28:25 +00:00
|
|
|
document.getElementById("to-mozilla").addEventListener("click", showMozillaFormat, false);
|
|
|
|
document.getElementById("to-mozilla-help").addEventListener("click", showToMozillaHelp, false);
|
2015-07-13 17:44:46 +00:00
|
|
|
document.getElementById("from-mozilla").addEventListener("click", fromMozillaFormat);
|
2015-06-23 16:24:53 +00:00
|
|
|
document.getElementById("beautify").addEventListener("click", beautify);
|
2015-04-11 10:28:25 +00:00
|
|
|
document.getElementById("save-button").addEventListener("click", save, false);
|
|
|
|
document.getElementById("sections-help").addEventListener("click", showSectionHelp, false);
|
2015-04-26 12:34:59 +00:00
|
|
|
document.getElementById("keyMap-help").addEventListener("click", showKeyMapHelp, false);
|
2015-06-10 12:55:32 +00:00
|
|
|
document.getElementById("cancel-button").addEventListener("click", goBackToManage);
|
2015-05-22 12:28:05 +00:00
|
|
|
document.getElementById("lint-help").addEventListener("click", showLintHelp);
|
|
|
|
document.getElementById("lint").addEventListener("click", gotoLintIssue);
|
|
|
|
window.addEventListener("resize", resizeLintReport);
|
2015-03-21 13:24:45 +00:00
|
|
|
|
2016-02-01 15:09:31 +00:00
|
|
|
// touch devices don't have onHover events so the element we'll be toggled via clicking (touching)
|
|
|
|
if ("ontouchstart" in document.body) {
|
|
|
|
document.querySelector("#lint h2").addEventListener("click", toggleLintReport);
|
|
|
|
}
|
|
|
|
|
2015-03-07 00:08:04 +00:00
|
|
|
setupGlobalSearch();
|
2015-03-21 13:24:45 +00:00
|
|
|
setCleanGlobal();
|
|
|
|
updateTitle();
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
2015-05-24 23:03:10 +00:00
|
|
|
function maximizeCodeHeight(sectionDiv, isLast) {
|
2016-02-02 06:18:23 +00:00
|
|
|
var cm = sectionDiv.CodeMirror;
|
2015-05-24 23:03:10 +00:00
|
|
|
var stats = maximizeCodeHeight.stats = maximizeCodeHeight.stats || {totalHeight: 0, deltas: []};
|
|
|
|
if (!stats.cmActualHeight) {
|
|
|
|
stats.cmActualHeight = getComputedHeight(cm.display.wrapper);
|
|
|
|
}
|
|
|
|
if (!stats.sectionMarginTop) {
|
|
|
|
stats.sectionMarginTop = parseFloat(getComputedStyle(sectionDiv).marginTop);
|
|
|
|
}
|
|
|
|
var sectionTop = sectionDiv.getBoundingClientRect().top - stats.sectionMarginTop;
|
|
|
|
if (!stats.firstSectionTop) {
|
|
|
|
stats.firstSectionTop = sectionTop;
|
|
|
|
}
|
|
|
|
var extrasHeight = getComputedHeight(sectionDiv) - stats.cmActualHeight;
|
|
|
|
var cmMaxHeight = window.innerHeight - extrasHeight - sectionTop - stats.sectionMarginTop;
|
|
|
|
var cmDesiredHeight = cm.display.sizer.clientHeight + 2*cm.defaultTextHeight();
|
|
|
|
var cmGrantableHeight = Math.max(stats.cmActualHeight, Math.min(cmMaxHeight, cmDesiredHeight));
|
|
|
|
stats.deltas.push(cmGrantableHeight - stats.cmActualHeight);
|
|
|
|
stats.totalHeight += cmGrantableHeight + extrasHeight;
|
|
|
|
if (!isLast) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stats.totalHeight += stats.firstSectionTop;
|
|
|
|
if (stats.totalHeight <= window.innerHeight) {
|
|
|
|
editors.forEach(function(cm, index) {
|
|
|
|
cm.setSize(null, stats.deltas[index] + stats.cmActualHeight);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// scale heights to fill the gap between last section and bottom edge of the window
|
|
|
|
var sections = document.getElementById("sections");
|
|
|
|
var available = window.innerHeight - sections.getBoundingClientRect().bottom -
|
|
|
|
parseFloat(getComputedStyle(sections).marginBottom);
|
|
|
|
if (available <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var totalDelta = stats.deltas.reduce(function(sum, d) { return sum + d; }, 0);
|
|
|
|
var q = available / totalDelta;
|
|
|
|
var baseHeight = stats.cmActualHeight - stats.sectionMarginTop;
|
|
|
|
stats.deltas.forEach(function(delta, index) {
|
|
|
|
editors[index].setSize(null, baseHeight + Math.floor(q * delta));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
function updateTitle() {
|
2015-04-22 11:29:52 +00:00
|
|
|
var DIRTY_TITLE = "* $";
|
2015-03-14 11:51:41 +00:00
|
|
|
|
2015-03-21 13:24:45 +00:00
|
|
|
var name = document.getElementById("name").savedValue;
|
|
|
|
var clean = isCleanGlobal();
|
2015-03-14 11:51:41 +00:00
|
|
|
var title = styleId === null ? t("addStyleTitle") : t('editStyleTitle', [name]);
|
2015-03-21 13:24:45 +00:00
|
|
|
document.title = clean ? title : DIRTY_TITLE.replace("$", title);
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function validate() {
|
|
|
|
var name = document.getElementById("name").value;
|
|
|
|
if (name == "") {
|
|
|
|
return t("styleMissingName");
|
|
|
|
}
|
|
|
|
// validate the regexps
|
2015-03-21 13:24:45 +00:00
|
|
|
if (document.querySelectorAll(".applies-to-list").some(function(list) {
|
|
|
|
return list.childNodes.some(function(li) {
|
2015-07-11 14:35:04 +00:00
|
|
|
if (li.className == template.appliesToEverything.className) {
|
2015-01-30 17:05:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var valueElement = li.querySelector("[name=applies-value]");
|
2015-03-21 13:24:45 +00:00
|
|
|
var type = li.querySelector("[name=applies-type]").value;
|
|
|
|
var value = valueElement.value;
|
|
|
|
if (type && value) {
|
|
|
|
if (type == "regexp") {
|
2015-01-30 17:05:06 +00:00
|
|
|
try {
|
2015-03-21 13:24:45 +00:00
|
|
|
new RegExp(value);
|
2015-01-30 17:05:06 +00:00
|
|
|
} catch (ex) {
|
|
|
|
valueElement.focus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
})) {
|
|
|
|
return t("styleBadRegexp");
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function save() {
|
2015-08-02 15:11:15 +00:00
|
|
|
updateLintReport(null, 0);
|
|
|
|
|
2015-03-03 00:58:04 +00:00
|
|
|
// save the contents of the CodeMirror editors back into the textareas
|
|
|
|
for (var i=0; i < editors.length; i++) {
|
|
|
|
editors[i].save();
|
|
|
|
}
|
2015-01-30 17:05:06 +00:00
|
|
|
|
|
|
|
var error = validate();
|
|
|
|
if (error) {
|
|
|
|
alert(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var name = document.getElementById("name").value;
|
|
|
|
var enabled = document.getElementById("enabled").checked;
|
2017-03-20 01:58:55 +00:00
|
|
|
saveStyle({
|
2015-01-30 17:05:06 +00:00
|
|
|
id: styleId,
|
|
|
|
name: name,
|
|
|
|
enabled: enabled,
|
2017-03-20 01:58:55 +00:00
|
|
|
reason: 'editSave',
|
2015-07-16 19:31:03 +00:00
|
|
|
sections: getSectionsHashes()
|
2017-03-20 01:58:55 +00:00
|
|
|
})
|
|
|
|
.then(saveComplete);
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
2015-07-16 19:31:03 +00:00
|
|
|
function getSectionsHashes() {
|
2015-01-30 17:05:06 +00:00
|
|
|
var sections = [];
|
2015-07-16 19:31:03 +00:00
|
|
|
getSections().forEach(function(div) {
|
2015-03-21 13:24:45 +00:00
|
|
|
var meta = getMeta(div);
|
2016-02-02 06:18:23 +00:00
|
|
|
var code = div.CodeMirror.getValue();
|
2015-03-21 13:24:45 +00:00
|
|
|
if (/^\s*$/.test(code) && Object.keys(meta).length == 0) {
|
2015-01-30 17:05:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
meta.code = code;
|
|
|
|
sections.push(meta);
|
|
|
|
});
|
|
|
|
return sections;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMeta(e) {
|
2016-03-17 02:44:17 +00:00
|
|
|
var meta = {urls: [], urlPrefixes: [], domains: [], regexps: []};
|
2015-03-21 13:24:45 +00:00
|
|
|
e.querySelector(".applies-to-list").childNodes.forEach(function(li) {
|
2015-07-11 14:35:04 +00:00
|
|
|
if (li.className == template.appliesToEverything.className) {
|
2015-01-30 17:05:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-03-21 13:24:45 +00:00
|
|
|
var type = li.querySelector("[name=applies-type]").value;
|
|
|
|
var value = li.querySelector("[name=applies-value]").value;
|
|
|
|
if (type && value) {
|
|
|
|
var property = CssToProperty[type];
|
2016-03-17 02:44:17 +00:00
|
|
|
meta[property].push(value);
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveComplete(style) {
|
2015-03-14 11:51:41 +00:00
|
|
|
styleId = style.id;
|
2015-03-21 13:24:45 +00:00
|
|
|
setCleanGlobal();
|
2015-03-03 00:58:04 +00:00
|
|
|
|
2015-01-30 17:05:06 +00:00
|
|
|
// Go from new style URL to edit style URL
|
|
|
|
if (location.href.indexOf("id=") == -1) {
|
2015-05-27 19:02:36 +00:00
|
|
|
history.replaceState({}, document.title, "edit.html?id=" + style.id);
|
2015-07-19 16:49:17 +00:00
|
|
|
tE("heading", "editStyleHeading", null, false);
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
2015-05-27 19:02:36 +00:00
|
|
|
updateTitle();
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function showMozillaFormat() {
|
2015-07-13 17:44:46 +00:00
|
|
|
var popup = showCodeMirrorPopup(t("styleToMozillaFormatTitle"), "", {readOnly: true});
|
|
|
|
popup.codebox.setValue(toMozillaFormat());
|
|
|
|
popup.codebox.execCommand("selectAll");
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function toMozillaFormat() {
|
2015-07-16 19:31:03 +00:00
|
|
|
return getSectionsHashes().map(function(section) {
|
2015-01-30 17:05:06 +00:00
|
|
|
var cssMds = [];
|
|
|
|
for (var i in propertyToCss) {
|
2015-03-21 13:24:45 +00:00
|
|
|
if (section[i]) {
|
|
|
|
cssMds = cssMds.concat(section[i].map(function (v){
|
|
|
|
return propertyToCss[i] + "(\"" + v.replace(/\\/g, "\\\\") + "\")";
|
|
|
|
}));
|
|
|
|
}
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
2015-03-21 13:24:45 +00:00
|
|
|
return cssMds.length ? "@-moz-document " + cssMds.join(", ") + " {\n" + section.code + "\n}" : section.code;
|
2015-01-30 17:05:06 +00:00
|
|
|
}).join("\n\n");
|
|
|
|
}
|
|
|
|
|
2015-07-13 17:44:46 +00:00
|
|
|
function fromMozillaFormat() {
|
|
|
|
var popup = showCodeMirrorPopup(t("styleFromMozillaFormatPrompt"), tHTML("<div>\
|
|
|
|
<button name='import-append' i18n-text='importAppendLabel' i18n-title='importAppendTooltip'></button>\
|
|
|
|
<button name='import-replace' i18n-text='importReplaceLabel' i18n-title='importReplaceTooltip'></button>\
|
|
|
|
</div>").innerHTML);
|
|
|
|
|
|
|
|
var contents = popup.querySelector(".contents");
|
|
|
|
contents.insertBefore(popup.codebox.display.wrapper, contents.firstElementChild);
|
|
|
|
popup.codebox.focus();
|
|
|
|
|
|
|
|
popup.querySelector("[name='import-append']").addEventListener("click", doImport);
|
|
|
|
popup.querySelector("[name='import-replace']").addEventListener("click", doImport);
|
|
|
|
|
|
|
|
popup.codebox.on("change", function() {
|
|
|
|
clearTimeout(popup.mozillaTimeout);
|
|
|
|
popup.mozillaTimeout = setTimeout(function() {
|
|
|
|
popup.classList.toggle("ready", trimNewLines(popup.codebox.getValue()));
|
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
|
|
|
|
function doImport() {
|
|
|
|
var replaceOldStyle = this.name == "import-replace";
|
2017-04-01 02:50:03 +00:00
|
|
|
popup.querySelector(".dismiss").click();
|
2015-07-13 17:44:46 +00:00
|
|
|
var mozStyle = trimNewLines(popup.codebox.getValue());
|
2017-02-08 13:53:38 +00:00
|
|
|
var parser = new parserlib.css.Parser(), lines = mozStyle.split("\n");
|
2016-02-02 06:30:08 +00:00
|
|
|
var sectionStack = [{code: "", start: {line: 1, col: 1}}];
|
2015-07-13 17:44:46 +00:00
|
|
|
var errors = "", oldSectionCount = editors.length;
|
2016-02-02 06:30:08 +00:00
|
|
|
var firstAddedCM;
|
2015-07-13 17:44:46 +00:00
|
|
|
|
|
|
|
parser.addListener("startdocument", function(e) {
|
2016-02-02 06:30:08 +00:00
|
|
|
var outerText = getRange(sectionStack.last.start, (--e.col, e));
|
2015-07-13 17:44:46 +00:00
|
|
|
var gapComment = outerText.match(/(\/\*[\s\S]*?\*\/)[\s\n]*$/);
|
2017-02-08 13:53:38 +00:00
|
|
|
var section = {code: "", start: backtrackTo(this, parserlib.css.Tokens.LBRACE, "end")};
|
2015-07-13 17:44:46 +00:00
|
|
|
// move last comment before @-moz-document inside the section
|
|
|
|
if (gapComment && !gapComment[1].match(/\/\*\s*AGENT_SHEET\s*\*\//)) {
|
|
|
|
section.code = gapComment[1] + "\n";
|
|
|
|
outerText = trimNewLines(outerText.substring(0, gapComment.index));
|
|
|
|
}
|
2017-03-17 23:05:58 +00:00
|
|
|
if (outerText.trim()) {
|
2016-02-02 06:30:08 +00:00
|
|
|
sectionStack.last.code = outerText;
|
|
|
|
doAddSection(sectionStack.last);
|
|
|
|
sectionStack.last.code = "";
|
|
|
|
}
|
2015-07-13 17:44:46 +00:00
|
|
|
e.functions.forEach(function(f) {
|
|
|
|
var m = f.match(/^(url|url-prefix|domain|regexp)\((['"]?)(.+?)\2?\)$/);
|
|
|
|
var aType = CssToProperty[m[1]];
|
|
|
|
var aValue = aType != "regexps" ? m[3] : m[3].replace(/\\\\/g, "\\");
|
|
|
|
(section[aType] = section[aType] || []).push(aValue);
|
|
|
|
});
|
|
|
|
sectionStack.push(section);
|
|
|
|
});
|
|
|
|
|
|
|
|
parser.addListener("enddocument", function(e) {
|
2017-02-08 13:53:38 +00:00
|
|
|
var end = backtrackTo(this, parserlib.css.Tokens.RBRACE, "start");
|
2015-07-13 17:44:46 +00:00
|
|
|
var section = sectionStack.pop();
|
2016-02-02 06:30:08 +00:00
|
|
|
section.code += getRange(section.start, end);
|
|
|
|
sectionStack.last.start = (++end.col, end);
|
2015-07-13 17:44:46 +00:00
|
|
|
doAddSection(section);
|
|
|
|
});
|
|
|
|
|
|
|
|
parser.addListener("endstylesheet", function() {
|
2016-02-02 06:30:08 +00:00
|
|
|
// add nonclosed outer sections (either broken or the last global one)
|
|
|
|
var endOfText = {line: lines.length, col: lines.last.length + 1};
|
|
|
|
sectionStack.last.code += getRange(sectionStack.last.start, endOfText);
|
|
|
|
sectionStack.forEach(doAddSection);
|
2015-07-13 17:44:46 +00:00
|
|
|
|
|
|
|
delete maximizeCodeHeight.stats;
|
2016-02-02 06:30:08 +00:00
|
|
|
editors.forEach(function(cm) {
|
|
|
|
maximizeCodeHeight(cm.getSection(), cm == editors.last);
|
2015-07-13 17:44:46 +00:00
|
|
|
});
|
|
|
|
|
2016-02-02 06:30:08 +00:00
|
|
|
makeSectionVisible(firstAddedCM);
|
|
|
|
firstAddedCM.focus();
|
|
|
|
|
2015-07-13 17:44:46 +00:00
|
|
|
if (errors) {
|
|
|
|
showHelp(t("issues"), errors);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
parser.addListener("error", function(e) {
|
|
|
|
errors += e.line + ":" + e.col + " " + e.message.replace(/ at line \d.+$/, "") + "<br>";
|
|
|
|
});
|
|
|
|
|
|
|
|
parser.parse(mozStyle);
|
|
|
|
|
2017-03-17 23:05:58 +00:00
|
|
|
function getRange( start, end) {
|
|
|
|
const L1 = start.line - 1, C1 = start.col - 1;
|
|
|
|
const L2 = end.line - 1, C2 = end.col - 1;
|
|
|
|
if (L1 == L2) {
|
|
|
|
return lines[L1].substr(C1, C2 - C1 + 1);
|
2015-07-13 17:44:46 +00:00
|
|
|
} else {
|
2017-03-17 23:05:58 +00:00
|
|
|
const middle = lines.slice(L1 + 1, L2).join('\n');
|
|
|
|
return lines[L1].substr(C1) + '\n' + middle +
|
|
|
|
(L2 >= lines.length ? '' : ((middle ? '\n' : '') + lines[L2].substring(0, C2)));
|
2015-07-13 17:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function doAddSection(section) {
|
2016-02-02 06:30:08 +00:00
|
|
|
if (!firstAddedCM) {
|
|
|
|
if (!initFirstSection(section)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// don't add empty sections
|
|
|
|
if (!(section.code || section.urls || section.urlPrefixes || section.domains || section.regexps)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setCleanItem(addSection(null, section), false);
|
|
|
|
firstAddedCM = firstAddedCM || editors.last;
|
|
|
|
}
|
|
|
|
// do onetime housekeeping as the imported text is confirmed to be a valid style
|
|
|
|
function initFirstSection(section) {
|
|
|
|
// skip adding the first global section when there's no code/comments
|
|
|
|
if (!section.code.replace("@namespace url(http://www.w3.org/1999/xhtml);", "") /* ignore boilerplate NS */
|
|
|
|
.replace(/[\s\n]/g, "")) { /* ignore all whitespace including new lines */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (replaceOldStyle) {
|
2015-07-13 17:44:46 +00:00
|
|
|
editors.slice(0).reverse().forEach(function(cm) {
|
2016-02-02 06:18:23 +00:00
|
|
|
removeSection({target: cm.getSection().firstElementChild});
|
2015-07-13 17:44:46 +00:00
|
|
|
});
|
2016-02-02 06:30:08 +00:00
|
|
|
} else if (!editors.last.getValue()) {
|
|
|
|
// nuke the last blank section
|
|
|
|
if (editors.last.getSection().querySelector(".applies-to-everything")) {
|
|
|
|
removeSection({target: editors.last.getSection()});
|
|
|
|
}
|
2015-07-13 17:44:46 +00:00
|
|
|
}
|
2016-02-02 06:30:08 +00:00
|
|
|
return true;
|
2015-07-13 17:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function backtrackTo(parser, tokenType, startEnd) {
|
|
|
|
var tokens = parser._tokenStream._lt;
|
|
|
|
for (var i = tokens.length - 2; i >= 0; --i) {
|
|
|
|
if (tokens[i].type == tokenType) {
|
|
|
|
return {line: tokens[i][startEnd+"Line"], col: tokens[i][startEnd+"Col"]};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function trimNewLines(s) {
|
|
|
|
return s.replace(/^[\s\n]+/, "").replace(/[\s\n]+$/, "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-30 17:05:06 +00:00
|
|
|
function showSectionHelp() {
|
2015-04-26 12:34:59 +00:00
|
|
|
showHelp(t("styleSectionsTitle"), t("sectionHelp"));
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function showAppliesToHelp() {
|
2015-04-26 12:34:59 +00:00
|
|
|
showHelp(t("appliesLabel"), t("appliesHelp"));
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function showToMozillaHelp() {
|
2015-07-13 17:44:46 +00:00
|
|
|
showHelp(t("styleMozillaFormatHeading"), t("styleToMozillaFormatHelp"));
|
2015-04-26 12:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function showKeyMapHelp() {
|
2015-10-05 11:27:17 +00:00
|
|
|
var keyMap = mergeKeyMaps({}, prefs.get("editor.keyMap"), CodeMirror.defaults.extraKeys);
|
2015-04-26 12:34:59 +00:00
|
|
|
var keyMapSorted = Object.keys(keyMap)
|
|
|
|
.map(function(key) { return {key: key, cmd: keyMap[key]} })
|
2015-04-26 20:03:39 +00:00
|
|
|
.concat([{key: "Shift-Ctrl-Wheel", cmd: "scrollWindow"}])
|
2015-04-26 12:34:59 +00:00
|
|
|
.sort(function(a, b) { return a.cmd < b.cmd || (a.cmd == b.cmd && a.key < b.key) ? -1 : 1 });
|
2015-10-05 11:27:17 +00:00
|
|
|
showHelp(t("cm_keyMap") + ": " + prefs.get("editor.keyMap"),
|
2015-04-26 12:34:59 +00:00
|
|
|
'<table class="keymap-list">' +
|
2015-05-13 17:26:53 +00:00
|
|
|
'<thead><tr><th><input placeholder="' + t("helpKeyMapHotkey") + '" type="search"></th>' +
|
|
|
|
'<th><input placeholder="' + t("helpKeyMapCommand") + '" type="search"></th></tr></thead>' +
|
2015-04-26 12:34:59 +00:00
|
|
|
"<tbody>" + keyMapSorted.map(function(value) {
|
|
|
|
return "<tr><td>" + value.key + "</td><td>" + value.cmd + "</td></tr>";
|
|
|
|
}).join("") +
|
|
|
|
"</tbody>" +
|
|
|
|
"</table>");
|
2015-05-13 17:26:53 +00:00
|
|
|
|
|
|
|
var table = document.querySelector("#help-popup table");
|
|
|
|
table.addEventListener("input", filterTable);
|
|
|
|
|
|
|
|
var inputs = table.querySelectorAll("input");
|
|
|
|
inputs[0].addEventListener("keydown", hotkeyHandler);
|
|
|
|
inputs[1].focus();
|
|
|
|
|
|
|
|
function hotkeyHandler(event) {
|
|
|
|
var keyName = CodeMirror.keyName(event);
|
|
|
|
if (keyName == "Esc" || keyName == "Tab" || keyName == "Shift-Tab") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
// normalize order of modifiers,
|
|
|
|
// for modifier-only keys ("Ctrl-Shift") a dummy main key has to be temporarily added
|
|
|
|
var keyMap = {};
|
|
|
|
keyMap[keyName.replace(/(Shift|Ctrl|Alt|Cmd)$/, "$&-dummy")] = "";
|
|
|
|
var normalizedKey = Object.keys(CodeMirror.normalizeKeyMap(keyMap))[0];
|
|
|
|
this.value = normalizedKey.replace("-dummy", "");
|
|
|
|
filterTable(event);
|
|
|
|
}
|
|
|
|
function filterTable(event) {
|
2015-04-26 12:34:59 +00:00
|
|
|
var input = event.target;
|
2015-05-10 00:33:06 +00:00
|
|
|
var query = stringAsRegExp(input.value, "gi");
|
2015-04-26 12:34:59 +00:00
|
|
|
var col = input.parentNode.cellIndex;
|
2015-05-13 17:26:53 +00:00
|
|
|
inputs[1 - col].value = "";
|
|
|
|
table.tBodies[0].childNodes.forEach(function(row) {
|
2015-04-26 12:34:59 +00:00
|
|
|
var cell = row.children[col];
|
2015-05-13 17:26:53 +00:00
|
|
|
cell.innerHTML = cell.textContent.replace(query, "<mark>$&</mark>");
|
|
|
|
row.style.display = query.test(cell.textContent) ? "" : "none";
|
|
|
|
// clear highlight from the other column
|
|
|
|
cell = row.children[1 - col];
|
|
|
|
cell.innerHTML = cell.textContent;
|
2015-04-26 12:34:59 +00:00
|
|
|
});
|
2015-05-13 17:26:53 +00:00
|
|
|
}
|
2015-04-26 12:34:59 +00:00
|
|
|
function mergeKeyMaps(merged) {
|
|
|
|
[].slice.call(arguments, 1).forEach(function(keyMap) {
|
|
|
|
if (typeof keyMap == "string") {
|
|
|
|
keyMap = CodeMirror.keyMap[keyMap];
|
|
|
|
}
|
|
|
|
Object.keys(keyMap).forEach(function(key) {
|
|
|
|
var cmd = keyMap[key];
|
|
|
|
// filter out '...', 'attach', etc. (hotkeys start with an uppercase letter)
|
|
|
|
if (!merged[key] && !key.match(/^[a-z]/) && cmd != "...") {
|
|
|
|
if (typeof cmd == "function") {
|
|
|
|
// for 'emacs' keymap: provide at least something meaningful (hotkeys and the function body)
|
|
|
|
// for 'vim*' keymaps: almost nothing as it doesn't rely on CM keymap mechanism
|
|
|
|
cmd = cmd.toString().replace(/^function.*?\{[\s\r\n]*([\s\S]+?)[\s\r\n]*\}$/, "$1");
|
|
|
|
merged[key] = cmd.length <= 200 ? cmd : cmd.substr(0, 200) + "...";
|
|
|
|
} else {
|
|
|
|
merged[key] = cmd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (keyMap.fallthrough) {
|
|
|
|
merged = mergeKeyMaps(merged, keyMap.fallthrough);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return merged;
|
|
|
|
}
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 12:28:05 +00:00
|
|
|
function showLintHelp() {
|
|
|
|
showHelp(t("issues"), t("issuesHelp") + "<ul>" +
|
|
|
|
CSSLint.getRules().map(function(rule) {
|
|
|
|
return "<li><b>" + rule.name + "</b><br>" + rule.desc + "</li>";
|
2015-07-04 06:04:04 +00:00
|
|
|
}).join("") + "</ul>"
|
2015-05-22 12:28:05 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-26 12:34:59 +00:00
|
|
|
function showHelp(title, text) {
|
|
|
|
var div = document.getElementById("help-popup");
|
2015-08-28 16:41:44 +00:00
|
|
|
div.classList.remove("big");
|
2015-04-26 12:34:59 +00:00
|
|
|
div.querySelector(".contents").innerHTML = text;
|
|
|
|
div.querySelector(".title").innerHTML = title;
|
|
|
|
|
|
|
|
if (getComputedStyle(div).display == "none") {
|
|
|
|
document.addEventListener("keydown", closeHelp);
|
2017-04-01 02:50:03 +00:00
|
|
|
div.querySelector(".dismiss").onclick = closeHelp; // avoid chaining on multiple showHelp() calls
|
2015-04-26 12:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
div.style.display = "block";
|
2015-07-13 17:44:46 +00:00
|
|
|
return div;
|
2015-04-26 12:34:59 +00:00
|
|
|
|
|
|
|
function closeHelp(e) {
|
|
|
|
if (e.type == "click" || (e.keyCode == 27 && !e.altKey && !e.ctrlKey && !e.shiftKey && !e.metaKey)) {
|
|
|
|
div.style.display = "";
|
2015-07-13 17:44:46 +00:00
|
|
|
document.querySelector(".contents").innerHTML = "";
|
2015-04-26 12:34:59 +00:00
|
|
|
document.removeEventListener("keydown", closeHelp);
|
|
|
|
}
|
|
|
|
}
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 17:44:46 +00:00
|
|
|
function showCodeMirrorPopup(title, html, options) {
|
|
|
|
var popup = showHelp(title, html);
|
2015-08-28 16:41:44 +00:00
|
|
|
popup.classList.add("big");
|
2015-07-13 17:44:46 +00:00
|
|
|
|
2017-03-15 11:58:59 +00:00
|
|
|
popup.codebox = CodeMirror(popup.querySelector(".contents"), Object.assign({
|
2015-07-13 17:44:46 +00:00
|
|
|
mode: "css",
|
|
|
|
lineNumbers: true,
|
|
|
|
lineWrapping: true,
|
|
|
|
foldGutter: true,
|
|
|
|
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter", "CodeMirror-lint-markers"],
|
|
|
|
matchBrackets: true,
|
|
|
|
lint: {getAnnotations: CodeMirror.lint.css, delay: 0},
|
|
|
|
styleActiveLine: true,
|
2015-10-05 11:27:17 +00:00
|
|
|
theme: prefs.get("editor.theme"),
|
|
|
|
keyMap: prefs.get("editor.keyMap")
|
2015-10-05 11:12:24 +00:00
|
|
|
}, options));
|
2015-07-13 17:44:46 +00:00
|
|
|
popup.codebox.focus();
|
|
|
|
popup.codebox.on("focus", function() { hotkeyRerouter.setState(false) });
|
|
|
|
popup.codebox.on("blur", function() { hotkeyRerouter.setState(true) });
|
|
|
|
return popup;
|
|
|
|
}
|
|
|
|
|
2015-01-30 17:28:05 +00:00
|
|
|
function getParams() {
|
|
|
|
var params = {};
|
|
|
|
var urlParts = location.href.split("?", 2);
|
|
|
|
if (urlParts.length == 1) {
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
urlParts[1].split("&").forEach(function(keyValue) {
|
|
|
|
var splitKeyValue = keyValue.split("=", 2);
|
|
|
|
params[decodeURIComponent(splitKeyValue[0])] = decodeURIComponent(splitKeyValue[1]);
|
|
|
|
});
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2016-01-31 00:06:04 +00:00
|
|
|
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
|
2015-02-09 04:02:08 +00:00
|
|
|
switch (request.method) {
|
2015-01-30 17:05:06 +00:00
|
|
|
case "styleUpdated":
|
2017-03-20 01:58:55 +00:00
|
|
|
if (styleId && styleId == request.style.id && request.reason != 'editSave') {
|
|
|
|
initWithStyle(request);
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "styleDeleted":
|
2017-01-30 19:29:12 +00:00
|
|
|
if (styleId && styleId == request.id) {
|
|
|
|
window.onbeforeunload = function() {};
|
2015-01-30 17:05:06 +00:00
|
|
|
window.close();
|
|
|
|
break;
|
|
|
|
}
|
2015-02-09 04:02:08 +00:00
|
|
|
break;
|
|
|
|
case "prefChanged":
|
2015-03-08 06:21:43 +00:00
|
|
|
if (request.prefName == "editor.smartIndent") {
|
|
|
|
CodeMirror.setOption("smartIndent", request.value);
|
2015-02-09 04:02:08 +00:00
|
|
|
}
|
2015-01-30 17:05:06 +00:00
|
|
|
}
|
|
|
|
});
|
2015-04-23 01:53:30 +00:00
|
|
|
|
2015-05-24 23:03:10 +00:00
|
|
|
function getComputedHeight(el) {
|
|
|
|
var compStyle = getComputedStyle(el);
|
|
|
|
return el.getBoundingClientRect().height +
|
|
|
|
parseFloat(compStyle.marginTop) + parseFloat(compStyle.marginBottom);
|
|
|
|
}
|