-
Notifications
You must be signed in to change notification settings - Fork 238
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
fix #662: 使用方向键翻页 #1118
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
})(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我这个比你的功能多一些。除了文章翻页,首页也能翻页,还添加了提示框。没提示框估计读者也不知道能翻页。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的,我抽空看一下。如果能整合到一个脚本中就好了,别人可以复用。谢谢。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可是不同 Hugo 主题里面那些元素的类名不一样,咋复用?
There was a problem hiding this comment.
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
就是这段没看明白在干啥。