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: add config option for not prevent-defaulting #3362

Open
wants to merge 1 commit 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
33 changes: 11 additions & 22 deletions packages/backend-html5/src/HTML5BackendImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class HTML5BackendImpl implements Backend {
const handleDragStart = (e: any) => this.handleDragStart(e, sourceId)
const handleSelectStart = (e: any) => this.handleSelectStart(e)

node.setAttribute('draggable', '' + this.monitor.canDragSource(sourceId))
node.setAttribute('draggable', 'true')
node.addEventListener('dragstart', handleDragStart)
node.addEventListener('selectstart', handleSelectStart)

Expand Down Expand Up @@ -205,11 +205,7 @@ export class HTML5BackendImpl implements Backend {
true,
)
target.addEventListener('dragover', this.handleTopDragOver as EventListener)
target.addEventListener(
'dragover',
this.handleTopDragOverCapture as EventListener,
true,
)
target.addEventListener('dragover', this.handleTopDragOverCapture, true)
target.addEventListener('drop', this.handleTopDrop as EventListener)
target.addEventListener(
'drop',
Expand Down Expand Up @@ -248,11 +244,7 @@ export class HTML5BackendImpl implements Backend {
'dragover',
this.handleTopDragOver as EventListener,
)
target.removeEventListener(
'dragover',
this.handleTopDragOverCapture as EventListener,
true,
)
target.removeEventListener('dragover', this.handleTopDragOverCapture, true)
target.removeEventListener('drop', this.handleTopDrop as EventListener)
target.removeEventListener(
'drop',
Expand Down Expand Up @@ -535,10 +527,6 @@ export class HTML5BackendImpl implements Backend {
public handleTopDragEnterCapture = (e: DragEvent): void => {
this.dragEnterTargetIds = []

if (this.isDraggingNativeItem()) {
this.currentNativeSource?.loadDataTransfer(e.dataTransfer)
}

const isFirstEnter = this.enterLeaveCounter.enter(e.target)
if (!isFirstEnter || this.monitor.isDragging()) {
return
Expand Down Expand Up @@ -590,12 +578,8 @@ export class HTML5BackendImpl implements Backend {
}
}

public handleTopDragOverCapture = (e: DragEvent): void => {
public handleTopDragOverCapture = (): void => {
this.dragOverTargetIds = []

if (this.isDraggingNativeItem()) {
this.currentNativeSource?.loadDataTransfer(e.dataTransfer)
}
}

public handleDragOver(e: DragEvent, targetId: string): void {
Expand Down Expand Up @@ -678,13 +662,12 @@ export class HTML5BackendImpl implements Backend {
this.dropTargetIds = []

if (this.isDraggingNativeItem()) {
e.preventDefault()
this.maybePreventDefaultOn(e)
this.currentNativeSource?.loadDataTransfer(e.dataTransfer)
} else if (matchNativeItemType(e.dataTransfer)) {
// Dragging some elements, like <a> and <img> may still behave like a native drag event,
// even if the current drag event matches a user-defined type.
// Stop the default behavior when we're not expecting a native item to be dropped.

e.preventDefault()
}

Expand Down Expand Up @@ -735,4 +718,10 @@ export class HTML5BackendImpl implements Backend {
e.preventDefault()
target.dragDrop()
}

private maybePreventDefaultOn = (evt: DragEvent) => {
if (this.options.isNativeItemDefaultPrevented) {
evt.preventDefault()
}
}
}
10 changes: 7 additions & 3 deletions packages/backend-html5/src/OptionsReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { HTML5BackendContext, HTML5BackendOptions } from './types'
export class OptionsReader {
public ownerDocument: Document | null = null
private globalContext: HTML5BackendContext
private optionsArgs?: HTML5BackendOptions
private optionsArgs: Partial<HTML5BackendOptions>

public constructor(
globalContext: HTML5BackendContext,
options?: HTML5BackendOptions,
options: Partial<HTML5BackendOptions> = {},
) {
this.globalContext = globalContext
this.optionsArgs = options
Expand All @@ -33,6 +33,10 @@ export class OptionsReader {
}

public get rootElement(): Node | undefined {
return this.optionsArgs?.rootElement || this.window
return this.optionsArgs?.rootElement ?? this.window
}

public get isNativeItemDefaultPrevented(): boolean {
return this.optionsArgs.isNativeItemDefaultPrevented ?? true
}
}
7 changes: 7 additions & 0 deletions packages/backend-html5/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ export interface HTML5BackendOptions {
* The root DOM node to use for subscribing to events. Default=Window
*/
rootElement: Node

/**
* Whether or not to preventDefault when dragging native items.
*
* default = true
*/
isNativeItemDefaultPrevented: boolean
}