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

onDocumentChange and onSelectionChange #4687

Closed
delijah opened this issue Nov 24, 2021 · 4 comments
Closed

onDocumentChange and onSelectionChange #4687

delijah opened this issue Nov 24, 2021 · 4 comments
Labels

Comments

@delijah
Copy link
Contributor

delijah commented Nov 24, 2021

Problem
We have some plugins on our slate js editor implementation, that rely on the editors selections. Depending on the selection they need to change their position. Currently the onChange event of slate is fired for changes on the document structure but also for changes on the selection. Since the value does not really change on selection changes, the component that contains the slate editor will not get rerendered. Which is fine in normal cases, but in our use case it blocks our plugin from being rendered correctly.

Solution
If we would have a onDocumentChange and a onSelectionChange event, i guess we could just force update/rerender the component inside onSelectionChange. Then we would only have to call the onChange handler from the parent component onDocumentChange.

Alternatives
We need a rerender after a selection change. Not sure how to achieve this otherwise. At the moment we are checking if 100ms after the last onChange call, a render happened, if yes, fine, if no, we force update/rerender. Not very nice, but it works.

What do you think about that? Would it make sense? Or is there a strong reason why those props where not implemented?

@schnuderl
Copy link

schnuderl commented Nov 24, 2021

you can distinguish in the onChange event like:

if (editor.operations.every(op => op.type === "set_selection")) ....

@delijah
Copy link
Contributor Author

delijah commented Dec 7, 2021

Oh wow, thank you, that helps 👍

@delijah delijah closed this as completed Dec 7, 2021
@Halanson
Copy link

I have added a listener to myEditor.onChange = () => { ... }, but even without any expression in the curly brackets, I can not type anything in my editor. If I move the cursor with the up/down arrow key or use the mouse the typed symbols immediately pop up. So what I'm doing wrong?

@hieumau
Copy link

hieumau commented Oct 10, 2023

I have added a listener to myEditor.onChange = () => { ... }, but even without any expression in the curly brackets, I can not type anything in my editor. If I move the cursor with the up/down arrow key or use the mouse the typed symbols immediately pop up. So what I'm doing wrong?

I face with same problem. The reason is that you override onChange event of editor but not have default function for the fallback case that you not handle correctly. Here is my code fix

     const {onChange} = editor
     editor.onChange = () => {
            if (editor.operations.every(op => op.type === "set_selection")) {
                console.log(...editor.operations)
            }
            return onChange()
        }

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

No branches or pull requests

4 participants