From 4f89494052982d1263c81a6a99845228fd531b4e Mon Sep 17 00:00:00 2001 From: tophf Date: Sun, 8 Nov 2020 19:51:33 +0300 Subject: [PATCH] save/restore section heights --- background/update.js | 5 ++++- edit/sections-editor-section.js | 10 ++++++++-- edit/sections-editor.js | 11 +++++++---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/background/update.js b/background/update.js index 33c2022c..7a9112ee 100644 --- a/background/update.js +++ b/background/update.js @@ -203,7 +203,10 @@ json.id = style.id; json.updateDate = Date.now(); - + json.sections.forEach((sec, i) => { + const ls = (style.sections[i] || {}).localState; + if (ls) sec.localState = ls; + }); // keep current state delete json.enabled; diff --git a/edit/sections-editor-section.js b/edit/sections-editor-section.js index d1446881..ea5a0236 100644 --- a/edit/sections-editor-section.js +++ b/edit/sections-editor-section.js @@ -24,8 +24,9 @@ function createSection(originalSection, genId) { const el = template.section.cloneNode(true); const elLabel = $('.code-label', el); const cm = cmFactory.create(wrapper => { + const {height} = originalSection.localState || {}; // 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); }, { value: originalSection.code, @@ -57,7 +58,12 @@ function createSection(originalSection, genId) { appliesTo, getModel() { 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() { linter.disableForEditor(cm); diff --git a/edit/sections-editor.js b/edit/sections-editor.js index a2c07773..3774e064 100644 --- a/edit/sections-editor.js +++ b/edit/sections-editor.js @@ -171,8 +171,10 @@ function SectionsEditor() { const lastSectionBottom = sections[sections.length - 1].el.getBoundingClientRect().bottom; const delta = Math.floor((window.innerHeight - lastSectionBottom) / sections.length); if (delta > 1) { - sections.forEach(({cm}) => { - cm.setSize(null, cm.display.lastWrapHeight + delta); + sections.forEach(({cm}, i) => { + 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); refreshOnView(cm, forceRefresh); registerEvents(section); - if (!base || init.code) { - // Fit a) during startup or b) when the clone button is clicked on a section with some code + /* Fit a) when the clone button is clicked on a section with non-empty code + or b) during startup if height wasn't saved in storage */ + if (base ? init.code : !(init.localState || {}).height) { fitToContent(section); } if (base) {