mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-20 16:49:06 +01:00
For #30066 This adds a new `documentPaste` api proposal that lets extensions hook into copy and paste. This can be used to do things such as: - Create link when pasting an image - Bring along imports when copy and pasting code
77 lines
2.4 KiB
TypeScript
77 lines
2.4 KiB
TypeScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* 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 * as URI from 'vscode-uri';
|
|
|
|
const imageFileExtensions = new Set<string>([
|
|
'.bmp',
|
|
'.gif',
|
|
'.ico',
|
|
'.jpe',
|
|
'.jpeg',
|
|
'.jpg',
|
|
'.png',
|
|
'.psd',
|
|
'.svg',
|
|
'.tga',
|
|
'.tif',
|
|
'.tiff',
|
|
'.webp',
|
|
]);
|
|
|
|
export function registerDropIntoEditor(selector: vscode.DocumentSelector) {
|
|
return vscode.languages.registerDocumentOnDropEditProvider(selector, new class implements vscode.DocumentOnDropEditProvider {
|
|
async provideDocumentOnDropEdits(document: vscode.TextDocument, position: vscode.Position, dataTransfer: vscode.DataTransfer, token: vscode.CancellationToken): Promise<vscode.SnippetTextEdit | undefined> {
|
|
const enabled = vscode.workspace.getConfiguration('markdown', document).get('editor.drop.enabled', true);
|
|
if (!enabled) {
|
|
return;
|
|
}
|
|
|
|
const replacementRange = new vscode.Range(position, position);
|
|
return tryInsertUriList(document, replacementRange, dataTransfer, token);
|
|
}
|
|
});
|
|
}
|
|
|
|
export async function tryInsertUriList(document: vscode.TextDocument, replacementRange: vscode.Range, dataTransfer: vscode.DataTransfer, token: vscode.CancellationToken): Promise<vscode.SnippetTextEdit | undefined> {
|
|
const urlList = await dataTransfer.get('text/uri-list')?.asString();
|
|
if (!urlList || token.isCancellationRequested) {
|
|
return undefined;
|
|
}
|
|
|
|
const uris: vscode.Uri[] = [];
|
|
for (const resource of urlList.split('\n')) {
|
|
try {
|
|
uris.push(vscode.Uri.parse(resource));
|
|
} catch {
|
|
// noop
|
|
}
|
|
}
|
|
|
|
if (!uris.length) {
|
|
return;
|
|
}
|
|
|
|
const snippet = new vscode.SnippetString();
|
|
uris.forEach((uri, i) => {
|
|
const mdPath = document.uri.scheme === uri.scheme
|
|
? encodeURI(path.relative(URI.Utils.dirname(document.uri).fsPath, uri.fsPath).replace(/\\/g, '/'))
|
|
: uri.toString(false);
|
|
|
|
const ext = URI.Utils.extname(uri).toLowerCase();
|
|
snippet.appendText(imageFileExtensions.has(ext) ? '`);
|
|
|
|
if (i <= uris.length - 1 && uris.length > 1) {
|
|
snippet.appendText(' ');
|
|
}
|
|
});
|
|
|
|
return new vscode.SnippetTextEdit(replacementRange, snippet);
|
|
}
|