question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onDocumentChange not called on block type change

See original GitHub issue

Hi @ianstormtaylor! I notice that if the block type changes (without any change to the text), the onDocumentChange is not being fired. Is that expected?

Intuitively, I would expect that a change in block type would be considered a material change in document, to invoke this event.

Here is a JSFiddle demonstrating this - https://jsfiddle.net/urr1z99g/1/

Steps to Reproduce

  1. Open console.
  2. Click on ‘Toggle Heading’ to start a heading.
  3. At this point, I would expect the onDocumentChange and onChange to be fired.

Thank you!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:26 (22 by maintainers)

github_iconTop GitHub Comments

4reactions
erquhartcommented, Jun 20, 2017

Here’s a workaround for now:

  1. Keep using onDocumentChange to avoid needless updates.
  2. For externally initiated transforms (via buttons, keydown, etc), call the onChange method from your Editor instance directly, and pass in the transformed state.
  3. Call setState after calling onChange.

Example:

handler = () => {
  const resolvedState = this.state.state.transform().setBlock('header-one').apply();
  this.ref.onChange(resolvedState);
  this.setState({ state: resolvedState });
}

This did not result in any additional renders of the Editor component in my testing.

@ianstormtaylor I did spend a fair amount of time looking into a proper fix, but this seems to go pretty deep into Slate’s update lifecycle, and it’ll take more time to figure out what’s happening when (and why), and ensuring that a fix doesn’t cause regressions. This workaround should at least take this issue out of blocker territory for most folks.

3reactions
vpontiscommented, Apr 9, 2017

So I took a little look at this problem. Yea, it does look like it’s a problem w/ all transformations that are called via Javascript.

onBeforeChange is called on Editor.componentWillReceiveProps. But onChange is called as a proxy through the EVENT_HANDLERS.

So here are some options:

  • Add a custom EVENT_HANDLER for the transformations
  • Somehow propagate onChange when in applyTransform
  • Call onChange in the componentWillReceiveProps

@ianstormtaylor, what do you think? I’m still learning how everything is architected so I don’t have an opinion yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

OnChangeBlockType Method of the IZoomViewerEvents ...
This method is implemented on the client-side. It is called by ABBYY FineReader Engine after the block type has been changed in Image...
Read more >
Is there any onDocumentChange event? - Stack Overflow
Window with text "you've just changed DOM!" appears. I try to emulate "advanced" CSS selectors (e.g. +, >, [attr]) in IE6 using Javascript....
Read more >
Eventing in Couchbase server to send an email once a ...
My motive is to make an HTTP call from evening function when a change is encountered in any document of the bucket. And...
Read more >
OnDocumentChange event not firing - Inventor
So I started a new project with the wizard, pasted your code, changed my reference, and it would not work. Only the onsave...
Read more >
rt - mozsearch - Searchfox
Name Description Size amIAddonManagerStartup.rs jsval readStartupData () 9520 domstubs.rs 413 imgIEncoder.rs /// * imgIEncoder interface /// 7873
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found