DragEvent handling, null check fixes (#630)

This commit is contained in:
dbseel
2026-04-01 12:00:00 +08:00
committed by GitHub
parent eb25068b54
commit 989e327730
2 changed files with 54 additions and 23 deletions

View File

@@ -271,7 +271,10 @@ export default class MutationBuffer {
const shadowHost: Element | null = n.getRootNode const shadowHost: Element | null = n.getRootNode
? (n.getRootNode() as ShadowRoot)?.host ? (n.getRootNode() as ShadowRoot)?.host
: null; : null;
const notInDoc = !this.doc.contains(n) && !this.doc.contains(shadowHost); // ensure shadowHost is a Node, or doc.contains will throw an error
const notInDoc =
!this.doc.contains(n) &&
(!(shadowHost instanceof Node) || !this.doc.contains(shadowHost));
if (!n.parentNode || notInDoc) { if (!n.parentNode || notInDoc) {
return; return;
} }
@@ -359,13 +362,16 @@ export default class MutationBuffer {
if (!node) { if (!node) {
for (let index = addList.length - 1; index >= 0; index--) { for (let index = addList.length - 1; index >= 0; index--) {
const _node = addList.get(index)!; const _node = addList.get(index)!;
const parentId = this.mirror.getId( // ensure _node is defined before attempting to find value
(_node.value.parentNode as Node) as INode, if (_node) {
); const parentId = this.mirror.getId(
const nextId = getNextId(_node.value); (_node.value.parentNode as Node) as INode,
if (parentId !== -1 && nextId !== -1) { );
node = _node; const nextId = getNextId(_node.value);
break; if (parentId !== -1 && nextId !== -1) {
node = _node;
break;
}
} }
} }
} }

View File

@@ -194,21 +194,43 @@ function initMoveObserver(
}, },
callbackThreshold, callbackThreshold,
); );
const updatePosition = throttle<MouseEvent | TouchEvent | DragEvent>(
// update position for mouse, touch, and drag events (drag event extends mouse event)
function handleUpdatePositionEvent(evt: MouseEvent | TouchEvent) {
const target = getEventTarget(evt);
const { clientX, clientY } = isTouchEvent(evt)
? evt.changedTouches[0]
: evt;
if (!timeBaseline) {
timeBaseline = Date.now();
}
positions.push({
x: clientX,
y: clientY,
id: mirror.getId(target as INode),
timeOffset: Date.now() - timeBaseline,
});
}
// separate call for non-drag events, in case DragEvent is not defined
const updatePosition = throttle<MouseEvent | TouchEvent>(
(evt) => { (evt) => {
const target = getEventTarget(evt); handleUpdatePositionEvent(evt);
const { clientX, clientY } = isTouchEvent(evt) wrappedCb(
? evt.changedTouches[0] evt instanceof MouseEvent
: evt; ? IncrementalSource.MouseMove
if (!timeBaseline) { : IncrementalSource.TouchMove,
timeBaseline = Date.now(); );
} },
positions.push({ threshold,
x: clientX, {
y: clientY, trailing: false,
id: mirror.getId(target as INode), },
timeOffset: Date.now() - timeBaseline, );
}); // call for drag events, when DragEvent is defined
const updateDragPosition = throttle<MouseEvent | TouchEvent | DragEvent>(
(evt) => {
handleUpdatePositionEvent(evt);
wrappedCb( wrappedCb(
evt instanceof DragEvent evt instanceof DragEvent
? IncrementalSource.Drag ? IncrementalSource.Drag
@@ -222,10 +244,13 @@ function initMoveObserver(
trailing: false, trailing: false,
}, },
); );
// it is possible DragEvent is undefined even on devices
// that support event 'drag'
const dragEventDefined = typeof DragEvent !== 'undefined';
const handlers = [ const handlers = [
on('mousemove', updatePosition, doc), on('mousemove', updatePosition, doc),
on('touchmove', updatePosition, doc), on('touchmove', updatePosition, doc),
on('drag', updatePosition, doc), on('drag', dragEventDefined ? updateDragPosition : updatePosition, doc),
]; ];
return () => { return () => {
handlers.forEach((h) => h()); handlers.forEach((h) => h());