From 55538f808d583e0f85c6d1ab912c537b5ca9e15f Mon Sep 17 00:00:00 2001 From: tophf Date: Thu, 12 Mar 2015 13:17:59 +0300 Subject: [PATCH] Edit style: allow adding sections midway, autofocus on manual add --- edit.html | 4 ---- edit.js | 22 ++++++++++++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/edit.html b/edit.html index 07d2537d..16588c1f 100644 --- a/edit.html +++ b/edit.html @@ -134,10 +134,6 @@ display: none; } #sections > div .add-section { - display: none; - } - #sections > div:last-of-type .add-section { - display: inline; margin-left: 0.4em; } .applies-to img { diff --git a/edit.js b/edit.js index 6b526039..b69031fe 100644 --- a/edit.js +++ b/edit.js @@ -171,11 +171,11 @@ function addAppliesTo(list, name, value) { list.appendChild(e); } -function addSection(section) { +function addSection(event, section) { var div = sectionTemplate.cloneNode(true); div.querySelector(".applies-to-help").addEventListener("click", showAppliesToHelp, false); div.querySelector(".remove-section").addEventListener("click", removeSection, false); - div.querySelector(".add-section").addEventListener("click", function() {addSection()}, false); + div.querySelector(".add-section").addEventListener("click", addSection, false); var appliesTo = div.querySelector(".applies-to-list"); @@ -211,8 +211,18 @@ function addSection(section) { } var sections = document.getElementById("sections"); - sections.appendChild(div); - setupCodeMirror(div.querySelector('.code')); + var section = event ? event.target.parentNode : null; + if (event && section.nextElementSibling) { + sections.insertBefore(div, section.nextElementSibling); + } else { + sections.appendChild(div); + } + var cm = setupCodeMirror(div.querySelector('.code')); + if (section) { + var index = Array.prototype.indexOf.call(sections.children, section); + editors.splice(index, 0, editors.pop()); + cm.focus(); + } } function removeAppliesTo(event) { @@ -334,7 +344,7 @@ function init() { } else if (params["url-prefix"]) { section.urlPrefixes = [params["url-prefix"]]; } - addSection(section); + addSection(null, section); // default to enabled document.getElementById("enabled").checked = true document.title = t("addStyleTitle"); @@ -359,7 +369,7 @@ function initWithStyle(style) { Array.prototype.forEach.call(document.querySelectorAll("#sections > div"), function(div) { div.parentNode.removeChild(div); }); - style.sections.forEach(addSection); + style.sections.forEach(function(section) { addSection(null, section) }); setupGlobalSearch(); }