Skip to content

Commit 21813fc

Browse files
Fix null errors on unmodifiable maps
1 parent d36a685 commit 21813fc

File tree

3 files changed

+36
-50
lines changed

3 files changed

+36
-50
lines changed

src/frontend/lib/providers/note_editor_provider.dart

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -369,14 +369,14 @@ class NoteEditorProvider with ChangeNotifier implements NoteEditorViewModel {
369369
// Check if this is a node we were trying to create
370370
if (_documentBuilder.uncommittedNodes.containsKey(nodeId)) {
371371
_logger.info('Node $nodeId was deleted before it was committed, removing from uncommitted nodes');
372-
_documentBuilder.uncommittedNodes.remove(nodeId);
372+
_documentBuilder.removeUncommittedNode(nodeId);
373373
return;
374374
}
375375

376376
_logger.info('Node $nodeId was deleted, will delete block $blockId on server');
377377

378378
// Remove from our mappings
379-
_documentBuilder.nodeToBlockMap.remove(nodeId);
379+
_documentBuilder.removeNodeMapping(nodeId);
380380

381381
// Remove from blocks list if it exists
382382
_blocks.remove(blockId);
@@ -437,13 +437,13 @@ class NoteEditorProvider with ChangeNotifier implements NoteEditorViewModel {
437437
// Create block through BlockService
438438
final block = await _blockService.createBlock(
439439
noteId!,
440-
extractedData, // Pass the complete object with content and metadata
440+
extractedData,
441441
blockType,
442442
order
443443
);
444444

445445
// Update our mappings
446-
_documentBuilder.nodeToBlockMap[nodeId] = block.id;
446+
_documentBuilder.linkNodeToBlock(nodeId, block.id);
447447
_blocks[block.id] = block;
448448

449449
// Add to note blocks map
@@ -453,7 +453,7 @@ class NoteEditorProvider with ChangeNotifier implements NoteEditorViewModel {
453453
}
454454

455455
// Remove from uncommitted nodes
456-
_documentBuilder.uncommittedNodes.remove(nodeId);
456+
_documentBuilder.removeUncommittedNode(nodeId);
457457

458458
// Subscribe to this block
459459
_webSocketService.subscribe('block', id: block.id);
@@ -465,7 +465,7 @@ class NoteEditorProvider with ChangeNotifier implements NoteEditorViewModel {
465465
} catch (e) {
466466
_logger.error('Failed to create block for node $nodeId: $e');
467467
// Remove from uncommitted nodes to prevent infinite retry loops
468-
_documentBuilder.uncommittedNodes.remove(nodeId);
468+
_documentBuilder.removeUncommittedNode(nodeId);
469469
}
470470
}
471471

@@ -653,30 +653,6 @@ class NoteEditorProvider with ChangeNotifier implements NoteEditorViewModel {
653653
(_activeNoteIds.contains(eventNoteId ?? ''));
654654

655655
if (shouldUpdate) {
656-
try {
657-
// Try to construct block from event payload
658-
final existingBlock = _blocks[blockId];
659-
if (existingBlock != null) {
660-
final updatedBlock = Block(
661-
id: blockId,
662-
noteId: existingBlock.noteId,
663-
type: existingBlock.type,
664-
order: existingBlock.order,
665-
content: parsedMessage.payload['content'],
666-
metadata: parsedMessage.payload['metadata'],
667-
updatedAt: DateTime.now(),
668-
createdAt: existingBlock.createdAt
669-
);
670-
671-
_blocks[blockId] = updatedBlock;
672-
_updateDocumentWithBlock(updatedBlock);
673-
_enqueueNotification();
674-
return;
675-
}
676-
} catch (e) {
677-
_logger.error('Error creating block from payload: $e');
678-
// Fall back to fetching the block
679-
}
680656
fetchBlockById(blockId).then((block) {
681657
if (block != null) {
682658
_updateDocumentWithBlock(block);
@@ -799,8 +775,7 @@ class NoteEditorProvider with ChangeNotifier implements NoteEditorViewModel {
799775
if (!_noteBlocksMap[block.noteId]!.contains(block.id)) {
800776
_noteBlocksMap[block.noteId]!.add(block.id);
801777
}
802-
803-
// Let _updateDocumentWithBlock handle the document update logic
778+
804779
_updateDocumentWithBlock(block);
805780
}
806781

@@ -944,11 +919,16 @@ class NoteEditorProvider with ChangeNotifier implements NoteEditorViewModel {
944919
} else if (type == 'task') {
945920
metadata['is_completed'] = false;
946921
}
922+
923+
final payload = {
924+
"metadata": metadata,
925+
"content": content,
926+
};
947927

948928
// Create block on server
949929
final block = await _blockService.createBlock(
950930
_noteId!,
951-
content,
931+
payload,
952932
type,
953933
order
954934
);
@@ -994,7 +974,7 @@ class NoteEditorProvider with ChangeNotifier implements NoteEditorViewModel {
994974
if (node != null) {
995975
_documentBuilder.document.deleteNode(nodeId);
996976
}
997-
_documentBuilder.nodeToBlockMap.remove(nodeId);
977+
_documentBuilder.removeNodeMapping(nodeId);
998978
}
999979
} finally {
1000980
_updatingDocument = false;
@@ -1588,15 +1568,10 @@ class NoteEditorProvider with ChangeNotifier implements NoteEditorViewModel {
15881568
await _createBlockForNode(nodeId, node);
15891569
} else {
15901570
// Node no longer exists, remove from tracking
1591-
_documentBuilder.uncommittedNodes.remove(nodeId);
1571+
_documentBuilder.removeUncommittedNode(nodeId);
15921572
}
15931573
}
15941574
}
1595-
1596-
@override
1597-
void markBlockAsModified(String blockId) {
1598-
_documentBuilder.markBlockAsModified(blockId);
1599-
}
16001575

16011576
@override
16021577
Future<void> fetchBlockFromEvent(String blockId) async {

src/frontend/lib/utils/document_builder.dart

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class DocumentBuilder {
135135
}
136136

137137
// Convert blocks to document nodes and populate the document
138-
void populateDocumentFromBlocks(List<Block> blocks, {bool markAsModified = true}) {
138+
void populateDocumentFromBlocks(List<Block> blocks) {
139139
if (_updatingDocument) {
140140
_logger.debug('Already updating document, skipping');
141141
return;
@@ -220,13 +220,6 @@ class DocumentBuilder {
220220

221221
// Register this block as from server
222222
registerServerBlock(block, node.id);
223-
224-
// Only mark blocks as modified if explicitly requested
225-
// This allows us to differentiate between initial load and user edits
226-
if (markAsModified) {
227-
_blockNodeMapping.markBlockAsModified(block.id);
228-
}
229-
230223
} catch (e) {
231224
_logger.error('Error adding node to document: $e');
232225
// Continue with next node
@@ -838,7 +831,6 @@ class DocumentBuilder {
838831
document.replaceNodeById(nodeId, newNodes.first);
839832

840833
// Update mapping
841-
_blockNodeMapping.removeNodeMapping(nodeId);
842834
_blockNodeMapping.linkNodeToBlock(newNodes.first.id, block.id);
843835

844836
_logger.debug('Node updated for block ${block.id}');
@@ -1170,6 +1162,26 @@ class DocumentBuilder {
11701162
return null;
11711163
}
11721164
}
1165+
1166+
void linkNodeToBlock(String nodeId, String blockId) {
1167+
// Remove mapping for this node
1168+
_blockNodeMapping.linkNodeToBlock(nodeId, blockId);
1169+
}
1170+
1171+
void removeNodeMapping(String nodeId) {
1172+
// Remove mapping for this node
1173+
_blockNodeMapping.removeNodeMapping(nodeId);
1174+
}
1175+
1176+
void removeBlockMapping(String blockId) {
1177+
// Remove mapping for this node
1178+
_blockNodeMapping.removeBlockMapping(blockId);
1179+
}
1180+
1181+
void removeUncommittedNode(String nodeId) {
1182+
// Remove uncommitted nodes from the mapping
1183+
_blockNodeMapping.removeUncommittedNode(nodeId);
1184+
}
11731185

11741186
// Create Super Editor with configured components and keyboard handlers
11751187
Widget createSuperEditor({

src/frontend/lib/viewmodel/note_editor_viewmodel.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ abstract class NoteEditorViewModel extends BaseViewModel {
7272
// Server sync and events
7373
void updateBlockCache(List<Block> blocks);
7474
void commitAllContent();
75-
void markBlockAsModified(String blockId);
7675
Future<void> fetchBlockFromEvent(String blockId);
7776

7877
// Focus handling

0 commit comments

Comments
 (0)