Merge branch 'scroll-to-links-fix' into 'dev'
scroll to anchor fixes, profile comments link fix See merge request discoursio/discoursio-webapp!47
This commit is contained in:
commit
e218ef43f5
|
@ -10,7 +10,6 @@ import { useReactions } from '../../context/reactions'
|
||||||
import { byCreated } from '../../utils/sortby'
|
import { byCreated } from '../../utils/sortby'
|
||||||
import { ShowIfAuthenticated } from '../_shared/ShowIfAuthenticated'
|
import { ShowIfAuthenticated } from '../_shared/ShowIfAuthenticated'
|
||||||
import { useLocalize } from '../../context/localize'
|
import { useLocalize } from '../../context/localize'
|
||||||
import Cookie from 'js-cookie'
|
|
||||||
|
|
||||||
type CommentsOrder = 'createdAt' | 'rating' | 'newOnly'
|
type CommentsOrder = 'createdAt' | 'rating' | 'newOnly'
|
||||||
|
|
||||||
|
@ -108,7 +107,7 @@ export const CommentsTree = (props: Props) => {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div class={styles.commentsHeaderWrapper}>
|
<div class={styles.commentsHeaderWrapper}>
|
||||||
<h2 id="comments" class={styles.commentsHeader}>
|
<h2 class={styles.commentsHeader}>
|
||||||
{t('Comments')} {comments().length.toString() || ''}
|
{t('Comments')} {comments().length.toString() || ''}
|
||||||
<Show when={newReactions().length > 0}>
|
<Show when={newReactions().length > 0}>
|
||||||
<span class={styles.newReactions}> +{newReactions().length}</span>
|
<span class={styles.newReactions}> +{newReactions().length}</span>
|
||||||
|
@ -161,7 +160,7 @@ export const CommentsTree = (props: Props) => {
|
||||||
</ul>
|
</ul>
|
||||||
<ShowIfAuthenticated
|
<ShowIfAuthenticated
|
||||||
fallback={
|
fallback={
|
||||||
<div class={styles.signInMessage} id="comments">
|
<div class={styles.signInMessage}>
|
||||||
{t('To write a comment, you must')}{' '}
|
{t('To write a comment, you must')}{' '}
|
||||||
<a href="?modal=auth&mode=register" class={styles.link}>
|
<a href="?modal=auth&mode=register" class={styles.link}>
|
||||||
{t('sign up')}
|
{t('sign up')}
|
||||||
|
|
|
@ -66,19 +66,6 @@ export const FullArticle = (props: ArticleProps) => {
|
||||||
props.article.topics[0]
|
props.article.topics[0]
|
||||||
)
|
)
|
||||||
|
|
||||||
onMount(() => {
|
|
||||||
const windowHash = window.location.hash
|
|
||||||
if (windowHash?.length > 0) {
|
|
||||||
const comments = document.querySelector(windowHash)
|
|
||||||
if (comments) {
|
|
||||||
window.scrollTo({
|
|
||||||
top: comments.getBoundingClientRect().top,
|
|
||||||
behavior: 'smooth'
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
await loadReactionsBy({
|
await loadReactionsBy({
|
||||||
by: { shout: props.article.slug }
|
by: { shout: props.article.slug }
|
||||||
|
@ -105,20 +92,6 @@ export const FullArticle = (props: ArticleProps) => {
|
||||||
actions: { loadReactionsBy }
|
actions: { loadReactionsBy }
|
||||||
} = useReactions()
|
} = useReactions()
|
||||||
|
|
||||||
let commentsRef: HTMLDivElement | undefined
|
|
||||||
const scrollToComments = () => {
|
|
||||||
if (!isReactionsLoaded()) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
commentsRef.scrollIntoView({ behavior: 'smooth' })
|
|
||||||
}
|
|
||||||
const { searchParams } = useRouter()
|
|
||||||
createEffect(() => {
|
|
||||||
if (searchParams()?.scrollTo === 'comments') {
|
|
||||||
scrollToComments()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Title>{props.article.title}</Title>
|
<Title>{props.article.title}</Title>
|
||||||
|
@ -212,10 +185,10 @@ export const FullArticle = (props: ArticleProps) => {
|
||||||
</div>
|
</div>
|
||||||
</Show>
|
</Show>
|
||||||
|
|
||||||
<div class={styles.shoutStatsItem} onClick={() => scrollToComments()}>
|
<a href="#comments" class={styles.shoutStatsItem}>
|
||||||
<Icon name="comment" class={styles.icon} />
|
<Icon name="comment" class={styles.icon} />
|
||||||
{/*{props.article.stat?.commented || ''}*/}
|
{props.article.stat?.commented ?? ''}
|
||||||
</div>
|
</a>
|
||||||
|
|
||||||
<div class={styles.shoutStatsItem}>
|
<div class={styles.shoutStatsItem}>
|
||||||
<SharePopup
|
<SharePopup
|
||||||
|
@ -276,7 +249,7 @@ export const FullArticle = (props: ArticleProps) => {
|
||||||
)}
|
)}
|
||||||
</For>
|
</For>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div id="comments">
|
||||||
<Show when={isReactionsLoaded()}>
|
<Show when={isReactionsLoaded()}>
|
||||||
<CommentsTree
|
<CommentsTree
|
||||||
shoutId={props.article.id}
|
shoutId={props.article.id}
|
||||||
|
|
|
@ -12,12 +12,7 @@ import stylesHeader from '../Nav/Header.module.scss'
|
||||||
import { getDescription } from '../../utils/meta'
|
import { getDescription } from '../../utils/meta'
|
||||||
import { FeedArticlePopup } from './FeedArticlePopup'
|
import { FeedArticlePopup } from './FeedArticlePopup'
|
||||||
import { useLocalize } from '../../context/localize'
|
import { useLocalize } from '../../context/localize'
|
||||||
import { openPage } from '@nanostores/router'
|
|
||||||
import { router, useRouter } from '../../stores/router'
|
|
||||||
|
|
||||||
type ArticleSearchParams = {
|
|
||||||
scrollTo: 'comments'
|
|
||||||
}
|
|
||||||
interface ArticleCardProps {
|
interface ArticleCardProps {
|
||||||
settings?: {
|
settings?: {
|
||||||
noicon?: boolean
|
noicon?: boolean
|
||||||
|
@ -80,12 +75,6 @@ export const ArticleCard = (props: ArticleCardProps) => {
|
||||||
|
|
||||||
const { cover, layout, slug, authors, stat, body } = props.article
|
const { cover, layout, slug, authors, stat, body } = props.article
|
||||||
|
|
||||||
const { changeSearchParam } = useRouter<ArticleSearchParams>()
|
|
||||||
const scrollToComments = () => {
|
|
||||||
openPage(router, 'article', { slug: slug })
|
|
||||||
changeSearchParam('scrollTo', 'comments')
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section
|
<section
|
||||||
class={clsx(styles.shoutCard, `${props.settings?.additionalClass || ''}`)}
|
class={clsx(styles.shoutCard, `${props.settings?.additionalClass || ''}`)}
|
||||||
|
@ -166,7 +155,6 @@ export const ArticleCard = (props: ArticleCardProps) => {
|
||||||
</For>
|
</For>
|
||||||
</div>
|
</div>
|
||||||
</Show>
|
</Show>
|
||||||
|
|
||||||
<Show when={!props.settings?.nodate}>
|
<Show when={!props.settings?.nodate}>
|
||||||
<div class={styles.shoutDate}>{formattedDate()}</div>
|
<div class={styles.shoutDate}>{formattedDate()}</div>
|
||||||
</Show>
|
</Show>
|
||||||
|
@ -184,10 +172,10 @@ export const ArticleCard = (props: ArticleCardProps) => {
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class={clsx(styles.shoutCardDetailsItem, styles.shoutCardComments)}>
|
<div class={clsx(styles.shoutCardDetailsItem, styles.shoutCardComments)}>
|
||||||
<button type="button" onClick={scrollToComments}>
|
<a href={`/${slug + '#comments'}`}>
|
||||||
<Icon name="comment" class={clsx(styles.icon, styles.feedControlIcon)} />
|
<Icon name="comment" class={clsx(styles.icon, styles.feedControlIcon)} />
|
||||||
{stat?.commented || t('Add comment')}
|
{stat?.commented || t('Add comment')}
|
||||||
</button>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
@ -2,10 +2,9 @@ import { useSession } from '../../context/session'
|
||||||
import type { PopupProps } from '../_shared/Popup'
|
import type { PopupProps } from '../_shared/Popup'
|
||||||
import { Popup } from '../_shared/Popup'
|
import { Popup } from '../_shared/Popup'
|
||||||
import styles from '../_shared/Popup/Popup.module.scss'
|
import styles from '../_shared/Popup/Popup.module.scss'
|
||||||
import { getPagePath, openPage } from '@nanostores/router'
|
import { getPagePath } from '@nanostores/router'
|
||||||
import { router, useRouter } from '../../stores/router'
|
import { router } from '../../stores/router'
|
||||||
import { useLocalize } from '../../context/localize'
|
import { useLocalize } from '../../context/localize'
|
||||||
import type { AuthorPageSearchParams } from '../Views/Author'
|
|
||||||
|
|
||||||
type ProfilePopupProps = Omit<PopupProps, 'children'>
|
type ProfilePopupProps = Omit<PopupProps, 'children'>
|
||||||
|
|
||||||
|
@ -16,12 +15,6 @@ export const ProfilePopup = (props: ProfilePopupProps) => {
|
||||||
} = useSession()
|
} = useSession()
|
||||||
|
|
||||||
const { t } = useLocalize()
|
const { t } = useLocalize()
|
||||||
const { changeSearchParam } = useRouter<AuthorPageSearchParams>()
|
|
||||||
|
|
||||||
const openAuthorComments = () => {
|
|
||||||
openPage(router, 'author', { slug: userSlug() })
|
|
||||||
changeSearchParam('by', 'commented')
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Popup {...props} horizontalAnchor="right" variant="bordered">
|
<Popup {...props} horizontalAnchor="right" variant="bordered">
|
||||||
|
@ -36,13 +29,7 @@ export const ProfilePopup = (props: ProfilePopupProps) => {
|
||||||
<a href="#">{t('Subscriptions')}</a>
|
<a href="#">{t('Subscriptions')}</a>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<a
|
<a href={`${getPagePath(router, 'author', { slug: userSlug() })}/?by=commented`}>
|
||||||
href="#"
|
|
||||||
onClick={(event) => {
|
|
||||||
event.preventDefault()
|
|
||||||
openAuthorComments()
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{t('Comments')}
|
{t('Comments')}
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
|
|
|
@ -54,7 +54,11 @@ export const AuthorView = (props: AuthorProps) => {
|
||||||
|
|
||||||
const { searchParams, changeSearchParam } = useRouter<AuthorPageSearchParams>()
|
const { searchParams, changeSearchParam } = useRouter<AuthorPageSearchParams>()
|
||||||
|
|
||||||
changeSearchParam('by', 'rating')
|
onMount(() => {
|
||||||
|
if (!searchParams().by) {
|
||||||
|
changeSearchParam('by', 'rating')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
const loadMore = async () => {
|
const loadMore = async () => {
|
||||||
saveScrollPosition()
|
saveScrollPosition()
|
||||||
|
|
|
@ -26,7 +26,7 @@ export const SearchView = (props: Props) => {
|
||||||
const [query, setQuery] = createSignal(props.query)
|
const [query, setQuery] = createSignal(props.query)
|
||||||
const [offset, setOffset] = createSignal(0)
|
const [offset, setOffset] = createSignal(0)
|
||||||
|
|
||||||
const { searchParams, handleClientRouteLinkClick } = useRouter<SearchPageSearchParams>()
|
const { searchParams } = useRouter<SearchPageSearchParams>()
|
||||||
let searchEl: HTMLInputElement
|
let searchEl: HTMLInputElement
|
||||||
const handleQueryChange = (_ev) => {
|
const handleQueryChange = (_ev) => {
|
||||||
setQuery(searchEl.value)
|
setQuery(searchEl.value)
|
||||||
|
@ -72,18 +72,14 @@ export const SearchView = (props: Props) => {
|
||||||
selected: searchParams().by === 'relevance'
|
selected: searchParams().by === 'relevance'
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<a href="?by=relevance" onClick={handleClientRouteLinkClick}>
|
<a href="?by=relevance">{t('By relevance')}</a>
|
||||||
{t('By relevance')}
|
|
||||||
</a>
|
|
||||||
</li>
|
</li>
|
||||||
<li
|
<li
|
||||||
classList={{
|
classList={{
|
||||||
selected: searchParams().by === 'rating'
|
selected: searchParams().by === 'rating'
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<a href="?by=rating" onClick={handleClientRouteLinkClick}>
|
<a href="?by=rating">{t('Top rated')}</a>
|
||||||
{t('Top rated')}
|
|
||||||
</a>
|
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ import { useRouter } from '../stores/router'
|
||||||
import { Loading } from '../components/_shared/Loading'
|
import { Loading } from '../components/_shared/Loading'
|
||||||
import { ReactionsProvider } from '../context/reactions'
|
import { ReactionsProvider } from '../context/reactions'
|
||||||
import { FullArticle } from '../components/Article/FullArticle'
|
import { FullArticle } from '../components/Article/FullArticle'
|
||||||
|
import { setPageLoadManagerPromise } from '../utils/pageLoadManager'
|
||||||
|
|
||||||
export const ArticlePage = (props: PageProps) => {
|
export const ArticlePage = (props: PageProps) => {
|
||||||
const shouts = props.article ? [props.article] : []
|
const shouts = props.article ? [props.article] : []
|
||||||
|
@ -33,7 +34,9 @@ export const ArticlePage = (props: PageProps) => {
|
||||||
const articleValue = articleEntities()[slug()]
|
const articleValue = articleEntities()[slug()]
|
||||||
|
|
||||||
if (!articleValue || !articleValue.body) {
|
if (!articleValue || !articleValue.body) {
|
||||||
await loadShout(slug())
|
const loadShoutPromise = loadShout(slug())
|
||||||
|
setPageLoadManagerPromise(loadShoutPromise)
|
||||||
|
await loadShoutPromise
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ import type { Accessor } from 'solid-js'
|
||||||
import { createRouter, createSearchParams } from '@nanostores/router'
|
import { createRouter, createSearchParams } from '@nanostores/router'
|
||||||
import { isServer } from 'solid-js/web'
|
import { isServer } from 'solid-js/web'
|
||||||
import { useStore } from '@nanostores/solid'
|
import { useStore } from '@nanostores/solid'
|
||||||
|
import { getPageLoadManagerPromise } from '../utils/pageLoadManager'
|
||||||
|
|
||||||
export const ROUTES = {
|
export const ROUTES = {
|
||||||
home: '/',
|
home: '/',
|
||||||
|
@ -40,9 +41,8 @@ const routerStore = createRouter(ROUTES, {
|
||||||
|
|
||||||
export const router = routerStore
|
export const router = routerStore
|
||||||
|
|
||||||
const handleClientRouteLinkClick = (event) => {
|
const checkOpenOnClient = (link: HTMLAnchorElement, event) => {
|
||||||
const link = event.target.closest('a')
|
return (
|
||||||
if (
|
|
||||||
link &&
|
link &&
|
||||||
event.button === 0 &&
|
event.button === 0 &&
|
||||||
link.target !== '_blank' &&
|
link.target !== '_blank' &&
|
||||||
|
@ -52,48 +52,84 @@ const handleClientRouteLinkClick = (event) => {
|
||||||
!event.ctrlKey &&
|
!event.ctrlKey &&
|
||||||
!event.shiftKey &&
|
!event.shiftKey &&
|
||||||
!event.altKey
|
!event.altKey
|
||||||
) {
|
)
|
||||||
const url = new URL(link.href)
|
}
|
||||||
if (url.origin === location.origin) {
|
|
||||||
event.preventDefault()
|
|
||||||
|
|
||||||
if (url.pathname) {
|
const scrollToHash = (hash: string) => {
|
||||||
routerStore.open(url.pathname)
|
let selector = hash
|
||||||
}
|
|
||||||
|
|
||||||
if (url.search) {
|
if (/^#\d+/.test(selector)) {
|
||||||
const params = Object.fromEntries(new URLSearchParams(url.search))
|
// id="1" fix
|
||||||
searchParamsStore.open(params)
|
// https://stackoverflow.com/questions/20306204/using-queryselector-with-ids-that-are-numbers
|
||||||
}
|
selector = `[id="${selector.replace('#', '')}"]`
|
||||||
|
}
|
||||||
|
|
||||||
if (url.hash) {
|
const anchor = document.querySelector(selector)
|
||||||
let selector = url.hash
|
const headerOffset = 80 // 100px for header
|
||||||
|
const elementPosition = anchor ? anchor.getBoundingClientRect().top : 0
|
||||||
|
const newScrollTop = elementPosition + window.scrollY - headerOffset
|
||||||
|
|
||||||
if (/^#\d+/.test(selector)) {
|
window.scrollTo({
|
||||||
// id="1" fix
|
top: newScrollTop,
|
||||||
// https://stackoverflow.com/questions/20306204/using-queryselector-with-ids-that-are-numbers
|
behavior: 'smooth'
|
||||||
selector = `[id="${selector.replace('#', '')}"]`
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const anchor = document.querySelector(selector)
|
const handleClientRouteLinkClick = async (event) => {
|
||||||
const headerOffset = 80 // 100px for header
|
const link = event.target.closest('a')
|
||||||
const elementPosition = anchor ? anchor.getBoundingClientRect().top : 0
|
|
||||||
const newScrollTop = elementPosition + window.scrollY - headerOffset
|
|
||||||
|
|
||||||
window.scrollTo({
|
if (!checkOpenOnClient(link, event)) {
|
||||||
top: newScrollTop,
|
return
|
||||||
behavior: 'smooth'
|
}
|
||||||
})
|
|
||||||
|
|
||||||
return
|
const url = new URL(link.href)
|
||||||
}
|
if (url.origin !== location.origin) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
window.scrollTo({
|
event.preventDefault()
|
||||||
top: 0,
|
|
||||||
left: 0
|
if (url.pathname) {
|
||||||
})
|
routerStore.open(url.pathname)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.search) {
|
||||||
|
const params = Object.fromEntries(new URLSearchParams(url.search))
|
||||||
|
searchParamsStore.open(params)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!url.hash) {
|
||||||
|
window.scrollTo({
|
||||||
|
top: 0,
|
||||||
|
left: 0
|
||||||
|
})
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
await getPageLoadManagerPromise()
|
||||||
|
|
||||||
|
const images = document.querySelectorAll('img')
|
||||||
|
|
||||||
|
let imagesLoaded = 0
|
||||||
|
|
||||||
|
const imageLoadEventHandler = () => {
|
||||||
|
imagesLoaded++
|
||||||
|
if (imagesLoaded === images.length) {
|
||||||
|
scrollToHash(url.hash)
|
||||||
|
images.forEach((image) => image.removeEventListener('load', imageLoadEventHandler))
|
||||||
|
images.forEach((image) => image.removeEventListener('error', imageLoadEventHandler))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
images.forEach((image) => {
|
||||||
|
if (image.complete) {
|
||||||
|
imagesLoaded++
|
||||||
|
}
|
||||||
|
|
||||||
|
image.addEventListener('load', imageLoadEventHandler)
|
||||||
|
image.addEventListener('error', imageLoadEventHandler)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export const initRouter = (pathname: string, search: Record<string, string>) => {
|
export const initRouter = (pathname: string, search: Record<string, string>) => {
|
||||||
|
@ -134,7 +170,6 @@ export const useRouter = <TSearchParams extends Record<string, string> = Record<
|
||||||
return {
|
return {
|
||||||
page,
|
page,
|
||||||
searchParams,
|
searchParams,
|
||||||
changeSearchParam,
|
changeSearchParam
|
||||||
handleClientRouteLinkClick
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
11
src/utils/pageLoadManager.ts
Normal file
11
src/utils/pageLoadManager.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
const pageLoadManager: {
|
||||||
|
promise: Promise<any>
|
||||||
|
} = { promise: Promise.resolve() }
|
||||||
|
|
||||||
|
export const getPageLoadManagerPromise = () => {
|
||||||
|
return pageLoadManager.promise
|
||||||
|
}
|
||||||
|
|
||||||
|
export const setPageLoadManagerPromise = (promise: Promise<any>) => {
|
||||||
|
pageLoadManager.promise = promise
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user