Add: auto resize section editor on init

This commit is contained in:
eight 2019-06-18 01:12:19 +08:00
parent 514fa3204f
commit f0ab852a14
3 changed files with 38 additions and 2 deletions

View File

@ -311,6 +311,7 @@ input:invalid {
} }
.section .CodeMirror { .section .CodeMirror {
margin-bottom: .875rem; margin-bottom: .875rem;
box-sizing: border-box;
} }
/* deleted section */ /* deleted section */
.deleted-section { .deleted-section {

View File

@ -10,6 +10,7 @@ CodeMirror.defineExtension('refreshOnView', function () {
const cm = this; const cm = this;
if (typeof IntersectionObserver === 'undefined') { if (typeof IntersectionObserver === 'undefined') {
// uh // uh
cm.isRefreshed = true;
cm.refresh(); cm.refresh();
return; return;
} }
@ -18,6 +19,7 @@ CodeMirror.defineExtension('refreshOnView', function () {
for (const entry of entries) { for (const entry of entries) {
if (entry.isIntersecting) { if (entry.isIntersecting) {
// wrapper.style.visibility = 'visible'; // wrapper.style.visibility = 'visible';
cm.isRefreshed = true;
cm.refresh(); cm.refresh();
observer.disconnect(); observer.disconnect();
} }

View File

@ -48,12 +48,11 @@ function createSectionsEditor({style, onTitleChanged}) {
const initializing = new Promise(resolve => initSection({ const initializing = new Promise(resolve => initSection({
sections: style.sections.slice(), sections: style.sections.slice(),
done:() => { done:() => {
// FIXME: implement this with CSS?
// https://github.com/openstyles/stylus/commit/2895ce11e271788df0e4f7314b3b981fde086574
dirty.clear(); dirty.clear();
rerouteHotkeys(true); rerouteHotkeys(true);
resolve(); resolve();
updateHeader(); updateHeader();
sections.forEach(fitToContent);
} }
})); }));
@ -80,6 +79,40 @@ function createSectionsEditor({style, onTitleChanged}) {
getSearchableInputs, getSearchableInputs,
}; };
function fitToContent(section) {
if (section.cm.isRefreshed) {
resize();
} else {
section.cm.on('update', resize);
}
function resize() {
let contentHeight = section.el.querySelector('.CodeMirror-sizer').offsetHeight;
if (contentHeight < section.cm.defaultTextHeight()) {
return;
}
contentHeight += 9; // border & resize grip
section.cm.off('update', resize);
const cmHeight = section.cm.getWrapperElement().offsetHeight;
const maxHeight = cmHeight + window.innerHeight - section.el.offsetHeight;
section.cm.setSize(null, Math.min(contentHeight, maxHeight));
if (sections.every(s => s.cm.isRefreshed)) {
fitToAvailableSpace();
}
}
}
function fitToAvailableSpace() {
const available = window.innerHeight - container.offsetHeight;
if (available <= 0) {
return;
}
for (const section of sections) {
const cmHeight = section.cm.getWrapperElement().offsetHeight;
section.cm.setSize(null, cmHeight + Math.floor(available / sections.length));
}
}
function genId() { function genId() {
return INC_ID++; return INC_ID++;
} }