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

Detect when the post content changes #10992

Closed
daext opened this issue Oct 24, 2018 · 2 comments
Closed

Detect when the post content changes #10992

daext opened this issue Oct 24, 2018 · 2 comments
Labels
[Type] Question Questions about the design or development of the editor.

Comments

@daext
Copy link

daext commented Oct 24, 2018

Hello, for a plugin I'm currently developing I need to dynamically update the information displayed in a meta-box when the post content changes with the Gutenberg editor active. I did my researches and based on this post #4674 (comment) I ended up with this solution.

Can you please tell me if there are better methods to achieve this or if it's a valid solution in your opinion? (the code below works)

  const {subscribe} = wp.data;
  window.DAIM = {};

  const unssubscribe = subscribe(() => {

    if (wp.data.select('core/editor').getCurrentPostLastRevisionId() !== null &&
        window.DAIM.currentPostLastRevisionId !== null &&
        window.DAIM.currentPostLastRevisionId !== wp.data.select('core/editor').getCurrentPostLastRevisionId()) {

      console.log('The revision is changed, dynamically update the meta-box based on the current post content.');

    }

    window.DAIM.currentPostLastRevisionId = wp.data.select('core/editor').getCurrentPostLastRevisionId();

  });
@Soean Soean added the [Type] Question Questions about the design or development of the editor. label Oct 24, 2018
@SeanDS
Copy link
Contributor

SeanDS commented Jan 8, 2019

Thanks @daext, that code is exactly what I was looking for. I too would be interested in finding out if there is a better way to do this, perhaps with some sort of property change listener (not just the generic subscribe).

@talldan
Copy link
Contributor

talldan commented Apr 23, 2020

This will check whether there's a new revision. In terms of edits to the post content, you could also use something like the getEditedPostContent selector to detect changed content in the same way.

@talldan talldan closed this as completed Apr 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Question Questions about the design or development of the editor.
Projects
None yet
Development

No branches or pull requests

4 participants