在RXJS中重新创建ContextMenu事件
我想重新创建
用普通语言,该观察值应发出:
- 触摸start事件发生
- 在2000毫秒(基本上是长压)
- ,然后触摸端,
当时不应发出:触摸端在:
- 触摸端在不到2000毫秒中发生在2000毫秒中
- ,触摸start事件之后是touch move事件,
我无法弄清楚如何跳过触摸端,如果触摸端发生较早或接触触点后面是触摸机。这就是我到目前为止的:
const touchStart$ = fromEvent<TouchEvent>(this.el.nativeElement, "touchstart");
const touchEnd$ = fromEvent<TouchEvent>(this.el.nativeElement, "touchend");
const touchMove$ = fromEvent<TouchEvent>(this.el.nativeElement, "touchmove");
const contextmenu$ = touchStart$.pipe(
switchMap(event => touchEnd$.pipe(mapTo(event))),
switchMap(event => timer(2000).pipe(mapTo(event), takeUntil(merge(touchEnd$, touchMove$))))
);
contextmenu$.subscribe($event => {
console.log("CONTEXTMENU EVENT HAPPENED");
});
I want to recreate the contextmenu event from touch events in RxJS because iOS does not support the contextmenu event on touch.
In plain language, this observable should emit when:
- the touchstart event occurred
- for 2000ms (basically long-press)
- followed by touchend
It should not emit when:
- the touchend occurs in less than 2000ms
- the touchstart event is followed by touchmove event
I can't figure out how to skip if the touchend occurs sooner or if the touchstart is followed by touchmove. This is what I've so far:
const touchStart$ = fromEvent<TouchEvent>(this.el.nativeElement, "touchstart");
const touchEnd$ = fromEvent<TouchEvent>(this.el.nativeElement, "touchend");
const touchMove$ = fromEvent<TouchEvent>(this.el.nativeElement, "touchmove");
const contextmenu$ = touchStart$.pipe(
switchMap(event => touchEnd$.pipe(mapTo(event))),
switchMap(event => timer(2000).pipe(mapTo(event), takeUntil(merge(touchEnd$, touchMove$))))
);
contextmenu$.subscribe($event => {
console.log("CONTEXTMENU EVENT HAPPENED");
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的解决方案可以简化一点。内部
mapto
不需要,如果您不重复使用nameevent
,则可以在结尾处使用单个mapto
你的管道。另外,您可能需要使用take(1)
而不是takeuntil(touchMove $)
在Innerswitchmap
上第一次排放之后:我假设您得到了想要的行为,但是我认为通常会在持续时间通过时会发射。即,我们不需要等待
touchend
事件。如果是这种情况,它会变得更加简单:这里有几个带有彩色伐木的堆叠式:原始 | 简化
Your solution can be simplified a little bit. The inner
mapTo
isn't necessary, if you don't reuse the nameevent
, you can just use a singlemapTo
at the end of your pipe. Also, you may want to usetake(1)
instead oftakeUntil(touchMove$)
on the innerswitchMap
since you want to end the stream after the first emission:I'm assuming you got the behavior you wanted, but I think normally a longpress will fire when duration passes; i.e. we needn't wait for the
touchend
event. If that were the case, it gets even simpler:Here are a couple StackBlitzes with colorful logging: Original | Simplified
我提出了一个可能的解决方案:
I came up with a possible solution: