Skip to content

Commit

Permalink
Parameterizing document layout
Browse files Browse the repository at this point in the history
  • Loading branch information
jmatth committed Jan 11, 2024
1 parent b0d68b6 commit 50598eb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
5 changes: 5 additions & 0 deletions super_editor/lib/src/default_editor/super_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ class SuperEditor extends StatefulWidget {
this.plugins = const {},
this.scrollOff = AxisOffset.zero,
this.debugPaint = const DebugPaintConfig(),
this.documentLayoutBuilder,
}) : stylesheet = stylesheet ?? defaultStylesheet,
selectionStyles = selectionStyle ?? defaultSelectionStyle,
componentBuilders = componentBuilders != null
Expand Down Expand Up @@ -329,6 +330,9 @@ class SuperEditor extends StatefulWidget {
/// debugging.
final DebugPaintConfig debugPaint;

/// The [DocumentLayoutBuilder] to pass to [DocumentScaffold].
final DocumentLayoutBuilder? documentLayoutBuilder;

@override
SuperEditorState createState() => SuperEditorState();
}
Expand Down Expand Up @@ -617,6 +621,7 @@ class SuperEditorState extends State<SuperEditor> {
autoScrollController: _autoScrollController,
scroller: _scroller,
presenter: presenter,
documentLayoutBuilder: widget.documentLayoutBuilder,
componentBuilders: widget.componentBuilders,
underlays: [
// Add all underlays that the app wants.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@ import 'package:super_editor/src/infrastructure/content_layers.dart';
import 'package:super_editor/src/infrastructure/documents/document_scroller.dart';
import 'package:super_editor/src/infrastructure/viewport_size_reporting.dart';

typedef DocumentLayoutBuilder = Widget Function({
required SingleColumnLayoutPresenter presenter,
required List<ComponentBuilder> componentBuilders,
VoidCallback? onBuildScheduled,
required Key documentLayoutKey,
required DebugPaintConfig debugPaint,
});

Widget _defaultDocumentLayoutBuilder({
required SingleColumnLayoutPresenter presenter,
required List<ComponentBuilder> componentBuilders,
VoidCallback? onBuildScheduled,
required Key documentLayoutKey,
required DebugPaintConfig debugPaint,
}) {
return SingleColumnDocumentLayout(
key: documentLayoutKey,
presenter: presenter,
componentBuilders: componentBuilders,
onBuildScheduled: onBuildScheduled,
showDebugPaint: debugPaint.layout,
);
}

/// A scaffold that combines pieces to create a scrolling single-column document, with
/// gestures placed beneath the document.
///
Expand All @@ -26,8 +50,12 @@ class DocumentScaffold<ContextType> extends StatefulWidget {
this.underlays = const [],
this.overlays = const [],
this.debugPaint = const DebugPaintConfig(),
this.documentLayoutBuilder,
});

/// Function that is used to construct the child widget tree representing the laid out document.
final DocumentLayoutBuilder? documentLayoutBuilder;

/// [LayerLink] that's is attached to the document layout.
final LayerLink documentLayoutLink;

Expand Down Expand Up @@ -140,12 +168,12 @@ class _DocumentScaffoldState extends State<DocumentScaffold> {
child: CompositedTransformTarget(
link: widget.documentLayoutLink,
child: ContentLayers(
content: (onBuildScheduled) => SingleColumnDocumentLayout(
key: widget.documentLayoutKey,
content: (onBuildScheduled) => (widget.documentLayoutBuilder ?? _defaultDocumentLayoutBuilder).call(
documentLayoutKey: widget.documentLayoutKey,
presenter: widget.presenter,
componentBuilders: widget.componentBuilders,
onBuildScheduled: onBuildScheduled,
showDebugPaint: widget.debugPaint.layout,
debugPaint: widget.debugPaint,
),
underlays: widget.underlays,
overlays: widget.overlays,
Expand Down

0 comments on commit 50598eb

Please sign in to comment.