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

[SuperEditor] Allow custom view models to be aware of node selection (Resolves #1944) #2063

Merged
merged 5 commits into from
Jun 4, 2024

Conversation

angelosilvestre
Copy link
Collaborator

[SuperEditor] Allow custom view models to be aware of node selection. Resolves #1944

Currently, during the styling pass, we only apply the selection and selection color to our own viewmodels: TextComponentViewModel, ImageComponentViewModel and HorizontalRuleComponentViewModel.

Because of that, app-level view models, don't receive this information

Screen.Recording.2024-05-30.at.11.33.50.mov

This PR introduces a SelectionAwareViewModelMixin. Any view model that mixes it in will receive the DocumentNodeSelection and selection color during the style pass.

Using this mixin, app-level components can easily render their selection:

Screen.Recording.2024-05-30.at.11.36.11.mov

@angelosilvestre
Copy link
Collaborator Author

@kasyrm Could you please try this PR to see if it fits your needs?

@@ -168,6 +169,11 @@ class SingleColumnLayoutSelectionStyler extends SingleColumnLayoutStylePhase {
..selection = selection
..selectionColor = _selectionStyles.selectionColor;
}
if (viewModel is SelectionAwareViewModelMixin) {
Copy link
Contributor

Choose a reason for hiding this comment

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

To stress test this approach, can you refactor the other view models to use this mixin, too? If a solution is versatile enough for all 3rd party components then it should be versatile enough for our existing components.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated.

this.caret,
required this.caretColor,
}) : super(nodeId: nodeId, maxWidth: maxWidth, padding: padding);
}) : super(nodeId: nodeId, maxWidth: maxWidth, padding: padding) {
super.selection = selection;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you use super in the parameter list instead of down here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We can for nodeId, maxWidth and padding, but not for selection and selectionColor. Since the mixin can't have a constructor we cannot use the super keyword here.

@kasyrm
Copy link

kasyrm commented Jun 1, 2024

@kasyrm Could you please try this PR to see if it fits your needs?

Yes! Thank you :)
Is it correct to assume nodeSelection of selection will be UpstreamDownstreamNodeSelection, so it's possible to check whether component is fully selected or selection is collapsed?

@matthew-carroll
Copy link
Contributor

Is it correct to assume nodeSelection of selection will be UpstreamDownstreamNodeSelection

@angelosilvestre if that is assumed, it probably shouldn't get. Imagine something like a table component, or anything else more complicated than a singe block, that component would need to use its own definition of selection.

@angelosilvestre
Copy link
Collaborator Author

@kasyrm @matthew-carroll It depends on the node type. For BlockNode subclasses, it will be an
UpstreamDownstreamNodeSelection by default. For TextNode subclasses, it will be a TextNodeSelection by default.

Any node can override DocumentNode.computeSelection to return its own selection representation.

Copy link
Contributor

@matthew-carroll matthew-carroll left a comment

Choose a reason for hiding this comment

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

LGTM

@matthew-carroll matthew-carroll merged commit eed52c4 into main Jun 4, 2024
11 checks passed
@matthew-carroll matthew-carroll deleted the 1944_custom-viewmodel-selection-color branch June 4, 2024 01:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Selection colour of components with custom view models
3 participants