diff options
author | 0neGal <mail@0negal.com> | 2024-06-25 12:04:08 +0200 |
---|---|---|
committer | 0neGal <mail@0negal.com> | 2024-06-25 12:04:08 +0200 |
commit | a7ae187a21e15a7377cf83f91a11f49030c7b1f7 (patch) | |
tree | ad3d39348116edcaee9451803598cb33c25113e9 | |
parent | 25adf3d3369526ad393d4086ad2c82963bc02345 (diff) | |
download | Viper-a7ae187a21e15a7377cf83f91a11f49030c7b1f7.tar.gz Viper-a7ae187a21e15a7377cf83f91a11f49030c7b1f7.zip |
update selection bounds automatically
If a selected element has its bounds changed, so should the
`#selection`, this makes it so it now does that.
-rw-r--r-- | src/app/js/navigate.js | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/src/app/js/navigate.js b/src/app/js/navigate.js index b54bbfd..18c32b3 100644 --- a/src/app/js/navigate.js +++ b/src/app/js/navigate.js @@ -32,11 +32,9 @@ navigate.selection = (new_selection) => { return; } - // this adds space between the `selection_el` and `` let padding = 8; - // attempt to get the border radius of `active_el` let radius = getComputedStyle(active_el).borderRadius; @@ -66,6 +64,46 @@ navigate.selection = (new_selection) => { active_bounds.height + (padding * 2) + "px"; } +// data from the last iterations of the interval below +let last_sel = { + el: false, + bounds: false +} + +// auto update `#selection` if `.active-selection` changes bounds, but +// not element by itself +setInterval(() => { + // get active selection + let selected = document.querySelector(".active-selection"); + + // if there's no active selection, reset `last_sel` + if (! selected) { + last_sel.el = false; + last_sel.bounds = false; + + return; + } + + // get stringified bounds + let bounds = JSON.stringify(selected.getBoundingClientRect()); + + // if `last_sel.el` is not `selected` the selected element was + // changed, so we just set `last_el` and nothing more + if (last_sel.el != selected) { + last_sel.el = selected; + last_sel.bounds = bounds; + + return; + } + + // if stringified bounds changed we update `#selection` + if (bounds != last_sel.bounds) { + navigate.selection(); + last_sel.el = selected; + last_sel.bounds = bounds; + } +}, 50) + // these events cause the `#selection` element to reposition itself window.addEventListener("resize", () => {navigate.selection()}, true); window.addEventListener("scroll", () => {navigate.selection()}, true); |