Repository navigation
Support video and native context-menu media paste in Markdown #341552
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
Henning Dieterichs (hediet)
merged 5 commits into
main
from
hediet/markdown-media-paste
Oct 15, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f6ce382
Support video and native context-menu media paste in Markdown
hediet f5f64f0
Restore webview input focus before native clipboard commands
hediet 4e89d5a
Use lazy bounded Markdown clipboard media transfers
hediet 4067504
Use the published Markdown editor native-paste fixes
hediet 64a4f45
Merge branch 'main' into hediet/markdown-media-paste
hediet 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
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
16 changes: 16 additions & 0 deletions
16
extensions/markdown-language-features/markdown-editor-src/clipboardMedia.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,16 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import type { NativeClipboardData } from '@vscode/markdown-editor'; | ||
|
|
||
| export const clipboardFormatsUnavailableMessage = 'The clipboard formats are not available to this editor. Try copying the media itself instead of its file.'; | ||
|
|
||
| export function getNativeClipboardMedia(data: NativeClipboardData, plainText: boolean): { readonly text: string; readonly images: readonly File[] } { | ||
| const images = plainText ? [] : data.files.filter(file => /^(image|video|audio)\//.test(file.type)); | ||
| if (!plainText && data.files.length && !images.length && !data.text) { | ||
| throw new Error(clipboardFormatsUnavailableMessage); | ||
| } | ||
| return { text: data.text, images }; | ||
| } | ||
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
48 changes: 48 additions & 0 deletions
48
extensions/markdown-language-features/scripts/clipboardMedia.test.mts
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,48 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import assert from 'node:assert/strict'; | ||
| import { test } from 'node:test'; | ||
| import { getNativeClipboardMedia } from '../markdown-editor-src/clipboardMedia.ts'; | ||
|
|
||
| test('preserves native image, video, and audio file identity and bytes', async () => { | ||
|
hediet marked this conversation as resolved.
Resolved
|
||
| const files = [ | ||
| new File([new Uint8Array([1, 2, 3])], 'image.png', { type: 'image/png' }), | ||
| new File([new Uint8Array([4, 5, 6])], 'clip.mp4', { type: 'video/mp4' }), | ||
| new File([new Uint8Array([7, 8, 9])], 'sound.mp3', { type: 'audio/mpeg' }), | ||
| ]; | ||
| const result = getNativeClipboardMedia({ text: 'fallback', files }, false); | ||
| assert.deepStrictEqual({ | ||
| text: result.text, | ||
| files: await Promise.all(result.images.map(async file => ({ name: file.name, type: file.type, bytes: [...new Uint8Array(await file.arrayBuffer())] }))), | ||
| }, { | ||
| text: 'fallback', | ||
| files: [ | ||
| { name: 'image.png', type: 'image/png', bytes: [1, 2, 3] }, | ||
| { name: 'clip.mp4', type: 'video/mp4', bytes: [4, 5, 6] }, | ||
| { name: 'sound.mp3', type: 'audio/mpeg', bytes: [7, 8, 9] }, | ||
| ], | ||
| }); | ||
| assert.ok(result.images.every((file, index) => file === files[index])); | ||
| }); | ||
|
|
||
| test('reports unsupported and untyped native file-only payloads explicitly', () => { | ||
| for (const type of ['application/octet-stream', '']) { | ||
| assert.throws(() => getNativeClipboardMedia({ text: '', files: [new File(['data'], 'file.bin', { type })] }, false), /clipboard formats are not available/); | ||
| } | ||
| }); | ||
|
|
||
| test('preserves plain-text fallback, empty clipboard, and explicit plain-text paste', () => { | ||
| const files = [new File(['data'], 'file.bin')]; | ||
| assert.deepStrictEqual([ | ||
| getNativeClipboardMedia({ text: 'fallback', files }, false), | ||
| getNativeClipboardMedia({ text: '', files }, true), | ||
| getNativeClipboardMedia({ text: '', files: [] }, false), | ||
| ], [ | ||
| { text: 'fallback', images: [] }, | ||
| { text: '', images: [] }, | ||
| { text: '', images: [] }, | ||
| ]); | ||
| }); | ||
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.