-
-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui/goto): improved solution of navigating to comment via link ha…
- Loading branch information
Showing
6 changed files
with
61 additions
and
107 deletions.
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
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,41 @@ | ||
import type { ArtalkPlugin } from '@/types' | ||
import * as Utils from '@/lib/utils' | ||
|
||
export const GotoDispatcher: ArtalkPlugin = (ctx) => { | ||
let lastID = 0 | ||
const check = ({ locker }: { locker: boolean }) => { | ||
const commentID = extractCommentID() | ||
if (!commentID) return | ||
|
||
if (locker && lastID === commentID) return // if the commentID is the same as the last one, do nothing | ||
lastID = commentID // record the last commentID | ||
|
||
ctx.trigger('list-goto', commentID) // trigger event | ||
} | ||
|
||
const hashChangeHandler = () => check({ locker: false }) | ||
const listLoadedHandler = () => check({ locker: true }) | ||
ctx.on('mounted', () => { | ||
window.addEventListener('hashchange', hashChangeHandler) | ||
ctx.on('list-loaded', listLoadedHandler) | ||
}) | ||
ctx.on('unmounted', () => { | ||
window.removeEventListener('hashchange', hashChangeHandler) | ||
ctx.off('list-loaded', listLoadedHandler) | ||
}) | ||
} | ||
|
||
function extractCommentID(): number|null { | ||
// Try get from hash | ||
// Hash retrieval priority is higher than query, | ||
// Because click goto will change hash. | ||
const match = window.location.hash.match(/#atk-comment-([0-9]+)/) | ||
let commentId = match && match[1] && !Number.isNaN(parseFloat(match[1])) ? parseFloat(match[1]) : null | ||
|
||
// Fail over to get from query | ||
if (!commentId) { | ||
commentId = Number(Utils.getQueryParam('atk_comment')) // same as backend GetReplyLink() | ||
} | ||
|
||
return commentId || null | ||
} |
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,17 @@ | ||
import type { ArtalkPlugin } from '@/types' | ||
|
||
export const GotoFocus: ArtalkPlugin = (ctx) => { | ||
ctx.on('list-goto', async (commentID) => { | ||
// find the comment node | ||
let comment = ctx.getCommentNodes().find(c => c.getID() === commentID) | ||
if (!comment) { | ||
// fetch and insert the comment from the server | ||
const data = (await ctx.getApi().comments.getComment(commentID)).data | ||
ctx.get('list').getListLayout({ forceFlatMode: true }).insert(data.comment, data.reply_comment) | ||
comment = ctx.getCommentNodes().find(c => c.getID() === commentID) | ||
} | ||
if (!comment) return | ||
comment.focus() | ||
}) | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
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