mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 21:09:43 +00:00
Improve labels when dropping resources into markdown files (#180260)
This commit is contained in:
parent
d533996b89
commit
f6de066b4c
|
@ -85,7 +85,7 @@ function createInsertLinkEdit(activeEditor: vscode.TextEditor, selectedFiles: vs
|
|||
separator: insertAsImage ? '\n' : ' ',
|
||||
});
|
||||
|
||||
return snippet ? new vscode.SnippetTextEdit(selection, snippet) : undefined;
|
||||
return snippet ? new vscode.SnippetTextEdit(selection, snippet.snippet) : undefined;
|
||||
}));
|
||||
|
||||
const edit = new vscode.WorkspaceEdit();
|
||||
|
|
|
@ -55,7 +55,7 @@ class PasteEditProvider implements vscode.DocumentPasteEditProvider {
|
|||
const workspaceFolder = vscode.workspace.getWorkspaceFolder(file.uri);
|
||||
if (workspaceFolder) {
|
||||
const snippet = createUriListSnippet(document, [file.uri]);
|
||||
return snippet ? new vscode.DocumentPasteEdit(snippet) : undefined;
|
||||
return snippet ? new vscode.DocumentPasteEdit(snippet.snippet) : undefined;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -73,7 +73,7 @@ class PasteEditProvider implements vscode.DocumentPasteEditProvider {
|
|||
const workspaceEdit = new vscode.WorkspaceEdit();
|
||||
workspaceEdit.createFile(uri, { contents: file });
|
||||
|
||||
const pasteEdit = new vscode.DocumentPasteEdit(snippet);
|
||||
const pasteEdit = new vscode.DocumentPasteEdit(snippet.snippet);
|
||||
pasteEdit.additionalEdit = workspaceEdit;
|
||||
return pasteEdit;
|
||||
}
|
||||
|
|
|
@ -69,17 +69,7 @@ export async function tryGetUriListSnippet(document: vscode.TextDocument, dataTr
|
|||
}
|
||||
}
|
||||
|
||||
const snippet = createUriListSnippet(document, uris);
|
||||
if (!snippet) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
snippet: snippet,
|
||||
label: uris.length > 1
|
||||
? vscode.l10n.t('Insert uri links')
|
||||
: vscode.l10n.t('Insert uri link')
|
||||
};
|
||||
return createUriListSnippet(document, uris);
|
||||
}
|
||||
|
||||
interface UriListSnippetOptions {
|
||||
|
@ -97,7 +87,8 @@ interface UriListSnippetOptions {
|
|||
readonly separator?: string;
|
||||
}
|
||||
|
||||
export function createUriListSnippet(document: vscode.TextDocument, uris: readonly vscode.Uri[], options?: UriListSnippetOptions): vscode.SnippetString | undefined {
|
||||
|
||||
export function createUriListSnippet(document: vscode.TextDocument, uris: readonly vscode.Uri[], options?: UriListSnippetOptions): { snippet: vscode.SnippetString; label: string } | undefined {
|
||||
if (!uris.length) {
|
||||
return undefined;
|
||||
}
|
||||
|
@ -105,6 +96,10 @@ export function createUriListSnippet(document: vscode.TextDocument, uris: readon
|
|||
const dir = getDocumentDir(document);
|
||||
|
||||
const snippet = new vscode.SnippetString();
|
||||
|
||||
let insertedLinkCount = 0;
|
||||
let insertedImageCount = 0;
|
||||
|
||||
uris.forEach((uri, i) => {
|
||||
const mdPath = getMdPath(dir, uri);
|
||||
|
||||
|
@ -113,10 +108,17 @@ export function createUriListSnippet(document: vscode.TextDocument, uris: readon
|
|||
const insertAsVideo = videoFileExtensions.has(ext);
|
||||
|
||||
if (insertAsVideo) {
|
||||
insertedImageCount++;
|
||||
snippet.appendText(`<video src="${mdPath}" controls title="`);
|
||||
snippet.appendPlaceholder('Title');
|
||||
snippet.appendText('"></video>');
|
||||
} else {
|
||||
if (insertAsImage) {
|
||||
insertedImageCount++;
|
||||
} else {
|
||||
insertedLinkCount++;
|
||||
}
|
||||
|
||||
snippet.appendText(insertAsImage ? '![' : '[');
|
||||
|
||||
const placeholderText = options?.placeholderText ?? (insertAsImage ? 'Alt text' : 'label');
|
||||
|
@ -130,7 +132,21 @@ export function createUriListSnippet(document: vscode.TextDocument, uris: readon
|
|||
snippet.appendText(options?.separator ?? ' ');
|
||||
}
|
||||
});
|
||||
return snippet;
|
||||
|
||||
let label: string;
|
||||
if (insertedImageCount > 0 && insertedLinkCount > 0) {
|
||||
label = vscode.l10n.t('Insert Markdown images and links');
|
||||
} else if (insertedImageCount > 0) {
|
||||
label = insertedImageCount > 1
|
||||
? vscode.l10n.t('Insert Markdown images')
|
||||
: vscode.l10n.t('Insert Markdown image');
|
||||
} else {
|
||||
label = insertedLinkCount > 1
|
||||
? vscode.l10n.t('Insert Markdown links')
|
||||
: vscode.l10n.t('Insert Markdown link');
|
||||
}
|
||||
|
||||
return { snippet, label };
|
||||
}
|
||||
|
||||
function getMdPath(dir: vscode.Uri | undefined, file: vscode.Uri) {
|
||||
|
|
Loading…
Reference in a new issue