mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 21:09:43 +00:00
Small cleanups in markdown drop/paste (#189985)
- Observer cancellation more places - Create constants for mime types - Making code more consistent to show duplication
This commit is contained in:
parent
caebd5c3a7
commit
388d3683ea
|
@ -7,10 +7,10 @@ import * as vscode from 'vscode';
|
|||
import { MdLanguageClient } from './client/client';
|
||||
import { CommandManager } from './commandManager';
|
||||
import { registerMarkdownCommands } from './commands/index';
|
||||
import { registerPasteSupport } from './languageFeatures/copyFiles/copyPaste';
|
||||
import { registerLinkPasteSupport } from './languageFeatures/copyFiles/copyPasteLinks';
|
||||
import { registerPasteSupport } from './languageFeatures/copyFiles/pasteResourceProvider';
|
||||
import { registerLinkPasteSupport } from './languageFeatures/copyFiles/pasteUrlProvider';
|
||||
import { registerDiagnosticSupport } from './languageFeatures/diagnostics';
|
||||
import { registerDropIntoEditorSupport } from './languageFeatures/copyFiles/dropIntoEditor';
|
||||
import { registerDropIntoEditorSupport } from './languageFeatures/copyFiles/dropResourceProvider';
|
||||
import { registerFindFileReferenceSupport } from './languageFeatures/fileReferences';
|
||||
import { registerUpdateLinksOnRename } from './languageFeatures/linkUpdater';
|
||||
import { ILogger } from './logging';
|
||||
|
|
|
@ -4,14 +4,19 @@
|
|||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import { createEditForMediaFiles as createEditForMediaFiles, mediaMimes, tryGetUriListSnippet } from './shared';
|
||||
import { Mime, mediaMimes } from '../../util/mimes';
|
||||
import { Schemes } from '../../util/schemes';
|
||||
import { createEditForMediaFiles, tryGetUriListSnippet } from './shared';
|
||||
|
||||
|
||||
class MarkdownImageDropProvider implements vscode.DocumentDropEditProvider {
|
||||
class ResourceDropProvider implements vscode.DocumentDropEditProvider {
|
||||
|
||||
public static readonly id = 'insertLink';
|
||||
|
||||
public static readonly dropMimeTypes = [
|
||||
Mime.textUriList,
|
||||
...mediaMimes,
|
||||
];
|
||||
|
||||
private readonly _yieldTo = [
|
||||
{ mimeType: 'text/plain' },
|
||||
{ extensionId: 'vscode.ipynb', providerId: 'insertAttachment' },
|
||||
|
@ -36,10 +41,11 @@ class MarkdownImageDropProvider implements vscode.DocumentDropEditProvider {
|
|||
}
|
||||
|
||||
private async _getUriListEdit(document: vscode.TextDocument, dataTransfer: vscode.DataTransfer, token: vscode.CancellationToken): Promise<vscode.DocumentDropEdit | undefined> {
|
||||
const urlList = await dataTransfer.get('text/uri-list')?.asString();
|
||||
if (!urlList) {
|
||||
const urlList = await dataTransfer.get(Mime.textUriList)?.asString();
|
||||
if (!urlList || token.isCancellationRequested) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const snippet = await tryGetUriListSnippet(document, urlList, token);
|
||||
if (!snippet) {
|
||||
return undefined;
|
||||
|
@ -61,25 +67,19 @@ class MarkdownImageDropProvider implements vscode.DocumentDropEditProvider {
|
|||
return;
|
||||
}
|
||||
|
||||
const filesEdit = await createEditForMediaFiles(document, dataTransfer, token);
|
||||
if (!filesEdit) {
|
||||
const edit = await createEditForMediaFiles(document, dataTransfer, token);
|
||||
if (!edit) {
|
||||
return;
|
||||
}
|
||||
|
||||
const edit = new vscode.DocumentDropEdit(filesEdit.snippet);
|
||||
edit.label = filesEdit.label;
|
||||
edit.additionalEdit = filesEdit.additionalEdits;
|
||||
edit.yieldTo = this._yieldTo;
|
||||
return edit;
|
||||
const dropEdit = new vscode.DocumentDropEdit(edit.snippet);
|
||||
dropEdit.label = edit.label;
|
||||
dropEdit.additionalEdit = edit.additionalEdits;
|
||||
dropEdit.yieldTo = this._yieldTo;
|
||||
return dropEdit;
|
||||
}
|
||||
}
|
||||
|
||||
export function registerDropIntoEditorSupport(selector: vscode.DocumentSelector) {
|
||||
return vscode.languages.registerDocumentDropEditProvider(selector, new MarkdownImageDropProvider(), {
|
||||
id: MarkdownImageDropProvider.id,
|
||||
dropMimeTypes: [
|
||||
'text/uri-list',
|
||||
...mediaMimes,
|
||||
]
|
||||
});
|
||||
return vscode.languages.registerDocumentDropEditProvider(selector, new ResourceDropProvider(), ResourceDropProvider);
|
||||
}
|
|
@ -4,13 +4,19 @@
|
|||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import { Mime, mediaMimes } from '../../util/mimes';
|
||||
import { Schemes } from '../../util/schemes';
|
||||
import { createEditForMediaFiles, createEditAddingLinksForUriList, mediaMimes, getPasteUrlAsFormattedLinkSetting, PasteUrlAsFormattedLink } from './shared';
|
||||
import { PasteUrlAsFormattedLink, createEditAddingLinksForUriList, createEditForMediaFiles, getPasteUrlAsFormattedLinkSetting } from './shared';
|
||||
|
||||
class PasteEditProvider implements vscode.DocumentPasteEditProvider {
|
||||
class PasteResourceEditProvider implements vscode.DocumentPasteEditProvider {
|
||||
|
||||
public static readonly id = 'insertLink';
|
||||
|
||||
public static readonly pasteMimeTypes = [
|
||||
Mime.textUriList,
|
||||
...mediaMimes,
|
||||
];
|
||||
|
||||
private readonly _yieldTo = [
|
||||
{ mimeType: 'text/plain' },
|
||||
{ extensionId: 'vscode.ipynb', providerId: 'insertAttachment' },
|
||||
|
@ -32,19 +38,26 @@ class PasteEditProvider implements vscode.DocumentPasteEditProvider {
|
|||
return createEdit;
|
||||
}
|
||||
|
||||
const uriEdit = new vscode.DocumentPasteEdit('', '');
|
||||
const urlList = await dataTransfer.get('text/uri-list')?.asString();
|
||||
if (!urlList) {
|
||||
if (token.isCancellationRequested) {
|
||||
return;
|
||||
}
|
||||
|
||||
return this._getUriListEdit(document, ranges, dataTransfer, token);
|
||||
}
|
||||
|
||||
private async _getUriListEdit(document: vscode.TextDocument, ranges: readonly vscode.Range[], dataTransfer: vscode.DataTransfer, token: vscode.CancellationToken): Promise<vscode.DocumentPasteEdit | undefined> {
|
||||
const uriList = await dataTransfer.get(Mime.textUriList)?.asString();
|
||||
if (!uriList || token.isCancellationRequested) {
|
||||
return;
|
||||
}
|
||||
|
||||
const pasteUrlSetting = getPasteUrlAsFormattedLinkSetting(document);
|
||||
const pasteEdit = await createEditAddingLinksForUriList(document, ranges, urlList, false, pasteUrlSetting === PasteUrlAsFormattedLink.Smart, token);
|
||||
const pasteEdit = await createEditAddingLinksForUriList(document, ranges, uriList, false, pasteUrlSetting === PasteUrlAsFormattedLink.Smart, token);
|
||||
if (!pasteEdit) {
|
||||
return;
|
||||
}
|
||||
|
||||
uriEdit.label = pasteEdit.label;
|
||||
const uriEdit = new vscode.DocumentPasteEdit('', pasteEdit.label);
|
||||
uriEdit.additionalEdit = pasteEdit.additionalEdits;
|
||||
uriEdit.yieldTo = this._yieldTo;
|
||||
return uriEdit;
|
||||
|
@ -73,11 +86,5 @@ class PasteEditProvider implements vscode.DocumentPasteEditProvider {
|
|||
}
|
||||
|
||||
export function registerPasteSupport(selector: vscode.DocumentSelector,) {
|
||||
return vscode.languages.registerDocumentPasteEditProvider(selector, new PasteEditProvider(), {
|
||||
id: PasteEditProvider.id,
|
||||
pasteMimeTypes: [
|
||||
'text/uri-list',
|
||||
...mediaMimes,
|
||||
]
|
||||
});
|
||||
return vscode.languages.registerDocumentPasteEditProvider(selector, new PasteResourceEditProvider(), PasteResourceEditProvider);
|
||||
}
|
|
@ -4,14 +4,17 @@
|
|||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import { Mime } from '../../util/mimes';
|
||||
import { createEditAddingLinksForUriList, getPasteUrlAsFormattedLinkSetting, PasteUrlAsFormattedLink, validateLink } from './shared';
|
||||
|
||||
const textPlainMime = 'text/plain';
|
||||
|
||||
class PasteLinkEditProvider implements vscode.DocumentPasteEditProvider {
|
||||
class PasteUrlEditProvider implements vscode.DocumentPasteEditProvider {
|
||||
|
||||
public static readonly id = 'insertMarkdownLink';
|
||||
|
||||
public static readonly pasteMimeTypes = [
|
||||
Mime.textPlain,
|
||||
];
|
||||
|
||||
async provideDocumentPasteEdits(
|
||||
document: vscode.TextDocument,
|
||||
ranges: readonly vscode.Range[],
|
||||
|
@ -23,7 +26,7 @@ class PasteLinkEditProvider implements vscode.DocumentPasteEditProvider {
|
|||
return;
|
||||
}
|
||||
|
||||
const item = dataTransfer.get(textPlainMime);
|
||||
const item = dataTransfer.get(Mime.textPlain);
|
||||
const urlList = await item?.asString();
|
||||
if (token.isCancellationRequested || !urlList || !validateLink(urlList).isValid) {
|
||||
return;
|
||||
|
@ -36,14 +39,11 @@ class PasteLinkEditProvider implements vscode.DocumentPasteEditProvider {
|
|||
|
||||
const edit = new vscode.DocumentPasteEdit('', pasteEdit.label);
|
||||
edit.additionalEdit = pasteEdit.additionalEdits;
|
||||
edit.yieldTo = pasteEdit.markdownLink ? undefined : [{ mimeType: textPlainMime }];
|
||||
edit.yieldTo = pasteEdit.markdownLink ? undefined : [{ mimeType: Mime.textPlain }];
|
||||
return edit;
|
||||
}
|
||||
}
|
||||
|
||||
export function registerLinkPasteSupport(selector: vscode.DocumentSelector,) {
|
||||
return vscode.languages.registerDocumentPasteEditProvider(selector, new PasteLinkEditProvider(), {
|
||||
id: PasteLinkEditProvider.id,
|
||||
pasteMimeTypes: [textPlainMime]
|
||||
});
|
||||
return vscode.languages.registerDocumentPasteEditProvider(selector, new PasteUrlEditProvider(), PasteUrlEditProvider);
|
||||
}
|
|
@ -6,10 +6,11 @@
|
|||
import * as path from 'path';
|
||||
import * as vscode from 'vscode';
|
||||
import * as URI from 'vscode-uri';
|
||||
import { Schemes } from '../../util/schemes';
|
||||
import { NewFilePathGenerator } from './copyFiles';
|
||||
import { coalesce } from '../../util/arrays';
|
||||
import { getDocumentDir } from '../../util/document';
|
||||
import { mediaMimes } from '../../util/mimes';
|
||||
import { Schemes } from '../../util/schemes';
|
||||
import { NewFilePathGenerator } from './copyFiles';
|
||||
|
||||
enum MediaKind {
|
||||
Image,
|
||||
|
@ -49,19 +50,6 @@ export const mediaFileExtensions = new Map<string, MediaKind>([
|
|||
['wav', MediaKind.Audio],
|
||||
]);
|
||||
|
||||
export const mediaMimes = new Set([
|
||||
'image/bmp',
|
||||
'image/gif',
|
||||
'image/jpeg',
|
||||
'image/png',
|
||||
'image/webp',
|
||||
'video/mp4',
|
||||
'video/ogg',
|
||||
'audio/mpeg',
|
||||
'audio/aac',
|
||||
'audio/x-wav',
|
||||
]);
|
||||
|
||||
const smartPasteRegexes = [
|
||||
{ regex: /(\[[^\[\]]*](?:\([^\(\)]*\)|\[[^\[\]]*]))/g }, // In a Markdown link
|
||||
{ regex: /^```[\s\S]*?```$/gm }, // In a backtick fenced code block
|
||||
|
|
22
extensions/markdown-language-features/src/util/mimes.ts
Normal file
22
extensions/markdown-language-features/src/util/mimes.ts
Normal file
|
@ -0,0 +1,22 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
export const Mime = {
|
||||
textUriList: 'text/uri-list',
|
||||
textPlain: 'text/plain',
|
||||
} as const;
|
||||
|
||||
export const mediaMimes = new Set([
|
||||
'image/bmp',
|
||||
'image/gif',
|
||||
'image/jpeg',
|
||||
'image/png',
|
||||
'image/webp',
|
||||
'video/mp4',
|
||||
'video/ogg',
|
||||
'audio/mpeg',
|
||||
'audio/aac',
|
||||
'audio/x-wav',
|
||||
]);
|
Loading…
Reference in a new issue