Drop into text editor api proposal (#143334)

* Add drop into editor api proposal

For #142990

* Use AsyncEmitter
This commit is contained in:
Matt Bierner
2022-03-18 10:32:11 -08:00
committed by GitHub
parent b906904089
commit bfc026b97a
29 changed files with 402 additions and 121 deletions

View File

@@ -3,7 +3,9 @@
* 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';
import { CommandManager } from './commandManager';
import * as commands from './commands/index';
import LinkProvider from './features/documentLinkProvider';
@@ -45,6 +47,43 @@ export function activate(context: vscode.ExtensionContext) {
logger.updateConfiguration();
previewManager.updateConfiguration();
}));
context.subscriptions.push(vscode.workspace.onWillDropOnTextEditor(e => {
e.waitUntil((async () => {
const resourceUrls = await e.dataTransfer.get('resourceurls')?.asString();
if (!resourceUrls) {
return;
}
const uris: vscode.Uri[] = [];
for (const resource of JSON.parse(resourceUrls)) {
try {
uris.push(vscode.Uri.parse(resource));
} catch {
// noop
}
}
if (!uris.length) {
return;
}
const snippet = new vscode.SnippetString();
uris.forEach((uri, i) => {
const rel = path.relative(URI.Utils.dirname(e.editor.document.uri).fsPath, uri.fsPath);
snippet.appendText('[');
snippet.appendTabstop();
snippet.appendText(`](${rel})`);
if (i <= uris.length - 1 && uris.length > 1) {
snippet.appendText(' ');
}
});
return e.editor.insertSnippet(snippet, e.position);
})());
}));
}
function registerMarkdownLanguageFeatures(