mirror of
https://github.com/Microsoft/vscode
synced 2024-09-13 21:55:38 +00:00
Support creating image in workspace when pasting image data into markdown (#160554)
For #157043
This commit is contained in:
parent
d2c241ffc0
commit
3729f509b5
|
@ -3,11 +3,17 @@
|
|||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as path from 'path';
|
||||
import * as vscode from 'vscode';
|
||||
import { tryGetUriListSnippet } from './dropIntoEditor';
|
||||
import { Utils } from 'vscode-uri';
|
||||
import { createUriListSnippet, tryGetUriListSnippet } from './dropIntoEditor';
|
||||
|
||||
export function registerPasteSupport(selector: vscode.DocumentSelector) {
|
||||
return vscode.languages.registerDocumentPasteEditProvider(selector, new class implements vscode.DocumentPasteEditProvider {
|
||||
const supportedImageMimes = new Set([
|
||||
'image/png',
|
||||
'image/jpg',
|
||||
]);
|
||||
|
||||
class PasteEditProvider implements vscode.DocumentPasteEditProvider {
|
||||
|
||||
async provideDocumentPasteEdits(
|
||||
document: vscode.TextDocument,
|
||||
|
@ -20,10 +26,76 @@ export function registerPasteSupport(selector: vscode.DocumentSelector) {
|
|||
return;
|
||||
}
|
||||
|
||||
for (const imageMime of supportedImageMimes) {
|
||||
const file = dataTransfer.get(imageMime)?.asFile();
|
||||
if (file) {
|
||||
const edit = await this.makeCreateImagePasteEdit(document, file, token);
|
||||
if (token.isCancellationRequested) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (edit) {
|
||||
return edit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const snippet = await tryGetUriListSnippet(document, dataTransfer, token);
|
||||
return snippet ? new vscode.DocumentPasteEdit(snippet) : undefined;
|
||||
}
|
||||
}, {
|
||||
pasteMimeTypes: ['text/uri-list']
|
||||
|
||||
private async makeCreateImagePasteEdit(document: vscode.TextDocument, file: vscode.DataTransferFile, token: vscode.CancellationToken): Promise<vscode.DocumentPasteEdit | undefined> {
|
||||
if (file.uri) {
|
||||
// If file is already in workspace, we don't want to create a copy of it
|
||||
const workspaceFolder = vscode.workspace.getWorkspaceFolder(file.uri);
|
||||
if (workspaceFolder) {
|
||||
const snippet = createUriListSnippet(document, [file.uri]);
|
||||
return snippet ? new vscode.DocumentPasteEdit(snippet) : undefined;
|
||||
}
|
||||
}
|
||||
|
||||
const uri = await this.getNewFileName(document, file);
|
||||
if (token.isCancellationRequested) {
|
||||
return;
|
||||
}
|
||||
|
||||
const snippet = createUriListSnippet(document, [uri]);
|
||||
if (!snippet) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Note that there is currently no way to undo the file creation :/
|
||||
const workspaceEdit = new vscode.WorkspaceEdit();
|
||||
workspaceEdit.createFile(uri, { contents: await file.data() });
|
||||
|
||||
const pasteEdit = new vscode.DocumentPasteEdit(snippet);
|
||||
pasteEdit.additionalEdit = workspaceEdit;
|
||||
return pasteEdit;
|
||||
}
|
||||
|
||||
private async getNewFileName(document: vscode.TextDocument, file: vscode.DataTransferFile): Promise<vscode.Uri> {
|
||||
const root = Utils.dirname(document.uri);
|
||||
|
||||
const ext = path.extname(file.name);
|
||||
const baseName = path.basename(file.name, ext);
|
||||
for (let i = 0; ; ++i) {
|
||||
const name = i === 0 ? baseName : `${baseName}-${i}`;
|
||||
const uri = vscode.Uri.joinPath(root, `${name}.${ext}`);
|
||||
try {
|
||||
await vscode.workspace.fs.stat(uri);
|
||||
} catch {
|
||||
// Does not exist
|
||||
return uri;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function registerPasteSupport(selector: vscode.DocumentSelector,) {
|
||||
return vscode.languages.registerDocumentPasteEditProvider(selector, new PasteEditProvider(), {
|
||||
pasteMimeTypes: [
|
||||
'text/uri-list',
|
||||
...supportedImageMimes,
|
||||
]
|
||||
});
|
||||
}
|
||||
|
|
|
@ -53,8 +53,12 @@ export async function tryGetUriListSnippet(document: vscode.TextDocument, dataTr
|
|||
}
|
||||
}
|
||||
|
||||
return createUriListSnippet(document, uris);
|
||||
}
|
||||
|
||||
export function createUriListSnippet(document: vscode.TextDocument, uris: readonly vscode.Uri[]): vscode.SnippetString | undefined {
|
||||
if (!uris.length) {
|
||||
return;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const dir = getDocumentDir(document);
|
||||
|
@ -74,7 +78,6 @@ export async function tryGetUriListSnippet(document: vscode.TextDocument, dataTr
|
|||
snippet.appendText(' ');
|
||||
}
|
||||
});
|
||||
|
||||
return snippet;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue