Skip to content

Commit

Permalink
fix: multi-typers
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrltrent committed Jul 14, 2024
1 parent 3bddd02 commit f3ae5a3
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/widgets/file_edit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class _FileEditState extends State<FileEdit> {
late CodeController _controller;
String _lastSyncedContent = '';
bool _isUpdating = false;
TextSelection? _lastSelection;

@override
void initState() {
Expand Down Expand Up @@ -54,6 +55,7 @@ class _FileEditState extends State<FileEdit> {
void _onLocalChange() {
if (!_isUpdating) {
setState(() {});
_syncWithFirebase();
}
}

Expand All @@ -70,17 +72,24 @@ class _FileEditState extends State<FileEdit> {
void _mergeRemoteChanges(String remoteContent) {
_isUpdating = true;

// Here, a simple replacement is used. A more sophisticated OT or CRDT approach can be applied.
// Get current cursor position
final cursorPosition = _controller.selection.baseOffset;

// Merge remote changes with the current content
_controller.text = remoteContent;

// Restore cursor position if possible
if (cursorPosition >= 0 && cursorPosition <= _controller.text.length) {
_controller.selection = TextSelection.collapsed(offset: cursorPosition);
}

_isUpdating = false;
}

void _onKeyEvent() {
void _syncWithFirebase() {
final firebaseState = Provider.of<FirebaseState>(context, listen: false);
firebaseState.updateContent(widget.doc.id, _controller.text);
_lastSyncedContent = _controller.text;
print(_controller.text);
}

Mode _getLanguageByExtension(String extension) {
Expand Down Expand Up @@ -220,7 +229,7 @@ class _FileEditState extends State<FileEdit> {
child: KeyboardListener(
focusNode: FocusNode(),
onKeyEvent: (event) {
_onKeyEvent();
_onLocalChange();
},
child: CodeField(
lineNumbers: true,
Expand Down

0 comments on commit f3ae5a3

Please sign in to comment.