-
-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathChoiceList.svelte
More file actions
108 lines (96 loc) · 3.53 KB
/
ChoiceList.svelte
File metadata and controls
108 lines (96 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<script lang="ts">
import type IChoice from "../../types/choices/IChoice";
import ChoiceListItem from "./ChoiceListItem.svelte";
import MultiChoiceListItem from "./MultiChoiceListItem.svelte";
import {dndzone, SHADOW_PLACEHOLDER_ITEM_ID} from "svelte-dnd-action";
import type {DndEvent} from "svelte-dnd-action";
import {createEventDispatcher} from "svelte";
import type { App } from "obsidian";
export let choices: IChoice[] = [];
export let roots: IChoice[] | undefined;
export let app: App;
// When true, keeps drag disabled regardless of user action
export let forceDragDisabled: boolean = false;
let collapseId: string;
let dragDisabled: boolean = true;
const dispatcher = createEventDispatcher();
function emitChoicesReordered() {
dispatcher('reorderChoices', {choices});
}
function handleConsider(e: CustomEvent<DndEvent>) {
let {items: newItems, info: {id}} = e.detail;
collapseId = id;
// Remove internal placeholder item from state to avoid ghost gaps
const sanitized = (newItems as IChoice[]).filter(
(it) => it.id !== SHADOW_PLACEHOLDER_ITEM_ID
);
choices = sanitized;
}
function handleSort(e: CustomEvent<DndEvent>) {
let {items: newItems} = e.detail;
collapseId = "";
// Remove internal placeholder item from state to avoid ghost gaps
const sanitized = (newItems as IChoice[]).filter(
(it) => it.id !== SHADOW_PLACEHOLDER_ITEM_ID
);
choices = sanitized;
// Always re-disable dragging when the sort finalizes
dragDisabled = true;
emitChoicesReordered();
}
function startDrag(e: Event) {
if (forceDragDisabled) return; // Do not enable drag while forcing disabled (e.g., during filtering)
// prevent focus/selection side-effects before enabling drag
// @ts-ignore
if (typeof e?.preventDefault === 'function') e.preventDefault();
dragDisabled = false;
}
</script>
<div
use:dndzone={{items: choices, dragDisabled, dropTargetStyle: {}}}
on:consider={handleConsider}
on:finalize={handleSort}
class="choiceList"
style="{choices.length === 0 ? 'padding-bottom: 0.5rem' : ''}">
{#each choices.filter(c => c.id !== SHADOW_PLACEHOLDER_ITEM_ID) as choice(choice.id)}
{#if choice.type !== "Multi"}
<ChoiceListItem
{app}
roots={roots ?? choices}
bind:dragDisabled={dragDisabled}
on:deleteChoice
on:configureChoice
on:toggleCommand
on:duplicateChoice
on:renameChoice
on:moveChoice
startDrag={startDrag}
bind:choice
/>
{:else}
<MultiChoiceListItem
{app}
roots={roots ?? choices}
bind:dragDisabled={dragDisabled}
on:deleteChoice
on:configureChoice
on:toggleCommand
on:duplicateChoice
on:renameChoice
on:moveChoice
on:reorderChoices
startDrag={startDrag}
bind:collapseId
bind:choice
/>
{/if}
{/each}
</div>
<style>
.choiceList {
width: auto;
border: 0 solid black;
overflow-y: auto;
height: auto;
}
</style>