From 9e8f15b2bd518eaf3fd975f8485a58ecc4b92d03 Mon Sep 17 00:00:00 2001 From: tophf Date: Thu, 30 Dec 2021 00:41:49 +0300 Subject: [PATCH] sync header width in opened editors --- edit/header-resizer.js | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/edit/header-resizer.js b/edit/header-resizer.js index 910caf80..bcdbd722 100644 --- a/edit/header-resizer.js +++ b/edit/header-resizer.js @@ -4,12 +4,16 @@ 'use strict'; (function HeaderResizer() { + const PREF_ID = 'editor.headerWidth'; const el = $('#header-resizer'); let lastW, lastX; - + prefs.subscribe(PREF_ID, (key, val) => { + setLastWidth(); + setWidth(val); + }); el.onmousedown = e => { if (e.button) return; - lastW = $('#header').clientWidth; + setLastWidth(); lastX = e.clientX; document.body.classList.add('resizing-h'); document.on('mousemove', resize); @@ -17,15 +21,11 @@ }; function resize({clientX: x}) { - const w = editor.updateHeaderWidth(lastW + x - lastX); - const delta = w - lastW; - if (!delta) return; + const w = setWidth(lastW + x - lastX); + if (!w) return; lastW = w; lastX = x; - prefs.set('editor.headerWidth', w); - for (const el of $$('.CodeMirror-linewidget[style*="width:"]')) { - el.style.width = parseFloat(el.style.width) - delta + 'px'; - } + prefs.set(PREF_ID, w); } function resizeStop() { @@ -33,4 +33,18 @@ document.off('mousemove', resize); document.body.classList.remove('resizing-h'); } + + function setLastWidth() { + lastW = $('#header').clientWidth; + } + + /** @returns {number|void} new width in case it's different, otherwise void */ + function setWidth(w) { + const delta = (w = editor.updateHeaderWidth(w)) - lastW; + if (!delta) return; + for (const el of $$('.CodeMirror-linewidget[style*="width:"]')) { + el.style.width = parseFloat(el.style.width) - delta + 'px'; + } + return w; + } })();