-
Notifications
You must be signed in to change notification settings - Fork 3
Create onLongPress.ts #19
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
Closed
Closed
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
a387a1b
Create onLongPress.ts
vdegenne b731f27
remove private fields
vdegenne ddd5c5b
comments
vdegenne 7a5f8f3
chore(onLongPress): resolve modifications
vdegenne 829286b
separate type imports
vdegenne de52bd0
cover undefined element case
vdegenne 8ee9ab2
class modifiers
vdegenne 6e83c53
constructor modifier
vdegenne 3d4418e
timeout number
vdegenne e09f04c
rephrase Error
vdegenne 16831bb
standalone functions
vdegenne 6cfada3
refactor update
vdegenne 19ae219
infer the type of of timeoutMs
vdegenne 12b59ac
remove unused imports
vdegenne 9c4756e
explicit number type for callbackTimeoutMs
vdegenne 8841b7f
Merge branch '43081j:main' into patch-1
vdegenne 7c3464c
remove timeout casting for number
vdegenne ffff54d
explicit void returned types
vdegenne 8ca39be
declare pointerdown PointerEvent
vdegenne c3fe78b
onLongPress test unit
vdegenne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import {ElementPart, nothing} from 'lit'; | ||
import { | ||
Directive, | ||
directive, | ||
DirectiveClass, | ||
DirectiveResult, | ||
PartInfo, | ||
PartType, | ||
} from 'lit/directive.js'; | ||
|
||
class LongPressDirective extends Directive { | ||
__element!: Element; | ||
__fireTimeout?: number; | ||
|
||
__longPressCallback?: Function; | ||
43081j marked this conversation as resolved.
Show resolved
Hide resolved
|
||
__cancelTimeoutMs?: number; | ||
|
||
constructor(partInfo: PartInfo) { | ||
super(partInfo); | ||
|
||
if (partInfo.type !== PartType.ELEMENT) { | ||
throw new Error( | ||
"Can't bind `onLongPress` directive to anything " + | ||
vdegenne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'that is not an element' | ||
); | ||
} | ||
|
||
this.__element = (partInfo as ElementPart).element; | ||
vdegenne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
render(callback: (event?: Event) => void, cancelTimeoutMs = 2000) { | ||
// Cancel timeout/events and discard event listeners | ||
this.__abort(); | ||
|
||
this.__longPressCallback = callback; | ||
this.__cancelTimeoutMs = cancelTimeoutMs; | ||
|
||
// Because events were removed from __abort | ||
// we create them again | ||
this.__bindOnElementMouseDown = this.__onElementMouseDown.bind(this); | ||
vdegenne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.__element.addEventListener('mousedown', this.__bindOnElementMouseDown); | ||
this.__bindOnElementMouseUp = this.__onElementMouseUp.bind(this); | ||
this.__element.addEventListener('mouseup', this.__bindOnElementMouseUp); | ||
this.__bindOnElementMouseLeave = this.__onElementMouseLeave.bind(this); | ||
this.__element.addEventListener( | ||
'mouseleave', | ||
this.__bindOnElementMouseLeave | ||
); | ||
|
||
return nothing; | ||
} | ||
|
||
__onElementMouseDown() { | ||
vdegenne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.__initiateTimeout(); | ||
} | ||
__bindOnElementMouseDown!: (event: Event) => void; | ||
|
||
__onElementMouseUp(e: Event) { | ||
// TODO: when the mouse is released and long press event | ||
// was accepted, we should find a way to cancel the @click | ||
// event listener if it exists. | ||
|
||
this.__cancelTimeout(); | ||
} | ||
__bindOnElementMouseUp!: (event: Event) => void; | ||
|
||
__onElementMouseLeave() { | ||
this.__cancelTimeout(); | ||
} | ||
__bindOnElementMouseLeave!: (event: Event) => void; | ||
|
||
/** | ||
* Start the long press timeout, | ||
* when the timeout runs out the user-defined callback is called. | ||
*/ | ||
__initiateTimeout() { | ||
this.__fireTimeout = setTimeout(() => { | ||
this.__longPressCallback?.(this.__element); | ||
}, this.__cancelTimeoutMs); | ||
} | ||
|
||
/** | ||
* Cancel the long press timeout. | ||
* This function is called when the user release the mouse | ||
* or when the mouse leave the element. | ||
vdegenne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
__cancelTimeout() { | ||
if (this.__fireTimeout) { | ||
clearTimeout(this.__fireTimeout); | ||
} | ||
} | ||
|
||
/** | ||
* Abort the long press timeout on special occasions. | ||
*/ | ||
__abort() { | ||
// TODO: should call this.__abort() when the template is removed/destroyed? | ||
this.__cancelTimeout(); | ||
this.__element.removeEventListener( | ||
'mousedown', | ||
this.__bindOnElementMouseDown | ||
); | ||
this.__element.removeEventListener('mouseup', this.__bindOnElementMouseUp); | ||
this.__element.removeEventListener( | ||
'mouseleave', | ||
this.__bindOnElementMouseLeave | ||
); | ||
} | ||
} | ||
|
||
const onLongPressDirective = directive(LongPressDirective); | ||
|
||
export function onLongPress( | ||
callback: (event?: Event) => void, | ||
activateTimeoutMs = 1300 | ||
): DirectiveResult<DirectiveClass> { | ||
return onLongPressDirective(callback, activateTimeoutMs); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.