Skip to content

Commit

Permalink
fix mobile navigation to comment for hidden comments
Browse files Browse the repository at this point in the history
issues-82
  • Loading branch information
Yuriy Karpov authored and umputun committed May 19, 2021
1 parent 29ac37c commit 6f10a5a
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions frontend/app/components/root/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,13 @@ const messages = defineMessages({
},
});

const getCollapsedParents = (
hash: string,
childToParentComments: Record<string, string>,
collapsedThreads: Record<string, boolean>
) => {
const collapsedParents = [];
const getCollapsedParent = (hash: string, childToParentComments: Record<string, string>) => {
let id = hash.replace(`#${COMMENT_NODE_CLASSNAME_PREFIX}`, '');

while (childToParentComments[id]) {
id = childToParentComments[id];
if (collapsedThreads[id]) {
collapsedParents.push(id);
}
}

return collapsedParents;
return id;
};

/** main component fr main comments widget */
Expand Down Expand Up @@ -160,20 +151,29 @@ export class Root extends Component<Props, State> {
if (e) e.preventDefault();

if (!document.querySelector(hash)) {
const ids = getCollapsedParents(hash, this.props.childToParentComments, this.props.collapsedThreads);
ids.forEach((id) => this.props.setCollapse(id, false));
const id = getCollapsedParent(hash, this.props.childToParentComments);
const indexHash = this.props.topComments.findIndex((item) => item === id);
const multiplierCollapsed = Math.ceil(indexHash / MAX_SHOWN_ROOT_COMMENTS);
this.setState(
{
commentsShown: this.state.commentsShown + MAX_SHOWN_ROOT_COMMENTS * multiplierCollapsed,
},
() => setTimeout(() => this.toMessage(hash), 500)
);
} else {
this.toMessage(hash);
}
}
};

toMessage = (hash: string) => {
const comment = document.querySelector(hash);
if (comment) {
postMessage({ scrollTo: comment.getBoundingClientRect().top });
comment.classList.add('comment_highlighting');
setTimeout(() => {
const comment = document.querySelector(hash);
if (comment) {
postMessage({ scrollTo: comment.getBoundingClientRect().top });
comment.classList.add('comment_highlighting');
setTimeout(() => {
comment.classList.remove('comment_highlighting');
}, 5e3);
}
}, 500);
comment.classList.remove('comment_highlighting');
}, 5e3);
}
};

Expand Down

0 comments on commit 6f10a5a

Please sign in to comment.