Skip to content

Commit

Permalink
feat(plugin/page_vote): add page_vote plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Liumingxun committed Sep 21, 2024
1 parent 8707f4f commit 81bf6de
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions ui/artalk/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
<span id="ArtalkPV"></span>
|
<span id="ArtalkCount"></span>
<div class="artalk-page-vote"></div>
</div>
<div class="social">
<a
Expand Down
2 changes: 2 additions & 0 deletions ui/artalk/src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const defaults: ArtalkConfig = {

emoticons: 'https://cdn.jsdelivr.net/gh/ArtalkJS/Emoticons/grps/default.json',

pageVote: true,

vote: true,
voteDown: false,
uaBadge: true,
Expand Down
2 changes: 2 additions & 0 deletions ui/artalk/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PvCountWidget } from './stat'
import { VersionCheck } from './version-check'
import { AdminOnlyElem } from './admin-only-elem'
import { DarkMode } from './dark-mode'
import { PageVoteWidget } from './page-vote'
import type { ArtalkPlugin } from '@/types'

export const DefaultPlugins: ArtalkPlugin[] = [
Expand All @@ -17,4 +18,5 @@ export const DefaultPlugins: ArtalkPlugin[] = [
PvCountWidget,
VersionCheck,
DarkMode,
PageVoteWidget,
]
61 changes: 61 additions & 0 deletions ui/artalk/src/plugins/page-vote.ts
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

View workflow job for this annotation

GitHub Actions / test_ui (18, ubuntu-latest)

Unhandled error

Error: page vote's config `btnEl` selector .artalk-page-vote not found ❯ initPageVoteWidget src/plugins/page-vote.ts:37:28 ❯ src/plugins/page-vote.ts:29:5 ❯ Object.handler src/lib/watch-conf.ts:24:7 ❯ src/lib/event-manager.ts:56:13 ❯ EventManager.trigger src/lib/event-manager.ts:54:8 ❯ Context.trigger src/context.ts:156:17 ❯ Module.load src/load.ts:73:7 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 This error originated in "tests/ui-api.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "should can listen to events and the conf-remoter works (artalk.trigger, artalk.on, conf-remoter)". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 37 in ui/artalk/src/plugins/page-vote.ts

View workflow job for this annotation

GitHub Actions / test_ui (20, ubuntu-latest)

Unhandled error

Error: page vote's config `btnEl` selector .artalk-page-vote not found ❯ initPageVoteWidget src/plugins/page-vote.ts:37:28 ❯ src/plugins/page-vote.ts:29:5 ❯ Object.handler src/lib/watch-conf.ts:24:7 ❯ src/lib/event-manager.ts:56:13 ❯ EventManager.trigger src/lib/event-manager.ts:54:8 ❯ Context.trigger src/context.ts:156:17 ❯ Module.load src/load.ts:73:7 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 This error originated in "tests/ui-api.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "should can listen to events and the conf-remoter works (artalk.trigger, artalk.on, conf-remoter)". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

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()
})
})
}
})
}
12 changes: 12 additions & 0 deletions ui/artalk/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ export interface ArtalkConfig {
/** 评论投票反对按钮 */
voteDown: boolean

/** 页面投票按钮 */
pageVote?: {
/** 页面投票反对按钮 */
voteDown: boolean

/** 页面投票按钮绑定 Selector */
btnEl: string

/** 页面投票数绑定 Selector */
el: string
} | boolean

/** 评论预览功能 */
preview: boolean

Expand Down

0 comments on commit 81bf6de

Please sign in to comment.