-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin/page_vote): add
page_vote
plugin
- Loading branch information
1 parent
8707f4f
commit 81bf6de
Showing
5 changed files
with
78 additions
and
0 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
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,61 @@ | ||
import ActionBtn from '../components/action-btn' | ||
import type { ArtalkPlugin, ContextApi } from '@/types' | ||
import { Api } from '@/api' | ||
import $t from '@/i18n' | ||
|
||
export interface PageVoteOptions { | ||
getApi(): Api | ||
|
||
pageKey: string | ||
vote: boolean | ||
voteDown: boolean | ||
btnEl: string | ||
el: string | ||
} | ||
|
||
export const PageVoteWidget: ArtalkPlugin = (ctx) => { | ||
ctx.watchConf(['pageKey', 'pageVote'], (conf) => { | ||
const defaultOptions = { | ||
getApi: () => ctx.getApi(), | ||
pageKey: conf.pageKey, | ||
vote: true, | ||
voteDown: false, | ||
btnEl: '.artalk-page-vote', | ||
el: '.artalk-page-vote-count', | ||
} | ||
|
||
if (!conf.pageVote) return | ||
const pageVoteConfig = typeof conf.pageVote === 'object' ? conf.pageVote : {} | ||
initPageVoteWidget(ctx, { ...defaultOptions, ...pageVoteConfig }) | ||
}) | ||
} | ||
|
||
function initPageVoteWidget(ctx: ContextApi, options: PageVoteOptions) { | ||
if (!options.vote) return | ||
|
||
const btnContainer = document.querySelector(options.btnEl) as HTMLElement | ||
if (!btnContainer) throw Error(`page vote's config \`btnEl\` selector ${options.btnEl} not found`) | ||
Check failure on line 37 in ui/artalk/src/plugins/page-vote.ts GitHub Actions / test_ui (18, ubuntu-latest)Unhandled error
Check failure on line 37 in ui/artalk/src/plugins/page-vote.ts GitHub Actions / test_ui (20, ubuntu-latest)Unhandled error
|
||
|
||
const api = options.getApi() | ||
|
||
ctx.on('list-fetched', ({ data }) => { | ||
const voteUpBtn = new ActionBtn(() => `${$t('voteUp')}${data!.page.vote_up || 0}`).appendTo(btnContainer) | ||
voteUpBtn.setClick(() => { | ||
api.votes.vote('page_up', data!.page.id, { | ||
...api.getUserFields() | ||
}).then(res => { | ||
// todo: update vote count | ||
console.log(res) | ||
}) | ||
}) | ||
|
||
if (options.voteDown) { | ||
const voteDownBtn = new ActionBtn(() => `${$t('voteDown')}`).appendTo(btnContainer) | ||
voteDownBtn.setClick(() => { | ||
api.votes.vote('page_down', data!.page.id, { | ||
...api.getUserFields() | ||
}) | ||
}) | ||
} | ||
}) | ||
} |
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