Skip to content

Commit

Permalink
Spelling error decorations and suggestions (Partial for #388) (#2324)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-carroll authored and web-flow committed Oct 7, 2024
1 parent 5e0e046 commit 84aacd2
Show file tree
Hide file tree
Showing 78 changed files with 2,149 additions and 301 deletions.
4 changes: 4 additions & 0 deletions super_editor/lib/src/core/edit_context.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/widgets.dart';
import 'package:super_editor/src/default_editor/common_editor_operations.dart';
import 'package:super_editor/src/infrastructure/documents/document_scroller.dart';

Expand All @@ -19,6 +20,7 @@ class SuperEditorContext {
/// The [documentLayout] is passed as a [getDocumentLayout] callback that
/// should return the current layout as it might change.
SuperEditorContext({
required this.editorFocusNode,
required this.editor,
required this.document,
required DocumentLayout Function() getDocumentLayout,
Expand All @@ -27,6 +29,8 @@ class SuperEditorContext {
required this.commonOps,
}) : _getDocumentLayout = getDocumentLayout;

final FocusNode editorFocusNode;

/// The editor of the [Document] that allows executing commands that alter the
/// structure of the document.
final Editor editor;
Expand Down
3 changes: 3 additions & 0 deletions super_editor/lib/src/core/styles.dart
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ class Styles {
/// Applies an [UnderlineStyle] to all spelling errors in a text node.
static const String spellingErrorUnderlineStyle = 'spellingErrorUnderlineStyle';

/// Applies an [UnderlineStyle] to all grammar errors in a text node.
static const String grammarErrorUnderlineStyle = 'grammarErrorUnderlineStyle';

/// Applies a [AttributionStyleAdjuster] to a text node.
static const String inlineTextStyler = 'inlineTextStyler';

Expand Down
3 changes: 3 additions & 0 deletions super_editor/lib/src/default_editor/attributions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ const codeAttribution = NamedAttribution('code');
/// Spelling error attribution.
const spellingErrorAttribution = NamedAttribution('spelling-error');

/// Grammar error attribution.
const grammarErrorAttribution = NamedAttribution('grammar-error');

/// An attribution for superscript and subscript text.
class ScriptAttribution implements Attribution {
static const typeSuper = "superscript";
Expand Down
15 changes: 11 additions & 4 deletions super_editor/lib/src/default_editor/blockquote.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ class BlockquoteComponentBuilder implements ComponentBuilder {
textDirection: textDirection,
textAlignment: textAlign,
selectionColor: const Color(0x00000000),
spellingErrors: node.text
.getAttributionSpansByFilter((a) => a == spellingErrorAttribution)
.map((a) => TextRange(start: a.start, end: a.end + 1)) // +1 because text range end is exclusive
.toList(),
);
}

Expand Down Expand Up @@ -112,12 +108,17 @@ class BlockquoteComponentViewModel extends SingleColumnLayoutComponentViewModel
bool showComposingRegionUnderline = false,
UnderlineStyle spellingErrorUnderlineStyle = const SquiggleUnderlineStyle(color: Color(0xFFFF0000)),
List<TextRange> spellingErrors = const <TextRange>[],
UnderlineStyle grammarErrorUnderlineStyle = const SquiggleUnderlineStyle(color: Colors.blue),
List<TextRange> grammarErrors = const <TextRange>[],
}) : super(nodeId: nodeId, maxWidth: maxWidth, padding: padding) {
this.composingRegion = composingRegion;
this.showComposingRegionUnderline = showComposingRegionUnderline;

this.spellingErrorUnderlineStyle = spellingErrorUnderlineStyle;
this.spellingErrors = spellingErrors;

this.grammarErrorUnderlineStyle = grammarErrorUnderlineStyle;
this.grammarErrors = grammarErrors;
}

@override
Expand Down Expand Up @@ -168,6 +169,8 @@ class BlockquoteComponentViewModel extends SingleColumnLayoutComponentViewModel
highlightWhenEmpty: highlightWhenEmpty,
spellingErrorUnderlineStyle: spellingErrorUnderlineStyle,
spellingErrors: List.from(spellingErrors),
grammarErrorUnderlineStyle: grammarErrorUnderlineStyle,
grammarErrors: List.from(grammarErrors),
composingRegion: composingRegion,
showComposingRegionUnderline: showComposingRegionUnderline,
);
Expand All @@ -191,6 +194,8 @@ class BlockquoteComponentViewModel extends SingleColumnLayoutComponentViewModel
highlightWhenEmpty == other.highlightWhenEmpty &&
spellingErrorUnderlineStyle == other.spellingErrorUnderlineStyle &&
const DeepCollectionEquality().equals(spellingErrors, other.spellingErrors) &&
grammarErrorUnderlineStyle == other.grammarErrorUnderlineStyle &&
const DeepCollectionEquality().equals(grammarErrors, other.grammarErrors) &&
composingRegion == other.composingRegion &&
showComposingRegionUnderline == other.showComposingRegionUnderline;

Expand All @@ -209,6 +214,8 @@ class BlockquoteComponentViewModel extends SingleColumnLayoutComponentViewModel
highlightWhenEmpty.hashCode ^
spellingErrorUnderlineStyle.hashCode ^
spellingErrors.hashCode ^
grammarErrorUnderlineStyle.hashCode ^
grammarErrors.hashCode ^
composingRegion.hashCode ^
showComposingRegionUnderline.hashCode;
}
Expand Down
27 changes: 18 additions & 9 deletions super_editor/lib/src/default_editor/list_items.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,6 @@ class ListItemComponentBuilder implements ComponentBuilder {
text: node.text,
textStyleBuilder: noStyleBuilder,
selectionColor: const Color(0x00000000),
spellingErrors: node.text
.getAttributionSpansByFilter((a) => a == spellingErrorAttribution)
.map((a) => TextRange(start: a.start, end: a.end + 1)) // +1 because text range end is exclusive
.toList(),
),
ListItemType.ordered => OrderedListItemComponentViewModel(
nodeId: node.id,
Expand All @@ -149,10 +145,6 @@ class ListItemComponentBuilder implements ComponentBuilder {
text: node.text,
textStyleBuilder: noStyleBuilder,
selectionColor: const Color(0x00000000),
spellingErrors: node.text
.getAttributionSpansByFilter((a) => a == spellingErrorAttribution)
.map((a) => TextRange(start: a.start, end: a.end + 1)) // +1 because text range end is exclusive
.toList(),
),
};
}
Expand Down Expand Up @@ -213,14 +205,19 @@ abstract class ListItemComponentViewModel extends SingleColumnLayoutComponentVie
this.highlightWhenEmpty = false,
TextRange? composingRegion,
bool showComposingRegionUnderline = false,
UnderlineStyle spellingErrorUnderlineStyle = const SquiggleUnderlineStyle(color: Color(0xFFFF0000)),
UnderlineStyle spellingErrorUnderlineStyle = const SquiggleUnderlineStyle(color: Colors.red),
List<TextRange> spellingErrors = const <TextRange>[],
UnderlineStyle grammarErrorUnderlineStyle = const SquiggleUnderlineStyle(color: Colors.blue),
List<TextRange> grammarErrors = const <TextRange>[],
}) : super(nodeId: nodeId, maxWidth: maxWidth, padding: padding) {
this.composingRegion = composingRegion;
this.showComposingRegionUnderline = showComposingRegionUnderline;

this.spellingErrorUnderlineStyle = spellingErrorUnderlineStyle;
this.spellingErrors = spellingErrors;

this.grammarErrorUnderlineStyle = grammarErrorUnderlineStyle;
this.grammarErrors = grammarErrors;
}

int indent;
Expand Down Expand Up @@ -255,6 +252,8 @@ abstract class ListItemComponentViewModel extends SingleColumnLayoutComponentVie
highlightWhenEmpty == other.highlightWhenEmpty &&
spellingErrorUnderlineStyle == other.spellingErrorUnderlineStyle &&
const DeepCollectionEquality().equals(spellingErrors, spellingErrors) &&
grammarErrorUnderlineStyle == other.grammarErrorUnderlineStyle &&
const DeepCollectionEquality().equals(grammarErrors, grammarErrors) &&
composingRegion == other.composingRegion &&
showComposingRegionUnderline == other.showComposingRegionUnderline;

Expand All @@ -270,6 +269,8 @@ abstract class ListItemComponentViewModel extends SingleColumnLayoutComponentVie
highlightWhenEmpty.hashCode ^
spellingErrorUnderlineStyle.hashCode ^
spellingErrors.hashCode ^
grammarErrorUnderlineStyle.hashCode ^
grammarErrors.hashCode ^
composingRegion.hashCode ^
showComposingRegionUnderline.hashCode;
}
Expand All @@ -292,6 +293,8 @@ class UnorderedListItemComponentViewModel extends ListItemComponentViewModel {
super.showComposingRegionUnderline = false,
super.spellingErrorUnderlineStyle,
super.spellingErrors,
super.grammarErrorUnderlineStyle,
super.grammarErrors,
});

ListItemDotStyle dotStyle;
Expand Down Expand Up @@ -323,6 +326,8 @@ class UnorderedListItemComponentViewModel extends ListItemComponentViewModel {
showComposingRegionUnderline: showComposingRegionUnderline,
spellingErrorUnderlineStyle: spellingErrorUnderlineStyle,
spellingErrors: List.from(spellingErrors),
grammarErrorUnderlineStyle: grammarErrorUnderlineStyle,
grammarErrors: List.from(grammarErrors),
);
}

Expand Down Expand Up @@ -357,6 +362,8 @@ class OrderedListItemComponentViewModel extends ListItemComponentViewModel {
super.showComposingRegionUnderline = false,
super.spellingErrorUnderlineStyle,
super.spellingErrors,
super.grammarErrorUnderlineStyle,
super.grammarErrors,
});

final int? ordinalValue;
Expand Down Expand Up @@ -386,6 +393,8 @@ class OrderedListItemComponentViewModel extends ListItemComponentViewModel {
showComposingRegionUnderline: showComposingRegionUnderline,
spellingErrorUnderlineStyle: spellingErrorUnderlineStyle,
spellingErrors: List.from(spellingErrors),
grammarErrorUnderlineStyle: grammarErrorUnderlineStyle,
grammarErrors: List.from(grammarErrors),
);
}

Expand Down
20 changes: 14 additions & 6 deletions super_editor/lib/src/default_editor/paragraph.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:attributed_text/attributed_text.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:super_editor/src/core/document.dart';
Expand Down Expand Up @@ -104,10 +105,6 @@ class ParagraphComponentBuilder implements ComponentBuilder {
textDirection: textDirection,
textAlignment: textAlign,
selectionColor: const Color(0x00000000),
spellingErrors: node.text
.getAttributionSpansByFilter((a) => a == spellingErrorAttribution)
.map((a) => TextRange(start: a.start, end: a.end + 1)) // +1 because text range end is exclusive
.toList(),
);
}

Expand Down Expand Up @@ -153,14 +150,19 @@ class ParagraphComponentViewModel extends SingleColumnLayoutComponentViewModel w
this.highlightWhenEmpty = false,
TextRange? composingRegion,
bool showComposingRegionUnderline = false,
UnderlineStyle spellingErrorUnderlineStyle = const SquiggleUnderlineStyle(color: Color(0xFFFF0000)),
UnderlineStyle spellingErrorUnderlineStyle = const SquiggleUnderlineStyle(color: Colors.red),
List<TextRange> spellingErrors = const <TextRange>[],
UnderlineStyle grammarErrorUnderlineStyle = const SquiggleUnderlineStyle(color: Colors.blue),
List<TextRange> grammarErrors = const <TextRange>[],
}) : super(nodeId: nodeId, maxWidth: maxWidth, padding: padding) {
this.composingRegion = composingRegion;
this.showComposingRegionUnderline = showComposingRegionUnderline;

this.spellingErrorUnderlineStyle = spellingErrorUnderlineStyle;
this.spellingErrors = spellingErrors;

this.grammarErrorUnderlineStyle = grammarErrorUnderlineStyle;
this.grammarErrors = grammarErrors;
}

Attribution? blockType;
Expand Down Expand Up @@ -207,7 +209,9 @@ class ParagraphComponentViewModel extends SingleColumnLayoutComponentViewModel w
selectionColor: selectionColor,
highlightWhenEmpty: highlightWhenEmpty,
spellingErrorUnderlineStyle: spellingErrorUnderlineStyle,
spellingErrors: spellingErrors,
spellingErrors: List.from(spellingErrors),
grammarErrorUnderlineStyle: grammarErrorUnderlineStyle,
grammarErrors: List.from(grammarErrors),
composingRegion: composingRegion,
showComposingRegionUnderline: showComposingRegionUnderline,
);
Expand All @@ -231,6 +235,8 @@ class ParagraphComponentViewModel extends SingleColumnLayoutComponentViewModel w
highlightWhenEmpty == other.highlightWhenEmpty &&
spellingErrorUnderlineStyle == other.spellingErrorUnderlineStyle &&
const DeepCollectionEquality().equals(spellingErrors, other.spellingErrors) &&
grammarErrorUnderlineStyle == other.grammarErrorUnderlineStyle &&
const DeepCollectionEquality().equals(grammarErrors, other.grammarErrors) &&
composingRegion == other.composingRegion &&
showComposingRegionUnderline == other.showComposingRegionUnderline;

Expand All @@ -249,6 +255,8 @@ class ParagraphComponentViewModel extends SingleColumnLayoutComponentViewModel w
highlightWhenEmpty.hashCode ^
spellingErrorUnderlineStyle.hashCode ^
spellingErrors.hashCode ^
grammarErrorUnderlineStyle.hashCode ^
grammarErrors.hashCode ^
composingRegion.hashCode ^
showComposingRegionUnderline.hashCode;
}
Expand Down
Loading

0 comments on commit 84aacd2

Please sign in to comment.