Skip to content

Commit

Permalink
Adding the concept of hidden document components
Browse files Browse the repository at this point in the history
  • Loading branch information
jmatth committed Sep 14, 2023
1 parent b6949bb commit c1e8c9e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
29 changes: 28 additions & 1 deletion super_editor/lib/src/core/document_layout.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:flutter/widgets.dart';
import 'package:super_editor/src/core/editor.dart';

import 'document_selection.dart';
import 'document.dart';
import 'document_selection.dart';

/// Obtains a [DocumentLayout].
///
Expand Down Expand Up @@ -30,6 +30,26 @@ class DocumentLayoutEditable implements Editable {
void onTransactionStart() {}
}

class HiddenComponent extends StatelessWidget {
const HiddenComponent({super.key, required this.child, this.hiding = true});

final Widget child;
final bool hiding;

@override
Widget build(BuildContext context) {
return Visibility(
visible: !hiding,
maintainAnimation: false,
maintainInteractivity: false,
maintainSemantics: false,
maintainSize: false,
maintainState: true,
child: child,
);
}
}

/// Abstract representation of a document layout.
///
/// Regardless of how a document is displayed, a [DocumentLayout] needs
Expand Down Expand Up @@ -270,6 +290,10 @@ mixin DocumentComponent<T extends StatefulWidget> on State<T> {
/// document layout.
bool isVisualSelectionSupported() => true;

/// Returns `true` if the component is visually hidden and should be ignored
/// for the purposes of hit testing, cursor movement, etc.
bool get isHidden => context.findAncestorWidgetOfExactType<HiddenComponent>()?.hiding ?? false;

/// Returns the desired [MouseCursor] at the given (x,y) [localOffset], or
/// [null] if this component has no preference for the cursor style.
MouseCursor? getDesiredCursorAtOffset(Offset localOffset);
Expand Down Expand Up @@ -409,6 +433,9 @@ mixin ProxyDocumentComponent<T extends StatefulWidget> implements DocumentCompon
MouseCursor? getDesiredCursorAtOffset(Offset localOffset) {
return _childDocumentComponent.getDesiredCursorAtOffset(_getChildOffset(localOffset));
}

@override
bool get isHidden => _childDocumentComponent.isHidden;
}

/// Preferences for how the document selection should change, e.g.,
Expand Down
21 changes: 2 additions & 19 deletions super_editor/lib/src/default_editor/common_editor_operations.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import 'dart:math';
import 'dart:ui';

import 'package:attributed_text/attributed_text.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:super_editor/src/core/document.dart';
import 'package:super_editor/src/core/document_composer.dart';
import 'package:super_editor/src/core/document_layout.dart';
import 'package:super_editor/src/core/document_selection.dart';
import 'package:super_editor/src/core/editor.dart';
import 'package:super_editor/src/default_editor/list_items.dart';
import 'package:super_editor/src/default_editor/paragraph.dart';
import 'package:super_editor/src/default_editor/selection_upstream_downstream.dart';
import 'package:super_editor/src/default_editor/text.dart';
import 'package:super_editor/src/infrastructure/_logging.dart';
import 'package:super_editor/super_editor.dart';

import 'attributions.dart';
import 'horizontal_rule.dart';
import 'image.dart';
import 'multi_node_editing.dart';
import 'text_tools.dart';

/// Performs common, high-level editing and composition tasks
/// with a simplified API.
///
Expand Down Expand Up @@ -690,7 +673,7 @@ class CommonEditorOperations {

if (selectableNode != null) {
final nextComponent = documentLayoutResolver().getComponentByNodeId(selectableNode.id);
if (nextComponent != null) {
if (nextComponent != null && !nextComponent.isHidden) {
foundSelectableNode = nextComponent.isVisualSelectionSupported();
}
prevNode = selectableNode;
Expand All @@ -711,7 +694,7 @@ class CommonEditorOperations {

if (selectableNode != null) {
final nextComponent = documentLayoutResolver().getComponentByNodeId(selectableNode.id);
if (nextComponent != null) {
if (nextComponent != null && !nextComponent.isHidden) {
foundSelectableNode = nextComponent.isVisualSelectionSupported();
}
prevNode = selectableNode;
Expand Down

0 comments on commit c1e8c9e

Please sign in to comment.