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: Fixes text selection in contenteditable #99

Merged
merged 3 commits into from
Mar 27, 2021
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
12 changes: 12 additions & 0 deletions EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ selection.on('beforestart', ({event}) => {
> Feel free to submit a [PR](https://github.com/Simonwep/selection/compare) or create
> an [issue](https://github.com/Simonwep/selection/issues/new?assignees=Simonwep&labels=&template=feature_request.md&title=) if
> you got any ideas for more examples!

#### Preventing text-selection

As of v2.1.0, it will no longer prevent text-selection.
If this is wanted it can be done using two of the event hooks and a bit of css:

```js
selection
.on('beforestart', () => document.body.style.userSelect = 'none')
.on('stop', () => document.body.style.userSelect = 'unset');
```

13 changes: 9 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {css, eventPath, intersects, off, on, removeElement, selectAll, SelectAllSelectors, simplifyEvent} from '@utils';
import {css, eventPath, intersects, isTouchDevice, off, on, removeElement, selectAll, SelectAllSelectors, simplifyEvent} from '@utils';
import {EventTarget} from './EventEmitter';
import {AreaLocation, Coordinates, ScrollEvent, SelectionEvents, SelectionOptions, SelectionStore} from './types';

Expand Down Expand Up @@ -232,7 +232,7 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
}

_delayedTapMove(evt: MouseEvent | TouchEvent): void {
const {startThreshold, container, document} = this._options;
const {startThreshold, container, document, allowTouch} = this._options;
const {x1, y1} = this._areaLocation; // Coordinates of first "tap"
const {x, y} = simplifyEvent(evt);

Expand Down Expand Up @@ -289,7 +289,9 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
this._onTapMove(evt);
}

evt.preventDefault(); // Prevent swipe-down refresh
if (allowTouch && isTouchDevice) {
evt.preventDefault(); // Prevent swipe-down refresh
}
}

_prepareSelectionArea(): void {
Expand Down Expand Up @@ -341,6 +343,7 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
_onTapMove(evt: MouseEvent | TouchEvent): void {
const {x, y} = simplifyEvent(evt);
const {_scrollSpeed, _areaLocation, _options} = this;
const {allowTouch} = _options;
const {speedDivider} = _options.scrolling;
const scon = this._targetElement as Element;

Expand Down Expand Up @@ -399,7 +402,9 @@ export default class SelectionArea extends EventTarget<SelectionEvents> {
this._redrawSelectionArea();
}

evt.preventDefault(); // Prevent swipe-down refresh
if (allowTouch && isTouchDevice) {
evt.preventDefault(); // Prevent swipe-down refresh
}
}

_onScroll(): void {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Determines if the device's primary input supports touch
// See this article: https://css-tricks.com/touch-devices-not-judged-size/
export const isTouchDevice = window.matchMedia('(hover: none), (pointer: coarse)').matches;
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './events';
export * from './intersects';
export * from './removeElement';
export * from './selectAll';
export * from './constants';