|
19 | 19 | type DefaultTFolder,
|
20 | 20 | } from "$lib/tree.svelte.js";
|
21 | 21 | import { setTreeContext } from "./context.js";
|
22 |
| - import type { PasteOperation, TreeProps } from "./types.js"; |
| 22 | + import type { |
| 23 | + TreeCopyToClipboardMethodOptions, |
| 24 | + TreeProps, |
| 25 | + TreeRemoveMethodOptions, |
| 26 | + } from "./types.js"; |
23 | 27 |
|
24 | 28 | let {
|
25 | 29 | children,
|
|
248 | 252 | }
|
249 | 253 | }
|
250 | 254 |
|
251 |
| - export function copyToClipboard(itemId: string, operation: PasteOperation) { |
| 255 | + export function copyToClipboard(itemId: string, options: TreeCopyToClipboardMethodOptions = {}) { |
| 256 | + const { pasteOperation: newPasteOperation = "copy", batched = true } = options; |
| 257 | +
|
252 | 258 | clipboardIds.clear();
|
253 |
| - for (const id of selectedIds) { |
254 |
| - clipboardIds.add(id); |
| 259 | + if (batched) { |
| 260 | + for (const id of selectedIds) { |
| 261 | + clipboardIds.add(id); |
| 262 | + } |
255 | 263 | }
|
256 | 264 | clipboardIds.add(itemId);
|
257 |
| - pasteOperation = operation; |
| 265 | + pasteOperation = newPasteOperation; |
258 | 266 | onClipboardChange({ clipboardIds, pasteOperation });
|
259 | 267 | }
|
260 | 268 |
|
|
457 | 465 | }
|
458 | 466 | }
|
459 | 467 |
|
460 |
| - export async function remove(item: TreeItemState<TFile, TFolder>) { |
| 468 | + export async function remove( |
| 469 | + item: TreeItemState<TFile, TFolder>, |
| 470 | + options: TreeRemoveMethodOptions = {}, |
| 471 | + ) { |
| 472 | + const { batched = true } = options; |
| 473 | +
|
461 | 474 | const removed: Array<TreeItemState<TFile, TFolder>> = [];
|
462 | 475 | const removedOwners = new Set<TFolder | TTree>();
|
463 |
| - outer: for (const id of selectedIds) { |
464 |
| - const current = getItem(id); |
465 |
| - if (current === undefined) { |
466 |
| - continue; |
467 |
| - } |
| 476 | + if (batched) { |
| 477 | + outer: for (const id of selectedIds) { |
| 478 | + const current = getItem(id); |
| 479 | + if (current === undefined) { |
| 480 | + continue; |
| 481 | + } |
468 | 482 |
|
469 |
| - for (let ancestor = current.parent; ancestor !== undefined; ancestor = ancestor.parent) { |
470 |
| - if (ancestor.selected) { |
471 |
| - // If an ancestor is removed, its children are removed along with it. |
472 |
| - continue outer; |
| 483 | + for (let ancestor = current.parent; ancestor !== undefined; ancestor = ancestor.parent) { |
| 484 | + if (ancestor.selected) { |
| 485 | + // If an ancestor is removed, its children are removed along with it. |
| 486 | + continue outer; |
| 487 | + } |
473 | 488 | }
|
474 |
| - } |
475 | 489 |
|
476 |
| - removed.push(current); |
477 |
| - removedOwners.add(current.parent?.node ?? root); |
| 490 | + removed.push(current); |
| 491 | + removedOwners.add(current.parent?.node ?? root); |
| 492 | + } |
478 | 493 | }
|
479 | 494 |
|
480 | 495 | if (!removed.includes(item)) {
|
|
521 | 536 | }
|
522 | 537 |
|
523 | 538 | const node = item.node;
|
524 |
| - for (const owner of removedOwners) { |
525 |
| - owner.children = owner.children.filter( |
526 |
| - (child) => !selectedIds.has(child.id) && child !== node, |
527 |
| - ); |
| 539 | + if (batched) { |
| 540 | + for (const owner of removedOwners) { |
| 541 | + owner.children = owner.children.filter( |
| 542 | + (child) => !selectedIds.has(child.id) && child !== node, |
| 543 | + ); |
| 544 | + onChildrenChange({ |
| 545 | + operation: "remove", |
| 546 | + target: owner, |
| 547 | + children: owner.children, |
| 548 | + }); |
| 549 | + } |
| 550 | + } else { |
| 551 | + const owner = item.parent?.node ?? root; |
| 552 | + owner.children = owner.children.filter((child) => child !== node); |
528 | 553 | onChildrenChange({
|
529 | 554 | operation: "remove",
|
530 | 555 | target: owner,
|
|
845 | 870 | break;
|
846 | 871 | }
|
847 | 872 |
|
848 |
| - copyToClipboard(item.node.id, "copy"); |
| 873 | + copyToClipboard(item.node.id, { pasteOperation: "copy" }); |
849 | 874 | break;
|
850 | 875 | }
|
851 | 876 | case "x": {
|
852 | 877 | if (!isControlOrMeta(event)) {
|
853 | 878 | break;
|
854 | 879 | }
|
855 | 880 |
|
856 |
| - copyToClipboard(item.node.id, "cut"); |
| 881 | + copyToClipboard(item.node.id, { pasteOperation: "cut" }); |
857 | 882 | break;
|
858 | 883 | }
|
859 | 884 | case "v": {
|
|
0 commit comments