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

fix(markdown-preview/copy-image): invalid error with blob when download image. #233497

Closed
wants to merge 2 commits into from
Closed

fix(markdown-preview/copy-image): invalid error with blob when download image. #233497

wants to merge 2 commits into from

Conversation

Felipeex
Copy link

@Felipeex Felipeex commented Nov 9, 2024

When you copied an image with an external link, it gave a CORS error.

Screenshot 2024-11-09 at 15 32 21

Since it wasn't possible to fix the CORS issue directly to resolve this problem, instead of copying an image from an external source, it now copies the link to your clipboard so you can access the image. For local images, it still copies normally like before.

before:

Screen.Recording.2024-11-10.at.13.20.31.mov

after:

Screen.Recording.2024-11-10.at.13.22.08.mov

issue: #205624

@Felipeex Felipeex changed the title fix(markdown-preview/copy-image): invalid error with blob when downlo… fix(markdown-preview/copy-image): invalid error with blob when download image. Nov 9, 2024
@Felipeex
Copy link
Author

Felipeex commented Nov 9, 2024

@Felipeex please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"

Contributor License Agreement

@microsoft-github-policy-service agree

@@ -11,6 +11,7 @@ import { SettingsManager, getData } from './settings';
import throttle = require('lodash.throttle');
import morphdom from 'morphdom';
import type { ToWebviewMessage } from '../types/previewMessaging';
import { isURL } from '../src/util/is-url';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code here not access the parent directory. Instead only load from preview-src

'image/png': new Promise((resolve) => {
const imageSrc = image.getAttribute('data-src') || 'src-not-found';

if (isURL(imageSrc)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this try the image and then only write text if that fails?

const imageSrc = image.getAttribute('data-src') || 'src-not-found';

if (isURL(imageSrc)) {
await navigator.clipboard.writeText(imageSrc);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of writing text, maybe use apis to select the image and then use execCommand('copy') tocopy it. It still won't write out image data but it will better match what browser do. Most of them write out text/html too for example

@@ -16,6 +16,10 @@ export class CopyImageCommand implements Command {

public execute(args: { id: string; resource: string }) {
const source = vscode.Uri.parse(args.resource);
this._webviewManager.findPreview(source)?.copyImage(args.id);
const imagePreview = this._webviewManager.findPreview(source);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this

})]);
});

await navigator.clipboard.write([new ClipboardItem({ 'image/png': blobImage })]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd revert this too. You want to call write early so that browsers know there is pending image data. Now it will instead wait for the blob to be created and only then write the clipboard. It's possible that clipboard changes between those steps

@Felipeex Felipeex closed this Nov 12, 2024
@Felipeex Felipeex deleted the fix/preview-markdown-error branch November 14, 2024 14:48
@vs-code-engineering vs-code-engineering bot locked and limited conversation to collaborators Dec 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants