forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.tsx
More file actions
152 lines (132 loc) · 4.5 KB
/
Copy pathWindow.tsx
File metadata and controls
152 lines (132 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import React, { useCallback, useEffect } from "react";
import { WindowProps } from "./props";
import { useChatContext } from "./ChatContext";
import { useCopilotContext } from "@copilotkit/react-core";
import { isMacOS } from "@copilotkit/shared";
export const Window = ({
children,
clickOutsideToClose,
shortcut,
hitEscapeToClose,
}: WindowProps) => {
const windowRef = React.useRef<HTMLDivElement>(null);
const context = useCopilotContext();
const { open, setOpen } = useChatContext();
const handleClickOutside = useCallback(
(event: MouseEvent) => {
if (!clickOutsideToClose) {
return;
}
const parentElement = windowRef.current?.parentElement;
let className = "";
if (event.target instanceof HTMLElement) {
className = event.target.className;
}
if (
open &&
parentElement &&
!parentElement.contains(event.target as any) &&
// prevent closing the window when clicking on the debug menu
!className.includes("copilotKitDebugMenu")
) {
setOpen(false);
}
},
[clickOutsideToClose, open, setOpen],
);
const handleKeyDown = useCallback(
(event: KeyboardEvent) => {
const target = event.target as HTMLElement;
const isInput =
target.tagName === "INPUT" ||
target.tagName === "SELECT" ||
target.tagName === "TEXTAREA" ||
target.isContentEditable;
const isDescendantOfWrapper = windowRef.current?.contains(target);
if (
open &&
event.key === "Escape" &&
(!isInput || isDescendantOfWrapper) &&
hitEscapeToClose
) {
setOpen(false);
} else if (
event.key === shortcut &&
((isMacOS() && event.metaKey) || (!isMacOS() && event.ctrlKey)) &&
(!isInput || isDescendantOfWrapper)
) {
setOpen(!open);
}
},
[hitEscapeToClose, shortcut, open, setOpen],
);
const adjustForMobile = useCallback(() => {
const copilotKitWindow = windowRef.current;
const vv = window.visualViewport;
if (!copilotKitWindow || !vv) {
return;
}
if (window.innerWidth < 640 && open) {
copilotKitWindow.style.height = `${vv.height}px`;
copilotKitWindow.style.left = `${vv.offsetLeft}px`;
copilotKitWindow.style.top = `${vv.offsetTop}px`;
document.body.style.position = "fixed";
document.body.style.width = "100%";
document.body.style.height = `${window.innerHeight}px`;
document.body.style.overflow = "hidden";
document.body.style.touchAction = "none";
// Prevent scrolling on iOS
document.body.addEventListener("touchmove", preventScroll, {
passive: false,
});
} else {
copilotKitWindow.style.height = "";
copilotKitWindow.style.left = "";
copilotKitWindow.style.top = "";
document.body.style.position = "";
document.body.style.height = "";
document.body.style.width = "";
document.body.style.overflow = "";
document.body.style.top = "";
document.body.style.touchAction = "";
document.body.removeEventListener("touchmove", preventScroll);
}
}, [open]);
useEffect(() => {
document.addEventListener("mousedown", handleClickOutside);
document.addEventListener("keydown", handleKeyDown);
if (window.visualViewport) {
window.visualViewport.addEventListener("resize", adjustForMobile);
adjustForMobile();
}
return () => {
document.removeEventListener("mousedown", handleClickOutside);
document.removeEventListener("keydown", handleKeyDown);
if (window.visualViewport) {
window.visualViewport.removeEventListener("resize", adjustForMobile);
}
};
}, [adjustForMobile, handleClickOutside, handleKeyDown]);
return (
<div className={`copilotKitWindow ${open ? " open" : ""}`} ref={windowRef}>
{children}
</div>
);
};
const preventScroll = (event: TouchEvent): void => {
let targetElement = event.target as Element;
// Function to check if the target has the parent with a given class
const hasParentWithClass = (element: Element, className: string): boolean => {
while (element && element !== document.body) {
if (element.classList.contains(className)) {
return true;
}
element = element.parentElement!;
}
return false;
};
// Check if the target of the touch event is inside an element with the 'copilotKitMessages' class
if (!hasParentWithClass(targetElement, "copilotKitMessages")) {
event.preventDefault();
}
};