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

Derive onSelectionChange and onValueChange from onChange #5521

Closed
zbeyens opened this issue Oct 13, 2023 · 4 comments · Fixed by #5526
Closed

Derive onSelectionChange and onValueChange from onChange #5521

zbeyens opened this issue Oct 13, 2023 · 4 comments · Fixed by #5526
Labels

Comments

@zbeyens
Copy link
Contributor

zbeyens commented Oct 13, 2023

Problem
onChange currently triggers for all changes: value and selection. When I only want to listen to value changes, this is not optimal.

Solution
Keep onChange and add onSelectionChange to listen to selection change only and onValueChange to listen to value change only.

Bounty: udecode/plate#2700

@rishi-raj-jain
Copy link

@zbeyens @12joan

onValueChange from useStateSelector where type of op is set_selection would the right logic, right?

@zbeyens
Copy link
Contributor Author

zbeyens commented Oct 13, 2023

The goal is to listen to value change only, all operations except set_selection. We want to avoid using useSlateSelector for optimization.

@rishi-raj-jain
Copy link

Ah so totally a different hook.

@zbeyens
Copy link
Contributor Author

zbeyens commented Oct 14, 2023

  1. The onSelectionChange function should only listen to the set_selection operation, not all operations.
  2. Conversely, the onValueChange function should only listen to the other operations, excluding set_selection.

The solution needs to be built exactly like onChange prop. No hook is expected:

  • Slate prop: onValueChange
  • Slate prop: onSelectionChange
  • editor method: editor.onValueChange
  • editor method: editor.onSelectionChange

Tip: you can start from

const onContextChange = EDITOR_TO_ON_CHANGE.get(e)
if (onContextChange) {
onContextChange()
}
and call onSelectionChange if options.operation.type === 'set_selection', else onValueChange by passing a new parameter to onContextChange to call the corresponding callback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants