Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow configuring when to use the 'copy' dropEffect #3498

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 e.altKey
}
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)
}