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 insert image/link for untitled files #163897

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as nls from 'vscode-nls';
import { Command } from '../commandManager';
import { createUriListSnippet, getParentDocumentUri, imageFileExtensions } from '../languageFeatures/dropIntoEditor';
import { coalesce } from '../util/arrays';
import { Schemes } from '../util/schemes';

const localize = nls.loadMessageBundle();

Expand All @@ -27,7 +28,7 @@ export class InsertLinkFromWorkspace implements Command {
canSelectMany: true,
openLabel: localize('insertLink.openLabel', "Insert link"),
title: localize('insertLink.title', "Insert link"),
defaultUri: getParentDocumentUri(activeEditor.document),
defaultUri: getDefaultUri(activeEditor.document),
});

return insertLink(activeEditor, resources ?? [], false);
Expand All @@ -52,13 +53,21 @@ export class InsertImageFromWorkspace implements Command {
},
openLabel: localize('insertImage.openLabel', "Insert image"),
title: localize('insertImage.title', "Insert image"),
defaultUri: getParentDocumentUri(activeEditor.document),
defaultUri: getDefaultUri(activeEditor.document),
});

return insertLink(activeEditor, resources ?? [], true);
}
}

function getDefaultUri(document: vscode.TextDocument) {
const docUri = getParentDocumentUri(document);
if (docUri.scheme === Schemes.untitled) {
return vscode.workspace.workspaceFolders?.[0]?.uri;
}
return docUri;
}

async function insertLink(activeEditor: vscode.TextEditor, selectedFiles: vscode.Uri[], insertAsImage: boolean): Promise<void> {
if (!selectedFiles.length) {
return;
Expand Down