node_modules ignore

This commit is contained in:
oier committed 2025-05-08 23:43:47 +02:00
1 parent e19d52f172
commit 4574544c9f
65041 files changed
+10593536

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
import type { Action, Store } from 'redux';
import type { Backend, DragDropActions, DragDropManager, DragDropMonitor, HandlerRegistry } from '../interfaces.js';
import type { State } from '../reducers/index.js';
export declare class DragDropManagerImpl implements DragDropManager {
private store;
private monitor;
private backend;
private isSetUp;
constructor(store: Store<State>, monitor: DragDropMonitor);
receiveBackend(backend: Backend): void;
getMonitor(): DragDropMonitor;
getBackend(): Backend;
getRegistry(): HandlerRegistry;
getActions(): DragDropActions;
dispatch(action: Action<any>): void;
private handleRefCountChange;
}
+56
View File
@@ -0,0 +1,56 @@
import { createDragDropActions } from '../actions/dragDrop/index.js';
export class DragDropManagerImpl {
receiveBackend(backend) {
this.backend = backend;
}
getMonitor() {
return this.monitor;
}
getBackend() {
return this.backend;
}
getRegistry() {
return this.monitor.registry;
}
getActions() {
/* eslint-disable-next-line @typescript-eslint/no-this-alias */ const manager = this;
const { dispatch } = this.store;
function bindActionCreator(actionCreator) {
return (...args)=>{
const action = actionCreator.apply(manager, args);
if (typeof action !== 'undefined') {
dispatch(action);
}
};
}
const actions = createDragDropActions(this);
return Object.keys(actions).reduce((boundActions, key)=>{
const action = actions[key];
boundActions[key] = bindActionCreator(action);
return boundActions;
}, {});
}
dispatch(action) {
this.store.dispatch(action);
}
constructor(store, monitor){
this.isSetUp = false;
this.handleRefCountChange = ()=>{
const shouldSetUp = this.store.getState().refCount > 0;
if (this.backend) {
if (shouldSetUp && !this.isSetUp) {
this.backend.setup();
this.isSetUp = true;
} else if (!shouldSetUp && this.isSetUp) {
this.backend.teardown();
this.isSetUp = false;
}
}
};
this.store = store;
this.monitor = monitor;
store.subscribe(this.handleRefCountChange);
}
}
//# sourceMappingURL=DragDropManagerImpl.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../../src/classes/DragDropManagerImpl.ts"],"sourcesContent":["import type { Action, Store } from 'redux'\n\nimport { createDragDropActions } from '../actions/dragDrop/index.js'\nimport type {\n\tActionCreator,\n\tBackend,\n\tDragDropActions,\n\tDragDropManager,\n\tDragDropMonitor,\n\tHandlerRegistry,\n} from '../interfaces.js'\nimport type { State } from '../reducers/index.js'\nimport type { DragDropMonitorImpl } from './DragDropMonitorImpl.js'\n\nexport class DragDropManagerImpl implements DragDropManager {\n\tprivate store: Store<State>\n\tprivate monitor: DragDropMonitor\n\tprivate backend: Backend | undefined\n\tprivate isSetUp = false\n\n\tpublic constructor(store: Store<State>, monitor: DragDropMonitor) {\n\t\tthis.store = store\n\t\tthis.monitor = monitor\n\t\tstore.subscribe(this.handleRefCountChange)\n\t}\n\n\tpublic receiveBackend(backend: Backend): void {\n\t\tthis.backend = backend\n\t}\n\n\tpublic getMonitor(): DragDropMonitor {\n\t\treturn this.monitor\n\t}\n\n\tpublic getBackend(): Backend {\n\t\treturn this.backend as Backend\n\t}\n\n\tpublic getRegistry(): HandlerRegistry {\n\t\treturn (this.monitor as DragDropMonitorImpl).registry\n\t}\n\n\tpublic getActions(): DragDropActions {\n\t\t/* eslint-disable-next-line @typescript-eslint/no-this-alias */\n\t\tconst manager = this\n\t\tconst { dispatch } = this.store\n\n\t\tfunction bindActionCreator(actionCreator: ActionCreator<any>) {\n\t\t\treturn (...args: any[]) => {\n\t\t\t\tconst action = actionCreator.apply(manager, args as any)\n\t\t\t\tif (typeof action !== 'undefined') {\n\t\t\t\t\tdispatch(action)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tconst actions = createDragDropActions(this)\n\n\t\treturn Object.keys(actions).reduce(\n\t\t\t(boundActions: DragDropActions, key: string) => {\n\t\t\t\tconst action: ActionCreator<any> = (actions as any)[\n\t\t\t\t\tkey\n\t\t\t\t] as ActionCreator<any>\n\t\t\t\t;(boundActions as any)[key] = bindActionCreator(action)\n\t\t\t\treturn boundActions\n\t\t\t},\n\t\t\t{} as DragDropActions,\n\t\t)\n\t}\n\n\tpublic dispatch(action: Action<any>): void {\n\t\tthis.store.dispatch(action)\n\t}\n\n\tprivate handleRefCountChange = (): void => {\n\t\tconst shouldSetUp = this.store.getState().refCount > 0\n\t\tif (this.backend) {\n\t\t\tif (shouldSetUp && !this.isSetUp) {\n\t\t\t\tthis.backend.setup()\n\t\t\t\tthis.isSetUp = true\n\t\t\t} else if (!shouldSetUp && this.isSetUp) {\n\t\t\t\tthis.backend.teardown()\n\t\t\t\tthis.isSetUp = false\n\t\t\t}\n\t\t}\n\t}\n}\n"],"names":["createDragDropActions","DragDropManagerImpl","receiveBackend","backend","getMonitor","monitor","getBackend","getRegistry","registry","getActions","manager","dispatch","store","bindActionCreator","actionCreator","args","action","apply","actions","Object","keys","reduce","boundActions","key","isSetUp","handleRefCountChange","shouldSetUp","getState","refCount","setup","teardown","subscribe"],"mappings":"AAEA,SAASA,qBAAqB,QAAQ,8BAA8B,CAAA;AAYpE,OAAO,MAAMC,mBAAmB;IAY/B,AAAOC,cAAc,CAACC,OAAgB,EAAQ;QAC7C,IAAI,CAACA,OAAO,GAAGA,OAAO;KACtB;IAED,AAAOC,UAAU,GAAoB;QACpC,OAAO,IAAI,CAACC,OAAO,CAAA;KACnB;IAED,AAAOC,UAAU,GAAY;QAC5B,OAAO,IAAI,CAACH,OAAO,CAAW;KAC9B;IAED,AAAOI,WAAW,GAAoB;QACrC,OAAO,AAAC,IAAI,CAACF,OAAO,CAAyBG,QAAQ,CAAA;KACrD;IAED,AAAOC,UAAU,GAAoB;QACpC,+DAA+D,CAC/D,MAAMC,OAAO,GAAG,IAAI;QACpB,MAAM,EAAEC,QAAQ,CAAA,EAAE,GAAG,IAAI,CAACC,KAAK;QAE/B,SAASC,iBAAiB,CAACC,aAAiC,EAAE;YAC7D,OAAO,CAAC,GAAGC,IAAI,AAAO,GAAK;gBAC1B,MAAMC,MAAM,GAAGF,aAAa,CAACG,KAAK,CAACP,OAAO,EAAEK,IAAI,CAAQ;gBACxD,IAAI,OAAOC,MAAM,KAAK,WAAW,EAAE;oBAClCL,QAAQ,CAACK,MAAM,CAAC;iBAChB;aACD,CAAA;SACD;QAED,MAAME,OAAO,GAAGlB,qBAAqB,CAAC,IAAI,CAAC;QAE3C,OAAOmB,MAAM,CAACC,IAAI,CAACF,OAAO,CAAC,CAACG,MAAM,CACjC,CAACC,YAA6B,EAAEC,GAAW,GAAK;YAC/C,MAAMP,MAAM,GAAuB,AAACE,OAAO,AAAQ,CAClDK,GAAG,CACH,AAAsB,AACtB;YAAA,AAACD,YAAY,AAAQ,CAACC,GAAG,CAAC,GAAGV,iBAAiB,CAACG,MAAM,CAAC;YACvD,OAAOM,YAAY,CAAA;SACnB,EACD,EAAE,CACF,CAAA;KACD;IAED,AAAOX,QAAQ,CAACK,MAAmB,EAAQ;QAC1C,IAAI,CAACJ,KAAK,CAACD,QAAQ,CAACK,MAAM,CAAC;KAC3B;IApDD,YAAmBJ,KAAmB,EAAEP,OAAwB,CAAE;QAFlE,KAAQmB,OAAO,GAAG,KAAK,AAlBxB,CAkBwB;QAwDvB,KAAQC,oBAAoB,GAAG,IAAY;YAC1C,MAAMC,WAAW,GAAG,IAAI,CAACd,KAAK,CAACe,QAAQ,EAAE,CAACC,QAAQ,GAAG,CAAC;YACtD,IAAI,IAAI,CAACzB,OAAO,EAAE;gBACjB,IAAIuB,WAAW,IAAI,CAAC,IAAI,CAACF,OAAO,EAAE;oBACjC,IAAI,CAACrB,OAAO,CAAC0B,KAAK,EAAE;oBACpB,IAAI,CAACL,OAAO,GAAG,IAAI;iBACnB,MAAM,IAAI,CAACE,WAAW,IAAI,IAAI,CAACF,OAAO,EAAE;oBACxC,IAAI,CAACrB,OAAO,CAAC2B,QAAQ,EAAE;oBACvB,IAAI,CAACN,OAAO,GAAG,KAAK;iBACpB;aACD;SACD,AArFF,CAqFE;QAhEA,IAAI,CAACZ,KAAK,GAAGA,KAAK;QAClB,IAAI,CAACP,OAAO,GAAGA,OAAO;QACtBO,KAAK,CAACmB,SAAS,CAAC,IAAI,CAACN,oBAAoB,CAAC;KAC1C;CA8DD"}
+31
View File
@@ -0,0 +1,31 @@
import type { Store } from 'redux';
import type { DragDropMonitor, HandlerRegistry, Identifier, Listener, Unsubscribe, XYCoord } from '../interfaces.js';
import type { State } from '../reducers/index.js';
export declare class DragDropMonitorImpl implements DragDropMonitor {
private store;
readonly registry: HandlerRegistry;
constructor(store: Store<State>, registry: HandlerRegistry);
subscribeToStateChange(listener: Listener, options?: {
handlerIds?: string[];
}): Unsubscribe;
subscribeToOffsetChange(listener: Listener): Unsubscribe;
canDragSource(sourceId: string | undefined): boolean;
canDropOnTarget(targetId: string | undefined): boolean;
isDragging(): boolean;
isDraggingSource(sourceId: string | undefined): boolean;
isOverTarget(targetId: string | undefined, options?: {
shallow: boolean;
}): boolean;
getItemType(): Identifier;
getItem(): any;
getSourceId(): string | null;
getTargetIds(): string[];
getDropResult(): any;
didDrop(): boolean;
isSourcePublic(): boolean;
getInitialClientOffset(): XYCoord | null;
getInitialSourceClientOffset(): XYCoord | null;
getClientOffset(): XYCoord | null;
getSourceClientOffset(): XYCoord | null;
getDifferenceFromInitialOffset(): XYCoord | null;
}
+152
View File
@@ -0,0 +1,152 @@
import { invariant } from '@react-dnd/invariant';
import { getDifferenceFromInitialOffset, getSourceClientOffset } from '../utils/coords.js';
import { areDirty } from '../utils/dirtiness.js';
import { matchesType } from '../utils/matchesType.js';
export class DragDropMonitorImpl {
subscribeToStateChange(listener, options = {}) {
const { handlerIds } = options;
invariant(typeof listener === 'function', 'listener must be a function.');
invariant(typeof handlerIds === 'undefined' || Array.isArray(handlerIds), 'handlerIds, when specified, must be an array of strings.');
let prevStateId = this.store.getState().stateId;
const handleChange = ()=>{
const state = this.store.getState();
const currentStateId = state.stateId;
try {
const canSkipListener = currentStateId === prevStateId || currentStateId === prevStateId + 1 && !areDirty(state.dirtyHandlerIds, handlerIds);
if (!canSkipListener) {
listener();
}
} finally{
prevStateId = currentStateId;
}
};
return this.store.subscribe(handleChange);
}
subscribeToOffsetChange(listener) {
invariant(typeof listener === 'function', 'listener must be a function.');
let previousState = this.store.getState().dragOffset;
const handleChange = ()=>{
const nextState = this.store.getState().dragOffset;
if (nextState === previousState) {
return;
}
previousState = nextState;
listener();
};
return this.store.subscribe(handleChange);
}
canDragSource(sourceId) {
if (!sourceId) {
return false;
}
const source = this.registry.getSource(sourceId);
invariant(source, `Expected to find a valid source. sourceId=${sourceId}`);
if (this.isDragging()) {
return false;
}
return source.canDrag(this, sourceId);
}
canDropOnTarget(targetId) {
// undefined on initial render
if (!targetId) {
return false;
}
const target = this.registry.getTarget(targetId);
invariant(target, `Expected to find a valid target. targetId=${targetId}`);
if (!this.isDragging() || this.didDrop()) {
return false;
}
const targetType = this.registry.getTargetType(targetId);
const draggedItemType = this.getItemType();
return matchesType(targetType, draggedItemType) && target.canDrop(this, targetId);
}
isDragging() {
return Boolean(this.getItemType());
}
isDraggingSource(sourceId) {
// undefined on initial render
if (!sourceId) {
return false;
}
const source = this.registry.getSource(sourceId, true);
invariant(source, `Expected to find a valid source. sourceId=${sourceId}`);
if (!this.isDragging() || !this.isSourcePublic()) {
return false;
}
const sourceType = this.registry.getSourceType(sourceId);
const draggedItemType = this.getItemType();
if (sourceType !== draggedItemType) {
return false;
}
return source.isDragging(this, sourceId);
}
isOverTarget(targetId, options = {
shallow: false
}) {
// undefined on initial render
if (!targetId) {
return false;
}
const { shallow } = options;
if (!this.isDragging()) {
return false;
}
const targetType = this.registry.getTargetType(targetId);
const draggedItemType = this.getItemType();
if (draggedItemType && !matchesType(targetType, draggedItemType)) {
return false;
}
const targetIds = this.getTargetIds();
if (!targetIds.length) {
return false;
}
const index = targetIds.indexOf(targetId);
if (shallow) {
return index === targetIds.length - 1;
} else {
return index > -1;
}
}
getItemType() {
return this.store.getState().dragOperation.itemType;
}
getItem() {
return this.store.getState().dragOperation.item;
}
getSourceId() {
return this.store.getState().dragOperation.sourceId;
}
getTargetIds() {
return this.store.getState().dragOperation.targetIds;
}
getDropResult() {
return this.store.getState().dragOperation.dropResult;
}
didDrop() {
return this.store.getState().dragOperation.didDrop;
}
isSourcePublic() {
return Boolean(this.store.getState().dragOperation.isSourcePublic);
}
getInitialClientOffset() {
return this.store.getState().dragOffset.initialClientOffset;
}
getInitialSourceClientOffset() {
return this.store.getState().dragOffset.initialSourceClientOffset;
}
getClientOffset() {
return this.store.getState().dragOffset.clientOffset;
}
getSourceClientOffset() {
return getSourceClientOffset(this.store.getState().dragOffset);
}
getDifferenceFromInitialOffset() {
return getDifferenceFromInitialOffset(this.store.getState().dragOffset);
}
constructor(store, registry){
this.store = store;
this.registry = registry;
}
}
//# sourceMappingURL=DragDropMonitorImpl.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../../src/classes/DragDropMonitorImpl.ts"],"sourcesContent":["import { invariant } from '@react-dnd/invariant'\nimport type { Store } from 'redux'\n\nimport type {\n\tDragDropMonitor,\n\tHandlerRegistry,\n\tIdentifier,\n\tListener,\n\tUnsubscribe,\n\tXYCoord,\n} from '../interfaces.js'\nimport type { State } from '../reducers/index.js'\nimport {\n\tgetDifferenceFromInitialOffset,\n\tgetSourceClientOffset,\n} from '../utils/coords.js'\nimport { areDirty } from '../utils/dirtiness.js'\nimport { matchesType } from '../utils/matchesType.js'\n\nexport class DragDropMonitorImpl implements DragDropMonitor {\n\tprivate store: Store<State>\n\tpublic readonly registry: HandlerRegistry\n\n\tpublic constructor(store: Store<State>, registry: HandlerRegistry) {\n\t\tthis.store = store\n\t\tthis.registry = registry\n\t}\n\n\tpublic subscribeToStateChange(\n\t\tlistener: Listener,\n\t\toptions: { handlerIds?: string[] } = {},\n\t): Unsubscribe {\n\t\tconst { handlerIds } = options\n\t\tinvariant(typeof listener === 'function', 'listener must be a function.')\n\t\tinvariant(\n\t\t\ttypeof handlerIds === 'undefined' || Array.isArray(handlerIds),\n\t\t\t'handlerIds, when specified, must be an array of strings.',\n\t\t)\n\n\t\tlet prevStateId = this.store.getState().stateId\n\t\tconst handleChange = () => {\n\t\t\tconst state = this.store.getState()\n\t\t\tconst currentStateId = state.stateId\n\t\t\ttry {\n\t\t\t\tconst canSkipListener =\n\t\t\t\t\tcurrentStateId === prevStateId ||\n\t\t\t\t\t(currentStateId === prevStateId + 1 &&\n\t\t\t\t\t\t!areDirty(state.dirtyHandlerIds, handlerIds))\n\n\t\t\t\tif (!canSkipListener) {\n\t\t\t\t\tlistener()\n\t\t\t\t}\n\t\t\t} finally {\n\t\t\t\tprevStateId = currentStateId\n\t\t\t}\n\t\t}\n\n\t\treturn this.store.subscribe(handleChange)\n\t}\n\n\tpublic subscribeToOffsetChange(listener: Listener): Unsubscribe {\n\t\tinvariant(typeof listener === 'function', 'listener must be a function.')\n\n\t\tlet previousState = this.store.getState().dragOffset\n\t\tconst handleChange = () => {\n\t\t\tconst nextState = this.store.getState().dragOffset\n\t\t\tif (nextState === previousState) {\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tpreviousState = nextState\n\t\t\tlistener()\n\t\t}\n\n\t\treturn this.store.subscribe(handleChange)\n\t}\n\n\tpublic canDragSource(sourceId: string | undefined): boolean {\n\t\tif (!sourceId) {\n\t\t\treturn false\n\t\t}\n\t\tconst source = this.registry.getSource(sourceId)\n\t\tinvariant(source, `Expected to find a valid source. sourceId=${sourceId}`)\n\n\t\tif (this.isDragging()) {\n\t\t\treturn false\n\t\t}\n\n\t\treturn source.canDrag(this, sourceId)\n\t}\n\n\tpublic canDropOnTarget(targetId: string | undefined): boolean {\n\t\t// undefined on initial render\n\t\tif (!targetId) {\n\t\t\treturn false\n\t\t}\n\t\tconst target = this.registry.getTarget(targetId)\n\t\tinvariant(target, `Expected to find a valid target. targetId=${targetId}`)\n\n\t\tif (!this.isDragging() || this.didDrop()) {\n\t\t\treturn false\n\t\t}\n\n\t\tconst targetType = this.registry.getTargetType(targetId)\n\t\tconst draggedItemType = this.getItemType()\n\t\treturn (\n\t\t\tmatchesType(targetType, draggedItemType) && target.canDrop(this, targetId)\n\t\t)\n\t}\n\n\tpublic isDragging(): boolean {\n\t\treturn Boolean(this.getItemType())\n\t}\n\n\tpublic isDraggingSource(sourceId: string | undefined): boolean {\n\t\t// undefined on initial render\n\t\tif (!sourceId) {\n\t\t\treturn false\n\t\t}\n\t\tconst source = this.registry.getSource(sourceId, true)\n\t\tinvariant(source, `Expected to find a valid source. sourceId=${sourceId}`)\n\n\t\tif (!this.isDragging() || !this.isSourcePublic()) {\n\t\t\treturn false\n\t\t}\n\n\t\tconst sourceType = this.registry.getSourceType(sourceId)\n\t\tconst draggedItemType = this.getItemType()\n\t\tif (sourceType !== draggedItemType) {\n\t\t\treturn false\n\t\t}\n\n\t\treturn source.isDragging(this, sourceId)\n\t}\n\n\tpublic isOverTarget(\n\t\ttargetId: string | undefined,\n\t\toptions = { shallow: false },\n\t): boolean {\n\t\t// undefined on initial render\n\t\tif (!targetId) {\n\t\t\treturn false\n\t\t}\n\n\t\tconst { shallow } = options\n\t\tif (!this.isDragging()) {\n\t\t\treturn false\n\t\t}\n\n\t\tconst targetType = this.registry.getTargetType(targetId)\n\t\tconst draggedItemType = this.getItemType()\n\t\tif (draggedItemType && !matchesType(targetType, draggedItemType)) {\n\t\t\treturn false\n\t\t}\n\n\t\tconst targetIds = this.getTargetIds()\n\t\tif (!targetIds.length) {\n\t\t\treturn false\n\t\t}\n\n\t\tconst index = targetIds.indexOf(targetId)\n\t\tif (shallow) {\n\t\t\treturn index === targetIds.length - 1\n\t\t} else {\n\t\t\treturn index > -1\n\t\t}\n\t}\n\n\tpublic getItemType(): Identifier {\n\t\treturn this.store.getState().dragOperation.itemType as Identifier\n\t}\n\n\tpublic getItem(): any {\n\t\treturn this.store.getState().dragOperation.item\n\t}\n\n\tpublic getSourceId(): string | null {\n\t\treturn this.store.getState().dragOperaLine truncated
+26
View File
@@ -0,0 +1,26 @@
import type { Store } from 'redux';
import type { DragSource, DropTarget, HandlerRegistry, Identifier, SourceType, TargetType } from '../interfaces.js';
import type { State } from '../reducers/index.js';
export declare class HandlerRegistryImpl implements HandlerRegistry {
private types;
private dragSources;
private dropTargets;
private pinnedSourceId;
private pinnedSource;
private store;
constructor(store: Store<State>);
addSource(type: SourceType, source: DragSource): string;
addTarget(type: TargetType, target: DropTarget): string;
containsHandler(handler: DragSource | DropTarget): boolean;
getSource(sourceId: string, includePinned?: boolean): DragSource;
getTarget(targetId: string): DropTarget;
getSourceType(sourceId: string): Identifier;
getTargetType(targetId: string): Identifier | Identifier[];
isSourceId(handlerId: string): boolean;
isTargetId(handlerId: string): boolean;
removeSource(sourceId: string): void;
removeTarget(targetId: string): void;
pinSource(sourceId: string): void;
unpinSource(): void;
private addHandler;
}
+129
View File
@@ -0,0 +1,129 @@
import { asap } from '@react-dnd/asap';
import { invariant } from '@react-dnd/invariant';
import { addSource, addTarget, removeSource, removeTarget } from '../actions/registry.js';
import { validateSourceContract, validateTargetContract, validateType } from '../contracts.js';
import { HandlerRole } from '../interfaces.js';
import { getNextUniqueId } from '../utils/getNextUniqueId.js';
function getNextHandlerId(role) {
const id = getNextUniqueId().toString();
switch(role){
case HandlerRole.SOURCE:
return `S${id}`;
case HandlerRole.TARGET:
return `T${id}`;
default:
throw new Error(`Unknown Handler Role: ${role}`);
}
}
function parseRoleFromHandlerId(handlerId) {
switch(handlerId[0]){
case 'S':
return HandlerRole.SOURCE;
case 'T':
return HandlerRole.TARGET;
default:
throw new Error(`Cannot parse handler ID: ${handlerId}`);
}
}
function mapContainsValue(map, searchValue) {
const entries = map.entries();
let isDone = false;
do {
const { done , value: [, value] , } = entries.next();
if (value === searchValue) {
return true;
}
isDone = !!done;
}while (!isDone)
return false;
}
export class HandlerRegistryImpl {
addSource(type, source) {
validateType(type);
validateSourceContract(source);
const sourceId = this.addHandler(HandlerRole.SOURCE, type, source);
this.store.dispatch(addSource(sourceId));
return sourceId;
}
addTarget(type, target) {
validateType(type, true);
validateTargetContract(target);
const targetId = this.addHandler(HandlerRole.TARGET, type, target);
this.store.dispatch(addTarget(targetId));
return targetId;
}
containsHandler(handler) {
return mapContainsValue(this.dragSources, handler) || mapContainsValue(this.dropTargets, handler);
}
getSource(sourceId, includePinned = false) {
invariant(this.isSourceId(sourceId), 'Expected a valid source ID.');
const isPinned = includePinned && sourceId === this.pinnedSourceId;
const source = isPinned ? this.pinnedSource : this.dragSources.get(sourceId);
return source;
}
getTarget(targetId) {
invariant(this.isTargetId(targetId), 'Expected a valid target ID.');
return this.dropTargets.get(targetId);
}
getSourceType(sourceId) {
invariant(this.isSourceId(sourceId), 'Expected a valid source ID.');
return this.types.get(sourceId);
}
getTargetType(targetId) {
invariant(this.isTargetId(targetId), 'Expected a valid target ID.');
return this.types.get(targetId);
}
isSourceId(handlerId) {
const role = parseRoleFromHandlerId(handlerId);
return role === HandlerRole.SOURCE;
}
isTargetId(handlerId) {
const role = parseRoleFromHandlerId(handlerId);
return role === HandlerRole.TARGET;
}
removeSource(sourceId) {
invariant(this.getSource(sourceId), 'Expected an existing source.');
this.store.dispatch(removeSource(sourceId));
asap(()=>{
this.dragSources.delete(sourceId);
this.types.delete(sourceId);
});
}
removeTarget(targetId) {
invariant(this.getTarget(targetId), 'Expected an existing target.');
this.store.dispatch(removeTarget(targetId));
this.dropTargets.delete(targetId);
this.types.delete(targetId);
}
pinSource(sourceId) {
const source = this.getSource(sourceId);
invariant(source, 'Expected an existing source.');
this.pinnedSourceId = sourceId;
this.pinnedSource = source;
}
unpinSource() {
invariant(this.pinnedSource, 'No source is pinned at the time.');
this.pinnedSourceId = null;
this.pinnedSource = null;
}
addHandler(role, type, handler) {
const id = getNextHandlerId(role);
this.types.set(id, type);
if (role === HandlerRole.SOURCE) {
this.dragSources.set(id, handler);
} else if (role === HandlerRole.TARGET) {
this.dropTargets.set(id, handler);
}
return id;
}
constructor(store){
this.types = new Map();
this.dragSources = new Map();
this.dropTargets = new Map();
this.pinnedSourceId = null;
this.pinnedSource = null;
this.store = store;
}
}
//# sourceMappingURL=HandlerRegistryImpl.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../../src/classes/HandlerRegistryImpl.ts"],"sourcesContent":["import { asap } from '@react-dnd/asap'\nimport { invariant } from '@react-dnd/invariant'\nimport type { Store } from 'redux'\n\nimport {\n\taddSource,\n\taddTarget,\n\tremoveSource,\n\tremoveTarget,\n} from '../actions/registry.js'\nimport {\n\tvalidateSourceContract,\n\tvalidateTargetContract,\n\tvalidateType,\n} from '../contracts.js'\nimport type {\n\tDragSource,\n\tDropTarget,\n\tHandlerRegistry,\n\tIdentifier,\n\tSourceType,\n\tTargetType,\n} from '../interfaces.js'\nimport { HandlerRole } from '../interfaces.js'\nimport type { State } from '../reducers/index.js'\nimport { getNextUniqueId } from '../utils/getNextUniqueId.js'\n\nfunction getNextHandlerId(role: HandlerRole): string {\n\tconst id = getNextUniqueId().toString()\n\tswitch (role) {\n\t\tcase HandlerRole.SOURCE:\n\t\t\treturn `S${id}`\n\t\tcase HandlerRole.TARGET:\n\t\t\treturn `T${id}`\n\t\tdefault:\n\t\t\tthrow new Error(`Unknown Handler Role: ${role}`)\n\t}\n}\n\nfunction parseRoleFromHandlerId(handlerId: string) {\n\tswitch (handlerId[0]) {\n\t\tcase 'S':\n\t\t\treturn HandlerRole.SOURCE\n\t\tcase 'T':\n\t\t\treturn HandlerRole.TARGET\n\t\tdefault:\n\t\t\tthrow new Error(`Cannot parse handler ID: ${handlerId}`)\n\t}\n}\n\nfunction mapContainsValue<T>(map: Map<string, T>, searchValue: T) {\n\tconst entries = map.entries()\n\tlet isDone = false\n\tdo {\n\t\tconst {\n\t\t\tdone,\n\t\t\tvalue: [, value],\n\t\t} = entries.next()\n\t\tif (value === searchValue) {\n\t\t\treturn true\n\t\t}\n\t\tisDone = !!done\n\t} while (!isDone)\n\treturn false\n}\n\nexport class HandlerRegistryImpl implements HandlerRegistry {\n\tprivate types: Map<string, SourceType | TargetType> = new Map()\n\tprivate dragSources: Map<string, DragSource> = new Map()\n\tprivate dropTargets: Map<string, DropTarget> = new Map()\n\tprivate pinnedSourceId: string | null = null\n\tprivate pinnedSource: any = null\n\tprivate store: Store<State>\n\n\tpublic constructor(store: Store<State>) {\n\t\tthis.store = store\n\t}\n\n\tpublic addSource(type: SourceType, source: DragSource): string {\n\t\tvalidateType(type)\n\t\tvalidateSourceContract(source)\n\n\t\tconst sourceId = this.addHandler(HandlerRole.SOURCE, type, source)\n\t\tthis.store.dispatch(addSource(sourceId))\n\t\treturn sourceId\n\t}\n\n\tpublic addTarget(type: TargetType, target: DropTarget): string {\n\t\tvalidateType(type, true)\n\t\tvalidateTargetContract(target)\n\n\t\tconst targetId = this.addHandler(HandlerRole.TARGET, type, target)\n\t\tthis.store.dispatch(addTarget(targetId))\n\t\treturn targetId\n\t}\n\n\tpublic containsHandler(handler: DragSource | DropTarget): boolean {\n\t\treturn (\n\t\t\tmapContainsValue(this.dragSources, handler) ||\n\t\t\tmapContainsValue(this.dropTargets, handler)\n\t\t)\n\t}\n\n\tpublic getSource(sourceId: string, includePinned = false): DragSource {\n\t\tinvariant(this.isSourceId(sourceId), 'Expected a valid source ID.')\n\t\tconst isPinned = includePinned && sourceId === this.pinnedSourceId\n\t\tconst source = isPinned ? this.pinnedSource : this.dragSources.get(sourceId)\n\t\treturn source\n\t}\n\n\tpublic getTarget(targetId: string): DropTarget {\n\t\tinvariant(this.isTargetId(targetId), 'Expected a valid target ID.')\n\t\treturn this.dropTargets.get(targetId) as DropTarget\n\t}\n\n\tpublic getSourceType(sourceId: string): Identifier {\n\t\tinvariant(this.isSourceId(sourceId), 'Expected a valid source ID.')\n\t\treturn this.types.get(sourceId) as Identifier\n\t}\n\n\tpublic getTargetType(targetId: string): Identifier | Identifier[] {\n\t\tinvariant(this.isTargetId(targetId), 'Expected a valid target ID.')\n\t\treturn this.types.get(targetId) as Identifier | Identifier[]\n\t}\n\n\tpublic isSourceId(handlerId: string): boolean {\n\t\tconst role = parseRoleFromHandlerId(handlerId)\n\t\treturn role === HandlerRole.SOURCE\n\t}\n\n\tpublic isTargetId(handlerId: string): boolean {\n\t\tconst role = parseRoleFromHandlerId(handlerId)\n\t\treturn role === HandlerRole.TARGET\n\t}\n\n\tpublic removeSource(sourceId: string): void {\n\t\tinvariant(this.getSource(sourceId), 'Expected an existing source.')\n\t\tthis.store.dispatch(removeSource(sourceId))\n\t\tasap(() => {\n\t\t\tthis.dragSources.delete(sourceId)\n\t\t\tthis.types.delete(sourceId)\n\t\t})\n\t}\n\n\tpublic removeTarget(targetId: string): void {\n\t\tinvariant(this.getTarget(targetId), 'Expected an existing target.')\n\t\tthis.store.dispatch(removeTarget(targetId))\n\t\tthis.dropTargets.delete(targetId)\n\t\tthis.types.delete(targetId)\n\t}\n\n\tpublic pinSource(sourceId: string): void {\n\t\tconst source = this.getSource(sourceId)\n\t\tinvariant(source, 'Expected an existing source.')\n\n\t\tthis.pinnedSourceId = sourceId\n\t\tthis.pinnedSource = source\n\t}\n\n\tpublic unpinSource(): void {\n\t\tinvariant(this.pinnedSource, 'No source is pinned at the time.')\n\n\t\tthis.pinnedSourceId = null\n\t\tthis.pinnedSource = null\n\t}\n\n\tprivate addHandlerLine truncated