forked from AppFlowy-IO/appflowy-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add diff document/nodes function (AppFlowy-IO#748)
* feat: transform operations * feat: add document diff function
- Loading branch information
Showing
9 changed files
with
330 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import 'package:appflowy_editor/appflowy_editor.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
// Not completed yet | ||
class CollabEditor extends StatefulWidget { | ||
const CollabEditor({super.key}); | ||
|
||
@override | ||
State<CollabEditor> createState() => _CollabEditorState(); | ||
} | ||
|
||
class _CollabEditorState extends State<CollabEditor> { | ||
final EditorState editorStateA = | ||
EditorState(document: Document.blank(withInitialText: true)); | ||
final EditorState editorStateB = | ||
EditorState(document: Document.blank(withInitialText: true)); | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
|
||
editorStateA.transactionStream.listen((event) { | ||
WidgetsBinding.instance.addPostFrameCallback((timeStamp) { | ||
if (event.$1 == TransactionTime.before) { | ||
editorStateB.apply(event.$2, isRemote: true); | ||
} | ||
}); | ||
}); | ||
|
||
editorStateB.transactionStream.listen((event) { | ||
WidgetsBinding.instance.addPostFrameCallback((timeStamp) { | ||
if (event.$1 == TransactionTime.before) { | ||
editorStateA.apply(event.$2, isRemote: true); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
editorStateA.dispose(); | ||
editorStateB.dispose(); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
body: Row( | ||
children: [ | ||
Expanded( | ||
child: AppFlowyEditor( | ||
editorState: editorStateA, | ||
), | ||
), | ||
const VerticalDivider(), | ||
Expanded( | ||
child: AppFlowyEditor( | ||
editorState: editorStateB, | ||
), | ||
), | ||
], | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import 'package:appflowy_editor/appflowy_editor.dart'; | ||
import 'package:collection/collection.dart'; | ||
|
||
const _equality = DeepCollectionEquality(); | ||
|
||
List<Operation> diffDocuments(Document oldDocument, Document newDocument) { | ||
return diffNodes(oldDocument.root, newDocument.root); | ||
} | ||
|
||
List<Operation> diffNodes(Node oldNode, Node newNode) { | ||
List<Operation> operations = []; | ||
|
||
if (!_equality.equals(oldNode.attributes, newNode.attributes)) { | ||
operations.add( | ||
UpdateOperation(oldNode.path, newNode.attributes, oldNode.attributes), | ||
); | ||
} | ||
|
||
final oldChildrenById = { | ||
for (final child in oldNode.children) child.id: child, | ||
}; | ||
final newChildrenById = { | ||
for (final child in newNode.children) child.id: child, | ||
}; | ||
|
||
// Identify insertions and updates | ||
for (final newChild in newNode.children) { | ||
final oldChild = oldChildrenById[newChild.id]; | ||
if (oldChild == null) { | ||
// Insert operation | ||
operations.add(InsertOperation(newChild.path, [newChild])); | ||
} else { | ||
// Recursive diff for updates | ||
operations.addAll(diffNodes(oldChild, newChild)); | ||
} | ||
} | ||
|
||
// Identify deletions | ||
oldChildrenById.keys | ||
.where((id) => !newChildrenById.containsKey(id)) | ||
.forEach((id) { | ||
final oldChild = oldChildrenById[id]!; | ||
operations.add(DeleteOperation(oldChild.path, [oldChild])); | ||
}); | ||
|
||
return operations; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.