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

fix(drag-drop): prevent text selection while dragging on Safari #14405

Merged
merged 1 commit into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions src/cdk/drag-drop/drag-drop-registry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ describe('DragDropRegistry', () => {
expect(dispatchFakeEvent(document, 'wheel').defaultPrevented).toBe(true);
});

it('should not prevent the default `selectstart` actions when nothing is being dragged', () => {
expect(dispatchFakeEvent(document, 'selectstart').defaultPrevented).toBe(false);
});

it('should prevent the default `selectstart` action when an item is being dragged', () => {
registry.startDragging(testComponent.dragItems.first, createMouseEvent('mousedown'));
expect(dispatchFakeEvent(document, 'selectstart').defaultPrevented).toBe(true);
});


});

@Component({
Expand Down
30 changes: 18 additions & 12 deletions src/cdk/drag-drop/drag-drop-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ const activeCapturingEventOptions = normalizePassiveListenerOptions({
capture: true
});

/** Handler for a pointer event callback. */
type PointerEventHandler = (event: TouchEvent | MouseEvent) => void;

/**
* Service that keeps track of all the drag item and drop container
* instances, and manages global event listeners on the `document`.
Expand All @@ -42,8 +39,8 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
private _activeDragInstances = new Set<I>();

/** Keeps track of the event listeners that we've bound to the `document`. */
private _globalListeners = new Map<'touchmove' | 'mousemove' | 'touchend' | 'mouseup' | 'wheel', {
handler: PointerEventHandler,
private _globalListeners = new Map<string, {
handler: (event: Event) => void,
options?: AddEventListenerOptions | boolean
}>();

Expand Down Expand Up @@ -87,7 +84,7 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
this._ngZone.runOutsideAngular(() => {
// The event handler has to be explicitly active,
// because newer browsers make it passive by default.
this._document.addEventListener('touchmove', this._preventScrollListener,
this._document.addEventListener('touchmove', this._preventDefaultWhileDragging,
activeCapturingEventOptions);
});
}
Expand All @@ -104,7 +101,7 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
this.stopDragging(drag);

if (this._dragInstances.size === 0) {
this._document.removeEventListener('touchmove', this._preventScrollListener,
this._document.removeEventListener('touchmove', this._preventDefaultWhileDragging,
activeCapturingEventOptions);
}
}
Expand All @@ -127,19 +124,27 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
// use `preventDefault` to prevent the page from scrolling while the user is dragging.
this._globalListeners
.set(moveEvent, {
handler: e => this.pointerMove.next(e),
handler: (e: Event) => this.pointerMove.next(e as TouchEvent | MouseEvent),
options: activeCapturingEventOptions
})
.set(upEvent, {
handler: e => this.pointerUp.next(e),
handler: (e: Event) => this.pointerUp.next(e as TouchEvent | MouseEvent),
options: true
})
// Preventing the default action on `mousemove` isn't enough to disable text selection
// on Safari so we need to prevent the selection event as well. Alternatively this can
// be done by setting `user-select: none` on the `body`, however it has causes a style
// recalculation which can be expensive on pages with a lot of elements.
.set('selectstart', {
handler: this._preventDefaultWhileDragging,
options: activeCapturingEventOptions
});

// TODO(crisbeto): prevent mouse wheel scrolling while
// dragging until we've set up proper scroll handling.
if (!isTouchEvent) {
this._globalListeners.set('wheel', {
handler: this._preventScrollListener,
handler: this._preventDefaultWhileDragging,
options: activeCapturingEventOptions
});
}
Expand Down Expand Up @@ -184,9 +189,10 @@ export class DragDropRegistry<I, C extends {id: string}> implements OnDestroy {
}

/**
* Listener used to prevent `touchmove` and `wheel` events while the element is being dragged.
* Event listener that will prevent the default browser action while the user is dragging.
* @param event Event whose default action should be prevented.
*/
private _preventScrollListener = (event: Event) => {
private _preventDefaultWhileDragging = (event: Event) => {
if (this._activeDragInstances.size) {
event.preventDefault();
}
Expand Down
2 changes: 2 additions & 0 deletions src/cdk/drag-drop/drag-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,8 @@ export class DragRef<T = any> {
zIndex: '1000'
});

toggleNativeDragInteractions(preview, false);

preview.classList.add('cdk-drag-preview');
preview.setAttribute('dir', this._dir ? this._dir.value : 'ltr');

Expand Down