Simplify getCodeMirrorForSection/getSectionForCodeMirror

This commit is contained in:
tophf 2016-02-02 08:18:23 +02:00
parent ddb6f400e7
commit 426b4d37ba

59
edit.js
View File

@ -84,9 +84,9 @@ function setCleanItem(node, isClean) {
if (isClean) { if (isClean) {
delete dirty[node.id]; delete dirty[node.id];
// A div would indicate a section // code sections have .CodeMirror property
if (node.nodeName.toLowerCase() == "div") { if (node.CodeMirror) {
node.savedValue = getCodeMirrorForSection(node).changeGeneration(); node.savedValue = node.CodeMirror.changeGeneration();
} else { } else {
node.savedValue = "checkbox" === node.type ? node.checked : node.value; node.savedValue = "checkbox" === node.type ? node.checked : node.value;
} }
@ -113,7 +113,7 @@ function setCleanSection(section) {
section.querySelectorAll(".style-contributor").forEach(function(node) { setCleanItem(node, true) }); section.querySelectorAll(".style-contributor").forEach(function(node) { setCleanItem(node, true) });
// #header section has no codemirror // #header section has no codemirror
var cm = getCodeMirrorForSection(section) var cm = section.CodeMirror;
if (cm) { if (cm) {
section.savedValue = cm.changeGeneration(); section.savedValue = cm.changeGeneration();
indicateCodeChange(cm); indicateCodeChange(cm);
@ -219,6 +219,10 @@ function initCodeMirror() {
}); });
} }
CM.prototype.getSection = function() {
return this.display.wrapper.parentNode;
}
// preload the theme so that CodeMirror can calculate its metrics in DOMContentLoaded->setupLivePrefs() // preload the theme so that CodeMirror can calculate its metrics in DOMContentLoaded->setupLivePrefs()
var theme = prefs.get("editor.theme"); var theme = prefs.get("editor.theme");
document.getElementById("cm-theme").href = theme == "default" ? "" : "codemirror/theme/" + theme + ".css"; document.getElementById("cm-theme").href = theme == "default" ? "" : "codemirror/theme/" + theme + ".css";
@ -352,16 +356,12 @@ function setupCodeMirror(textarea, index) {
} }
function indicateCodeChange(cm) { function indicateCodeChange(cm) {
var section = getSectionForCodeMirror(cm); var section = cm.getSection();
setCleanItem(section, cm.isClean(section.savedValue)); setCleanItem(section, cm.isClean(section.savedValue));
updateTitle(); updateTitle();
updateLintReport(cm); updateLintReport(cm);
} }
function getSectionForCodeMirror(cm) {
return cm.display.wrapper.parentNode;
}
function getSectionForChild(e) { function getSectionForChild(e) {
return e.closest("#sections > div"); return e.closest("#sections > div");
} }
@ -370,12 +370,6 @@ function getSections() {
return document.querySelectorAll("#sections > div"); return document.querySelectorAll("#sections > div");
} }
function getCodeMirrorForSection(section) {
// #header section has no codemirror
var wrapper = section.querySelector(".CodeMirror");
return wrapper && wrapper.CodeMirror;
}
// remind Chrome to repaint a previously invisible editor box by toggling any element's transform // 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) // this bug is present in some versions of Chrome (v37-40 or something)
document.addEventListener("scroll", function(event) { document.addEventListener("scroll", function(event) {
@ -499,7 +493,7 @@ function addSection(event, section) {
var sections = document.getElementById("sections"); var sections = document.getElementById("sections");
if (event) { if (event) {
var clickedSection = event.target.parentNode; var clickedSection = getSectionForChild(event.target);
sections.insertBefore(div, clickedSection.nextElementSibling); sections.insertBefore(div, clickedSection.nextElementSibling);
var newIndex = getSections().indexOf(clickedSection) + 1; var newIndex = getSections().indexOf(clickedSection) + 1;
var cm = setupCodeMirror(codeElement, newIndex); var cm = setupCodeMirror(codeElement, newIndex);
@ -508,9 +502,10 @@ function addSection(event, section) {
renderLintReport(); renderLintReport();
} else { } else {
sections.appendChild(div); sections.appendChild(div);
setupCodeMirror(codeElement); var cm = setupCodeMirror(codeElement);
} }
div.CodeMirror = cm;
setCleanSection(div); setCleanSection(div);
return div; return div;
} }
@ -525,8 +520,8 @@ function removeAppliesTo(event) {
} }
function removeSection(event) { function removeSection(event) {
var section = event.target.parentNode; var section = getSectionForChild(event.target);
var cm = getCodeMirrorForSection(section); var cm = section.CodeMirror;
removeAreaAndSetDirty(section); removeAreaAndSetDirty(section);
editors.splice(editors.indexOf(cm), 1); editors.splice(editors.indexOf(cm), 1);
renderLintReport(); renderLintReport();
@ -549,7 +544,7 @@ function removeAreaAndSetDirty(area) {
} }
function makeSectionVisible(cm) { function makeSectionVisible(cm) {
var section = getSectionForCodeMirror(cm); var section = cm.getSection();
var bounds = section.getBoundingClientRect(); var bounds = section.getBoundingClientRect();
if ((bounds.bottom > window.innerHeight && bounds.top > 0) || (bounds.top < 0 && bounds.bottom < window.innerHeight)) { if ((bounds.bottom > window.innerHeight && bounds.top > 0) || (bounds.top < 0 && bounds.bottom < window.innerHeight)) {
if (bounds.top < 0) { if (bounds.top < 0) {
@ -680,7 +675,7 @@ function setupGlobalSearch() {
originalCommand[reverse ? "findPrev" : "findNext"](activeCM); originalCommand[reverse ? "findPrev" : "findNext"](activeCM);
function searchAppliesTo(cm) { function searchAppliesTo(cm) {
var inputs = [].slice.call(getSectionForCodeMirror(cm).querySelectorAll(".applies-value")); var inputs = [].slice.call(cm.getSection().querySelectorAll(".applies-value"));
if (reverse) { if (reverse) {
inputs = inputs.reverse(); inputs = inputs.reverse();
} }
@ -799,7 +794,7 @@ function jumpToLine(cm) {
} }
function refocusMinidialog(cm) { function refocusMinidialog(cm) {
var section = getSectionForCodeMirror(cm); var section = cm.getSection();
if (!section.querySelector(".CodeMirror-dialog")) { if (!section.querySelector(".CodeMirror-dialog")) {
return; return;
} }
@ -821,7 +816,7 @@ function getEditorInSight(nearbyElement) {
// priority: 1. associated CM for applies-to element 2. last active if visible 3. first visible // priority: 1. associated CM for applies-to element 2. last active if visible 3. first visible
var cm; var cm;
if (nearbyElement && nearbyElement.className.indexOf("applies-") >= 0) { if (nearbyElement && nearbyElement.className.indexOf("applies-") >= 0) {
cm = getCodeMirrorForSection(getSectionForChild(nearbyElement)); cm = getSectionForChild(nearbyElement).CodeMirror;
} else { } else {
cm = editors.lastActive; cm = editors.lastActive;
} }
@ -838,7 +833,7 @@ function getEditorInSight(nearbyElement) {
function offscreenDistance(cm) { function offscreenDistance(cm) {
var LINES_VISIBLE = 2; // closest editor should have at least # lines visible var LINES_VISIBLE = 2; // closest editor should have at least # lines visible
var bounds = getSectionForCodeMirror(cm).getBoundingClientRect(); var bounds = cm.getSection().getBoundingClientRect();
if (bounds.top < 0) { if (bounds.top < 0) {
return -bounds.top; return -bounds.top;
} else if (bounds.top < window.innerHeight - cm.defaultTextHeight() * LINES_VISIBLE) { } else if (bounds.top < window.innerHeight - cm.defaultTextHeight() * LINES_VISIBLE) {
@ -994,7 +989,7 @@ function beautify(event) {
options.indent_char = tabs ? "\t" : " "; options.indent_char = tabs ? "\t" : " ";
var section = getSectionForChild(event.target); var section = getSectionForChild(event.target);
var scope = section ? [getCodeMirrorForSection(section)] : editors; var scope = section ? [section.CodeMirror] : editors;
showHelp(t("styleBeautify"), "<div class='beautify-options'>" + showHelp(t("styleBeautify"), "<div class='beautify-options'>" +
optionHtml(".selector1,", "selector_separator_newline") + optionHtml(".selector1,", "selector_separator_newline") +
@ -1114,7 +1109,7 @@ function initWithStyle(style) {
function add() { function add() {
var sectionDiv = addSection(null, queue.shift()); var sectionDiv = addSection(null, queue.shift());
maximizeCodeHeight(sectionDiv, !queue.length); maximizeCodeHeight(sectionDiv, !queue.length);
updateLintReport(getCodeMirrorForSection(sectionDiv), prefs.get("editor.lintDelay")); updateLintReport(sectionDiv.CodeMirror, prefs.get("editor.lintDelay"));
} }
} }
@ -1141,7 +1136,7 @@ function initHooks() {
} }
function maximizeCodeHeight(sectionDiv, isLast) { function maximizeCodeHeight(sectionDiv, isLast) {
var cm = getCodeMirrorForSection(sectionDiv); var cm = sectionDiv.CodeMirror;
var stats = maximizeCodeHeight.stats = maximizeCodeHeight.stats || {totalHeight: 0, deltas: []}; var stats = maximizeCodeHeight.stats = maximizeCodeHeight.stats || {totalHeight: 0, deltas: []};
if (!stats.cmActualHeight) { if (!stats.cmActualHeight) {
stats.cmActualHeight = getComputedHeight(cm.display.wrapper); stats.cmActualHeight = getComputedHeight(cm.display.wrapper);
@ -1254,7 +1249,7 @@ function getSectionsHashes() {
var sections = []; var sections = [];
getSections().forEach(function(div) { getSections().forEach(function(div) {
var meta = getMeta(div); var meta = getMeta(div);
var code = getCodeMirrorForSection(div).getValue(); var code = div.CodeMirror.getValue();
if (/^\s*$/.test(code) && Object.keys(meta).length == 0) { if (/^\s*$/.test(code) && Object.keys(meta).length == 0) {
return; return;
} }
@ -1373,7 +1368,7 @@ function fromMozillaFormat() {
if (!replaceOldStyle) { if (!replaceOldStyle) {
var lastOldCM = editors[oldSectionCount - 1]; var lastOldCM = editors[oldSectionCount - 1];
var lastOldSection = getSectionForCodeMirror(lastOldCM); var lastOldSection = lastOldCM.getSection();
var addAfter = {target: lastOldSection.querySelector(".add-section")}; var addAfter = {target: lastOldSection.querySelector(".add-section")};
if (oldSectionCount < editors.length if (oldSectionCount < editors.length
@ -1383,7 +1378,7 @@ function fromMozillaFormat() {
oldSectionCount--; oldSectionCount--;
} }
} else { } else {
var addAfter = {target: getSectionForCodeMirror(editors[0]).previousElementSibling.firstElementChild}; var addAfter = {target: editors[0].getSection().previousElementSibling.firstElementChild};
} }
var globalSection = sectionStack[0]; var globalSection = sectionStack[0];
@ -1399,7 +1394,7 @@ function fromMozillaFormat() {
delete maximizeCodeHeight.stats; delete maximizeCodeHeight.stats;
editors.forEach(function(cm, i) { editors.forEach(function(cm, i) {
maximizeCodeHeight(getSectionForCodeMirror(cm), i == editors.length - 1); maximizeCodeHeight(cm.getSection(), i == editors.length - 1);
}); });
makeSectionVisible(editors[oldSectionCount]); makeSectionVisible(editors[oldSectionCount]);
@ -1428,7 +1423,7 @@ function fromMozillaFormat() {
if (replaceOldStyle && oldSectionCount > 0) { if (replaceOldStyle && oldSectionCount > 0) {
oldSectionCount = 0; oldSectionCount = 0;
editors.slice(0).reverse().forEach(function(cm) { editors.slice(0).reverse().forEach(function(cm) {
removeSection({target: getSectionForCodeMirror(cm).firstElementChild}); removeSection({target: cm.getSection().firstElementChild});
}); });
} }
setCleanItem(addSection(null, section), false); setCleanItem(addSection(null, section), false);