Compare commits
13 Commits
fix-editor
...
feat/e2b
Author | SHA1 | Date | |
---|---|---|---|
bd6284df8f | |||
c262fb2a31 | |||
ed709210e3 | |||
97c8598717 | |||
9ec59bc781 | |||
687416e6e9 | |||
006c5cea66 | |||
869ae6c148 | |||
7353e88567 | |||
a0fb905a04 | |||
0df074924f | |||
e5b320d1c5 | |||
b561f1e962 |
@ -192,8 +192,6 @@ io.on("connection", async (socket) => {
|
|||||||
|
|
||||||
// todo: send diffs + debounce for efficiency
|
// todo: send diffs + debounce for efficiency
|
||||||
socket.on("saveFile", async (fileId: string, body: string) => {
|
socket.on("saveFile", async (fileId: string, body: string) => {
|
||||||
if (!fileId) return; // handles saving when no file is open
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (Buffer.byteLength(body, "utf-8") > MAX_BODY_SIZE) {
|
if (Buffer.byteLength(body, "utf-8") > MAX_BODY_SIZE) {
|
||||||
socket.emit(
|
socket.emit(
|
||||||
|
@ -63,6 +63,14 @@ const CodeEditor = dynamic(() => import("@/components/editor"), {
|
|||||||
loading: () => <Loading />,
|
loading: () => <Loading />,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function getReactDefinitionFile() {
|
||||||
|
const reactDefinitionFile = fs.readFileSync(
|
||||||
|
"node_modules/@types/react/index.d.ts",
|
||||||
|
"utf8"
|
||||||
|
)
|
||||||
|
return reactDefinitionFile
|
||||||
|
}
|
||||||
|
|
||||||
export default async function CodePage({ params }: { params: { id: string } }) {
|
export default async function CodePage({ params }: { params: { id: string } }) {
|
||||||
const user = await currentUser()
|
const user = await currentUser()
|
||||||
const sandboxId = params.id
|
const sandboxId = params.id
|
||||||
@ -86,6 +94,8 @@ export default async function CodePage({ params }: { params: { id: string } }) {
|
|||||||
return notFound()
|
return notFound()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const reactDefinitionFile = getReactDefinitionFile()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="overflow-hidden overscroll-none w-screen flex flex-col h-screen bg-background">
|
<div className="overflow-hidden overscroll-none w-screen flex flex-col h-screen bg-background">
|
||||||
<Room id={sandboxId}>
|
<Room id={sandboxId}>
|
||||||
@ -94,6 +104,7 @@ export default async function CodePage({ params }: { params: { id: string } }) {
|
|||||||
<CodeEditor
|
<CodeEditor
|
||||||
userData={userData}
|
userData={userData}
|
||||||
sandboxData={sandboxData}
|
sandboxData={sandboxData}
|
||||||
|
reactDefinitionFile={reactDefinitionFile}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</Room>
|
</Room>
|
||||||
|
@ -35,9 +35,11 @@ import { ImperativePanelHandle } from "react-resizable-panels"
|
|||||||
export default function CodeEditor({
|
export default function CodeEditor({
|
||||||
userData,
|
userData,
|
||||||
sandboxData,
|
sandboxData,
|
||||||
|
reactDefinitionFile,
|
||||||
}: {
|
}: {
|
||||||
userData: User
|
userData: User
|
||||||
sandboxData: Sandbox
|
sandboxData: Sandbox
|
||||||
|
reactDefinitionFile: string
|
||||||
}) {
|
}) {
|
||||||
const socketRef = useRef<Socket | null>(null);
|
const socketRef = useRef<Socket | null>(null);
|
||||||
|
|
||||||
@ -103,16 +105,6 @@ export default function CodeEditor({
|
|||||||
const [provider, setProvider] = useState<TypedLiveblocksProvider>()
|
const [provider, setProvider] = useState<TypedLiveblocksProvider>()
|
||||||
const userInfo = useSelf((me) => me.info)
|
const userInfo = useSelf((me) => me.info)
|
||||||
|
|
||||||
// Liveblocks providers map to prevent reinitializing providers
|
|
||||||
type ProviderData = {
|
|
||||||
provider: LiveblocksProvider<never, never, never, never>;
|
|
||||||
yDoc: Y.Doc;
|
|
||||||
yText: Y.Text;
|
|
||||||
binding?: MonacoBinding;
|
|
||||||
onSync: (isSynced: boolean) => void;
|
|
||||||
};
|
|
||||||
const providersMap = useRef(new Map<string, ProviderData>());
|
|
||||||
|
|
||||||
// Refs for libraries / features
|
// Refs for libraries / features
|
||||||
const editorContainerRef = useRef<HTMLDivElement>(null)
|
const editorContainerRef = useRef<HTMLDivElement>(null)
|
||||||
const monacoRef = useRef<typeof monaco | null>(null)
|
const monacoRef = useRef<typeof monaco | null>(null)
|
||||||
@ -340,15 +332,10 @@ export default function CodeEditor({
|
|||||||
|
|
||||||
if (!editorRef || !tab || !model) return
|
if (!editorRef || !tab || !model) return
|
||||||
|
|
||||||
let providerData: ProviderData;
|
const yDoc = new Y.Doc()
|
||||||
|
const yText = yDoc.getText(tab.id)
|
||||||
|
const yProvider: any = new LiveblocksProvider(room, yDoc)
|
||||||
|
|
||||||
// When a file is opened for the first time, create a new provider and store in providersMap.
|
|
||||||
if (!providersMap.current.has(tab.id)) {
|
|
||||||
const yDoc = new Y.Doc();
|
|
||||||
const yText = yDoc.getText(tab.id);
|
|
||||||
const yProvider = new LiveblocksProvider(room, yDoc);
|
|
||||||
|
|
||||||
// Inserts the file content into the editor once when the tab is changed.
|
|
||||||
const onSync = (isSynced: boolean) => {
|
const onSync = (isSynced: boolean) => {
|
||||||
if (isSynced) {
|
if (isSynced) {
|
||||||
const text = yText.toString()
|
const text = yText.toString()
|
||||||
@ -366,51 +353,22 @@ export default function CodeEditor({
|
|||||||
|
|
||||||
yProvider.on("sync", onSync)
|
yProvider.on("sync", onSync)
|
||||||
|
|
||||||
// Save the provider to the map.
|
setProvider(yProvider)
|
||||||
providerData = { provider: yProvider, yDoc, yText, onSync };
|
|
||||||
providersMap.current.set(tab.id, providerData);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// When a tab is opened that has been open before, reuse the existing provider.
|
|
||||||
providerData = providersMap.current.get(tab.id)!;
|
|
||||||
}
|
|
||||||
|
|
||||||
const binding = new MonacoBinding(
|
const binding = new MonacoBinding(
|
||||||
providerData.yText,
|
yText,
|
||||||
model,
|
model,
|
||||||
new Set([editorRef]),
|
new Set([editorRef]),
|
||||||
providerData.provider.awareness as unknown as Awareness
|
yProvider.awareness as Awareness
|
||||||
);
|
)
|
||||||
|
|
||||||
providerData.binding = binding;
|
|
||||||
|
|
||||||
setProvider(providerData.provider);
|
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
// Cleanup logic
|
yDoc.destroy()
|
||||||
if (binding) {
|
yProvider.destroy()
|
||||||
binding.destroy();
|
binding.destroy()
|
||||||
|
yProvider.off("sync", onSync)
|
||||||
}
|
}
|
||||||
if (providerData.binding) {
|
}, [editorRef, room, activeFileContent])
|
||||||
providerData.binding = undefined;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}, [room, activeFileContent]);
|
|
||||||
|
|
||||||
// Added this effect to clean up when the component unmounts
|
|
||||||
useEffect(() => {
|
|
||||||
return () => {
|
|
||||||
// Clean up all providers when the component unmounts
|
|
||||||
providersMap.current.forEach((data) => {
|
|
||||||
if (data.binding) {
|
|
||||||
data.binding.destroy();
|
|
||||||
}
|
|
||||||
data.provider.disconnect();
|
|
||||||
data.yDoc.destroy();
|
|
||||||
});
|
|
||||||
providersMap.current.clear();
|
|
||||||
};
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
// Connection/disconnection effect
|
// Connection/disconnection effect
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
Reference in New Issue
Block a user