Skip to content

Commit

Permalink
clients/editor: youtube embed wizard
Browse files Browse the repository at this point in the history
  • Loading branch information
zegl committed Feb 5, 2024
1 parent 5c92fe9 commit e6b3195
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 16 deletions.
51 changes: 35 additions & 16 deletions clients/apps/web/src/components/Feed/PostEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Article } from '@polar-sh/sdk'
import Link from 'next/link'
import {
Button,
Input,
Select,
SelectContent,
SelectItem,
Expand All @@ -19,7 +20,10 @@ import { StaggerReveal } from '../Shared/StaggerReveal'
import LongformPost from './LongformPost'
import { PublishSettings } from './Publishing/PublishSettings'
import { PostToolbar } from './Toolbar/PostToolbar'
import { useMarkdownComponents } from './Toolbar/useMarkdownComponents'
import {
useMarkdownComponents,
youtubeIdFromURL,
} from './Toolbar/useMarkdownComponents'
import { EditorHelpers, useEditorHelpers } from './useEditorHelpers'

const defaultPostEditorContext: EditorHelpers = {
Expand Down Expand Up @@ -263,24 +267,39 @@ const Sidebar = () => {
</>
) : null}

{/*
<div className="flex flex-col gap-y-2">
<h3>YouTube</h3>
<Input placeholder="Video URL" />
<Button
variant={'outline'}
size={'sm'}
onClick={() => {
insertAd()
}}
>
Add
</Button>
</div>*/}
<SidebarYouTube />
</div>
</ShadowBoxOnMd>
</div>
</div>
)
}

const SidebarYouTube = () => {
const [url, setURL] = useState('')
const { insertYouTube } = useMarkdownComponents()

const validURL = Boolean(youtubeIdFromURL(url))

return (
<div className="flex flex-col gap-y-2">
<h3 className="font-medium">YouTube</h3>

<Input
placeholder="Video URL"
value={url}
onChange={(e) => setURL(e.target.value)}
/>
<Button
variant={'outline'}
size={'sm'}
onClick={() => {
insertYouTube(url)
}}
disabled={!validURL}
>
Add
</Button>
</div>
)
}
25 changes: 25 additions & 0 deletions clients/apps/web/src/components/Feed/Toolbar/components.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { youtubeIdFromURL } from './useMarkdownComponents'

test('youtubeIdFromURL', () => {
expect(
youtubeIdFromURL('https://www.youtube.com/watch?v=86Gy035z_KA'),
).toStrictEqual('86Gy035z_KA')

expect(
youtubeIdFromURL('https://www.youtube.com/watch?v=86Gy035z_KA&t=10'),
).toStrictEqual('86Gy035z_KA')

expect(
youtubeIdFromURL('https://m.youtube.com/watch?v=86Gy035z_KA&t=10'),
).toStrictEqual('86Gy035z_KA')

expect(
youtubeIdFromURL('https://youtu.be/86Gy035z_KA?si=o__Vh0bePjpLiZ3i&t=107'),
).toStrictEqual('86Gy035z_KA')

expect(
youtubeIdFromURL(
'https://www.youtube.com/embed/86Gy035z_KA?si=o__Vh0bePjpLiZ3i',
),
).toStrictEqual('86Gy035z_KA')
})
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,39 @@ export const useMarkdownComponents = () => {
[insertTextAtCursor],
)

const insertYouTube = useCallback(
(url: string) => {
const id = youtubeIdFromURL(url)
if (!id) {
return
}

if (!bodyRef.current) {
return
}

insertTextAtCursor(
`<iframe src="https://www.youtube.com/embed/${id}"></iframe>`,
)
},
[insertTextAtCursor],
)

return {
insertPaywall,
insertSubscribeNow,
insertAd,
insertYouTube,
}
}

const youtubeUrlRe =
/^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube(-nocookie)?\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|live\/|v\/)?)([\w\-]+)(\S+)?$/

export const youtubeIdFromURL = (url: string): string | null => {
const res = url.match(youtubeUrlRe)
if (res && res.length >= 7) {
return res[6]
}
return null
}

0 comments on commit e6b3195

Please sign in to comment.