mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 21:09:43 +00:00
Standardize text/uri-list handling (#152756)
This creates a common set of functions for creating and parsing the `text/uri-list` mime type. As part of this, I also aligned us with the standard, which uses `\r\n` between lines instead of just `\n`
This commit is contained in:
parent
09fa37468c
commit
321423db2b
|
@ -44,7 +44,7 @@ export async function tryGetUriListSnippet(document: vscode.TextDocument, dataTr
|
|||
}
|
||||
|
||||
const uris: vscode.Uri[] = [];
|
||||
for (const resource of urlList.split('\n')) {
|
||||
for (const resource of urlList.split('\r\n')) {
|
||||
try {
|
||||
uris.push(vscode.Uri.parse(resource));
|
||||
} catch {
|
||||
|
|
|
@ -56,8 +56,7 @@ export function addExternalEditorsDropData(dataTransfer: VSDataTransfer, dragEve
|
|||
}
|
||||
|
||||
if (editorData.length) {
|
||||
const str = distinct(editorData).join('\n');
|
||||
dataTransfer.replace(Mimes.uriList, createStringDataTransferItem(str));
|
||||
dataTransfer.replace(Mimes.uriList, createStringDataTransferItem(UriList.create(editorData)));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -65,3 +64,13 @@ export function addExternalEditorsDropData(dataTransfer: VSDataTransfer, dragEve
|
|||
dataTransfer.delete(internal);
|
||||
}
|
||||
}
|
||||
|
||||
export const UriList = Object.freeze({
|
||||
// http://amundsen.com/hypermedia/urilist/
|
||||
create: (entries: ReadonlyArray<string | URI>): string => {
|
||||
return distinct(entries.map(x => x.toString())).join('\r\n');
|
||||
},
|
||||
parse: (str: string): string[] => {
|
||||
return str.split('\r\n').filter(value => !value.startsWith('#'));
|
||||
}
|
||||
});
|
||||
|
|
|
@ -10,7 +10,7 @@ import { createStringDataTransferItem, VSDataTransfer } from 'vs/base/common/dat
|
|||
import { Disposable } from 'vs/base/common/lifecycle';
|
||||
import { Mimes } from 'vs/base/common/mime';
|
||||
import { generateUuid } from 'vs/base/common/uuid';
|
||||
import { toVSDataTransfer } from 'vs/editor/browser/dnd';
|
||||
import { toVSDataTransfer, UriList } from 'vs/editor/browser/dnd';
|
||||
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
|
||||
import { IBulkEditService, ResourceEdit } from 'vs/editor/browser/services/bulkEditService';
|
||||
import { EditorOption } from 'vs/editor/common/config/editorOptions';
|
||||
|
@ -176,8 +176,7 @@ export class CopyPasteController extends Disposable implements IEditorContributi
|
|||
if (!dataTransfer.has(Mimes.uriList)) {
|
||||
const resources = await this._clipboardService.readResources();
|
||||
if (resources.length) {
|
||||
const value = resources.join('\n');
|
||||
dataTransfer.append(Mimes.uriList, createStringDataTransferItem(value));
|
||||
dataTransfer.append(Mimes.uriList, createStringDataTransferItem(UriList.create(resources)));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ import { Disposable } from 'vs/base/common/lifecycle';
|
|||
import { Mimes } from 'vs/base/common/mime';
|
||||
import { relativePath } from 'vs/base/common/resources';
|
||||
import { URI } from 'vs/base/common/uri';
|
||||
import { addExternalEditorsDropData, toVSDataTransfer } from 'vs/editor/browser/dnd';
|
||||
import { addExternalEditorsDropData, toVSDataTransfer, UriList } from 'vs/editor/browser/dnd';
|
||||
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
|
||||
import { registerEditorContribution } from 'vs/editor/browser/editorExtensions';
|
||||
import { IBulkEditService, ResourceEdit } from 'vs/editor/browser/services/bulkEditService';
|
||||
|
@ -137,9 +137,9 @@ class DefaultOnDropProvider implements DocumentOnDropEditProvider {
|
|||
return undefined;
|
||||
}
|
||||
|
||||
private getUriListInsertText(urlList: string): string | undefined {
|
||||
private getUriListInsertText(strUriList: string): string | undefined {
|
||||
const uris: URI[] = [];
|
||||
for (const resource of urlList.split('\n')) {
|
||||
for (const resource of UriList.parse(strUriList)) {
|
||||
try {
|
||||
uris.push(URI.parse(resource));
|
||||
} catch {
|
||||
|
|
|
@ -19,6 +19,7 @@ import { FileAccess, Schemas } from 'vs/base/common/network';
|
|||
import { isWindows } from 'vs/base/common/platform';
|
||||
import { basename, isEqual } from 'vs/base/common/resources';
|
||||
import { URI } from 'vs/base/common/uri';
|
||||
import { UriList } from 'vs/editor/browser/dnd';
|
||||
import { CodeDataTransfers, createDraggedEditorInputFromRawResourcesData, Extensions, extractEditorsAndFilesDropData, IDragAndDropContributionRegistry, IDraggedResourceEditorInput, IResourceStat } from 'vs/platform/dnd/browser/dnd';
|
||||
import { IFileService } from 'vs/platform/files/common/files';
|
||||
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
|
||||
|
@ -60,7 +61,7 @@ export async function extractTreeDropData(dataTransfer: VSDataTransfer): Promise
|
|||
if (dataTransfer.has(resourcesKey)) {
|
||||
try {
|
||||
const asString = await dataTransfer.get(resourcesKey)?.asString();
|
||||
const rawResourcesData = JSON.stringify(asString?.split('\n').filter(value => !value.startsWith('#')));
|
||||
const rawResourcesData = JSON.stringify(UriList.parse(asString ?? ''));
|
||||
editors.push(...createDraggedEditorInputFromRawResourcesData(rawResourcesData));
|
||||
} catch (error) {
|
||||
// Invalid transfer
|
||||
|
@ -70,11 +71,6 @@ export async function extractTreeDropData(dataTransfer: VSDataTransfer): Promise
|
|||
return editors;
|
||||
}
|
||||
|
||||
export function convertResourceUrlsToUriList(resourceUrls: string): string {
|
||||
const asJson: URI[] = JSON.parse(resourceUrls);
|
||||
return asJson.map(uri => uri.toString()).join('\n');
|
||||
}
|
||||
|
||||
export interface IResourcesDropHandlerOptions {
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue