Editor: better Undo in CSS-Beautify

This commit is contained in:
tophf 2015-07-04 09:36:56 +03:00
parent be8552dcf6
commit 0cb7936fd3

39
edit.js
View File

@ -828,7 +828,6 @@ function gotoLintIssue(event) {
} }
function beautify(event) { function beautify(event) {
var undoCount = 1;
if (exports.css_beautify) { // thanks to csslint's definition of 'exports' if (exports.css_beautify) { // thanks to csslint's definition of 'exports'
doBeautify(); doBeautify();
} else { } else {
@ -844,15 +843,6 @@ function beautify(event) {
var section = querySelectorParent(event.target, "#sections > div"); var section = querySelectorParent(event.target, "#sections > div");
var scope = section ? [getCodeMirrorForSection(section)] : editors; var scope = section ? [getCodeMirrorForSection(section)] : editors;
scope.forEach(function(cm) {
setTimeout(function() {
var text = cm.getValue();
var newText = exports.css_beautify(text, options);
if (newText != text) {
cm.setValue(newText);
}
}, 0);
});
showHelp(t("styleBeautify"), "<div class='beautify-options'>" + showHelp(t("styleBeautify"), "<div class='beautify-options'>" +
optionHtml(".selector1,", "selector_separator_newline") + optionHtml(".selector1,", "selector_separator_newline") +
@ -867,8 +857,31 @@ function beautify(event) {
var undoButton = document.querySelector("#help-popup button[role='undo']"); var undoButton = document.querySelector("#help-popup button[role='undo']");
undoButton.textContent = t(scope.length == 1 ? "undo" : "undoGlobal"); undoButton.textContent = t(scope.length == 1 ? "undo" : "undoGlobal");
undoButton.addEventListener("click", function() { undoButton.addEventListener("click", function() {
scope.forEach(CodeMirror.commands.undo); var undoable = false;
undoButton.disabled = --undoCount == 0; 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);
}); });
document.querySelector(".beautify-options").addEventListener("change", function(event) { document.querySelector(".beautify-options").addEventListener("change", function(event) {
@ -877,8 +890,6 @@ function beautify(event) {
prefs.setPref("editor.beautify", options); prefs.setPref("editor.beautify", options);
event.target.parentNode.setAttribute("newline", value.toString()); event.target.parentNode.setAttribute("newline", value.toString());
doBeautify(); doBeautify();
undoCount++;
undoButton.disabled = false;
}); });
function optionHtml(label, optionName, indent) { function optionHtml(label, optionName, indent) {