Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(macos): Implement actions for ExpandSelectionToDocumentBoundaryIntent and ExpandSelectionToLineBreakIntent to use keyboard shortcuts, unrelated cleanup to the bug fix. #2279

Merged
merged 20 commits into from
Oct 23, 2024

Conversation

EchoEllet
Copy link
Collaborator

@EchoEllet EchoEllet commented Sep 24, 2024

Description

Implement actions for ExpandSelectionToDocumentBoundaryIntent and ExpandSelectionToLineBreakIntent in addition to unrelated cleanup that is not required to have the bug fix.

This PR will be updated soon, see #1192 for more details about the issue.

Related Issues

ExtendSelectionToNextParagraphBoundaryOrCaretLocationIntent is also missing and will throw when pressing (option + shift + arrow up/down).

Type of Change

  • New feature: Adds new functionality without breaking existing features.
  • 🛠️ Bug fix: Resolves an issue without altering current behavior.
  • 🧹 Code refactor: Code restructuring that does not affect behavior.
  • Breaking change: Alters existing functionality and requires updates.
  • 🧪 Tests: Adds new tests or modifies existing tests.
  • 📝 Documentation: Updates or additions to documentation.
  • 🗑️ Chore: Routine tasks, or maintenance.
  • Build configuration change: Changes to build or deploy processes.

Suggestions

Not for merge yet.

TODO

  • Explain the changes
  • Add tests, improve docs, and clear the Todos in code.

@EchoEllet EchoEllet changed the title Fix/1192 fix(keyboard): Implement actions for ExpandSelectionToDocumentBoundaryIntent and ExpandSelectionToLineBreakIntent. Sep 24, 2024
@EchoEllet EchoEllet changed the title fix(keyboard): Implement actions for ExpandSelectionToDocumentBoundaryIntent and ExpandSelectionToLineBreakIntent. fix(macos): Implement actions for ExpandSelectionToDocumentBoundaryIntent and ExpandSelectionToLineBreakIntent to use keyboard shortcuts. Sep 24, 2024
@EchoEllet EchoEllet added minor Minimal impact or cosmetic issue. Can be resolved at a later time without affecting overall function macOS Issues or feature requests specific to the macOS platform. labels Sep 24, 2024
@EchoEllet
Copy link
Collaborator Author

@CatHood0 I will address the change of
QuillKeyboardServiceWidget directly in this PR, worrying about having a clean history, separation, and organization of changes is not our first priority at the moment, I keep forgetting about adding this to the Todo and already forgot many things since I delay them later, my TODO is already full.

The support for shortcuts is not clean enough, some of them are overridable:

  HideSelectionToolbarIntent:
        _makeOverridable(QuillEditorHideSelectionToolbarAction(this)),
    UndoTextIntent: _makeOverridable(QuillEditorUndoKeyboardAction(this)),
    RedoTextIntent: _makeOverridable(QuillEditorRedoKeyboardAction(this)),

Some of them do start with QuillEditor prefix and some of them do not:

    ToggleTextStyleIntednt: _formatSelectionAction,
    IndentSelectionIntent: _indentSelectionAction,
    QuillEditorApplyHeaderIntent: _applyHeaderAction,
    QuillEditorApplyCheckListIntent: _applyCheckListAction,
    // TODO: Double check and see if those and related should be overridable
    QuillEditorApplyLinkIntent: QuillEditorApplyLinkAction(this),
    ScrollToDocumentBoundaryIntent: NavigateToDocumentBoundaryAction(this),

Others are not instantiated directly and have private member and the only usage is in the shortcuts:

    ToggleTextStyleIntent: _formatSelectionAction,
    IndentSelectionIntent: _indentSelectionAction,
    QuillEditorApplyHeaderIntent: _applyHeaderAction,
    QuillEditorApplyCheckListIntent: _applyCheckListAction,

It's not clear how should we name things, we need to agree on something, each class is named very differently without conventions or guidelines, it's also not clear that if we should add the SingleActivator (see #1937 comment) or if they are already registered with Flutter, or if we're using something somewhere that do register those, why they those actions are missing in the first place? It's causing issues on the desktop with keyboard shortcuts.

In addition to other questions and issues, I didn't register them in the TODOs.

The file default_single_activator_actions.dart should also be somewhere inside raw_editor.

…efaultSinlgeActivatorIntents() and QuillKeyboardServiceWidget to EditorKeyboardShortcuts, move their files inside raw_editor directory
@EchoEllet EchoEllet changed the title fix(macos): Implement actions for ExpandSelectionToDocumentBoundaryIntent and ExpandSelectionToLineBreakIntent to use keyboard shortcuts. fix(macos): Implement actions for ExpandSelectionToDocumentBoundaryIntent and ExpandSelectionToLineBreakIntent to use keyboard shortcuts, unrelated cleanup to the bug fix. Sep 25, 2024
@EchoEllet
Copy link
Collaborator Author

It seems that there are even more important things to clean up, I have also noticed some duplicated logic, and a new update will be soon however it's not related to this PR so it might be confusing to review. I have tried to separate it into a new PR though decided not to.

@EchoEllet
Copy link
Collaborator Author

EchoEllet commented Sep 25, 2024

or if we're using something somewhere that do register those, why they those actions are missing in the first place

It seems that we're using intentForMacOSSelector() from Flutter inside the raw_editor_state.dart without implementing the actions for each intent.

Details

image

image

We need to file an issue for this and fix them all in a clean way so we don't lose track of this.

Reported in #2288.

Comment on lines 155 to 165
// Navigate to the start or end of the document
SingleActivator(
LogicalKeyboardKey.home,
control: !_isDesktopMacOS,
meta: _isDesktopMacOS,
): const ScrollToDocumentBoundaryIntent(forward: false),
SingleActivator(
LogicalKeyboardKey.end,
control: !_isDesktopMacOS,
meta: _isDesktopMacOS,
): const ScrollToDocumentBoundaryIntent(forward: true),
Copy link
Collaborator Author

@EchoEllet EchoEllet Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those two included in #1937. I wasn't and still not sure if this is needed, seems to be redundant though.

Comment on lines +334 to +373
// Plain text of the document (needed to find line breaks)
final text = state.controller.plainTextEditingValue.text;

final currentSelection = state.controller.selection;

// Calculate the next or previous line break based on direction
final searchStartOffset = currentSelection.extentOffset;

final targetLineBreak = () {
if (intent.forward) {
final nextLineBreak = text.indexOf('\n', searchStartOffset);
final noNextLineBreak = nextLineBreak == -1;
return noNextLineBreak ? text.length : nextLineBreak + 1;
}

// Backward

// Ensure (searchStartOffset - 1) is not negative to avoid [RangeError]
final safePreviousSearchOffset =
(searchStartOffset > 0) ? (searchStartOffset - 1) : 0;

final previousLineBreak =
text.lastIndexOf('\n', safePreviousSearchOffset);

final noPreviousLineBreak = previousLineBreak == -1;
return noPreviousLineBreak ? 0 : previousLineBreak;
}();

// Create a new selection, extending it to the line break was found
final newSelection = currentSelection.copyWith(
extentOffset: targetLineBreak,
);

return Actions.invoke(
context ?? (throw StateError('BuildContext should not be null.')),
UpdateSelectionIntent(
state.textEditingValue,
newSelection,
SelectionChangedCause.keyboard,
),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will need to know if this functionality already exist as a function to call, prefer to not repeat it twice, it can be confusing for other developers and for me in the future.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related _linebreak()

Copy link
Collaborator

@AtlasAutocode AtlasAutocode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The support for shortcuts is not clean enough, some of them are overridable:

Just a comment: I have had a brief encounter with _makeOverridable and was surprised by how it works:

Overridable means it can be overridden by prior implementations. That is exactly the opposite of what I understand by the term.

For example: Widget build Stack:
Widget 1 overridable action
Widget 2 overridable action
Widget 3 uses action from Widget 1! Because the prior implementation takes priority.

In order for the implementation in Widget 2 to correct a defect in Widget 1's implementation, it has to not use _makeOverridable.

I hope I am wrong in this.

In my case, I seem to recollect that there was an error in how Flutter handled a macOS shortcut key and since Flutter was at the top of the rendering stack it always took priority so my fix would never work if I used _makeOverridable!

@EchoEllet
Copy link
Collaborator Author

EchoEllet commented Sep 25, 2024

The current code is quite confusing, even after the cleanup we have editor_keyboard_shortcut_actions.dart and raw_editor_actions.dart, ScrollToDocumentBoundaryIntent is already registered intent by default so including it in:

  SingleActivator(
      LogicalKeyboardKey.home,
      control: !_isDesktopMacOS,
      meta: _isDesktopMacOS,
    ): const ScrollToDocumentBoundaryIntent(forward: false),
    SingleActivator(
      LogicalKeyboardKey.end,
      control: !_isDesktopMacOS,
      meta: _isDesktopMacOS,
    ): const ScrollToDocumentBoundaryIntent(forward: true),

Doesn't make any difference, I still want to know if this is macOS specific and why we don't need to register a activator for it before moving further.

Was added in #1937.

…tcutsActionsManager, move raw_editor_actions to keyboard_shortcuts/editor_keyboard_shortcut_actions_manager.dart
@EchoEllet
Copy link
Collaborator Author

See #2288, while cleaning up, I'm having even more questions and the answers are not clear, I have spent almost one or two days trying to fix a minor bug, the bug has been fixed without fixing the related code quality issues, I prefer to completely rewrite the Keyboard shortcuts with their actions and intent instead and might do in a separate PR or might improve it later.

Hopefully was able to move around 170 lines of code from QuillRawEditorState.

@CatHood0 @AtlasAutocode If you can review the PR, once addressing all the changes, will merge this PR for now.

@CatHood0
Copy link
Collaborator

CatHood0 commented Sep 25, 2024

Sorry I haven't been able to fix a few issues yet. I've had a hard time these days. I'll take the time tonight to do some code reviews and fix issues that were already on my list.

@EchoEllet
Copy link
Collaborator Author

No need to be sorry, take as much as time as you need.

I appreciate your commitment to tackling the code reviews and issues tonight. If you need any assistance or just want to discuss anything, feel free to reach out.

@EchoEllet EchoEllet marked this pull request as ready for review October 23, 2024 16:10
@EchoEllet
Copy link
Collaborator Author

This PR is not the best in improving the organization and code quality though it's slightly more organized than the current code. I will merge however I have many questions, some things are unclear, and it's less time-consuming to rewrite the shortcuts functionality. This is a bug fix and unrelated and unrelated to the issue.

I'm open to feedback if anything can be improved here.

@EchoEllet EchoEllet merged commit e7d3391 into master Oct 23, 2024
2 checks passed
@EchoEllet EchoEllet deleted the fix/1192 branch October 23, 2024 16:17
CatHood0 pushed a commit to CatHood0/flutter-quill that referenced this pull request Oct 28, 2024
…tent and ExpandSelectionToLineBreakIntent to use keyboard shortcuts, unrelated cleanup to the bug fix. (singerdmx#2279)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
macOS Issues or feature requests specific to the macOS platform. minor Minimal impact or cosmetic issue. Can be resolved at a later time without affecting overall function
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Desktop] Error when using selection shortcut on Mac
4 participants