Skip to content
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
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
]
75 changes: 75 additions & 0 deletions ui/artalk/src/plugins/page-vote.ts
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`)
Copy link
Author

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?

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)
})
})
}
})
}
21 changes: 21 additions & 0 deletions ui/artalk/src/style/_action-btn.scss
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extract this style for reuse
I think it can be further optimized, all of div.atk-actions span.atk-common-action-btn have duplicate style.

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);
}
}
3 changes: 2 additions & 1 deletion ui/artalk/src/style/_color.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.artalk,
.atk-layer-wrap {
.atk-layer-wrap,
.atk-page-vote {
--at-color-font: #2a2e2e;
--at-color-deep: #2a2e2e;
--at-color-sub: #757575;
Expand Down
22 changes: 3 additions & 19 deletions ui/artalk/src/style/comment.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@import 'action-btn';

.atk-comment-wrap {
overflow: hidden;
position: relative;
Expand Down Expand Up @@ -263,25 +265,7 @@
flex-wrap: wrap;

& > span {
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);
}
@extend %action-btn;
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions ui/artalk/src/style/main.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@import 'action-btn';
@import './_color.scss';
@import './comment.scss';
@import './list.scss';
Expand Down Expand Up @@ -542,6 +543,9 @@

/* Common Action Btn */
.atk-common-action-btn {

@extend %action-btn;

&.atk-btn-confirm {
color: var(--at-color-yellow) !important;
}
Expand Down
9 changes: 9 additions & 0 deletions ui/artalk/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ export interface ArtalkConfig {
/** 评论投票反对按钮 */
voteDown: boolean

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

/** 页面投票按钮绑定 Selector */
btnEl?: string
} | boolean

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

Expand Down