Spaces:
Running
Running
Update BlockEditor.tsx
Browse files
client/src/components/BlockEditor/BlockEditor.tsx
CHANGED
|
@@ -8,7 +8,7 @@ import { BlockDefinition, BlockShape, CATEGORY_COLORS, CATEGORY_ICONS } from '..
|
|
| 8 |
import { useProjectStore } from '../../store/projectStore';
|
| 9 |
import { useEditorStore } from '../../store/editorStore';
|
| 10 |
import { useCollaborationStore } from '../../store/collaborationStore';
|
| 11 |
-
import {
|
| 12 |
import { getColor } from '../Collaboration/CollaboratorAvatars';
|
| 13 |
import { Maximize2, Trash2, MousePointer2 } from 'lucide-react';
|
| 14 |
|
|
@@ -576,7 +576,71 @@ export default function BlockEditor() {
|
|
| 576 |
const setBlocksXml = useEditorStore((s) => s.setBlocksXml);
|
| 577 |
const setBlockCode = useEditorStore((s) => s.setBlockCode);
|
| 578 |
const saveGeneratedCodeToFile = useEditorStore((s) => s.saveGeneratedCodeToFile);
|
| 579 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 580 |
const remotePointers = useCollaborationStore((s) => s.remotePointers);
|
| 581 |
const remoteBlockDrags = useCollaborationStore((s) => s.remoteBlockDrags);
|
| 582 |
const pendingBlockConnects = useCollaborationStore((s) => s.pendingBlockConnects);
|
|
@@ -922,6 +986,7 @@ export default function BlockEditor() {
|
|
| 922 |
const dom = Blockly.Xml.workspaceToDom(ws);
|
| 923 |
const xml = Blockly.Xml.domToText(dom);
|
| 924 |
setBlocksXml(activeFileId, xml);
|
|
|
|
| 925 |
} catch (e) {}
|
| 926 |
}
|
| 927 |
// Cleanup pointer move listener
|
|
|
|
| 8 |
import { useProjectStore } from '../../store/projectStore';
|
| 9 |
import { useEditorStore } from '../../store/editorStore';
|
| 10 |
import { useCollaborationStore } from '../../store/collaborationStore';
|
| 11 |
+
import { getSocket } from '../../hooks/useWebSocket';
|
| 12 |
import { getColor } from '../Collaboration/CollaboratorAvatars';
|
| 13 |
import { Maximize2, Trash2, MousePointer2 } from 'lucide-react';
|
| 14 |
|
|
|
|
| 576 |
const setBlocksXml = useEditorStore((s) => s.setBlocksXml);
|
| 577 |
const setBlockCode = useEditorStore((s) => s.setBlockCode);
|
| 578 |
const saveGeneratedCodeToFile = useEditorStore((s) => s.saveGeneratedCodeToFile);
|
| 579 |
+
const projectId = currentProject?.id;
|
| 580 |
+
const lastPointerEmitRef = useRef(0);
|
| 581 |
+
const lastBlockDragEmitRef = useRef(0);
|
| 582 |
+
|
| 583 |
+
const emitBlockStateSync = useCallback((fileId: string, xml: string) => {
|
| 584 |
+
const s = getSocket();
|
| 585 |
+
if (s?.connected && projectId) {
|
| 586 |
+
s.emit('block_state_sync', { projectId, fileId, xml });
|
| 587 |
+
}
|
| 588 |
+
}, [projectId]);
|
| 589 |
+
|
| 590 |
+
const emitPointerMove = useCallback((x: number, y: number) => {
|
| 591 |
+
const now = Date.now();
|
| 592 |
+
if (now - lastPointerEmitRef.current < 100) return;
|
| 593 |
+
lastPointerEmitRef.current = now;
|
| 594 |
+
const s = getSocket();
|
| 595 |
+
if (s?.connected && projectId) {
|
| 596 |
+
s.emit('pointer_move', { projectId, x, y });
|
| 597 |
+
}
|
| 598 |
+
}, [projectId]);
|
| 599 |
+
|
| 600 |
+
const emitBlockDragStart = useCallback((blockId: string, x: number, y: number) => {
|
| 601 |
+
const s = getSocket();
|
| 602 |
+
if (s?.connected && projectId) {
|
| 603 |
+
s.emit('block_drag_start', { projectId, blockId, x, y });
|
| 604 |
+
}
|
| 605 |
+
}, [projectId]);
|
| 606 |
+
|
| 607 |
+
const emitBlockDragMove = useCallback((blockId: string, x: number, y: number) => {
|
| 608 |
+
const now = Date.now();
|
| 609 |
+
if (now - lastBlockDragEmitRef.current < 50) return;
|
| 610 |
+
lastBlockDragEmitRef.current = now;
|
| 611 |
+
const s = getSocket();
|
| 612 |
+
if (s?.connected && projectId) {
|
| 613 |
+
s.emit('block_drag_move', { projectId, blockId, x, y });
|
| 614 |
+
}
|
| 615 |
+
}, [projectId]);
|
| 616 |
+
|
| 617 |
+
const emitBlockDragEnd = useCallback((blockId: string) => {
|
| 618 |
+
const s = getSocket();
|
| 619 |
+
if (s?.connected && projectId) {
|
| 620 |
+
s.emit('block_drag_end', { projectId, blockId });
|
| 621 |
+
}
|
| 622 |
+
}, [projectId]);
|
| 623 |
+
|
| 624 |
+
const emitBlockConnect = useCallback((childId: string, parentId: string, inputName?: string) => {
|
| 625 |
+
const s = getSocket();
|
| 626 |
+
if (s?.connected && projectId) {
|
| 627 |
+
s.emit('block_connect', { projectId, childId, parentId, inputName });
|
| 628 |
+
}
|
| 629 |
+
}, [projectId]);
|
| 630 |
+
|
| 631 |
+
const emitBlockChange = useCallback((blockId: string, name: string, value: any, element: string) => {
|
| 632 |
+
const s = getSocket();
|
| 633 |
+
if (s?.connected && projectId) {
|
| 634 |
+
s.emit('block_change', { projectId, blockId, name, value, element });
|
| 635 |
+
}
|
| 636 |
+
}, [projectId]);
|
| 637 |
+
|
| 638 |
+
const emitBlockCreateEvent = useCallback((blockId: string, fileId: string, xml: string) => {
|
| 639 |
+
const s = getSocket();
|
| 640 |
+
if (s?.connected && projectId) {
|
| 641 |
+
s.emit('block_create_event', { projectId, fileId, blockId, xml });
|
| 642 |
+
}
|
| 643 |
+
}, [projectId]);
|
| 644 |
const remotePointers = useCollaborationStore((s) => s.remotePointers);
|
| 645 |
const remoteBlockDrags = useCollaborationStore((s) => s.remoteBlockDrags);
|
| 646 |
const pendingBlockConnects = useCollaborationStore((s) => s.pendingBlockConnects);
|
|
|
|
| 986 |
const dom = Blockly.Xml.workspaceToDom(ws);
|
| 987 |
const xml = Blockly.Xml.domToText(dom);
|
| 988 |
setBlocksXml(activeFileId, xml);
|
| 989 |
+
emitBlockStateSync(activeFileId, xml);
|
| 990 |
} catch (e) {}
|
| 991 |
}
|
| 992 |
// Cleanup pointer move listener
|