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

Use ref to move contentDOM #1960

Merged
merged 1 commit into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions packages/react/src/NodeViewContent.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import React, { useEffect } from 'react'
import React from 'react'
import { useReactNodeView } from './useReactNodeView'

export interface NodeViewContentProps {
[key: string]: any,
as?: React.ElementType,
}

export const NodeViewContent: React.FC<NodeViewContentProps> = React.forwardRef((props, ref) => {
export const NodeViewContent: React.FC<NodeViewContentProps> = props => {
const Tag = props.as || 'div'
const { maybeMoveContentDOM } = useReactNodeView()

useEffect(() => {
maybeMoveContentDOM?.()
}, [])
const { nodeViewContentRef } = useReactNodeView()

return (
<Tag
{...props}
ref={ref}
ref={nodeViewContentRef}
data-node-view-content=""
style={{
...props.style,
whiteSpace: 'pre-wrap',
}}
/>
)
})
}
31 changes: 12 additions & 19 deletions packages/react/src/ReactNodeViewRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Decoration, NodeView as ProseMirrorNodeView } from 'prosemirror-view'
import { Node as ProseMirrorNode } from 'prosemirror-model'
import { Editor } from './Editor'
import { ReactRenderer } from './ReactRenderer'
import { ReactNodeViewContext } from './useReactNodeView'
import { ReactNodeViewContext, ReactNodeViewContextProps } from './useReactNodeView'

export interface ReactNodeViewRendererOptions extends NodeViewRendererOptions {
update: ((props: {
Expand Down Expand Up @@ -49,12 +49,20 @@ class ReactNodeView extends NodeView<React.FunctionComponent, Editor, ReactNodeV
}

const ReactNodeViewProvider: React.FunctionComponent = componentProps => {
const onDragStart = this.onDragStart.bind(this)
const maybeMoveContentDOM = this.maybeMoveContentDOM.bind(this)
const Component = this.component
const onDragStart = this.onDragStart.bind(this)
const nodeViewContentRef: ReactNodeViewContextProps['nodeViewContentRef'] = element => {
if (
element
&& this.contentDOMElement
&& element.firstChild !== this.contentDOMElement
) {
element.appendChild(this.contentDOMElement)
}
}

return (
<ReactNodeViewContext.Provider value={{ onDragStart, maybeMoveContentDOM }}>
<ReactNodeViewContext.Provider value={{ onDragStart, nodeViewContentRef }}>
<Component {...componentProps} />
</ReactNodeViewContext.Provider>
)
Expand Down Expand Up @@ -98,27 +106,12 @@ class ReactNodeView extends NodeView<React.FunctionComponent, Editor, ReactNodeV
return null
}

this.maybeMoveContentDOM()

return this.contentDOMElement
}

maybeMoveContentDOM(): void {
const contentElement = this.dom.querySelector('[data-node-view-content]')

if (
this.contentDOMElement
&& contentElement
&& !contentElement.contains(this.contentDOMElement)
) {
contentElement.appendChild(this.contentDOMElement)
}
}

update(node: ProseMirrorNode, decorations: Decoration[]) {
const updateProps = (props?: Record<string, any>) => {
this.renderer.updateProps(props)
this.maybeMoveContentDOM()
}

if (typeof this.options.update === 'function') {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/useReactNodeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createContext, useContext } from 'react'

export interface ReactNodeViewContextProps {
onDragStart: (event: DragEvent) => void,
maybeMoveContentDOM: () => void,
nodeViewContentRef: (element: HTMLElement | null) => void,
}

export const ReactNodeViewContext = createContext<Partial<ReactNodeViewContextProps>>({
Expand Down