Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flowy_infra_ui/style_widget/snap_bar.dart';

bool documentContainsSubPageReference(Node node, String viewId) {
if (node.attributes[SubPageBlockKeys.viewId] == viewId) {
return true;
}
return node.children.any(
(child) => documentContainsSubPageReference(child, viewId),
);
}

class SubPageTransactionHandler extends BlockTransactionHandler {
SubPageTransactionHandler() : super(type: SubPageBlockKeys.type);

Expand All @@ -38,7 +47,12 @@ class SubPageTransactionHandler extends BlockTransactionHandler {

for (final node in removed) {
if (!context.mounted) return;
await _subPageDeleted(context, node);
await _subPageDeleted(
context,
editorState,
node,
parentViewId: viewId,
);
}

for (final node in added) {
Expand All @@ -56,18 +70,35 @@ class SubPageTransactionHandler extends BlockTransactionHandler {

Future<void> _subPageDeleted(
BuildContext context,
Node node,
) async {
EditorState editorState,
Node node, {
required String parentViewId,
}) async {
if (node.type != type) {
return;
}

final view = node.attributes[SubPageBlockKeys.viewId];
if (view == null) {
final viewId = node.attributes[SubPageBlockKeys.viewId] as String?;
if (viewId == null) {
return;
}

// A moved block is represented by deleting the old node and inserting a
// copy. Do not delete the page while another block still references it.
if (documentContainsSubPageReference(editorState.document.root, viewId)) {
return;
}

// Moving a page from the sidebar updates its parent before the old block
// is removed from the document. That removal must not move the page to
// trash.
final viewOrResult = await ViewBackendService.getView(viewId);
final view = viewOrResult.toNullable();
if (view == null || view.parentViewId != parentViewId) {
return;
}

final result = await ViewBackendService.deleteView(viewId: view);
final result = await ViewBackendService.deleteView(viewId: viewId);
result.fold(
(_) {},
(error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:appflowy/plugins/document/presentation/editor_plugins/sub_page/sub_page_block_component.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/sub_page/sub_page_transaction_handler.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
test('finds sub-page references in nested nodes', () {
final root = Node(
type: 'page',
children: [
Node(
type: 'column',
children: [
Node(
type: SubPageBlockKeys.type,
attributes: {SubPageBlockKeys.viewId: 'page-id'},
),
],
),
],
);

expect(documentContainsSubPageReference(root, 'page-id'), isTrue);
expect(documentContainsSubPageReference(root, 'other-page'), isFalse);
});
}
Loading