我一直在玩 RxJS 6 。我正在尝试实施dragstart, dragmove and dragend
。这是我到目前为止的代码。
import { fromEvent } from 'rxjs';
import { concatMap } from 'rxjs/internal/operators/concatMap';
import { map } from 'rxjs/internal/operators/map';
import { takeUntil } from 'rxjs/internal/operators/takeUntil';
import { repeat } from 'rxjs/internal/operators/repeat';
import { first } from 'rxjs/internal/operators/first';
const mousedown = fromEvent<MouseEvent>(window, 'mousedown');
const mousemove = fromEvent<MouseEvent>(window, 'mousemove');
const mouseup = fromEvent<MouseEvent>(window, 'mouseup');
const dragstart = mousedown.pipe(
first()
);
const dragmove = mousedown.pipe(
concatMap((dragStartEvent) => mousemove.pipe(
takeUntil(mouseup))
)
);
const dragend = mousedown.pipe(
(dragEvent) => mouseup.pipe(first())
);
const log = (prefix: string) => (data: MouseEvent) => console.log(`${prefix}: x: ${data.clientX}, y:${data.clientY}`);
dragstart.subscribe(log('dragstart'));
dragmove.subscribe(log('dragmove'));
dragend.subscribe(log('dragend'));
问题是运营商takeUntil
和first
将流标记为已完成。
这意味着dragstart
和dragend
只会触发一次。有没有办法在这个事件发生后以某种方式重置流?
例如,当dragstart
流收到事件时重置mouseup
流。
在当前实现中,dragstart
和dragend
将在加载页面后完全记录一次到控制台(&amp;拖动)。之后,流完成,不再通过它发送任何事件。我想在每次拖动操作时重置流,所以dragstart&amp; dragend将在每次拖动操作时工作,而不是仅在加载页面后的第一次操作。
首次拖动操作
第二次拖动操作
答案 0 :(得分:0)
请看这里的工作示例 https://codepen.io/anon/pen/bMWjEV enter link description here
const { fromEvent } = Rx.Observable;
const target = document.querySelector('.box');
const mouseup = fromEvent(target, 'mouseup');
const mousemove = fromEvent(document, 'mousemove');
const mousedown = fromEvent(target, 'mousedown');
let log = (prefix: string, x:number, y:number) => console.log(`${prefix}: x: ${x}, y:${y}`);
const mousedrag = mousedown.selectMany((md) => {
const startX = md.clientX + window.scrollX,
startY = md.clientY + window.scrollY,
startLeft = parseInt(md.target.style.left, 10) || 0,
startTop = parseInt(md.target.style.top, 10) || 0;
return mousemove.map((mm) => {
mm.preventDefault();
log('mousemove',mm.clientX, mm.clientY);
return {
left: startLeft + mm.clientX - startX,
top: startTop + mm.clientY - startY
};
}).takeUntil(mouseup);
});
subscription = mousedrag.subscribe((pos) => {
log('dragstart',pos.top, pos.left)
target.style.top = pos.top + 'px';
target.style.left = pos.left + 'px';
});
.box {
position: relative;
width: 100px;
height: 100px;
background: red;
cursor: pointer;
border:solid 10px green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/2.5.2/rx.all.js"></script>
<div class="box"></div>
答案 1 :(得分:0)
但是您却告诉过这样做:)
如果您从dragstart流中删除管道和“ first”运算符
const dragstart = mousedown
该流不会完成。 拖动动作无论如何都不会完成,因为它是从鼠标向下而不是从拖动开始流映射的。
如果您告诉结果流仅选择第一个流,则该流将在发出一次之后完成。
此代码应该有效:
const mousedown = fromEvent<MouseEvent>(window, 'mousedown');
const mousemove = fromEvent<MouseEvent>(window, 'mousemove');
const mouseup = fromEvent<MouseEvent>(window, 'mouseup');
// I dont think it's necessary to duplicate
const dragstart = mousedown
// No need to map to something else
const dragend = mouseup
const dragmove = dragstart.pipe(
concatMapTo(
mousemove.pipe(
takeUntil( dragend )
)
)
);
const log = (prefix: string) => (data: MouseEvent) => console.log(`${prefix}: x: ${data.clientX}, y:${data.clientY}`);
dragstart.subscribe(log('dragstart'));
dragmove.subscribe(log('dragmove'));
dragend.subscribe(log('dragend'));