Skip to content

Commit bc2c4db

Browse files
committed
use cmd+shift+v to paste adjacent
1 parent 0e69210 commit bc2c4db

File tree

4 files changed

+24
-40
lines changed

4 files changed

+24
-40
lines changed

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

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,7 @@
259259
item: TreeItemState<TFile, TFolder>,
260260
predicate: (ancestor: TreeItemState<TFile, TFolder, TFolder>) => boolean,
261261
) {
262-
for (let ancestor = item.parent; ancestor !== undefined; ancestor = ancestor.parent) {
263-
if (predicate(ancestor)) {
264-
return ancestor;
265-
}
266-
}
262+
return getNearestAncestor(item, predicate) !== undefined;
267263
}
268264
269265
function isItemSelected(item: TreeItemState<TFile, TFolder>) {
@@ -804,38 +800,46 @@
804800
break;
805801
}
806802
807-
const dropDestinationItem = getDropDestinationItem(item);
808-
if (dropDestinationItem === undefined) {
809-
// Pasting at the root level is always allowed.
810-
paste(root);
811-
break;
803+
let pasteDestinationItem: TreeItemState<TFile, TFolder, TFolder> | undefined;
804+
switch (item.node.type) {
805+
case "file": {
806+
pasteDestinationItem = item.parent;
807+
break;
808+
}
809+
case "folder": {
810+
if (event.shiftKey) {
811+
pasteDestinationItem = item.parent;
812+
} else {
813+
pasteDestinationItem = item as TreeItemState<TFile, TFolder, TFolder>;
814+
}
815+
break;
816+
}
812817
}
813818
814-
if (clipboard.operation === "cut") {
815-
if (dropDestinationItem.inClipboard) {
816-
// Pasting an item inside itself is not allowed.
819+
if (pasteDestinationItem !== undefined && clipboard.operation === "cut") {
820+
if (pasteDestinationItem.inClipboard) {
817821
onCircularReference({
818-
source: dropDestinationItem,
819-
destination: dropDestinationItem.node,
822+
source: pasteDestinationItem,
823+
destination: pasteDestinationItem.node,
820824
});
821825
break;
822826
}
823827
824828
const nearestCopiedAncestor = getNearestAncestor(
825-
dropDestinationItem,
829+
pasteDestinationItem,
826830
isItemInClipboard,
827831
);
828832
if (nearestCopiedAncestor !== undefined) {
829-
// Pasting an item inside itself is not allowed.
830833
onCircularReference({
831834
source: nearestCopiedAncestor,
832-
destination: dropDestinationItem.node,
835+
destination: pasteDestinationItem.node,
833836
});
834837
break;
835838
}
836839
}
837840
838-
paste(dropDestinationItem.node).then((didPaste) => {
841+
const pasteDestination = pasteDestinationItem?.node ?? root;
842+
paste(pasteDestination).then((didPaste) => {
839843
if (didPaste) {
840844
event.currentTarget.focus();
841845
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export type OnCircularReferenceArgs<
5353
TFile extends FileNode = FileNode,
5454
TFolder extends FolderNode<TFile | TFolder> = DefaultTFolder<TFile>,
5555
> = {
56-
source: TreeItemState<TFile, TFolder>;
56+
source: TreeItemState<TFile, TFolder, TFolder>;
5757
destination: TFolder;
5858
};
5959

sites/preview/src/lib/components/Tree.svelte

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,19 @@
22
import { Tree, type FolderNode } from "svelte-file-tree";
33
import { flip } from "svelte/animate";
44
import { SvelteSet } from "svelte/reactivity";
5-
import { isControlOrMeta } from "$lib/helpers.js";
65
import TreeItem from "./TreeItem.svelte";
76
import type { TreeProps } from "./types.js";
87
98
const { root }: TreeProps = $props();
109
1110
const expandedIds = new SvelteSet<string>();
1211
let dropDestination: FolderNode | undefined = $state.raw();
13-
14-
let tree: Tree | null = $state.raw(null);
1512
</script>
1613

1714
<div class="flex min-h-svh p-2">
1815
<Tree
1916
{root}
2017
{expandedIds}
21-
bind:this={tree}
22-
tabindex={0}
2318
class={[
2419
"relative grow p-6 focus-visible:outline-2 focus-visible:outline-current",
2520
{
@@ -35,12 +30,6 @@
3530
onDropDestinationChange={(args) => {
3631
dropDestination = args.dropDestination;
3732
}}
38-
onkeydown={(event) => {
39-
if (event.key === "v" && isControlOrMeta(event)) {
40-
event.preventDefault();
41-
tree!.paste(root);
42-
}
43-
}}
4433
>
4534
{#snippet children({ items })}
4635
{#each items.filter((item) => item.visible) as item (item.node.id)}

sites/preview/src/lib/helpers.ts

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

0 commit comments

Comments
 (0)