Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: allow configuring when to use the 'copy' dropEffect
  • Loading branch information
TheUnlocked committed Sep 6, 2022
commit 67c5e9045a543c1f036ee802fba1b6464ae66ca8
8 changes: 4 additions & 4 deletions packages/backend-html5/src/HTML5BackendImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class HTML5BackendImpl implements Backend {
private currentNativeSource: NativeDragSource | null = null
private currentNativeHandle: Identifier | null = null
private currentDragSourceNode: Element | null = null
private altKeyPressed = false
private isCopying = false
private mouseMoveTimeoutTimer: number | null = null
private asyncEndDragFrameId: number | null = null
private dragOverTargetIds: string[] | null = null
Expand Down Expand Up @@ -267,7 +267,7 @@ export class HTML5BackendImpl implements Backend {
const sourceNodeOptions = this.sourceNodeOptions.get(sourceId)

return {
dropEffect: this.altKeyPressed ? 'copy' : 'move',
dropEffect: this.isCopying ? 'copy' : 'move',
...(sourceNodeOptions || {}),
}
}
Expand Down Expand Up @@ -597,7 +597,7 @@ export class HTML5BackendImpl implements Backend {
return
}

this.altKeyPressed = e.altKey
this.isCopying = this.options.isCopying(e)

// If the target changes position as the result of `dragenter`, `dragover` might still
// get dispatched despite target being no longer there. The easy solution is to check
Expand Down Expand Up @@ -650,7 +650,7 @@ export class HTML5BackendImpl implements Backend {
return
}

this.altKeyPressed = e.altKey
this.isCopying = this.options.isCopying(e)
this.lastClientOffset = getEventClientOffset(e)

this.scheduleHover(dragOverTargetIds)
Expand Down
17 changes: 17 additions & 0 deletions packages/backend-html5/src/OptionsReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,21 @@ export class OptionsReader {
public get rootElement(): Node | undefined {
return this.optionsArgs?.rootElement || this.window
}

public isCopying(e: DragEvent): boolean {
if (this.optionsArgs?.copyKey === undefined) {
return false
}
switch (this.optionsArgs?.copyKey) {
case 'shift':
return e.shiftKey
case 'ctrl':
return e.ctrlKey
case 'alt':
return e.altKey
case 'meta':
return e.metaKey
}
return this.optionsArgs?.copyKey(e)
}
}
6 changes: 6 additions & 0 deletions packages/backend-html5/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type HTML5BackendContext = Window | undefined
export type DragModifierKey = 'shift' | 'ctrl' | 'alt' | 'meta'

/**
* Configuration options for the HTML5Backend
Expand All @@ -8,4 +9,9 @@ export interface HTML5BackendOptions {
* The root DOM node to use for subscribing to events. Default=Window
*/
rootElement: Node

/**
* The modifier key to indicate a copy, or a custom callback predicate which should return true if copying. Default='alt'
*/
copyKey: DragModifierKey | ((ev: DragEvent) => boolean)
}