DEV: Use headerOffset from core

This commit is contained in:
Penar Musaraj 2022-01-09 17:32:36 -05:00
parent 5b2f5a455e
commit f4b335d154
No known key found for this signature in database
GPG Key ID: E390435D881FF0F7
1 changed files with 7 additions and 17 deletions

View File

@ -1,5 +1,5 @@
<script type="text/discourse-plugin" version="0.1"> <script type="text/discourse-plugin" version="0.1">
const minimumOffset = require("discourse/lib/offset-calculator").minimumOffset; const headerOffset = require("discourse/lib/offset-calculator").headerOffset;
const { iconHTML } = require("discourse-common/lib/icon-library"); const { iconHTML } = require("discourse-common/lib/icon-library");
const { run } = Ember; const { run } = Ember;
@ -137,7 +137,11 @@
self.triggerShowHide(elem); self.triggerShowHide(elem);
} }
self.scrollTo($(this)); const target = document.querySelector(`[data-d-toc="${this.getAttribute("data-d-toc")}"]`);
window.scrollTo({
behavior: "smooth",
top: `${target.getBoundingClientRect().top + window.scrollY - headerOffset() - 10}`
});
}); });
$("#main").on( $("#main").on(
@ -157,7 +161,6 @@
$("html, body") $("html, body")
.promise() .promise()
.done(function () { .done(function () {
const winScrollTop = $(window).scrollTop();
const anchors = $(self.options.cooked).find("[data-d-toc]"); const anchors = $(self.options.cooked).find("[data-d-toc]");
let closestAnchorDistance = null; let closestAnchorDistance = null;
@ -165,7 +168,7 @@
anchors.each(function (idx) { anchors.each(function (idx) {
const distance = Math.abs( const distance = Math.abs(
$(this).offset().top - minimumOffset() - winScrollTop $(this).offset().top - headerOffset() - window.scrollY
); );
if ( if (
closestAnchorDistance == null || closestAnchorDistance == null ||
@ -219,19 +222,6 @@
return elem.slideDown(TOC_ANIMATION_SPEED); return elem.slideDown(TOC_ANIMATION_SPEED);
}, },
scrollTo: function (elem) {
const currentDiv = $(`[data-d-toc="${elem.attr("data-d-toc")}"]`);
$("html, body").animate(
{
scrollTop: `${currentDiv.offset().top - minimumOffset()}`
},
{
duration: SMOOTH_SCROLL_SPEED
}
);
},
setOptions: () => { setOptions: () => {
$.Widget.prototype._setOptions.apply(this, arguments); $.Widget.prototype._setOptions.apply(this, arguments);
} }