Skip to content

Commit

Permalink
EPUB/Snapshot: Don't use pointerId to distinguish pointers
Browse files Browse the repository at this point in the history
Doesn't stay the same across events in iOS WebViews.
  • Loading branch information
AbeJellinek committed Nov 27, 2024
1 parent aff6e1c commit e1821b1
Showing 1 changed file with 22 additions and 30 deletions.
52 changes: 22 additions & 30 deletions src/dom/common/dom-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ abstract class DOMView<State extends DOMViewState, Data> {

protected _touchAnnotationStartPosition: CaretPosition | null = null;

protected _touchAnnotationPointerID: number | null = null;

protected _draggingNoteAnnotation: WADMAnnotation | null = null;

protected _resizingAnnotationID: string | null = null;
Expand Down Expand Up @@ -1060,15 +1058,14 @@ abstract class DOMView<State extends DOMViewState, Data> {
}

protected _handlePointerDown(event: PointerEvent) {
if (event.button == 0 && !this._touchAnnotationPointerID) {
if (event.button == 0 && event.isPrimary) {
this._gotPointerUp = false;
this._pointerMovedWhileDown = false;

if ((event.pointerType === 'touch' || event.pointerType === 'pen')
&& (this._tool.type === 'highlight' || this._tool.type === 'underline')
&& event.target !== this._annotationShadowRoot.host) {
this._touchAnnotationStartPosition = caretPositionFromPoint(this._iframeDocument, event.clientX, event.clientY);
this._touchAnnotationPointerID = event.pointerId;
this._iframeDocument.body.classList.add('creating-touch-annotation');
event.stopPropagation();
}
Expand Down Expand Up @@ -1103,7 +1100,7 @@ abstract class DOMView<State extends DOMViewState, Data> {
}

protected _handlePointerUp(event: PointerEvent) {
if (event.button !== 0 || event.pointerId !== this._touchAnnotationPointerID) {
if (event.button !== 0 || !event.isPrimary) {
return;
}

Expand All @@ -1120,38 +1117,33 @@ abstract class DOMView<State extends DOMViewState, Data> {
}
}
this._touchAnnotationStartPosition = null;
this._touchAnnotationPointerID = null;
this._iframeDocument.body.classList.remove('creating-touch-annotation');
}

protected _handlePointerMove(event: PointerEvent) {
if (event.buttons % 1 == 0) {
this._pointerMovedWhileDown = true;

if (this._touchAnnotationStartPosition
&& (event.pointerType === 'touch' || event.pointerType === 'pen')
&& (this._tool.type === 'highlight' || this._tool.type === 'underline')) {
if (event.pointerId !== this._touchAnnotationPointerID) {
return;
if (event.buttons % 1 != 0 || !event.isPrimary) {
return;
}
this._pointerMovedWhileDown = true;
if (this._touchAnnotationStartPosition
&& (event.pointerType === 'touch' || event.pointerType === 'pen')
&& (this._tool.type === 'highlight' || this._tool.type === 'underline')) {
let endPos = caretPositionFromPoint(this._iframeDocument, event.clientX, event.clientY);
if (endPos) {
let range = this._iframeDocument.createRange();
range.setStart(this._touchAnnotationStartPosition.offsetNode, this._touchAnnotationStartPosition.offset);
range.setEnd(endPos.offsetNode, endPos.offset);
if (range.collapsed) {
range.setStart(endPos.offsetNode, endPos.offset);
range.setEnd(this._touchAnnotationStartPosition.offsetNode, this._touchAnnotationStartPosition.offset);
}

let endPos = caretPositionFromPoint(this._iframeDocument, event.clientX, event.clientY);
if (endPos) {
let range = this._iframeDocument.createRange();
range.setStart(this._touchAnnotationStartPosition.offsetNode, this._touchAnnotationStartPosition.offset);
range.setEnd(endPos.offsetNode, endPos.offset);
if (range.collapsed) {
range.setStart(endPos.offsetNode, endPos.offset);
range.setEnd(this._touchAnnotationStartPosition.offsetNode, this._touchAnnotationStartPosition.offset);
}
let annotation = this._getAnnotationFromRange(range, this._tool.type, this._tool.color);
if (annotation) {
this._previewAnnotation = annotation;
this._renderAnnotations();
}
let annotation = this._getAnnotationFromRange(range, this._tool.type, this._tool.color);
if (annotation) {
this._previewAnnotation = annotation;
this._renderAnnotations();
}
event.stopPropagation();
}
event.stopPropagation();
}
}

Expand Down

0 comments on commit e1821b1

Please sign in to comment.