Skip to content

Commit

Permalink
Prevent placing note off-screen
Browse files Browse the repository at this point in the history
  • Loading branch information
AbeJellinek committed Aug 7, 2023
1 parent 1a36b84 commit 7404b77
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/dom/common/dom-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,10 @@ abstract class DOMView<State extends DOMViewState, Data> {

if (this._tool.type == 'note') {
let range = this._getNoteTargetRange(event);
this._previewAnnotation = this._getAnnotationFromRange(range, 'note', this._tool.color);
this._renderAnnotations();
if (range) {
this._previewAnnotation = this._getAnnotationFromRange(range, 'note', this._tool.color);
this._renderAnnotations();
}
}
}

Expand All @@ -467,8 +469,10 @@ abstract class DOMView<State extends DOMViewState, Data> {
}
event.preventDefault();
let range = this._getNoteTargetRange(event);
this._previewAnnotation = this._getAnnotationFromRange(range, 'note', this._draggingNoteAnnotation.color);
this._renderAnnotations();
if (range) {
this._previewAnnotation = this._getAnnotationFromRange(range, 'note', this._draggingNoteAnnotation.color);
this._renderAnnotations();
}
}

protected _handleDragOver(event: DragEvent) {
Expand All @@ -489,7 +493,7 @@ abstract class DOMView<State extends DOMViewState, Data> {
this._options.onUpdateAnnotations([this._draggingNoteAnnotation]);
}

protected _getNoteTargetRange(event: PointerEvent | DragEvent) {
protected _getNoteTargetRange(event: PointerEvent | DragEvent): Range | null {
let target = event.target as Element;
// Disable pointer events and rerender so we can get the cursor position in the text layer,
// not the annotation layer, even if the mouse is over the annotation layer
Expand All @@ -516,6 +520,11 @@ abstract class DOMView<State extends DOMViewState, Data> {
}
range.selectNode(node);
}
let rect = range.getBoundingClientRect();
if (rect.right <= 0 || rect.left >= this._iframeWindow.innerWidth
|| rect.bottom <= 0 || rect.top >= this._iframeWindow.innerHeight) {
return null;
}
return range;
}

Expand Down

0 comments on commit 7404b77

Please sign in to comment.