Skip to content

Commit

Permalink
Simplify auto scrolling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tztsai committed Nov 10, 2024
1 parent 9fb046f commit 3523d09
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
2 changes: 1 addition & 1 deletion content/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ details summary {
h1, h2, h3, h4, h5, h6 {
display: inline;
}
blockquote {
p {
font-weight: normal;
font-style: italic;
opacity: 0.8;
Expand Down
18 changes: 6 additions & 12 deletions content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,10 @@ var focusOnDetails = (details, scroll = 'follow') => {
}

scrollLock = true;
isDownward = true;
isNewSection = false;
details.open = true;

const rect1 = details.getBoundingClientRect();
while (focusedDetails && !focusedDetails.contains(details)) {
if (focusedDetails.getBoundingClientRect().top > rect1.top)
isDownward = false;
if (details.firstChild.firstChild < focusedDetails.firstChild.firstChild)
isNewSection = true;
focusedDetails.open = false;
focusedDetails = focusedDetails.parentElement.closest('details');
}
Expand All @@ -207,13 +201,13 @@ var focusOnDetails = (details, scroll = 'follow') => {
const rect2 = details.getBoundingClientRect();

// keep the top of the section at the same position
dy = rect2.top - rect1.top;
if (isNewSection && isDownward) {
// dy += rect2.height + 30;
// focusedDetails = details.nextElementSibling;
var dy = 0;
if (rect2.top < rect1.top && details.firstChild.firstChild.tagName === 'H2') {
dy += rect2.bottom - rect1.top - 30;
focusedDetails = details.lastChild.lastChild;
}
else if (isDownward) { // move towards the end of the block
dy += Math.max(Math.min(rect1.top, rect1.height - 50), 0);
else if (rect2.bottom > window.innerHeight) {
dy += Math.min(rect2.top, rect2.bottom - window.innerHeight);
}

if (Math.abs(dy) > 200) {
Expand Down
5 changes: 2 additions & 3 deletions content/mdwise.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function writeSummary(details, txt) {
} else {
var summary = details.querySelector('summary');

const p = document.createElement('blockquote');
const p = document.createElement('p');
p.textContent = txt.trim();
summary.appendChild(p);

Expand Down Expand Up @@ -107,8 +107,7 @@ function writeSummary(details, txt) {
});

createInput();
// m.redraw();
update();
update(); // render the new content
}
});
return input;
Expand Down

0 comments on commit 3523d09

Please sign in to comment.