Skip to content

Commit

Permalink
Resolved rebasing compilation issues, also fixed non-primary selectio…
Browse files Browse the repository at this point in the history
…n change re-painting and removed the non-primary caret.
  • Loading branch information
matthew-carroll committed Jul 23, 2022
1 parent 3694ac5 commit 723fcf0
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:example/demos/example_editor/_task.dart';
import 'package:flutter/foundation.dart';
import 'package:example/logging.dart';
Expand Down
6 changes: 3 additions & 3 deletions super_editor/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ packages:
name: attributed_text
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
version: "0.2.0"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -466,14 +466,14 @@ packages:
path: "../../super_editor_markdown"
relative: true
source: path
version: "0.1.0"
version: "0.1.2"
super_text_layout:
dependency: "direct main"
description:
path: "../../super_text_layout"
relative: true
source: path
version: "0.1.0"
version: "0.1.3"
term_glyph:
dependency: transitive
description:
Expand Down
23 changes: 23 additions & 0 deletions super_editor/lib/src/core/document_composer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,29 @@ abstract class NonPrimarySelectionListener {
void onSelectionRemoved(String id);
}

/// A [NonPrimarySelectionListener] that delegates to given callbacks.
abstract class CallbackNonPrimarySelectionListener implements NonPrimarySelectionListener {
CallbackNonPrimarySelectionListener({
void Function(NonPrimarySelection selection)? onSelectionAdded,
void Function(NonPrimarySelection selection)? onSelectionChanged,
void Function(String id)? onSelectionRemoved,
}) : _onSelectionAdded = onSelectionAdded,
_onSelectionChanged = onSelectionChanged,
_onSelectionRemoved = onSelectionRemoved;

final void Function(NonPrimarySelection selection)? _onSelectionAdded;
@override
void onSelectionAdded(NonPrimarySelection selection) => _onSelectionAdded?.call(selection);

final void Function(NonPrimarySelection selection)? _onSelectionChanged;
@override
void onSelectionChanged(NonPrimarySelection selection) => _onSelectionChanged?.call(selection);

final void Function(String id)? _onSelectionRemoved;
@override
void onSelectionRemoved(String id) => _onSelectionRemoved?.call(id);
}

/// Holds preferences about user input, to be used for the
/// next character that is entered. This facilitates things
/// like a "bold mode" or "italics mode" when there is no
Expand Down
7 changes: 6 additions & 1 deletion super_editor/lib/src/core/styles.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,14 @@ class CascadingPadding {

/// A document selection with associated visual styles.
class StyledSelection<SelectionType> {
const StyledSelection(this.selection, this.styles);
const StyledSelection({
required this.selection,
this.hasCaret = false,
required this.styles,
});

final SelectionType selection;
final bool hasCaret;
final SelectionStyles styles;

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:super_editor/src/core/document_selection.dart';
import 'package:super_editor/src/core/styles.dart';
import 'package:super_editor/src/default_editor/horizontal_rule.dart';
import 'package:super_editor/src/default_editor/image.dart';
import 'package:super_editor/src/default_editor/list_items.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';
Expand All @@ -15,7 +14,7 @@ import '_presenter.dart';

/// [SingleColumnLayoutStylePhase] that applies visual selections to each component,
/// e.g., text selections, image selections, caret positioning.
class SingleColumnLayoutSelectionStyler extends SingleColumnLayoutStylePhase {
class SingleColumnLayoutSelectionStyler extends SingleColumnLayoutStylePhase implements NonPrimarySelectionListener {
SingleColumnLayoutSelectionStyler({
required Document document,
required DocumentComposer composer,
Expand All @@ -27,17 +26,18 @@ class SingleColumnLayoutSelectionStyler extends SingleColumnLayoutStylePhase {
_nonPrimarySelectionStyler = nonPrimarySelectionStyler {
// Our styles need to be re-applied whenever the document selection changes.
_composer.selectionNotifier.addListener(markDirty);
_composer.addNonPrimarySelectionListener(this);
}

@override
void dispose() {
_composer.selectionNotifier.removeListener(markDirty);
_composer.removeNonPrimarySelectionListener(this);
super.dispose();
}

final Document _document;
final DocumentComposer _composer;
final SelectionStyles _selectionStyles;
final NonPrimarySelectionStyler? _nonPrimarySelectionStyler;

SelectionStyles _selectionStyles;
Expand All @@ -61,6 +61,13 @@ class SingleColumnLayoutSelectionStyler extends SingleColumnLayoutStylePhase {
markDirty();
}

@override
void onSelectionAdded(NonPrimarySelection selection) => markDirty();
@override
void onSelectionChanged(NonPrimarySelection selection) => markDirty();
@override
void onSelectionRemoved(String id) => markDirty();

@override
SingleColumnLayoutViewModel style(Document document, SingleColumnLayoutViewModel viewModel) {
editorStyleLog.info("(Re)calculating selection view model for document layout");
Expand Down Expand Up @@ -129,11 +136,9 @@ class SingleColumnLayoutSelectionStyler extends SingleColumnLayoutStylePhase {
editorStyleLog.finer(' - extent: ${textSelection?.extent}');

if (viewModel is TextComponentViewModel) {
viewModel
..styledSelections = styledSelections
..caret = showCaret ? textSelection?.extent : null
..caretColor = _selectionStyles.caretColor
..highlightWhenEmpty = highlightWhenEmpty;
viewModel..styledSelections = styledSelections;
// ..caret = showCaret ? textSelection?.extent : null
// ..caretColor = _selectionStyles.caretColor;
}
}
if (viewModel is ImageComponentViewModel) {
Expand Down Expand Up @@ -195,11 +200,12 @@ class SingleColumnLayoutSelectionStyler extends SingleColumnLayoutStylePhase {
TextSelection(baseOffset: textNodeSelection.baseOffset, extentOffset: textNodeSelection.extentOffset);

styledSelections.add(StyledSelection(
textSelection,
selection: textSelection,
hasCaret: false, // non-primary selections never have a caret
// TODO: the styler decides whether to highlight an empty block, but it shouldn't.
// That decision needs to be made based on whether the user is selecting multiple
// blocks.
_nonPrimarySelectionStyler!(nonPrimarySelection)!,
styles: _nonPrimarySelectionStyler!(nonPrimarySelection)!,
));
}
}
Expand Down Expand Up @@ -233,8 +239,9 @@ class SingleColumnLayoutSelectionStyler extends SingleColumnLayoutStylePhase {
TextSelection(baseOffset: textNodeSelection.baseOffset, extentOffset: textNodeSelection.extentOffset);

styledSelections.add(StyledSelection(
textSelection,
SelectionStyles(
selection: textSelection,
hasCaret: composer.selection!.extent.nodeId == node.id,
styles: SelectionStyles(
selectionColor: _selectionStyles.selectionColor,
highlightEmptyTextBlocks: nodeSelection.highlightWhenEmpty,
caretColor: _selectionStyles.caretColor,
Expand Down
2 changes: 1 addition & 1 deletion super_editor/lib/src/default_editor/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ class TextComponentState extends State<TextComponent> with DocumentComponent imp
caretStyle: CaretStyle(
color: selection.styles.caretColor,
),
hasCaret: true,
hasCaret: selection.hasCaret,
)
],
),
Expand Down

0 comments on commit 723fcf0

Please sign in to comment.