Skip to content

Commit bba01e7

Browse files
committed
breaking: remove onChildrenChange and onClipboardChange
1 parent 8ba7f60 commit bba01e7

File tree

4 files changed

+55
-109
lines changed

4 files changed

+55
-109
lines changed

packages/svelte-file-tree/src/lib/components/Tree.svelte

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
<script
2-
lang="ts"
3-
generics="TFile extends FileNode = FileNode, TFolder extends FolderNode<TFile | TFolder> = DefaultTFolder<TFile>, TTree extends FileTree<TFile | TFolder> = FileTree<TFile | TFolder>"
4-
>
1+
<script lang="ts" module>
52
import type { DragLocation } from "@atlaskit/pragmatic-drag-and-drop/dist/types/internal-types.js";
63
import {
74
dropTargetForElements,
85
type ElementDropTargetGetFeedbackArgs,
6+
type ElementEventBasePayload,
7+
type ElementGetFeedbackArgs,
98
} from "@atlaskit/pragmatic-drag-and-drop/element/adapter";
109
import {
1110
dropTargetForExternal,
1211
type ExternalDropTargetGetFeedbackArgs,
1312
} from "@atlaskit/pragmatic-drag-and-drop/external/adapter";
1413
import { DEV } from "esm-env";
14+
import { getContext, hasContext, setContext } from "svelte";
1515
import { SvelteSet } from "svelte/reactivity";
1616
import { falsePredicate, isControlOrMeta, noop, truePredicate } from "$lib/internal/helpers.js";
1717
import {
@@ -21,14 +21,60 @@
2121
TreeItemState,
2222
type DefaultTFolder,
2323
} from "$lib/tree.svelte.js";
24-
import { setTreeContext } from "./context.js";
2524
import { DragData } from "./data.js";
2625
import type {
2726
TreeCopyToClipboardMethodOptions,
2827
TreeProps,
2928
TreeRemoveMethodOptions,
3029
} from "./types.js";
3130
31+
export type TreeItemEvent<TEvent extends Event = Event> = TEvent & {
32+
currentTarget: HTMLDivElement;
33+
};
34+
35+
export type TreeContext<
36+
TFile extends FileNode = FileNode,
37+
TFolder extends FolderNode<TFile | TFolder> = DefaultTFolder<TFile>,
38+
TTree extends FileTree<TFile | TFolder> = FileTree<TFile | TFolder>,
39+
> = {
40+
root: () => TTree;
41+
tabbableId: () => string;
42+
getItemElementId: (itemId: string) => string;
43+
onFocusIn: (item: TreeItemState<TFile, TFolder>, event: TreeItemEvent<FocusEvent>) => void;
44+
onKeyDown: (item: TreeItemState<TFile, TFolder>, event: TreeItemEvent<KeyboardEvent>) => void;
45+
onClick: (item: TreeItemState<TFile, TFolder>, event: TreeItemEvent<MouseEvent>) => void;
46+
getDropDestination: (item: TreeItemState<TFile, TFolder>) => TFolder | TTree;
47+
canDrag: (item: TreeItemState<TFile, TFolder>, args: ElementGetFeedbackArgs) => boolean;
48+
canDropElement: (
49+
item: TreeItemState<TFile, TFolder>,
50+
args: ElementDropTargetGetFeedbackArgs,
51+
) => boolean;
52+
canDropExternal: (
53+
item: TreeItemState<TFile, TFolder>,
54+
args: ExternalDropTargetGetFeedbackArgs,
55+
) => boolean;
56+
onDragStart: (item: TreeItemState<TFile, TFolder>, args: ElementEventBasePayload) => void;
57+
onDestroyItem: (item: TreeItemState<TFile, TFolder>) => void;
58+
};
59+
60+
const CONTEXT_KEY = Symbol("TreeContext");
61+
62+
export function getTreeContext<
63+
TFile extends FileNode = FileNode,
64+
TFolder extends FolderNode<TFile | TFolder> = DefaultTFolder<TFile>,
65+
TTree extends FileTree<TFile | TFolder> = FileTree<TFile | TFolder>,
66+
>(): TreeContext<TFile, TFolder, TTree> {
67+
if (DEV && !hasContext(CONTEXT_KEY)) {
68+
throw new Error("No parent <Tree> found");
69+
}
70+
return getContext(CONTEXT_KEY);
71+
}
72+
</script>
73+
74+
<script
75+
lang="ts"
76+
generics="TFile extends FileNode = FileNode, TFolder extends FolderNode<TFile | TFolder> = DefaultTFolder<TFile>, TTree extends FileTree<TFile | TFolder> = FileTree<TFile | TFolder>"
77+
>
3278
let {
3379
children,
3480
root,
@@ -64,8 +110,6 @@
64110
}
65111
}
66112
},
67-
onClipboardChange = noop,
68-
onChildrenChange = noop,
69113
onResolveNameConflict = () => "cancel",
70114
onCircularReference = noop,
71115
canCopy = truePredicate,
@@ -269,13 +313,11 @@
269313
}
270314
clipboardIds.add(itemId);
271315
pasteOperation = newPasteOperation;
272-
onClipboardChange({ clipboardIds, pasteOperation });
273316
}
274317
275318
export function clearClipboard() {
276319
clipboardIds.clear();
277320
pasteOperation = undefined;
278-
onClipboardChange({ clipboardIds, pasteOperation });
279321
}
280322
281323
async function copy(destination: TFolder | TTree) {
@@ -334,11 +376,6 @@
334376
for (const copy of copies) {
335377
destinationChildren.push(copy);
336378
}
337-
onChildrenChange({
338-
operation: "insert",
339-
target: destination,
340-
children: destinationChildren,
341-
});
342379
343380
onCopy({ sources, copies, destination });
344381
return true;
@@ -409,22 +446,12 @@
409446
410447
for (const owner of sourceOwners) {
411448
owner.children = owner.children.filter((child) => !sourceIds.has(child.id));
412-
onChildrenChange({
413-
operation: "remove",
414-
target: owner,
415-
children: owner.children,
416-
});
417449
}
418450
419451
const destinationChildren = destination.children;
420452
for (const source of sources) {
421453
destinationChildren.push(source.node);
422454
}
423-
onChildrenChange({
424-
operation: "insert",
425-
target: destination,
426-
children: destinationChildren,
427-
});
428455
429456
onMove({ sources, destination });
430457
return true;
@@ -547,31 +574,16 @@
547574
owner.children = owner.children.filter(
548575
(child) => !selectedIds.has(child.id) && child !== node,
549576
);
550-
onChildrenChange({
551-
operation: "remove",
552-
target: owner,
553-
children: owner.children,
554-
});
555577
}
556578
} else {
557579
const owner = item.parent?.node ?? root;
558580
owner.children = owner.children.filter((child) => child !== node);
559-
onChildrenChange({
560-
operation: "remove",
561-
target: owner,
562-
children: owner.children,
563-
});
564581
}
565582
566-
const currentClipboardSize = clipboardIds.size;
567583
for (const item of removed) {
568584
onRemoveNode(item.node);
569585
}
570586
571-
if (clipboardIds.size !== currentClipboardSize) {
572-
onClipboardChange({ clipboardIds, pasteOperation });
573-
}
574-
575587
onRemove({ removed });
576588
return true;
577589
}
@@ -665,7 +677,7 @@
665677
}
666678
}
667679
668-
setTreeContext<TFile, TFolder, TTree>({
680+
const context: TreeContext<TFile, TFolder, TTree> = {
669681
root: () => root,
670682
tabbableId: () => tabbableId ?? root.children[0].id,
671683
getItemElementId,
@@ -1000,7 +1012,8 @@
10001012
tabbableId = undefined;
10011013
}
10021014
},
1003-
});
1015+
};
1016+
setContext(CONTEXT_KEY, context);
10041017
10051018
$effect(() => {
10061019
return dropTargetForElements({

packages/svelte-file-tree/src/lib/components/TreeItem.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import type { EventHandler } from "svelte/elements";
1111
import { noop } from "$lib/internal/helpers.js";
1212
import type { DefaultTFolder, FileNode, FileTree, FolderNode } from "$lib/tree.svelte.js";
13-
import { getTreeContext } from "./context.js";
13+
import { getTreeContext } from "./Tree.svelte";
1414
import { DragData } from "./data.js";
1515
import type { TreeItemProps } from "./types.js";
1616

packages/svelte-file-tree/src/lib/components/context.ts

Lines changed: 0 additions & 65 deletions
This file was deleted.

packages/svelte-file-tree/src/lib/components/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,6 @@ export interface TreeProps<
139139
ref?: HTMLDivElement | null;
140140
isItemDisabled?: (node: TFile | TFolder) => boolean;
141141
copyNode?: (node: TFile | TFolder) => TFile | TFolder;
142-
onClipboardChange?: (args: OnClipboardChangeArgs) => void;
143-
onChildrenChange?: (args: OnChildrenChangeArgs<TFile, TFolder, TTree>) => void;
144142
onResolveNameConflict?: (
145143
args: OnResolveNameConflictArgs<TFile, TFolder, TTree>,
146144
) => MaybePromise<NameConflictResolution>;

0 commit comments

Comments
 (0)