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

Add 'open image' context in markdown preview #234649

Merged
merged 4 commits into from
Dec 17, 2024

Conversation

notoriousmango
Copy link
Contributor

resolve #184553

This pull request aims to check whether an img.src is local or not, given that a local src attribute is automatically changed to a URI internally.
I've utilized the fact that any non-local src attribute will not be altered, providing a basis for differentiation.
I'm open to any suggestions for better ways to achieve this.

@notoriousmango
Copy link
Contributor Author

@microsoft-github-policy-service agree

Copy link
Collaborator

@mjbvz mjbvz left a comment

Choose a reason for hiding this comment

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

Thanks for taking a look. Looks good overall, mainly just want to keep most of the code in the command itself

@@ -679,6 +686,12 @@ export class DynamicMarkdownPreview extends Disposable implements IManagedMarkdo
});
}

openImage(imagePath: string): void {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's move this code into the command. The second argument to vscode.open is a ViewColumn if you need to open in a specific editor group


public execute(args: { resource: string, imageSource: string }) {
const source = vscode.Uri.parse(args.resource);
const { fsPath } = vscode.Uri.parse(args.imageSource);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Keep this as a uri. Otherwise it will only work for file: images

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking the user can only open local images so It should have fsPath. What is other possible options?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Local in this case would mean part of the workspace, but the workspace could be a virtual file system or a remote machine. Just using fsPath won't work in these cases

@mjbvz mjbvz added this to the November 2024 milestone Nov 26, 2024
@notoriousmango notoriousmango requested a review from mjbvz November 27, 2024 08:24

public execute(args: { resource: string; imageSource: string }) {
const source = vscode.Uri.parse(args.resource);
const imageSourceUri = vscode.Uri.file(vscode.Uri.parse(args.imageSource).path);
Copy link
Collaborator

Choose a reason for hiding this comment

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

You should only need a single call to vscode.Uri.parse. Using vscode.Uri.file again means it will only work if the workspace is on the local file system

If parse isn't enough, try seeing if you can pass the fill string uri to it. It should be something like: file:///path/to/workspace/image.png

public execute(args: { resource: string; imageSource: string }) {
const source = vscode.Uri.parse(args.resource);
const imageSourceUri = vscode.Uri.file(vscode.Uri.parse(args.imageSource).path);
vscode.commands.executeCommand('vscode.open', imageSourceUri, this._webviewManager.findPreview(source));
Copy link
Collaborator

@mjbvz mjbvz Nov 27, 2024

Choose a reason for hiding this comment

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

findPreview returns a preview which wont be understood by vscode.open. Instead you want to get the view column where the preview is showing. This isn't exposed today but can be accessed from the WebviewPanel stored on a MarkdownPreview.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am reusing openDocumentLink. I think It covers what I am trying to do

@mjbvz mjbvz modified the milestones: November 2024, January 2025 Dec 2, 2024
@mjbvz
Copy link
Collaborator

mjbvz commented Dec 11, 2024

@microsoft-github-policy-service rerun

@notoriousmango notoriousmango requested a review from mjbvz December 14, 2024 15:29
@mjbvz mjbvz enabled auto-merge December 17, 2024 19:49
@mjbvz mjbvz merged commit 4559089 into microsoft:main Dec 17, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

markdown preview: allow opening images in a new editor
4 participants