Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 10 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 28 additions & 7 deletions src/components/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const Editor = ({ socketRef, roomId, fileId, fileName, onCodeChange, userName, c
const bookmarksRef = useRef(new Map());
const timeoutsRef = useRef(new Map());
const [peers, setPeers] = useState([]);
const [charCount, setCharCount] = useState(0);

// Enforce readOnly dynamically
useEffect(() => {
Expand Down Expand Up @@ -104,9 +105,10 @@ const Editor = ({ socketRef, roomId, fileId, fileName, onCodeChange, userName, c
},
});
editorRef.current = editor;

editor.on('change', (instance) => {
if (onCodeChange) onCodeChange(fileId, instance.getValue());
const value = instance.getValue();
setCharCount(value.length);
if (onCodeChange) onCodeChange(fileId, value);
Comment thread
vedikapatil2410 marked this conversation as resolved.
});

if (onEditorReady) onEditorReady(fileId, editor);
Expand All @@ -127,11 +129,17 @@ const Editor = ({ socketRef, roomId, fileId, fileName, onCodeChange, userName, c

// Inject initialContent once provider syncs, but only if the doc is still empty
const handleSync = (isSynced) => {
if (isSynced && initialContent && ytext.length === 0) {
ydoc.transact(() => {
ytext.insert(0, initialContent);
});
}
if (isSynced) {
setCharCount(ytext.length);

if (initialContent && ytext.length === 0) {
ydoc.transact(() => {
ytext.insert(0, initialContent);
});

setCharCount(initialContent.length);
}
}
};
provider.on('sync', handleSync);

Expand Down Expand Up @@ -238,7 +246,20 @@ const Editor = ({ socketRef, roomId, fileId, fileName, onCodeChange, userName, c
</div>
</div>
) : (
<>
<textarea ref={textareaRef} id="realtimeEditor"></textarea>
<div style={{
textAlign: 'right',
padding: '4px 12px',
fontSize: '12px',
color: '#888',
backgroundColor: 'var(--bg-editor, #282a36)',
borderTop: '1px solid #444',
fontFamily: 'monospace'
}}>
Characters: {charCount}
</div>
</>
)}
</div>
);
Expand Down