Skip to content

Commit a5f48a8

Browse files
Publish extension methods to access MutableDocument and MutableComposer from Editor for convenience. (Resolves #2157) (#2161)
1 parent 2d943e4 commit a5f48a8

17 files changed

+105
-86
lines changed

super_editor/clones/quill/lib/deltas/deltas_display.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'package:dart_quill_delta/dart_quill_delta.dart';
2-
import 'package:feather/infrastructure/super_editor_extensions.dart';
32
import 'package:flutter/material.dart';
43
import 'package:material_symbols_icons/material_symbols_icons.dart';
54
import 'package:super_editor/super_editor.dart';

super_editor/clones/quill/lib/editor/toolbar.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import 'package:feather/infrastructure/popovers/color_selector.dart';
44
import 'package:feather/editor/editor.dart';
55
import 'package:feather/infrastructure/popovers/icon_selector.dart';
66
import 'package:feather/infrastructure/popovers/text_item_selector.dart';
7-
import 'package:feather/infrastructure/super_editor_extensions.dart';
87
import 'package:feather/theme.dart';
98
import 'package:flutter/material.dart';
109
import 'package:material_symbols_icons/material_symbols_icons.dart';

super_editor/clones/quill/lib/infrastructure/super_editor_extensions.dart

Lines changed: 0 additions & 7 deletions
This file was deleted.

super_editor/lib/src/core/editor.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,34 @@ class FunctionalEditListener implements EditListener {
988988
void onEdit(List<EditEvent> changeList) => _onEdit(changeList);
989989
}
990990

991+
/// Extensions that provide direct, type-safe access to [Editable]s that are
992+
/// expected to exist in all [Editor]s.
993+
///
994+
/// This extension is similar to [StandardEditablesInContext], except this extension
995+
/// operates on an [Editor] and the other operates on [EditContext]s. Both exist
996+
/// for convenience.
997+
extension StandardEditables on Editor {
998+
/// Finds and returns the [MutableDocument] within the [Editor].
999+
MutableDocument get document => context.find<MutableDocument>(Editor.documentKey);
1000+
1001+
/// Finds and returns the [MutableDocumentComposer] within the [Editor].
1002+
MutableDocumentComposer get composer => context.find<MutableDocumentComposer>(Editor.composerKey);
1003+
}
1004+
1005+
/// Extensions that provide direct, type-safe access to [Editable]s that are
1006+
/// expected to exist in all [EditContext]s.
1007+
///
1008+
/// This extension is similar to [StandardEditables], except this extension
1009+
/// operates on an [EditContext] and the other operates on [Editor]s. Both exist
1010+
/// for convenience.
1011+
extension StandardEditablesInContext on EditContext {
1012+
/// Finds and returns the [MutableDocument] within the [EditContext].
1013+
MutableDocument get document => find<MutableDocument>(Editor.documentKey);
1014+
1015+
/// Finds and returns the [MutableDocumentComposer] within the [EditContext].
1016+
MutableDocumentComposer get composer => find<MutableDocumentComposer>(Editor.composerKey);
1017+
}
1018+
9911019
/// An in-memory, mutable [Document].
9921020
class MutableDocument with Iterable<DocumentNode> implements Document, Editable {
9931021
/// Creates an in-memory, mutable version of a [Document].

super_editor/lib/src/default_editor/blockquote.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class ConvertBlockquoteToParagraphCommand extends EditCommand {
250250

251251
@override
252252
void execute(EditContext context, CommandExecutor executor) {
253-
final document = context.find<MutableDocument>(Editor.documentKey);
253+
final document = context.document;
254254
final node = document.getNodeById(nodeId);
255255
final blockquote = node as ParagraphNode;
256256
final newParagraphNode = ParagraphNode(
@@ -343,7 +343,7 @@ class SplitBlockquoteCommand extends EditCommand {
343343

344344
@override
345345
void execute(EditContext context, CommandExecutor executor) {
346-
final document = context.find<MutableDocument>(Editor.documentKey);
346+
final document = context.document;
347347
final node = document.getNodeById(nodeId);
348348
final blockquote = node as ParagraphNode;
349349
final text = blockquote.text;

super_editor/lib/src/default_editor/box_component.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ class DeleteUpstreamAtBeginningOfBlockNodeCommand extends EditCommand {
320320

321321
@override
322322
void execute(EditContext context, CommandExecutor executor) {
323-
final document = context.find<MutableDocument>(Editor.documentKey);
323+
final document = context.document;
324324
final composer = context.find<MutableDocumentComposer>(Editor.composerKey);
325325
final documentLayoutEditable = context.find<DocumentLayoutEditable>(Editor.layoutKey);
326326

super_editor/lib/src/default_editor/common_editor_operations.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,7 +2267,7 @@ class PasteEditorCommand extends EditCommand {
22672267

22682268
@override
22692269
void execute(EditContext context, CommandExecutor executor) {
2270-
final document = context.find<MutableDocument>(Editor.documentKey);
2270+
final document = context.document;
22712271
final composer = context.find<MutableDocumentComposer>(Editor.composerKey);
22722272
final currentNodeWithSelection = document.getNodeById(_pastePosition.nodeId);
22732273
if (currentNodeWithSelection is! TextNode) {
@@ -2447,7 +2447,7 @@ class DeleteUpstreamCharacterCommand extends EditCommand {
24472447

24482448
@override
24492449
void execute(EditContext context, CommandExecutor executor) {
2450-
final document = context.find<MutableDocument>(Editor.documentKey);
2450+
final document = context.document;
24512451
final composer = context.find<MutableDocumentComposer>(Editor.composerKey);
24522452
final selection = composer.selection;
24532453

@@ -2501,7 +2501,7 @@ class DeleteDownstreamCharacterCommand extends EditCommand {
25012501

25022502
@override
25032503
void execute(EditContext context, CommandExecutor executor) {
2504-
final document = context.find<MutableDocument>(Editor.documentKey);
2504+
final document = context.document;
25052505
final composer = context.find<MutableDocumentComposer>(Editor.composerKey);
25062506
final selection = composer.selection;
25072507

super_editor/lib/src/default_editor/composer/composer_reactions.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class UpdateComposerTextStylesReaction extends EditReaction {
7171
}
7272

7373
void _updateComposerStylesAtCaret(EditContext editContext) {
74-
final document = editContext.find<MutableDocument>(Editor.documentKey);
74+
final document = editContext.document;
7575
final composer = editContext.find<MutableDocumentComposer>(Editor.composerKey);
7676

7777
if (composer.selection?.extent == _previousSelectionExtent) {

super_editor/lib/src/default_editor/default_document_editor_reactions.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class OrderedListItemConversionReaction extends ParagraphPrefixConversionReactio
189189
// ordered list item. For example, the list has the items 1, 2, 3 and 4,
190190
// and the user types " 5. ".
191191

192-
final document = editContext.find<MutableDocument>(Editor.documentKey);
192+
final document = editContext.document;
193193

194194
final upstreamNode = document.getNodeBefore(paragraph);
195195
if (upstreamNode == null || upstreamNode is! ListItemNode || upstreamNode.type != ListItemType.ordered) {
@@ -298,7 +298,7 @@ class HorizontalRuleConversionReaction extends EditReaction {
298298
return;
299299
}
300300

301-
final document = editorContext.find<MutableDocument>(Editor.documentKey);
301+
final document = editorContext.document;
302302

303303
final didTypeSpace = EditInspector.didTypeSpace(document, changeList);
304304
if (!didTypeSpace) {
@@ -372,7 +372,7 @@ abstract class ParagraphPrefixConversionReaction extends EditReaction {
372372

373373
@override
374374
void react(EditContext editContext, RequestDispatcher requestDispatcher, List<EditEvent> changeList) {
375-
final document = editContext.find<MutableDocument>(Editor.documentKey);
375+
final document = editContext.document;
376376
final typedText = EditInspector.findLastTextUserTyped(document, changeList);
377377
if (typedText == null) {
378378
return;
@@ -435,7 +435,7 @@ class ImageUrlConversionReaction extends EditReaction {
435435
return;
436436
}
437437

438-
final document = editContext.find<MutableDocument>(Editor.documentKey);
438+
final document = editContext.document;
439439
final previousNode = document.getNodeById(selectionChange.oldSelection!.extent.nodeId);
440440
if (previousNode is! ParagraphNode) {
441441
// The intention indicated that the user pressed "enter" from a paragraph
@@ -563,7 +563,7 @@ class LinkifyReaction extends EditReaction {
563563

564564
@override
565565
void react(EditContext editContext, RequestDispatcher requestDispatcher, List<EditEvent> edits) {
566-
final document = editContext.find<MutableDocument>(Editor.documentKey);
566+
final document = editContext.document;
567567
final composer = editContext.find<MutableDocumentComposer>(Editor.composerKey);
568568
final selection = composer.selection;
569569

@@ -923,7 +923,7 @@ class DashConversionReaction extends EditReaction {
923923

924924
@override
925925
void react(EditContext editorContext, RequestDispatcher requestDispatcher, List<EditEvent> changeList) {
926-
final document = editorContext.find<MutableDocument>(Editor.documentKey);
926+
final document = editorContext.document;
927927
final composer = editorContext.find<MutableDocumentComposer>(Editor.composerKey);
928928

929929
if (changeList.length < 2) {

super_editor/lib/src/default_editor/list_items.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ class IndentListItemCommand extends EditCommand {
837837

838838
@override
839839
void execute(EditContext context, CommandExecutor executor) {
840-
final document = context.find<MutableDocument>(Editor.documentKey);
840+
final document = context.document;
841841
final node = document.getNodeById(nodeId);
842842
final listItem = node as ListItemNode;
843843
if (listItem.indent >= 6) {
@@ -875,7 +875,7 @@ class UnIndentListItemCommand extends EditCommand {
875875

876876
@override
877877
void execute(EditContext context, CommandExecutor executor) {
878-
final document = context.find<MutableDocument>(Editor.documentKey);
878+
final document = context.document;
879879
final node = document.getNodeById(nodeId);
880880
final listItem = node as ListItemNode;
881881
if (listItem.indent > 0) {
@@ -922,7 +922,7 @@ class ConvertListItemToParagraphCommand extends EditCommand {
922922

923923
@override
924924
void execute(EditContext context, CommandExecutor executor) {
925-
final document = context.find<MutableDocument>(Editor.documentKey);
925+
final document = context.document;
926926
final node = document.getNodeById(nodeId);
927927
final listItem = node as ListItemNode;
928928
final newMetadata = Map<String, dynamic>.from(paragraphMetadata ?? {});
@@ -969,7 +969,7 @@ class ConvertParagraphToListItemCommand extends EditCommand {
969969

970970
@override
971971
void execute(EditContext context, CommandExecutor executor) {
972-
final document = context.find<MutableDocument>(Editor.documentKey);
972+
final document = context.document;
973973
final node = document.getNodeById(nodeId);
974974
final paragraphNode = node as ParagraphNode;
975975

@@ -1012,7 +1012,7 @@ class ChangeListItemTypeCommand extends EditCommand {
10121012

10131013
@override
10141014
void execute(EditContext context, CommandExecutor executor) {
1015-
final document = context.find<MutableDocument>(Editor.documentKey);
1015+
final document = context.document;
10161016
final existingListItem = document.getNodeById(nodeId) as ListItemNode;
10171017

10181018
final newListItemNode = ListItemNode(
@@ -1058,7 +1058,7 @@ class SplitListItemCommand extends EditCommand {
10581058

10591059
@override
10601060
void execute(EditContext context, CommandExecutor executor) {
1061-
final document = context.find<MutableDocument>(Editor.documentKey);
1061+
final document = context.document;
10621062
final composer = context.find<MutableDocumentComposer>(Editor.composerKey);
10631063

10641064
final node = document.getNodeById(nodeId);

0 commit comments

Comments
 (0)