planning
All checks were successful
Publish To Prod / deploy_and_publish (push) Successful in 35s

This commit is contained in:
2024-10-14 09:15:30 +02:00
parent bcba00a730
commit 6e64e138e2
21059 changed files with 2317811 additions and 1 deletions

View File

@@ -0,0 +1,13 @@
import { NativeItemConfig } from './nativeTypesConfig';
import { DragDropMonitor } from 'dnd-core';
export declare class NativeDragSource {
item: any;
private config;
constructor(config: NativeItemConfig);
private initializeExposedProperties;
loadDataTransfer(dataTransfer: DataTransfer | null | undefined): void;
canDrag(): boolean;
beginDrag(): any;
isDragging(monitor: DragDropMonitor, handle: string): boolean;
endDrag(): void;
}

View File

@@ -0,0 +1,47 @@
export class NativeDragSource {
item;
config;
constructor(config) {
this.config = config;
this.item = {};
this.initializeExposedProperties();
}
initializeExposedProperties() {
Object.keys(this.config.exposeProperties).forEach((property) => {
Object.defineProperty(this.item, property, {
configurable: true,
enumerable: true,
get() {
// eslint-disable-next-line no-console
console.warn(`Browser doesn't allow reading "${property}" until the drop event.`);
return null;
},
});
});
}
loadDataTransfer(dataTransfer) {
if (dataTransfer) {
const newProperties = {};
Object.keys(this.config.exposeProperties).forEach((property) => {
newProperties[property] = {
value: this.config.exposeProperties[property](dataTransfer, this.config.matchesTypes),
configurable: true,
enumerable: true,
};
});
Object.defineProperties(this.item, newProperties);
}
}
canDrag() {
return true;
}
beginDrag() {
return this.item;
}
isDragging(monitor, handle) {
return handle === monitor.getSourceId();
}
endDrag() {
// empty
}
}

View File

@@ -0,0 +1 @@
export declare function getDataFromDataTransfer(dataTransfer: DataTransfer, typesToTry: string[], defaultValue: string): string;

View File

@@ -0,0 +1,4 @@
export function getDataFromDataTransfer(dataTransfer, typesToTry, defaultValue) {
const result = typesToTry.reduce((resultSoFar, typeToTry) => resultSoFar || dataTransfer.getData(typeToTry), '');
return result != null ? result : defaultValue;
}

View File

@@ -0,0 +1,3 @@
import { NativeDragSource } from './NativeDragSource';
export declare function createNativeDragSource(type: string, dataTransfer?: DataTransfer): NativeDragSource;
export declare function matchNativeItemType(dataTransfer: DataTransfer | null): string | null;

View File

@@ -0,0 +1,17 @@
import { nativeTypesConfig } from './nativeTypesConfig';
import { NativeDragSource } from './NativeDragSource';
export function createNativeDragSource(type, dataTransfer) {
const result = new NativeDragSource(nativeTypesConfig[type]);
result.loadDataTransfer(dataTransfer);
return result;
}
export function matchNativeItemType(dataTransfer) {
if (!dataTransfer) {
return null;
}
const dataTransferTypes = Array.prototype.slice.call(dataTransfer.types || []);
return (Object.keys(nativeTypesConfig).filter((nativeItemType) => {
const { matchesTypes } = nativeTypesConfig[nativeItemType];
return matchesTypes.some((t) => dataTransferTypes.indexOf(t) > -1);
})[0] || null);
}

View File

@@ -0,0 +1,10 @@
export interface NativeItemConfigExposePropreties {
[property: string]: (dataTransfer: DataTransfer, matchesTypes: string[]) => any;
}
export interface NativeItemConfig {
exposeProperties: NativeItemConfigExposePropreties;
matchesTypes: string[];
}
export declare const nativeTypesConfig: {
[key: string]: NativeItemConfig;
};

View File

@@ -0,0 +1,33 @@
import * as NativeTypes from '../NativeTypes';
import { getDataFromDataTransfer } from './getDataFromDataTransfer';
export const nativeTypesConfig = {
[NativeTypes.FILE]: {
exposeProperties: {
files: (dataTransfer) => Array.prototype.slice.call(dataTransfer.files),
items: (dataTransfer) => dataTransfer.items,
dataTransfer: (dataTransfer) => dataTransfer,
},
matchesTypes: ['Files'],
},
[NativeTypes.HTML]: {
exposeProperties: {
html: (dataTransfer, matchesTypes) => getDataFromDataTransfer(dataTransfer, matchesTypes, ''),
dataTransfer: (dataTransfer) => dataTransfer,
},
matchesTypes: ['Html', 'text/html'],
},
[NativeTypes.URL]: {
exposeProperties: {
urls: (dataTransfer, matchesTypes) => getDataFromDataTransfer(dataTransfer, matchesTypes, '').split('\n'),
dataTransfer: (dataTransfer) => dataTransfer,
},
matchesTypes: ['Url', 'text/uri-list'],
},
[NativeTypes.TEXT]: {
exposeProperties: {
text: (dataTransfer, matchesTypes) => getDataFromDataTransfer(dataTransfer, matchesTypes, ''),
dataTransfer: (dataTransfer) => dataTransfer,
},
matchesTypes: ['Text', 'text/plain'],
},
};