Skip to content

Updating ReactNodeViewRenderer element's attributes when the node updates #5494

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

Merged
merged 7 commits into from
Aug 18, 2024

Conversation

YaoKaiLun
Copy link
Contributor

@YaoKaiLun YaoKaiLun commented Aug 15, 2024

Updating ReactNodeViewRenderer element's attributes when the node updates to fix #4239

Changes Overview

Supporting function type for attrs option of ReactNodeViewRendererOptions to obtain latest attributes.

Implementation Approach

  attrs?:
    | Record<string, string>
    | ((props: {
        node: ProseMirrorNode
        HTMLAttributes: Record<string, any>
      }) => Record<string, string>)

Testing Done

  addNodeView() {
    return props => {
      const attrs = ({ node, HTMLAttributes }) => {
        return HTMLAttributes
      }

      return ReactNodeViewRenderer(Component, { attrs })(props)
    }
  },

Verification Steps

"count" attribute will real time update
image

Checklist

  • I have created a changeset for this PR if necessary.
  • My changes do not break the library.
  • I have added tests where applicable.
  • I have followed the project guidelines.
  • I have fixed any lint issues.

Related Issues

Copy link

changeset-bot bot commented Aug 15, 2024

🦋 Changeset detected

Latest commit: 3b83b28

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 54 packages
Name Type
@tiptap/react Patch
@tiptap/core Patch
@tiptap/extension-blockquote Patch
@tiptap/extension-bold Patch
@tiptap/extension-bubble-menu Patch
@tiptap/extension-bullet-list Patch
@tiptap/extension-character-count Patch
@tiptap/extension-code-block-lowlight Patch
@tiptap/extension-code-block Patch
@tiptap/extension-code Patch
@tiptap/extension-collaboration-cursor Patch
@tiptap/extension-collaboration Patch
@tiptap/extension-color Patch
@tiptap/extension-document Patch
@tiptap/extension-dropcursor Patch
@tiptap/extension-floating-menu Patch
@tiptap/extension-focus Patch
@tiptap/extension-font-family Patch
@tiptap/extension-gapcursor Patch
@tiptap/extension-hard-break Patch
@tiptap/extension-heading Patch
@tiptap/extension-highlight Patch
@tiptap/extension-history Patch
@tiptap/extension-horizontal-rule Patch
@tiptap/extension-image Patch
@tiptap/extension-italic Patch
@tiptap/extension-link Patch
@tiptap/extension-list-item Patch
@tiptap/extension-list-keymap Patch
@tiptap/extension-mention Patch
@tiptap/extension-ordered-list Patch
@tiptap/extension-paragraph Patch
@tiptap/extension-placeholder Patch
@tiptap/extension-strike Patch
@tiptap/extension-subscript Patch
@tiptap/extension-superscript Patch
@tiptap/extension-table-cell Patch
@tiptap/extension-table-header Patch
@tiptap/extension-table-row Patch
@tiptap/extension-table Patch
@tiptap/extension-task-item Patch
@tiptap/extension-task-list Patch
@tiptap/extension-text-align Patch
@tiptap/extension-text-style Patch
@tiptap/extension-text Patch
@tiptap/extension-typography Patch
@tiptap/extension-underline Patch
@tiptap/extension-youtube Patch
@tiptap/html Patch
@tiptap/pm Patch
@tiptap/starter-kit Patch
@tiptap/suggestion Patch
@tiptap/vue-2 Patch
@tiptap/vue-3 Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

netlify bot commented Aug 15, 2024

Deploy Preview for tiptap-embed ready!

Name Link
🔨 Latest commit 3b83b28
🔍 Latest deploy log https://app.netlify.com/sites/tiptap-embed/deploys/66c1c1235d884d0008cc83c8
😎 Deploy Preview https://deploy-preview-5494--tiptap-embed.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@YaoKaiLun YaoKaiLun changed the title Update ReactNodeViewRenderer element attributes when node update Update ReactNodeViewRenderer element attributes when the node updates Aug 15, 2024
@YaoKaiLun YaoKaiLun changed the title Update ReactNodeViewRenderer element attributes when the node updates Updating ReactNodeViewRenderer element attributes when the node updates Aug 15, 2024
@YaoKaiLun YaoKaiLun changed the title Updating ReactNodeViewRenderer element attributes when the node updates Updating ReactNodeViewRenderer element's attributes when the node updates Aug 15, 2024
@nperez0111
Copy link

Hi, appreciate your contribution here. Forgive me, but I'm having a hard time understanding what this resolves. Could you maybe add an example (codesandbox or even just in the repo is fine) to help me understand what exactly this is resolving?

Sorry to make you do more work, but I have to understand it before I can merge it

@YaoKaiLun
Copy link
Contributor Author

YaoKaiLun commented Aug 16, 2024

Hi, appreciate your contribution here. Forgive me, but I'm having a hard time understanding what this resolves. Could you maybe add an example (codesandbox or even just in the repo is fine) to help me understand what exactly this is resolving?

Sorry to make you do more work, but I have to understand it before I can merge it

@nperez0111 I want to set a data attributes in the "react-render" div element using global attributest like "data-sid", but it doesn't update when attrs.sid was updated

https://codesandbox.io/p/sandbox/cocky-jang-vdfw46
企业微信截图_7fdb91a7-8e43-4506-949e-a1ea51198f50

Also you can see this issue #4239, I think it will be solved by using this solution

useEffect(() => {
  updateAttributes({
    colspan: node.attrs.colspan,
    rowspan: node.attrs.rowspan,
    colwidth: node.attrs.colwidth
  });
}, [
  updateAttributes,
  node.attrs.colspan,
  node.attrs.rowspan,
  node.attrs.colwidth
]);

addNodeView() {
  return (props) =>
    ReactNodeViewRenderer(TableCellView, {
      as: "td",
      attrs: ({ node }) => node.attrs
    })(props);
}

Comment on lines -119 to -124
if (attrs) {
Object.keys(attrs).forEach(key => {
this.element.setAttribute(key, attrs[key])
})
}

Choose a reason for hiding this comment

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

I would prefer to keep this updating of the element within the ReactRenderer rather than have the ReactNodeViewRenderer managing it.

Could we add a method here that basically does this code here, named updateAttributes which ReactNodeViewRenderer calls when it wants to apply the changed attributes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good advice, thank you!

Comment on lines 234 to 236
Object.keys(attrsObj).forEach(key => {
this.renderer.element.setAttribute(key, attrsObj[key])
})

Choose a reason for hiding this comment

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

Would prefer to call into the ReactRenderer here so we keep these separated

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please review again~ @nperez0111

Copy link

@nperez0111 nperez0111 left a comment

Choose a reason for hiding this comment

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

Thanks for explaining & the code sandbox. I like this, just a minor nitpick

@YaoKaiLun YaoKaiLun closed this Aug 17, 2024
@YaoKaiLun YaoKaiLun reopened this Aug 17, 2024
Copy link

@nperez0111 nperez0111 left a comment

Choose a reason for hiding this comment

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

LGTM

@nperez0111 nperez0111 merged commit 4ff2a4e into ueberdosis:develop Aug 18, 2024
14 checks passed
@YaoKaiLun YaoKaiLun deleted the feature/update-attrs branch August 27, 2024 02:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

[Bug]: attributes updates are not applied to ReactNodeViewRenderer element
2 participants