Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #662: 使用方向键翻页 #1118

Merged
merged 7 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions layouts/partials/head_custom.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<script async src="//yihui.name/js/no-highlight.js"></script>
<script async src="{{ relURL "/js/pangu.js" }}"></script>
<script src="https://cdn.jsdelivr.net/npm/@xiee/utils/js/heading-anchor.min.js" defer></script>
<script async src="{{ relURL "/js/shortcuts.js" }}"></script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<script async src="{{ relURL "/js/shortcuts.js" }}"></script>
<script src="https://cdn.jsdelivr.net/npm/@xiee/utils/js/post-nav.min.js" defer></script>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我这个比你的功能多一些。除了文章翻页,首页也能翻页,还添加了提示框。没提示框估计读者也不知道能翻页。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,我抽空看一下。如果能整合到一个脚本中就好了,别人可以复用。谢谢。

Copy link
Contributor Author

@CyrusYip CyrusYip Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可是不同 Hugo 主题里面那些元素的类名不一样,咋复用?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yihui 我已经把你代码抄得差不多了,应该没问题了。

https://github.com/yihui/misc.js/blob/83b2aea9132d522ff8260df6df8ebdc874fad62d/js/post-nav.js#L15-L21

就是这段没看明白在干啥。


<script type="text/x-mathjax-config">
MathJax.Hub.Config({
Expand Down
58 changes: 58 additions & 0 deletions static/js/shortcuts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Press arrow keys to turn a page
(() => {
document.addEventListener("keydown", (event) => {
// Don't turn a page inside a code block
if (document.activeElement !== document.body) {
return;
}
let currentPage, previousPageLink, nextPageLink;
// Check if current page is homepage or post
// Code will fail on Chrome if this statement is not included inside AddEventListener and the user quickly press keys
if (document.querySelector(".article-list")) {
// homepage
currentPage = document.querySelector(".page-item.active");
previousPageLink = currentPage.previousElementSibling
.querySelector(".page-link")
.getAttribute("href");
nextPageLink = currentPage.nextElementSibling
.querySelector(".page-link")
.getAttribute("href");
} else if (document.querySelector("article")) {
// post
previousPageLink = document
.querySelector(".nav-prev > a")
.getAttribute("href");
nextPageLink = document
.querySelector(".nav-next > a")
.getAttribute("href");
} else {
return;
}
let targetLink;
// Ignore if modifier keys are active
if (event.ctrlKey || event.altKey || event.shiftKey || event.metaKey) {
return;
}
if (event.key === "ArrowLeft") {
previousPageLink && (targetLink = previousPageLink);
} else if (event.key === "ArrowRight") {
nextPageLink && (targetLink = nextPageLink);
}
targetLink && window.location.assign(targetLink);
});

// Add tooltips
const hint = "按左右键可翻页";
if (document.querySelector(".article-list")) {
document.querySelector(".pagination").setAttribute("title", hint);
} else if (document.querySelector("article")) {
document.querySelector(".nav-prev > a").setAttribute("title", hint);
document.querySelector(".nav-next > a").setAttribute("title", hint);
}

// make <pre> focusable
const preElements = document.querySelectorAll("pre");
preElements.forEach((element) => {
element.setAttribute("tabindex", "0");
});
Comment on lines +53 to +57
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tabindex 应该加到主题,此处代码只是临时使用。

})();