-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat(rich-markdown-editor): live media embeds + shared embed detection util #5290
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
647c498
feat(rich-markdown-editor): live media embeds + shared embed detectio…
waleedlatif1 ef83130
fix(media-embed): gate provider detection on parsed hostname
waleedlatif1 4aa6e9d
fix(rich-markdown-editor): unique widget key per duplicate embed URL
waleedlatif1 427f8d4
refactor(media-embed): tighten comments and drop a redundant guard
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...kspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/embed/embed-dom.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import type { EmbedInfo } from '@sim/utils/media-embed' | ||
|
|
||
| /** | ||
| * Iframes are rendered at native size then CSS-scaled down so embedded players keep their | ||
| * intended layout inside the editor's reading column. Mirrors the note-block renderer. | ||
| */ | ||
| const EMBED_SCALE = 0.78 | ||
| const EMBED_INVERSE_SCALE = `${(1 / EMBED_SCALE) * 100}%` | ||
|
|
||
| const IFRAME_ALLOW = | ||
| 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share' | ||
|
|
||
| /** | ||
| * Build the DOM player for a resolved {@link EmbedInfo}, matching the note-block renderer's | ||
| * markup. Returned as a non-editable element so it can back a ProseMirror widget decoration | ||
| * without entering the editable content. | ||
| */ | ||
| export function createEmbedDom(embedInfo: EmbedInfo): HTMLElement { | ||
| const container = document.createElement('div') | ||
| container.className = 'my-2 block w-full overflow-hidden rounded-md' | ||
| container.contentEditable = 'false' | ||
|
|
||
| if (embedInfo.type === 'iframe') { | ||
| const frame = document.createElement('div') | ||
| frame.className = 'block overflow-hidden' | ||
| frame.style.width = '100%' | ||
| frame.style.aspectRatio = embedInfo.aspectRatio || '16/9' | ||
|
|
||
| const iframe = document.createElement('iframe') | ||
| iframe.src = embedInfo.url | ||
| iframe.title = 'Media' | ||
| iframe.allow = IFRAME_ALLOW | ||
| iframe.allowFullscreen = true | ||
| iframe.loading = 'lazy' | ||
| iframe.className = 'origin-top-left' | ||
| iframe.style.width = EMBED_INVERSE_SCALE | ||
| iframe.style.height = EMBED_INVERSE_SCALE | ||
| iframe.style.transform = `scale(${EMBED_SCALE})` | ||
|
|
||
| frame.appendChild(iframe) | ||
| container.appendChild(frame) | ||
| return container | ||
| } | ||
|
|
||
| if (embedInfo.type === 'video') { | ||
| const video = document.createElement('video') | ||
| video.src = embedInfo.url | ||
| video.controls = true | ||
| video.preload = 'metadata' | ||
| video.className = 'aspect-video w-full' | ||
| container.appendChild(video) | ||
| return container | ||
| } | ||
|
|
||
| const audio = document.createElement('audio') | ||
| audio.src = embedInfo.url | ||
| audio.controls = true | ||
| audio.preload = 'metadata' | ||
| audio.className = 'w-full' | ||
| container.appendChild(audio) | ||
| return container | ||
| } |
64 changes: 64 additions & 0 deletions
64
.../[workspaceId]/files/components/file-viewer/rich-markdown-editor/embed/link-embed.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
| import { Editor } from '@tiptap/core' | ||
| import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest' | ||
| import { createMarkdownEditorExtensions } from '../editor-extensions' | ||
|
|
||
| // jsdom lacks elementFromPoint, which TipTap's Placeholder viewport tracking calls on mount. | ||
| beforeAll(() => { | ||
| document.elementFromPoint = vi.fn(() => null) | ||
| }) | ||
|
|
||
| let editor: Editor | null = null | ||
|
|
||
| function editorWith(content: string, embeds = true): Editor { | ||
| editor = new Editor({ | ||
| extensions: createMarkdownEditorExtensions({ placeholder: '', embeds }), | ||
| content, | ||
| }) | ||
| return editor | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| editor?.destroy() | ||
| editor = null | ||
| }) | ||
|
|
||
| const YOUTUBE_LINK = '<p><a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ">watch</a></p>' | ||
|
|
||
| describe('LinkEmbed', () => { | ||
| it('renders a player beneath a standalone embeddable link', () => { | ||
| const view = editorWith(YOUTUBE_LINK).view | ||
| const iframe = view.dom.querySelector('iframe') | ||
| expect(iframe?.getAttribute('src')).toBe('https://www.youtube.com/embed/dQw4w9WgXcQ') | ||
| }) | ||
|
|
||
| it('renders one player per link when the same URL appears twice', () => { | ||
| const view = editorWith(`${YOUTUBE_LINK}${YOUTUBE_LINK}`).view | ||
| expect(view.dom.querySelectorAll('iframe')).toHaveLength(2) | ||
| }) | ||
|
|
||
| it('keeps the underlying document a plain markdown link (lossless round-trip)', () => { | ||
| const markdown = editorWith(YOUTUBE_LINK).getMarkdown() | ||
| expect(markdown).toContain('https://www.youtube.com/watch?v=dQw4w9WgXcQ') | ||
| expect(markdown).not.toContain('<iframe') | ||
| }) | ||
|
|
||
| it('does not embed an inline link inside surrounding text', () => { | ||
| const view = editorWith( | ||
| '<p>see <a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ">here</a> now</p>' | ||
| ).view | ||
| expect(view.dom.querySelector('iframe')).toBeNull() | ||
| }) | ||
|
|
||
| it('does not embed a non-embeddable standalone link', () => { | ||
| const view = editorWith('<p><a href="https://example.com/article">read</a></p>').view | ||
| expect(view.dom.querySelector('iframe')).toBeNull() | ||
| }) | ||
|
|
||
| it('does nothing when the embeds option is disabled', () => { | ||
| const view = editorWith(YOUTUBE_LINK, false).view | ||
| expect(view.dom.querySelector('iframe')).toBeNull() | ||
| }) | ||
| }) |
88 changes: 88 additions & 0 deletions
88
...space/[workspaceId]/files/components/file-viewer/rich-markdown-editor/embed/link-embed.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { getEmbedInfo } from '@sim/utils/media-embed' | ||
| import { Extension } from '@tiptap/core' | ||
| import type { Node as ProseMirrorNode } from '@tiptap/pm/model' | ||
| import { Plugin, PluginKey } from '@tiptap/pm/state' | ||
| import { Decoration, DecorationSet } from '@tiptap/pm/view' | ||
| import { createEmbedDom } from './embed-dom' | ||
|
|
||
| const LINK_EMBED_PLUGIN_KEY = new PluginKey('linkEmbed') | ||
|
|
||
| /** | ||
| * The href of a paragraph that is a single, whole-text link (a "standalone link"), or null if | ||
| * the paragraph is empty, holds non-text content, or mixes a link with other text. Only | ||
| * standalone links become media embeds — a link inline within a sentence stays a plain link, | ||
| * matching how Notion and Linear auto-embed. | ||
| */ | ||
| function getStandaloneLinkHref(paragraph: ProseMirrorNode): string | null { | ||
| if (paragraph.childCount === 0) return null | ||
| let href: string | null = null | ||
| let isStandalone = true | ||
| paragraph.forEach((child) => { | ||
| if (!isStandalone) return | ||
| const linkMark = child.isText | ||
| ? child.marks.find((mark) => mark.type.name === 'link') | ||
| : undefined | ||
| if (!linkMark) { | ||
| isStandalone = false | ||
| return | ||
| } | ||
| const childHref = linkMark.attrs.href as string | ||
| if (href === null) href = childHref | ||
| else if (href !== childHref) isStandalone = false | ||
| }) | ||
| return isStandalone ? href : null | ||
| } | ||
|
|
||
| function buildDecorations(doc: ProseMirrorNode): DecorationSet { | ||
| const decorations: Decoration[] = [] | ||
| /** Per-source occurrence count, so repeated embeds of the same URL get distinct, stable keys. */ | ||
| const sourceCounts = new Map<string, number>() | ||
| doc.descendants((node, pos) => { | ||
| if (node.type.name !== 'paragraph') return undefined | ||
| const href = getStandaloneLinkHref(node) | ||
| if (href) { | ||
| const embedInfo = getEmbedInfo(href) | ||
| if (embedInfo) { | ||
| const source = `embed:${embedInfo.type}:${embedInfo.url}` | ||
| const index = sourceCounts.get(source) ?? 0 | ||
| sourceCounts.set(source, index + 1) | ||
| decorations.push( | ||
| Decoration.widget(pos + node.nodeSize, () => createEmbedDom(embedInfo), { | ||
| side: 1, | ||
| key: `${source}:${index}`, | ||
| }) | ||
| ) | ||
| } | ||
| } | ||
| // Paragraphs hold only inline content, so there is nothing more to descend into. | ||
| return false | ||
| }) | ||
| return DecorationSet.create(doc, decorations) | ||
| } | ||
|
|
||
| /** | ||
| * Renders supported media links (YouTube, Vimeo, Spotify, Dropbox, …) as live players beneath a | ||
| * standalone link, in both the editing and read-only surfaces. Implemented as widget decorations | ||
| * so the underlying document stays a plain markdown link — embeds never enter the schema or the | ||
| * serialized markdown, keeping round-trips lossless. | ||
| */ | ||
| export const LinkEmbed = Extension.create({ | ||
| name: 'linkEmbed', | ||
|
|
||
| addProseMirrorPlugins() { | ||
| return [ | ||
| new Plugin({ | ||
| key: LINK_EMBED_PLUGIN_KEY, | ||
| state: { | ||
| init: (_, { doc }) => buildDecorations(doc), | ||
| apply: (tr, current) => (tr.docChanged ? buildDecorations(tr.doc) : current), | ||
| }, | ||
| props: { | ||
| decorations(state) { | ||
| return LINK_EMBED_PLUGIN_KEY.getState(state) | ||
| }, | ||
| }, | ||
| }), | ||
| ] | ||
| }, | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.