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: support hd youtube placeholders #326

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 9 additions & 4 deletions packages/react-notion-x/src/components/lite-youtube-embed.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react'
import { useNotionContext } from '../context'
import { cs } from '../utils'

const qs = (params: Record<string, string>) => {
Expand Down Expand Up @@ -32,15 +33,19 @@ export const LiteYouTubeEmbed: React.FC<{
style,
className
}) => {
const { windowWidth, hasOnlyHDVideos } = useNotionContext()

const muteParam = mute || defaultPlay ? '1' : '0' // Default play must be muted
const queryString = React.useMemo(
() => qs({ autoplay: '1', mute: muteParam, ...params }),
[muteParam, params]
)
// const mobileResolution = 'hqdefault'
// const desktopResolution = 'maxresdefault'
const resolution = 'hqdefault'
const posterUrl = `https://i.ytimg.com/vi/${id}/${resolution}.jpg`

const posterUrl = React.useMemo(() => {
return `https://img.youtube.com/vi/${id}/${
!hasOnlyHDVideos || windowWidth < 480 ? 'hqdefault' : 'maxresdefault'
}.jpg`
}, [hasOnlyHDVideos, id, windowWidth]) // Switch to maxresdefault in any device that isn't mobile
const ytUrl = 'https://www.youtube-nocookie.com'
const iframeSrc = `${ytUrl}/embed/${id}?${queryString}`

Expand Down
16 changes: 13 additions & 3 deletions packages/react-notion-x/src/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from './types'
import { defaultMapPageUrl, defaultMapImageUrl } from './utils'
import { Checkbox as DefaultCheckbox } from './components/checkbox'
import { useWindowSize } from 'react-use'

export interface NotionContext {
recordMap: ExtendedRecordMap
Expand All @@ -38,6 +39,8 @@ export interface NotionContext {
defaultPageCoverPosition?: number

zoom: any
windowWidth?: number
hasOnlyHDVideos?: boolean
}

export interface PartialNotionContext {
Expand Down Expand Up @@ -66,6 +69,8 @@ export interface PartialNotionContext {
defaultPageCoverPosition?: number

zoom?: any
windowWidth?: number
hasOnlyHDVideos?: boolean
}

const DefaultLink: React.FC = (props) => (
Expand Down Expand Up @@ -163,7 +168,9 @@ const defaultNotionContext: NotionContext = {
defaultPageCover: null,
defaultPageCoverPosition: 0.5,

zoom: null
zoom: null,
windowWidth: 1920,
hasOnlyHDVideos: false
}

const ctx = React.createContext<NotionContext>(defaultNotionContext)
Expand All @@ -176,6 +183,8 @@ export const NotionContextProvider: React.FC<PartialNotionContext> = ({
rootPageId,
...rest
}) => {
const { width } = useWindowSize()

for (const key of Object.keys(rest)) {
if (rest[key] === undefined) {
delete rest[key]
Expand Down Expand Up @@ -212,9 +221,10 @@ export const NotionContextProvider: React.FC<PartialNotionContext> = ({
rootPageId,
mapPageUrl: mapPageUrl ?? defaultMapPageUrl(rootPageId),
mapImageUrl: mapImageUrl ?? defaultMapImageUrl,
components: { ...defaultComponents, ...wrappedThemeComponents }
components: { ...defaultComponents, ...wrappedThemeComponents },
windowWidth: width
}),
[mapImageUrl, mapPageUrl, wrappedThemeComponents, rootPageId, rest]
[mapImageUrl, mapPageUrl, wrappedThemeComponents, rootPageId, width, rest]
)

return <ctx.Provider value={value}>{children}</ctx.Provider>
Expand Down
4 changes: 4 additions & 0 deletions packages/react-notion-x/src/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export const NotionRenderer: React.FC<{
blockId?: string
hideBlockId?: boolean
disableHeader?: boolean

hasOnlyHDVideos?: boolean // Enables the use of better looking placeholder images (youtube)
}> = ({
components,
recordMap,
Expand All @@ -72,6 +74,7 @@ export const NotionRenderer: React.FC<{
defaultPageIcon,
defaultPageCover,
defaultPageCoverPosition,
hasOnlyHDVideos,
...rest
}) => {
const zoom = React.useMemo(
Expand Down Expand Up @@ -106,6 +109,7 @@ export const NotionRenderer: React.FC<{
defaultPageCover={defaultPageCover}
defaultPageCoverPosition={defaultPageCoverPosition}
zoom={zoom}
hasOnlyHDVideos={hasOnlyHDVideos}
>
<NotionBlockRenderer {...rest} />
</NotionContextProvider>
Expand Down