Proper comment-node depth coloring.

This commit is contained in:
Dessalines 2020-03-05 15:10:46 -05:00
parent a5c58eb090
commit d903ecb6d3
4 changed files with 20 additions and 4 deletions

View file

@ -27,7 +27,7 @@ import {
pictshareAvatarThumbnail,
showAvatars,
setupTippy,
randomHsl,
colorList,
} from '../utils';
import moment from 'moment';
import { MomentTime } from './moment-time';
@ -94,7 +94,9 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
score: this.props.node.comment.score,
upvotes: this.props.node.comment.upvotes,
downvotes: this.props.node.comment.downvotes,
borderColor: randomHsl(),
borderColor: this.props.node.comment.depth
? colorList[this.props.node.comment.depth % colorList.length]
: colorList[0],
};
constructor(props: any, context: any) {

View file

@ -311,16 +311,27 @@ export class Post extends Component<any, PostState> {
}
let tree: Array<CommentNodeI> = [];
for (let comment of this.state.comments) {
let child = map.get(comment.id);
if (comment.parent_id) {
map.get(comment.parent_id).children.push(map.get(comment.id));
let parent_ = map.get(comment.parent_id);
parent_.children.push(child);
} else {
tree.push(map.get(comment.id));
tree.push(child);
}
this.setDepth(child);
}
return tree;
}
setDepth(node: CommentNodeI, i: number = 0): void {
for (let child of node.children) {
child.comment.depth = i;
this.setDepth(child, i + 1);
}
}
commentsTree() {
let nodes = this.buildCommentsTree();
return (

View file

@ -208,6 +208,7 @@ export interface Comment {
saved?: boolean;
user_mention_id?: number; // For mention type
recipient_id?: number;
depth?: number;
}
export interface Category {

2
ui/src/utils.ts vendored
View file

@ -768,6 +768,8 @@ export function postSort(
}
}
export const colorList: Array<string> = [...Array(10)].map(() => randomHsl());
export function randomHsl() {
return `hsla(${Math.random() * 360}, 100%, 50%, 1)`;
}