save/restore section heights

This commit is contained in:
tophf 2020-11-08 19:51:33 +03:00
parent a94969e47d
commit 4f89494052
3 changed files with 19 additions and 7 deletions

View File

@ -203,7 +203,10 @@
json.id = style.id; json.id = style.id;
json.updateDate = Date.now(); json.updateDate = Date.now();
json.sections.forEach((sec, i) => {
const ls = (style.sections[i] || {}).localState;
if (ls) sec.localState = ls;
});
// keep current state // keep current state
delete json.enabled; delete json.enabled;

View File

@ -24,8 +24,9 @@ function createSection(originalSection, genId) {
const el = template.section.cloneNode(true); const el = template.section.cloneNode(true);
const elLabel = $('.code-label', el); const elLabel = $('.code-label', el);
const cm = cmFactory.create(wrapper => { const cm = cmFactory.create(wrapper => {
const {height} = originalSection.localState || {};
// making it tall during initial load so IntersectionObserver sees only one adjacent CM // making it tall during initial load so IntersectionObserver sees only one adjacent CM
wrapper.style.height = '100vh'; wrapper.style.height = height ? height + 'px' : '100vh';
elLabel.after(wrapper); elLabel.after(wrapper);
}, { }, {
value: originalSection.code, value: originalSection.code,
@ -57,7 +58,12 @@ function createSection(originalSection, genId) {
appliesTo, appliesTo,
getModel() { getModel() {
const items = appliesTo.map(a => !a.all && [a.type, a.value]); const items = appliesTo.map(a => !a.all && [a.type, a.value]);
return DocFuncMapper.toSection(items, {code: cm.getValue()}); const height = Number(cm.display.wrapper.style.height.match(/[0-9.]+(?=px)|$/)[0]) || 0;
return DocFuncMapper.toSection(items, Object.assign({
code: cm.getValue(),
}, height && {
localState: {height},
}));
}, },
remove() { remove() {
linter.disableForEditor(cm); linter.disableForEditor(cm);

View File

@ -171,8 +171,10 @@ function SectionsEditor() {
const lastSectionBottom = sections[sections.length - 1].el.getBoundingClientRect().bottom; const lastSectionBottom = sections[sections.length - 1].el.getBoundingClientRect().bottom;
const delta = Math.floor((window.innerHeight - lastSectionBottom) / sections.length); const delta = Math.floor((window.innerHeight - lastSectionBottom) / sections.length);
if (delta > 1) { if (delta > 1) {
sections.forEach(({cm}) => { sections.forEach(({cm}, i) => {
cm.setSize(null, cm.display.lastWrapHeight + delta); if (!(style.sections[i].localState || {}).height) {
cm.setSize(null, cm.display.wrapper.offsetHeight + delta);
}
}); });
} }
} }
@ -575,8 +577,9 @@ function SectionsEditor() {
container.insertBefore(section.el, base ? base.el.nextSibling : null); container.insertBefore(section.el, base ? base.el.nextSibling : null);
refreshOnView(cm, forceRefresh); refreshOnView(cm, forceRefresh);
registerEvents(section); registerEvents(section);
if (!base || init.code) { /* Fit a) when the clone button is clicked on a section with non-empty code
// Fit a) during startup or b) when the clone button is clicked on a section with some code or b) during startup if height wasn't saved in storage */
if (base ? init.code : !(init.localState || {}).height) {
fitToContent(section); fitToContent(section);
} }
if (base) { if (base) {