Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

fileTreeDragMention.ts

102 lines3.5 KB

1import {
2 COMPOSER_MENTION_DRAG_TYPE,
3 composerMentionFromTreePath,
4} from "~/components/chat/composerMentionDrag";
5​
6interface FileTreeDragTransfer {
7 setData(format: string, data: string): void;
8}
9​
10export interface FileTreeDragStartEvent {
11 readonly dataTransfer: FileTreeDragTransfer | null;
12 composedPath(): ReadonlyArray<unknown>;
14​
15export interface FileTreeDragMentionHost {
16 /** Drop the tree's gesture-applied selection of the dragged row. */
17 deselect(treePath: string): void;
18 /** The path a dragged row mentions; defaults to the tree path itself. */
19 mentionPath?(treePath: string): string;
21​
22export interface FileTreeDragMentionController {
23 /**
24 * True from the moment a row drag starts until it ends. The tree selects
25 * the dragged row as part of the gesture; selection changes made while
26 * this is set are gesture side effects, not requests to open a file.
27 */
28 isDragInProgress(): boolean;
29 /** Mirror of the tree's current selection, needed for multi-row drags. */
30 handleSelectionChange(selectedPaths: ReadonlyArray<string>): void;
31 handleDragStart(event: FileTreeDragStartEvent): void;
32 handleDragEnd(): void;
34​
35const itemPathOf = (node: unknown): string | null => {
36 if (typeof node !== "object" || node === null) {
37 return null;
38 }
39 const element = node as { getAttribute?: (name: string) => string | null };
40 return typeof element.getAttribute === "function" ? element.getAttribute("data-item-path") : null;
41};
42​
43/**
44 * Tags file-tree drags with the composer mention payload and keeps the drag
45 * from acting like a click: while the drag runs, selection changes are
46 * suppressed, and when it ends the dragged rows are deselected so nothing is
47 * left highlighted and a later click on them still fires a selection change.
48 */
49export function createFileTreeDragMentionController(
50 host: FileTreeDragMentionHost,
51): FileTreeDragMentionController {
52 let selection: ReadonlyArray<string> = [];
53 let draggedPaths: ReadonlyArray<string> = [];
54 return {
55 isDragInProgress: () => draggedPaths.length > 0,
56 handleSelectionChange(selectedPaths) {
57 selection = selectedPaths;
58 },
59 handleDragStart(event) {
60 if (event.dataTransfer === null) {
61 return;
62 }
63 // Only drags that originate on a tree row are mentions; a text/plain
64 // fallback would also tag drags of selected text from the panel chrome.
65 let itemPath: string | null = null;
66 for (const node of event.composedPath()) {
67 itemPath = itemPathOf(node);
68 if (itemPath !== null) {
69 break;
70 }
71 }
72 if (itemPath === null) {
73 return;
74 }
75 // Same rule the tree applies to the drag itself: dragging a row that is
76 // part of the current selection drags the whole selection.
77 const dragged = selection.includes(itemPath) ? selection : [itemPath];
78 const mentions = dragged
79 .map((path) => {
80 const treePath = path.replace(/\/+$/, "");
81 return composerMentionFromTreePath(
82 treePath && host.mentionPath ? host.mentionPath(treePath) : path,
83 );
84 })
85 .filter((mention): mention is string => mention !== null);
86 if (mentions.length === 0) {
87 return;
88 }
89 draggedPaths = dragged;
90 event.dataTransfer.setData(COMPOSER_MENTION_DRAG_TYPE, mentions.join(" "));
91 },
92 handleDragEnd() {
93 if (draggedPaths.length === 0) {
94 return;
95 }
96 for (const path of draggedPaths) {
97 host.deselect(path);
98 }
99 draggedPaths = [];
100 },
101 };