mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: support import document from v0.1.x (#2601)
This commit is contained in:
@ -3,15 +3,54 @@ import 'dart:convert';
|
||||
import 'package:appflowy_backend/log.dart';
|
||||
import 'package:appflowy_backend/protobuf/flowy-document2/protobuf.dart';
|
||||
import 'package:appflowy_editor/appflowy_editor.dart'
|
||||
show Document, Node, Attributes, Delta, ParagraphBlockKeys;
|
||||
show Document, Node, Attributes, Delta, ParagraphBlockKeys, NodeIterator;
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:nanoid/nanoid.dart';
|
||||
|
||||
extension AppFlowyEditor on DocumentDataPB {
|
||||
DocumentDataPB? fromDocument(Document document) {
|
||||
final blocks = <String, BlockPB>{};
|
||||
extension DocumentDataPBFromTo on DocumentDataPB {
|
||||
static DocumentDataPB? fromDocument(Document document) {
|
||||
final startNode = document.first;
|
||||
final endNode = document.last;
|
||||
if (startNode == null || endNode == null) {
|
||||
return null;
|
||||
}
|
||||
final pageId = document.root.id;
|
||||
|
||||
// generate the block
|
||||
final blocks = <String, BlockPB>{};
|
||||
final nodes = NodeIterator(
|
||||
document: document,
|
||||
startNode: startNode,
|
||||
endNode: endNode,
|
||||
).toList();
|
||||
for (final node in nodes) {
|
||||
if (blocks.containsKey(node.id)) {
|
||||
assert(false, 'duplicate node id: ${node.id}');
|
||||
}
|
||||
final parentId = node.parent?.id;
|
||||
final childrenId = nanoid(10);
|
||||
blocks[node.id] = node.toBlock(
|
||||
parentId: parentId,
|
||||
childrenId: childrenId,
|
||||
);
|
||||
}
|
||||
// root
|
||||
blocks[pageId] = document.root.toBlock(
|
||||
parentId: '',
|
||||
childrenId: pageId,
|
||||
);
|
||||
|
||||
// generate the meta
|
||||
final childrenMap = <String, ChildrenPB>{};
|
||||
blocks.forEach((key, value) {
|
||||
final parentId = value.parentId;
|
||||
if (parentId.isNotEmpty) {
|
||||
childrenMap[parentId] ??= ChildrenPB.create();
|
||||
childrenMap[parentId]!.children.add(value.id);
|
||||
}
|
||||
});
|
||||
final meta = MetaPB(childrenMap: childrenMap);
|
||||
|
||||
return DocumentDataPB(
|
||||
blocks: blocks,
|
||||
pageId: pageId,
|
||||
@ -91,6 +130,7 @@ extension BlockToNode on BlockPB {
|
||||
|
||||
extension NodeToBlock on Node {
|
||||
BlockPB toBlock({
|
||||
String? parentId,
|
||||
String? childrenId,
|
||||
}) {
|
||||
assert(id.isNotEmpty);
|
||||
@ -101,6 +141,9 @@ extension NodeToBlock on Node {
|
||||
if (childrenId != null && childrenId.isNotEmpty) {
|
||||
block.childrenId = childrenId;
|
||||
}
|
||||
if (parentId != null && parentId.isNotEmpty) {
|
||||
block.parentId = parentId;
|
||||
}
|
||||
return block;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user