Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
647c498ef831304aa6e9d427f8d4File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| } |
| 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() | ||
| }) | ||
| }) |
| 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) | ||
| }, | ||
| }, | ||
| }), | ||
| ] | ||
| }, | ||
| }) | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.