diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2025-01-22 22:14:33 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2025-01-22 22:14:33 +0300 |
| commit | f0687060d772b511d3002f8077fb3b74876c58c5 (patch) | |
| tree | fac4630c90a5b640b81782f3547f07d38ea67401 /plugins/shorten_expanded/init.js | |
| parent | a071edaa9d79ea646d6c36bb8b26de087d14f795 (diff) | |
shorten_expanded: add simple event debounce
Diffstat (limited to 'plugins/shorten_expanded/init.js')
| -rw-r--r-- | plugins/shorten_expanded/init.js | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/plugins/shorten_expanded/init.js b/plugins/shorten_expanded/init.js index 85d75d313..fd816b52a 100644 --- a/plugins/shorten_expanded/init.js +++ b/plugins/shorten_expanded/init.js @@ -2,13 +2,16 @@ Plugins.Shorten_Expanded = { threshold: 1.5, // of window height - observer: new ResizeObserver((entries) => { - entries.forEach((entry) => { - const row = entry.target; - - Plugins.Shorten_Expanded.shorten_if_needed(row); - }); - }), + debounce: (callback, wait) => { + let timeoutId = null; + return (...args) => { + window.clearTimeout(timeoutId); + timeoutId = window.setTimeout(() => { + callback(...args); + }, wait); + }; + }, + observer: false, shorten_if_needed: function(row) { const content = row.querySelector(".content"); @@ -63,10 +66,20 @@ Plugins.Shorten_Expanded = { } } -require(['dojo/_base/kernel', 'dojo/ready'], function (dojo, ready) { - ready(function() { +require(['dojo/_base/kernel', 'dojo/ready'], (dojo, ready) => { + ready(() => { + const self = Plugins.Shorten_Expanded; + + self.observer = new ResizeObserver(self.debounce((entries) => { + entries.forEach((entry) => { + const row = entry.target; + + self.shorten_if_needed(row); + }); + })), + PluginHost.register(PluginHost.HOOK_ARTICLE_RENDERED_CDM, function(row) { - Plugins.Shorten_Expanded.process_row(row); + self.process_row(row); return true; }); }); |