-
-
Notifications
You must be signed in to change notification settings - Fork 133
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
feat(plugin/page_vote): add page_vote
plugin
#983
Open
Liumingxun
wants to merge
6
commits into
ArtalkJS:master
Choose a base branch
from
Liumingxun:page-vote
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
81bf6de
feat(plugin/page_vote): add `page_vote` plugin
Liumingxun d991186
feat(plugin/page_vote): update vote count text
Liumingxun 8d1f055
feat(plugin/page_vote): reuse actions btn style
Liumingxun 60e9eb7
perf(plugin/page-vote): make type strong
Liumingxun 6a4e9c0
feat: watch `darkMode` conf
Liumingxun e9c4730
doc(plugin/page_vote): add config doc
Liumingxun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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,75 @@ | ||
import ActionBtn from '../components/action-btn' | ||
import type { ArtalkPlugin, ContextApi } from '@/types' | ||
import $t from '@/i18n' | ||
|
||
interface PageVoteOptions { | ||
pageKey: string | ||
vote: boolean | ||
voteDown: boolean | ||
btnEl: string | ||
} | ||
|
||
export const PageVoteWidget: ArtalkPlugin = (ctx) => { | ||
ctx.watchConf(['pageKey', 'pageVote', 'darkMode'], (conf) => { | ||
if (!conf.pageVote) return | ||
|
||
const defaultOptions = { | ||
pageKey: conf.pageKey, | ||
vote: true, | ||
voteDown: false, | ||
btnEl: '.artalk-page-vote', | ||
} | ||
|
||
const pageVoteConfig = typeof conf.pageVote === 'object' ? conf.pageVote : {} | ||
initPageVoteWidget(ctx, { ...defaultOptions, ...pageVoteConfig }) | ||
}) | ||
} | ||
|
||
function initPageVoteWidget(ctx: ContextApi, options: PageVoteOptions) { | ||
const btnContainer = document.querySelector<HTMLElement>(options.btnEl) | ||
if (!btnContainer) return // throw Error(`page vote's config \`btnEl\` selector ${options.btnEl} not found`) | ||
btnContainer.classList.add('atk-page-vote') | ||
if (ctx.getConf().darkMode) btnContainer.classList.add('atk-dark-mode') | ||
else btnContainer.classList.remove('atk-dark-mode') | ||
|
||
const api = ctx.getApi() | ||
|
||
ctx.on('list-fetched', ({ data }) => { | ||
if (!data) return | ||
|
||
const voteUpBtn = new ActionBtn(() => `${$t('voteUp')} (${data.page.vote_up || 0})`).appendTo(btnContainer) | ||
const voteDownBtn = options.voteDown | ||
? new ActionBtn(() => `${$t('voteDown')} (${data.page.vote_down || 0})`).appendTo(btnContainer) | ||
: null | ||
|
||
voteUpBtn.setClick(() => { | ||
api.votes.vote('page_up', data.page.id, { | ||
...api.getUserFields() | ||
}).then(({ data: { up, down } }) => { | ||
data.page.vote_up = up | ||
data.page.vote_down = down | ||
voteUpBtn.updateText() | ||
voteDownBtn?.updateText() | ||
}).catch((err) => { | ||
voteUpBtn.setError($t('voteFail')) | ||
console.error(err) | ||
}) | ||
}) | ||
|
||
if (voteDownBtn) { | ||
voteDownBtn.setClick(() => { | ||
api.votes.vote('page_down', data.page.id, { | ||
...api.getUserFields() | ||
}).then(({ data: { up, down } }) => { | ||
data.page.vote_up = up | ||
data.page.vote_down = down | ||
voteUpBtn.updateText() | ||
voteDownBtn.updateText() | ||
}).catch((err) => { | ||
voteDownBtn.setError($t('voteFail')) | ||
console.error(err) | ||
}) | ||
}) | ||
} | ||
}) | ||
} |
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. extract this style for reuse |
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,21 @@ | ||
%action-btn { | ||
color: var(--at-color-meta); | ||
font-size: 13px; | ||
line-height: 25px; | ||
display: inline-flex; | ||
cursor: pointer; | ||
user-select: none; | ||
|
||
&.atk-error, | ||
&.atk-error:hover { | ||
color: var(--at-color-red); | ||
} | ||
|
||
&:not(:last-child):not(.atk-hide) { | ||
margin-right: 16px; | ||
} | ||
|
||
&:hover { | ||
color: var(--at-color-deep); | ||
} | ||
} |
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
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
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.
to avoid test failed, it skips when enable page vote but there is no element to render, is this ok?