@@ -8,10 +8,12 @@ import {
88} from "svelte-toolbelt" ;
99import { watch } from "runed" ;
1010import { on } from "svelte/events" ;
11- import type { TextSelectionLayerImplProps } from "./types.js" ;
11+ import type { PointerHandler , TextSelectionLayerImplProps } from "./types.js" ;
1212import { noop } from "$lib/internal/noop.js" ;
1313import { isHTMLElement } from "$lib/internal/is.js" ;
1414
15+ const noopPointer : PointerHandler = ( ) => { } ;
16+
1517interface TextSelectionLayerStateOpts
1618 extends ReadableBoxedValues <
1719 Required <
@@ -30,6 +32,9 @@ export class TextSelectionLayerState {
3032 readonly opts : TextSelectionLayerStateOpts ;
3133 readonly domContext : DOMContext ;
3234 #unsubSelectionLock = noop ;
35+ #enabledSnapshot = false ;
36+ #onPointerDownSnapshot: PointerHandler = noopPointer ;
37+ #onPointerUpSnapshot: PointerHandler = noopPointer ;
3338
3439 constructor ( opts : TextSelectionLayerStateOpts ) {
3540 this . opts = opts ;
@@ -38,14 +43,24 @@ export class TextSelectionLayerState {
3843 let unsubEvents = noop ;
3944
4045 watch (
41- ( ) => this . opts . enabled . current ,
42- ( isEnabled ) => {
43- if ( isEnabled ) {
46+ ( ) =>
47+ [
48+ this . opts . enabled . current ,
49+ this . opts . onPointerDown . current ,
50+ this . opts . onPointerUp . current ,
51+ ] as const ,
52+ ( [ enabled , onPointerDown , onPointerUp ] ) => {
53+ this . #enabledSnapshot = enabled ;
54+ this . #onPointerDownSnapshot = onPointerDown ;
55+ this . #onPointerUpSnapshot = onPointerUp ;
56+
57+ if ( enabled ) {
4458 globalThis . bitsTextSelectionLayers . set ( this , this . opts . enabled ) ;
4559 unsubEvents ( ) ;
4660 unsubEvents = this . #addEventListeners( ) ;
4761 }
4862 return ( ) => {
63+ this . #enabledSnapshot = false ;
4964 unsubEvents ( ) ;
5065 this . #resetSelectionLock( ) ;
5166 globalThis . bitsTextSelectionLayers . delete ( this ) ;
@@ -60,22 +75,26 @@ export class TextSelectionLayerState {
6075 on (
6176 this . domContext . getDocument ( ) ,
6277 "pointerup" ,
63- composeHandlers ( this . #resetSelectionLock, this . opts . onPointerUp . current )
78+ composeHandlers ( this . #resetSelectionLock, this . #pointerupUserHandler )
6479 )
6580 ) ;
6681 }
6782
83+ #pointerupUserHandler = ( e : PointerEvent ) => {
84+ this . #onPointerUpSnapshot( e ) ;
85+ } ;
86+
6887 #pointerdown = ( e : PointerEvent ) => {
6988 const node = this . opts . ref . current ;
7089 const target = e . target ;
71- if ( ! isHTMLElement ( node ) || ! isHTMLElement ( target ) || ! this . opts . enabled . current ) return ;
90+ if ( ! isHTMLElement ( node ) || ! isHTMLElement ( target ) || ! this . #enabledSnapshot ) return ;
7291 /**
7392 * We only lock user-selection overflow if layer is the top most layer and
7493 * pointerdown occurred inside the node. You are still allowed to select text
7594 * outside the node provided pointerdown occurs outside the node.
7695 */
7796 if ( ! isHighestLayer ( this ) || ! contains ( node , target ) ) return ;
78- this . opts . onPointerDown . current ( e ) ;
97+ this . #onPointerDownSnapshot ( e ) ;
7998 if ( e . defaultPrevented ) return ;
8099 this . #unsubSelectionLock = preventTextSelectionOverflow (
81100 node ,
0 commit comments