Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/lib/svelte-json-tree/SvelteJsonTree/JSONNested.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,34 @@
$expanded = !$expanded;
}

// Track mouse events to prevent collapse during text selection
let mouseDownTarget: EventTarget | null = null;
let mouseDownTime = 0;

function handleMouseDown(event: MouseEvent) {
mouseDownTarget = event.target;
mouseDownTime = Date.now();
}

function handleClick(event: MouseEvent) {
const clickTime = Date.now();
const timeDiff = clickTime - mouseDownTime;

// If the mouse moved to a different target or took longer than 200ms,
// it's likely a text selection drag, so don't toggle
if (mouseDownTarget !== event.target || timeDiff > 200) {
return;
}

// Check if there's any text selection
const selection = window.getSelection();
if (selection && selection.toString().length > 0) {
return;
}

toggleExpand();
}

$: child_expanded = keys.map(() => writable(false));
</script>

Expand All @@ -56,7 +84,7 @@

{#if $expanded}
<!-- svelte-ignore a11y-click-events-have-key-events a11y-no-noninteractive-element-interactions -->
<ul on:click|stopPropagation={toggleExpand}>
<ul on:mousedown={handleMouseDown} on:click|stopPropagation={handleClick}>
{#each keys as key, index}
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
<li class:indent={$expanded} on:click|stopPropagation={() => {}}>
Expand Down