account for header in compact mode when fitting to content

This commit is contained in:
tophf 2020-10-11 13:40:13 +03:00
parent 3fe3129a55
commit f54a9eab29

View File

@ -64,6 +64,7 @@ function createSectionsEditor({style, onTitleChanged}) {
xo.observe(cm.display.wrapper); xo.observe(cm.display.wrapper);
let sectionOrder = ''; let sectionOrder = '';
let headerOffset; // in compact mode the header is at the top so it reduces the available height
const initializing = initSections(style.sections.slice()); const initializing = initSections(style.sections.slice());
const livePreview = createLivePreview(); const livePreview = createLivePreview();
@ -90,22 +91,26 @@ function createSectionsEditor({style, onTitleChanged}) {
}; };
function fitToContent(section) { function fitToContent(section) {
if (section.cm.display.renderedView) { const {cm, cm: {display: {wrapper, sizer}}} = section;
if (cm.display.renderedView) {
resize(); resize();
} else { } else {
section.cm.on('update', resize); cm.on('update', resize);
} }
function resize() { function resize() {
let contentHeight = section.cm.display.sizer.offsetHeight; let contentHeight = sizer.offsetHeight;
if (contentHeight < section.cm.defaultTextHeight()) { if (contentHeight < cm.defaultTextHeight()) {
return; return;
} }
if (headerOffset == null) {
headerOffset = wrapper.getBoundingClientRect().top;
}
contentHeight += 9; // border & resize grip contentHeight += 9; // border & resize grip
section.cm.off('update', resize); cm.off('update', resize);
const cmHeight = section.cm.display.wrapper.offsetHeight; const cmHeight = wrapper.offsetHeight;
const maxHeight = window.innerHeight - (section.el.offsetHeight - cmHeight); const maxHeight = (window.innerHeight - headerOffset) - (section.el.offsetHeight - cmHeight);
section.cm.setSize(null, Math.min(contentHeight, maxHeight)); cm.setSize(null, Math.min(contentHeight, maxHeight));
} }
} }