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

feat/#5521/derive on selection change and on value change #5523

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/curly-ligers-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'slate': minor
'slate-react': minor
---

Add `onValueChange` to watch value changes and `onSelectorChange` to watch selector changes
1 change: 1 addition & 0 deletions packages/slate-react/src/components/slate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const Slate = (props: {
children: React.ReactNode
onChange?: (value: Descendant[]) => void
}) => {
// do we need to add onValueChange and onSelectorChange here?
Copy link
Author

@ologbonowiwi ologbonowiwi Oct 17, 2023

Choose a reason for hiding this comment

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

here is where I need to add the new handlers, @zbeyens?

and execute them inside onContextChange?

or do you want me to add a new parameter on onContextChange, where we'll send the onValueChange/onSelectorChange (from this line)

Copy link
Contributor

Choose a reason for hiding this comment

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

Whatever that makes it work :) Will be confirmed by unit tests.

const { editor, children, onChange, initialValue, ...rest } = props

const [context, setContext] = React.useState<SlateContextValue>(() => {
Expand Down
18 changes: 17 additions & 1 deletion packages/slate-react/src/plugin/with-react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ export const withReact = <T extends BaseEditor>(
clipboardFormatKey = 'x-slate-fragment'
): T & ReactEditor => {
const e = editor as T & ReactEditor
const { apply, onChange, deleteBackward, addMark, removeMark } = e
const {
apply,
onChange,
onValueChange,
onSelectionChange,
deleteBackward,
addMark,
removeMark,
} = e

// The WeakMap which maps a key to a specific HTMLElement must be scoped to the editor instance to
// avoid collisions between editors in the DOM that share the same value.
Expand Down Expand Up @@ -366,6 +374,14 @@ export const withReact = <T extends BaseEditor>(
onContextChange()
Copy link
Contributor

@zbeyens zbeyens Oct 17, 2023

Choose a reason for hiding this comment

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

You need to pass a parameter to this function as stated in #5521 (comment)

Copy link
Author

Choose a reason for hiding this comment

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

I'm not getting what you mean. What should I pass as parameter here?

Copy link
Contributor

Choose a reason for hiding this comment

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

We need 2 new props to Slate: onSelectionChange and onValueChange. These callbacks should be called in onContextChange like onChange.

}

switch (options?.operation?.type) {
case 'set_selection':
onSelectionChange(options)
break
default:
onValueChange(options)
}

onChange(options)
})
}
Expand Down
2 changes: 2 additions & 0 deletions packages/slate/src/interfaces/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export interface BaseEditor {
markableVoid: (element: Element) => boolean
normalizeNode: (entry: NodeEntry, options?: { operation?: Operation }) => void
onChange: (options?: { operation?: Operation }) => void
onSelectionChange: (options?: { operation?: Operation }) => void
onValueChange: (options?: { operation?: Operation }) => void
shouldNormalize: ({
iteration,
dirtyPaths,
Expand Down
Loading