Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
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
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
switch (this.optionsArgs?.copyKey) {
case 'shift':
return e.shiftKey
case 'ctrl':
return e.ctrlKey
case undefined:
case 'alt':
return e.altKey
case 'meta':
return e.metaKey

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this won't type check without a non-null assertion which is forbidden by the linter. Seems to be a TypeScript issue (minimal reproduction: https://www.typescriptlang.org/play?ts=4.9.0-dev.20221007#code/CYUwxgNghgTiAEYD2A7AzgF3gDwFzwG94AjWAfgAoBKfANyQEth4BfeAH3gFcVQAzBihDAA3ACg+PMBgap4fJEmqEx8NfDQB3BhjAALeBWxkAdKRhUV664ihoEPfoOG5VN93AxcYKcdZZuathmsNTiAUA).

There was an unnecessary optional chaining operator there though which I've removed.

}
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)
}