-
-
Notifications
You must be signed in to change notification settings - Fork 556
refactor: new react mounting system #1438
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c6634d2
refactor: new react mounting system
YousefED 6b201c7
remove ts ignores
YousefED 2d6c856
additional cleanup
YousefED 10d15a0
fix
YousefED e60fcc7
revert
YousefED 758d2bd
add comment
YousefED aeccf1d
fix
YousefED File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,69 @@ | ||
import { BlockNoteEditor } from "@blocknote/core"; | ||
import { ReactRenderer } from "@tiptap/react"; | ||
import { useEffect, useState } from "react"; | ||
import { useSyncExternalStore } from "react"; | ||
import { createPortal } from "react-dom"; | ||
|
||
const Portals: React.FC<{ renderers: Record<string, ReactRenderer> }> = ({ | ||
renderers, | ||
}) => { | ||
return ( | ||
<> | ||
{Object.entries(renderers).map(([key, renderer]) => { | ||
return createPortal(renderer.reactElement, renderer.element, key); | ||
})} | ||
</> | ||
); | ||
}; | ||
// this file takes the methods we need from | ||
// https://github.com/ueberdosis/tiptap/blob/develop/packages/react/src/EditorContent.tsx | ||
|
||
/** | ||
* Replacement of https://github.com/ueberdosis/tiptap/blob/6676c7e034a46117afdde560a1b25fe75411a21d/packages/react/src/EditorContent.tsx | ||
* that only takes care of the Portals. | ||
* | ||
* Original implementation is messy, and we use a "mount" system in BlockNoteTiptapEditor.tsx that makes this cleaner | ||
*/ | ||
export function EditorContent(props: { | ||
editor: BlockNoteEditor<any, any, any>; | ||
children: any; | ||
}) { | ||
const [renderers, setRenderers] = useState<Record<string, ReactRenderer>>({}); | ||
export function getContentComponent() { | ||
const subscribers = new Set<() => void>(); | ||
let renderers: Record<string, React.ReactPortal> = {}; | ||
|
||
useEffect(() => { | ||
props.editor._tiptapEditor.contentComponent = { | ||
/** | ||
* Used by TipTap | ||
*/ | ||
setRenderer(id: string, renderer: ReactRenderer) { | ||
setRenderers((renderers) => ({ ...renderers, [id]: renderer })); | ||
}, | ||
return { | ||
/** | ||
* Subscribe to the editor instance's changes. | ||
*/ | ||
subscribe(callback: () => void) { | ||
subscribers.add(callback); | ||
return () => { | ||
subscribers.delete(callback); | ||
}; | ||
}, | ||
getSnapshot() { | ||
return renderers; | ||
}, | ||
getServerSnapshot() { | ||
return renderers; | ||
}, | ||
/** | ||
* Adds a new NodeView Renderer to the editor. | ||
*/ | ||
setRenderer(id: string, renderer: ReactRenderer) { | ||
renderers = { | ||
...renderers, | ||
[id]: createPortal(renderer.reactElement, renderer.element, id), | ||
}; | ||
|
||
/** | ||
* Used by TipTap | ||
*/ | ||
removeRenderer(id: string) { | ||
setRenderers((renderers) => { | ||
const nextRenderers = { ...renderers }; | ||
subscribers.forEach((subscriber) => subscriber()); | ||
}, | ||
/** | ||
* Removes a NodeView Renderer from the editor. | ||
*/ | ||
removeRenderer(id: string) { | ||
const nextRenderers = { ...renderers }; | ||
|
||
delete nextRenderers[id]; | ||
delete nextRenderers[id]; | ||
renderers = nextRenderers; | ||
subscribers.forEach((subscriber) => subscriber()); | ||
}, | ||
}; | ||
} | ||
|
||
return nextRenderers; | ||
}); | ||
}, | ||
}; | ||
// Without queueMicrotask, custom IC / styles will give a React FlushSync error | ||
queueMicrotask(() => { | ||
props.editor._tiptapEditor.createNodeViews(); | ||
}); | ||
return () => { | ||
props.editor._tiptapEditor.contentComponent = null; | ||
}; | ||
}, [props.editor._tiptapEditor]); | ||
type ContentComponent = ReturnType<typeof getContentComponent>; | ||
|
||
return ( | ||
<> | ||
<Portals renderers={renderers} /> | ||
{props.children} | ||
</> | ||
/** | ||
* This component renders all of the editor's node views. | ||
*/ | ||
export const Portals: React.FC<{ contentComponent: ContentComponent }> = ({ | ||
contentComponent, | ||
}) => { | ||
// For performance reasons, we render the node view portals on state changes only | ||
const renderers = useSyncExternalStore( | ||
contentComponent.subscribe, | ||
contentComponent.getSnapshot, | ||
contentComponent.getServerSnapshot | ||
); | ||
} | ||
|
||
// This allows us to directly render the portals without any additional wrapper | ||
return <>{Object.values(renderers)}</>; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice code 😂