Skip to content

Commit

Permalink
feat(client): add recent comment widget
Browse files Browse the repository at this point in the history
  • Loading branch information
lizheming committed Dec 6, 2020
1 parent c2ca659 commit 1f43c13
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
26 changes: 25 additions & 1 deletion packages/client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import ReactDOM from 'react-dom';
import App from './App';
import Context from './context';
import Visitor from './utils/visitor';
import { fetchCount } from './utils/fetch';
import { fetchCount, fetchRecent } from './utils/fetch';
import mathML from './utils/mathml';
import './index.css';
import './recent.css';
import './math.css';

export default function Waline({
Expand Down Expand Up @@ -82,3 +83,26 @@ export default function Waline({
};

Waline.version = VERSION;
Waline.Widget = {
RecentComments({el, serverURL, count}) {
//评论列表展示
const root = document.querySelector(el);
if(!root) {
return Promise.resolve();
}

return fetchRecent({serverURL, count}).then(comments => {
if(!comments.length) {
return comments;
}
root.innerHTML = `
<ul class="waline-widget-list">
${comments.map(cmt =>
`<li class="waline-widget-item"><a href="${cmt.url}">${cmt.nick}</a>:${cmt.comment}</li>`
).join('')}
</ul>`;

return comments;
});
}
};
3 changes: 3 additions & 0 deletions packages/client/src/recent.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.waline-widget-item p {
display: inline;
}
5 changes: 5 additions & 0 deletions packages/client/src/utils/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ export function fetchCount({serverURL, path}) {
return fetch(url).then(resp => resp.text());
}

export function fetchRecent({serverURL, count}) {
const url = `${serverURL}/comment?type=recent&count=${count}`;
return fetch(url).then(resp => resp.json());
}

export function fetchList({serverURL, path, page, pageSize}) {
const url = `${serverURL}/comment?path=${encodeURIComponent(path)}&pageSize=${pageSize}&page=${page}`;
return fetch(url).then(resp => resp.json());
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/controller/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module.exports = class extends BaseRest {
limit: count
});

return this.json(comments.map(({ip, ...cmt}) => cmt));
return this.json(await Promise.all(comments.map(formatCmt)));
}

case 'count': {
Expand Down

0 comments on commit 1f43c13

Please sign in to comment.