Skip to content

Commit

Permalink
Merge branch 'hotfix/5.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
cotes2020 committed Jan 7, 2022
1 parent a41b491 commit cdc6a18
Show file tree
Hide file tree
Showing 15 changed files with 146 additions and 118 deletions.
3 changes: 2 additions & 1 deletion _javascript/commons/topbar-switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

$(function() {
const $topbarWrapper = $("#topbar-wrapper");
const $topbarTitle = $("#topbar-title");
const $panel = $("#panel-wrapper");
const $searchInput = $("#search-input");

Expand Down Expand Up @@ -54,7 +55,7 @@ $(function() {
}

$(window).scroll(function(event) {
if ($("#topbar-title").is(":hidden")) {
if ($topbarTitle.is(":hidden")) {
didScroll = true;
}
});
Expand Down
65 changes: 40 additions & 25 deletions _javascript/commons/topbar-title.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,61 @@
/*
* Top bar title auto change while scrolling in mobile screens.
* Top bar title auto change while scrolling up/down in mobile screens.
*/

$(function() {
const titleSelector = "div.post>h1:first-of-type";
const $pageTitle = $(titleSelector);
const $topbarTitle = $("#topbar-title");

if ($pageTitle.length === 0 /* on Home page */
|| $pageTitle.hasClass("dynamic-title")
|| $topbarTitle.is(":hidden")) {/* not in mobile views */
return;
}

const topbarTitle = $("#topbar-title");
const postTitle = $("div.post>h1");

const DEFAULT = topbarTitle.text().trim();

let title = (postTitle.length > 0) ?
postTitle.text().trim() : $("h1").text().trim();
const defaultTitleText = $topbarTitle.text().trim();
let titleText = $pageTitle.text().trim();
let hasScrolled = false;
let lastScrollTop = 0;

if ($("#page-category").length || $("#page-tag").length) {
/* The title in Category or Tag page will be "<title> <count_of_posts>" */
if (/\s/.test(title)) {
title = title.replace(/[0-9]/g, "").trim();
if (/\s/.test(titleText)) {
titleText = titleText.replace(/[0-9]/g, "").trim();
}
}

/* Replace topbar title while scroll screens. */
$(window).scroll(function () {
if ($("#post-list").length /* in Home page */
|| postTitle.is(":hidden") /* is tab pages */
|| topbarTitle.is(":hidden") /* not mobile screens */
|| $("#sidebar.sidebar-expand").length) { /* when the sidebar trigger is clicked */
return false;
let options = {
rootMargin: '-48px 0px 0px 0px', // 48px equals to the topbar height (3rem)
threshold: [0, 1]
};

let observer = new IntersectionObserver((entries) => {
if (!hasScrolled) {
hasScrolled = true;
return;
}

if ($(this).scrollTop() >= 95) {
if (topbarTitle.text() !== title) {
topbarTitle.text(title);
let curScrollTop = $(window).scrollTop();
let isScrollDown = lastScrollTop < curScrollTop;
lastScrollTop = curScrollTop;
let heading = entries[0];

if (isScrollDown) {
if (heading.intersectionRatio === 0) {
$topbarTitle.text(titleText);
}
} else {
if (topbarTitle.text() !== DEFAULT) {
topbarTitle.text(DEFAULT);
if (heading.intersectionRatio === 1) {
$topbarTitle.text(defaultTitleText);
}
}
});
}, options);

observer.observe(document.querySelector(titleSelector));

/* Click title remove hover effect. */
topbarTitle.click(function() {
/* Click title will scroll to top */
$topbarTitle.click(function() {
$("body,html").animate({scrollTop: 0}, 800);
});

Expand Down
2 changes: 1 addition & 1 deletion _javascript/copyright
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Chirpy v5.0.1 (https://github.com/cotes2020/jekyll-theme-chirpy/)
* Chirpy v5.0.2 (https://github.com/cotes2020/jekyll-theme-chirpy/)
* © 2019 Cotes Chung
* MIT Licensed
*/
155 changes: 83 additions & 72 deletions _javascript/utils/smooth-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,81 +21,92 @@ $(function() {
.not("[href='#']")
.not("[href='#0']")
.click(function(event) {
if (this.pathname.replace(/^\//, "") === location.pathname.replace(/^\//, "")) {
if (location.hostname === this.hostname) {
const hash = decodeURI(this.hash);
let toFootnoteRef = RegExp(/^#fnref:/).test(hash);
let toFootnote = toFootnoteRef? false : RegExp(/^#fn:/).test(hash);
let selector = hash.includes(":") ? hash.replace(/\:/g, "\\:") : hash;
let $target = $(selector);

let parent = $(this).parent().prop("tagName");
let isAnchor = RegExp(/^H\d/).test(parent);
let isMobileViews = !$topbarTitle.is(":hidden");

if (typeof $target !== "undefined") {
event.preventDefault();

if (history.pushState) { /* add hash to URL */
history.pushState(null, null, hash);
}

let curOffset = isAnchor? $(this).offset().top : $(window).scrollTop();
let destOffset = $target.offset().top -= REM / 2;

if (destOffset < curOffset) { // scroll up
if (!isAnchor && !toFootnote) { // trigger by ToC item
if (!isMobileViews) { // on desktop/tablet screens
$topbarWrapper.removeClass("topbar-down").addClass("topbar-up");
// Send message to `${JS_ROOT}/commons/topbar-switch.js`
$topbarWrapper.attr(ATTR_TOC_SCROLLING, true);
tocScrollUpCount += 1;
}
}

if ((isAnchor || toFootnoteRef) && isMobileViews) {
destOffset -= topbarHeight;
}
}

$("html").animate({
scrollTop: destOffset
}, 500, () => {
$target.focus();

/* clean up old scroll mark */
if ($(`[${SCROLL_MARK}=true]`).length) {
$(`[${SCROLL_MARK}=true]`).attr(SCROLL_MARK, false);
}

/* Clean :target links */
if ($(":target").length) { /* element that visited by the URL with hash */
$(":target").attr(SCROLL_MARK, false);
}

/* set scroll mark to footnotes */
if (toFootnote || toFootnoteRef) {
$target.attr(SCROLL_MARK, true);
}

if ($target.is(":focus")) { /* Checking if the target was focused */
return false;
} else {
$target.attr("tabindex", "-1"); /* Adding tabindex for elements not focusable */
$target.focus(); /* Set focus again */
}

if (typeof $topbarWrapper.attr(ATTR_TOC_SCROLLING) !== "undefined") {
tocScrollUpCount -= 1;

if (tocScrollUpCount <= 0) {
$topbarWrapper.attr(ATTR_TOC_SCROLLING, "false");
}
}
});

if (this.pathname.replace(/^\//, "") !== location.pathname.replace(/^\//, "")) {
return;
}

if (location.hostname !== this.hostname) {
return;
}

const hash = decodeURI(this.hash);
let toFootnoteRef = RegExp(/^#fnref:/).test(hash);
let toFootnote = toFootnoteRef ? false : RegExp(/^#fn:/).test(hash);
let selector = hash.includes(":") ? hash.replace(/\:/g, "\\:") : hash;
let $target = $(selector);

let parent = $(this).parent().prop("tagName");
let isAnchor = RegExp(/^H\d/).test(parent);
let isMobileViews = !$topbarTitle.is(":hidden");

if (typeof $target === "undefined") {
return;
}

event.preventDefault();

if (history.pushState) { /* add hash to URL */
history.pushState(null, null, hash);
}

let curOffset = isAnchor ? $(this).offset().top : $(window).scrollTop();
let destOffset = $target.offset().top -= REM / 2;

if (destOffset < curOffset) { // scroll up
if (!isAnchor && !toFootnote) { // trigger by ToC item
if (!isMobileViews) { // on desktop/tablet screens
$topbarWrapper.removeClass("topbar-down").addClass("topbar-up");
// Send message to `${JS_ROOT}/commons/topbar-switch.js`
$topbarWrapper.attr(ATTR_TOC_SCROLLING, true);
tocScrollUpCount += 1;
}
}

if ((isAnchor || toFootnoteRef) && isMobileViews) {
destOffset -= topbarHeight;
}

} else {
if (isMobileViews) {
destOffset -= topbarHeight;
}
}

$("html").animate({
scrollTop: destOffset
}, 500, () => {
$target.focus();

/* clean up old scroll mark */
if ($(`[${SCROLL_MARK}=true]`).length) {
$(`[${SCROLL_MARK}=true]`).attr(SCROLL_MARK, false);
}

/* Clean :target links */
if ($(":target").length) { /* element that visited by the URL with hash */
$(":target").attr(SCROLL_MARK, false);
}

/* set scroll mark to footnotes */
if (toFootnote || toFootnoteRef) {
$target.attr(SCROLL_MARK, true);
}

if ($target.is(":focus")) { /* Checking if the target was focused */
return false;
} else {
$target.attr("tabindex", "-1"); /* Adding tabindex for elements not focusable */
$target.focus(); /* Set focus again */
}

if (typeof $topbarWrapper.attr(ATTR_TOC_SCROLLING) !== "undefined") {
tocScrollUpCount -= 1;

if (tocScrollUpCount <= 0) {
$topbarWrapper.attr(ATTR_TOC_SCROLLING, "false");
}
}
});
}); /* click() */
});
8 changes: 4 additions & 4 deletions _sass/addon/commons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,10 @@ $sidebar-display: "sidebar-display";
min-width: 150px;
}

#search-hints {
display: none;
}

#search-result-wrapper {
margin-top: 3rem;
}
Expand Down Expand Up @@ -1492,10 +1496,6 @@ $sidebar-display: "sidebar-display";
}
}

#search-hints {
display: none;
}

.post-content {
font-size: 1.03rem;
}
Expand Down
2 changes: 1 addition & 1 deletion _sass/jekyll-theme-chirpy.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
* The styles for Jekyll theme Chirpy
*
* Chirpy v5.0.1 (https://github.com/cotes2020/jekyll-theme-chirpy)
* Chirpy v5.0.2 (https://github.com/cotes2020/jekyll-theme-chirpy)
* © 2019 Cotes Chung
* MIT Licensed
*/
Expand Down
4 changes: 2 additions & 2 deletions assets/js/dist/categories.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cdc6a18

Please sign in to comment.