Merge pull request 'Rotate scale comments by reading time' (#39) from feature/comment-auto-interval into main
CI / Deploy / test (push) Successful in 57s
CI / Deploy / deploy (push) Successful in 7s

Reviewed-on: #39
This commit was merged in pull request #39.
This commit is contained in:
gnilebein committed 2026-11-09 14:11:23 +01:00
commit db22863608
6 files changed
+68 -25

No files matched your search

+38 -15
View File
@@ -297,7 +297,7 @@
* so position, pause and timer live here and survive the re-render.
*/
var Comments = {
intervalMs: 8000,
seconds: 0,
key: null,
index: 0,
paused: false,
@@ -305,9 +305,15 @@
items: [],
node: null,
// Automatic interval: time to read the comment on the stage, within sensible bounds.
AUTO_BASE_MS: 2500,
AUTO_CHARS_PER_SECOND: 15,
AUTO_MIN_MS: 5000,
AUTO_MAX_MS: 30000,
/**
* @param {Array} items comments, oldest first
* @param {number} [seconds] time per comment (slide setting), default 8
* @param {number} [seconds] time per comment (slide setting), 0 = depending on its length
*/
render: function (items, seconds) {
// A different first comment means a different slide (or a reset): start over.
@@ -317,11 +323,11 @@
Comments.index = 0;
Comments.paused = false;
}
var intervalMs = seconds > 0 ? seconds * 1000 : 8000;
if (intervalMs !== Comments.intervalMs) {
seconds = seconds > 0 ? seconds : 0;
if (seconds !== Comments.seconds) {
// Setting changed (or another slide): apply it right away.
Comments.intervalMs = intervalMs;
clearInterval(Comments.timer);
Comments.seconds = seconds;
clearTimeout(Comments.timer);
Comments.timer = null;
}
Comments.items = items;
@@ -357,8 +363,9 @@
Comments.paused = !Comments.paused;
} else {
Comments.step(action === 'next' ? 1 : -1);
Comments.restart();
}
// The comment shown now gets its full reading time.
Comments.restart();
Comments.update(false);
});
Comments.node = node;
@@ -374,21 +381,37 @@
Comments.index = (Comments.index + delta + n) % n;
},
/**
* How long a comment stays on the stage: the fixed setting, or (automatic)
* a base time plus reading time for its length, between AUTO_MIN_MS and AUTO_MAX_MS.
*
* @param {Object} item comment
* @return {number} milliseconds
*/
delay: function (item) {
if (Comments.seconds > 0) {
return Comments.seconds * 1000;
}
var length = item && item.text ? Array.from(item.text).length : 0;
var ms = Comments.AUTO_BASE_MS + length / Comments.AUTO_CHARS_PER_SECOND * 1000;
return Math.round(Math.min(Comments.AUTO_MAX_MS, Math.max(Comments.AUTO_MIN_MS, ms)));
},
/** (Re)start the countdown for the comment currently shown. */
restart: function () {
clearInterval(Comments.timer);
Comments.timer = setInterval(function () {
clearTimeout(Comments.timer);
Comments.timer = setTimeout(function () {
if (!Comments.node || !document.body.contains(Comments.node)) {
// Slide changed: stop until the next comment list is rendered.
clearInterval(Comments.timer);
Comments.timer = null;
return;
}
if (Comments.paused || Comments.items.length < 2 || document.hidden) {
return;
if (!Comments.paused && Comments.items.length > 1 && !document.hidden) {
Comments.step(1);
Comments.update(true);
}
Comments.step(1);
Comments.update(true);
}, Comments.intervalMs);
Comments.restart();
}, Comments.delay(Comments.items[Comments.index]));
},
update: function (animate) {