Editor: better async adding of sections in initWithStyle

This commit is contained in:
tophf 2015-06-22 21:18:33 +03:00
parent fa6b0f4477
commit 5caab7ab47

17
edit.js
View File

@ -925,11 +925,20 @@ function initWithStyle(style) {
document.querySelectorAll("#sections > div").forEach(function(div) { document.querySelectorAll("#sections > div").forEach(function(div) {
div.parentNode.removeChild(div); div.parentNode.removeChild(div);
}); });
(style.sections.length == 0 ? [{code: ""}] : style.sections).forEach(function(section) { var queue = style.sections.length ? style.sections : [{code: ""}];
setTimeout(function() { var queueStart = new Date().getTime();
maximizeCodeHeight(addSection(null, section), editors.length == style.sections.length); // after 200ms the sections will be added asynchronously
while (new Date().getTime() - queueStart <= 200 && queue.length) {
maximizeCodeHeight(addSection(null, queue.shift()), !queue.length);
}
if (queue.length) {
setTimeout(function processQueue() {
maximizeCodeHeight(addSection(null, queue.shift()), !queue.length);
if (queue.length) {
setTimeout(processQueue, 0);
}
}, 0); }, 0);
}); }
initHooks(); initHooks();
} }