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

composerMentionDrag.ts

118 lines4.1 KB

1import { PullRequestContextMetadata } from "@t3tools/contracts";
2import { serializeComposerFileLink } from "@t3tools/shared/composerTrigger";
3import { Option, Schema } from "effect";
4​
5export const COMPOSER_MENTION_DRAG_TYPE = "application/x-t3code-composer-mention";
6​
7/**
8 * Drag payload type carrying a pull request, as JSON `PullRequestContextMetadata`. Set on drags
9 * that start on a pull request row; the composer drops it as the same chip the `#` picker adds.
10 */
11export const COMPOSER_PULL_REQUEST_DRAG_TYPE = "application/x-t3code-pull-request";
12​
13const PullRequestDragPayload = Schema.fromJsonString(PullRequestContextMetadata);
14const decodePullRequestPayload = Schema.decodeUnknownOption(PullRequestDragPayload);
15​
16/** The `COMPOSER_PULL_REQUEST_DRAG_TYPE` data for a pull request row's drag. */
17export const encodePullRequestDragPayload = Schema.encodeSync(PullRequestDragPayload);
18​
19export function composerMentionFromTreePath(treePath: string): string | null {
20 const relativePath = treePath.replace(/\/+$/, "");
21 if (relativePath.length === 0) {
22 return null;
23 }
24 return serializeComposerFileLink(relativePath);
26​
27export function dataTransferHasComposerMention(types: ReadonlyArray<string>): boolean {
28 return (
29 types.includes(COMPOSER_MENTION_DRAG_TYPE) || types.includes(COMPOSER_PULL_REQUEST_DRAG_TYPE)
30 );
32​
33export interface ComposerMentionDragTransfer {
34 readonly types: ReadonlyArray<string>;
35 getData(format: string): string;
36 dropEffect: string;
38​
39export interface ComposerMentionDragEvent {
40 readonly dataTransfer: ComposerMentionDragTransfer;
41 readonly nativeEvent: { stopPropagation(): void };
42 preventDefault(): void;
43 stopPropagation(): void;
45​
46/**
47 * Deliberately narrow: there is no way to focus the editor from here. Focusing
48 * it synchronously during the drop makes the not-yet-reconciled editor sync its
49 * stale empty state back over the inserted mention; the insert path already
50 * focuses on the next frame, after the editor has caught up.
51 */
52export interface ComposerMentionDropHost {
53 insertMentionAtEnd(text: string): boolean;
54 insertPullRequestAtEnd(pullRequest: PullRequestContextMetadata): boolean;
55 setDragActive(active: boolean): void;
56 onInsertRejected(): void;
58​
59export interface ComposerMentionDragHandlers {
60 onDragEnter(event: ComposerMentionDragEvent): void;
61 onDragOver(event: ComposerMentionDragEvent): void;
62 onDrop(event: ComposerMentionDragEvent): void;
64​
65export function makeComposerMentionDragHandlers(
66 host: ComposerMentionDropHost,
67): ComposerMentionDragHandlers {
68 // Claim the event for the composer: React's stopPropagation only halts the
69 // synthetic dispatch, so the native event must be stopped too or the
70 // editor's own DOM listeners still process the drag.
71 const claim = (event: ComposerMentionDragEvent): boolean => {
72 if (!dataTransferHasComposerMention(event.dataTransfer.types)) {
73 return false;
74 }
75 event.preventDefault();
76 event.stopPropagation();
77 event.nativeEvent.stopPropagation();
78 return true;
79 };
80 return {
81 onDragEnter(event) {
82 if (claim(event)) {
83 host.setDragActive(true);
84 }
85 },
86 onDragOver(event) {
87 if (!claim(event)) {
88 return;
89 }
90 // The tree constrains its drags to effectAllowed "move" (pull request links allow
91 // anything); naming any other effect makes the browser cancel the drop without firing it.
92 event.dataTransfer.dropEffect = "move";
93 host.setDragActive(true);
94 },
95 onDrop(event) {
96 if (!claim(event)) {
97 return;
98 }
99 host.setDragActive(false);
100 if (event.dataTransfer.types.includes(COMPOSER_PULL_REQUEST_DRAG_TYPE)) {
101 const pullRequest = decodePullRequestPayload(
102 event.dataTransfer.getData(COMPOSER_PULL_REQUEST_DRAG_TYPE),
103 );
104 if (Option.isSome(pullRequest) && !host.insertPullRequestAtEnd(pullRequest.value)) {
105 host.onInsertRejected();
106 }
107 return;
108 }
109 const mention = event.dataTransfer.getData(COMPOSER_MENTION_DRAG_TYPE);
110 if (mention.length === 0) {
111 return;
112 }
113 if (!host.insertMentionAtEnd(`${mention} `)) {
114 host.onInsertRejected();
115 }
116 },
117 };