Skip to content

Commit

Permalink
feat: finally something that works
Browse files Browse the repository at this point in the history
  • Loading branch information
nperez0111 committed Jun 26, 2024
1 parent 9b43191 commit 3a066ba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions demos/src/Examples/CustomParagraph/React/Paragraph.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '@tiptap/react'

const ParagraphComponent = ({ node }) => {
console.count('render')
return (
<NodeViewWrapper style={{ position: 'relative' }}>
<span contentEditable={false} className="label" style={{
Expand Down
18 changes: 17 additions & 1 deletion packages/react/src/ReactRenderer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Editor } from '@tiptap/core'
import React from 'react'
import { flushSync } from 'react-dom'

import { Editor as ExtendedEditor } from './Editor.js'

Expand Down Expand Up @@ -71,6 +72,8 @@ type ComponentType<R, P> =
React.FunctionComponent<P> |
React.ForwardRefExoticComponent<React.PropsWithoutRef<P> & React.RefAttributes<R>>;

let first = true

/**
* The ReactRenderer class. It's responsible for rendering React components inside the editor.
* @example
Expand Down Expand Up @@ -121,7 +124,20 @@ export class ReactRenderer<R = unknown, P = unknown> {
})
}

this.render()
if (first) {
// TODO this is totally a hack, on the first render of the editor (parent editor)
// We need to delay the rendering of the ReactRenderer components (because it would render before the editor is ready)
// setTimeout(() => {
this.render()
first = false
// }, 100)
} else {
// On first render, we need to flush the render synchronously
// Renders afterwards can be async, but this fixes a cursor positioning issue
flushSync(() => {
this.render()
})
}
}

render(): void {
Expand Down

0 comments on commit 3a066ba

Please sign in to comment.