refactoring

This commit is contained in:
Philipp Kühn
2021-03-15 09:00:44 +01:00
parent 385ee1d1f4
commit e765c8e981
10 changed files with 48 additions and 63 deletions

View File

@@ -1,6 +1,7 @@
import React from 'react'
import { Editor as CoreEditor } from '@tiptap/core'
import { EditorContentProps, EditorContentState } from './EditorContent'
export class Editor extends CoreEditor {
public contentComponent: React.Component | null = null
public contentComponent: React.Component<EditorContentProps, EditorContentState> | null = null
}

View File

@@ -2,11 +2,6 @@ import React from 'react'
import ReactDOM from 'react-dom'
import { Editor } from './Editor'
import { ReactRenderer } from './ReactRenderer'
import { ReactNodeViewContext } from './useReactNodeView'
type EditorContentProps = {
editor: Editor | null
}
const Portals: React.FC<{ renderers: Map<string, ReactRenderer> }> = ({ renderers }) => {
return (
@@ -22,7 +17,16 @@ const Portals: React.FC<{ renderers: Map<string, ReactRenderer> }> = ({ renderer
)
}
export class PureEditorContent extends React.Component<EditorContentProps, any> {
export interface EditorContentProps {
editor: Editor | null,
}
export interface EditorContentState {
editor: Editor | null,
renderers: Map<string, ReactRenderer>
}
export class PureEditorContent extends React.Component<EditorContentProps, EditorContentState> {
editorContentRef: React.RefObject<any>
constructor(props: EditorContentProps) {

View File

@@ -78,30 +78,23 @@ class ReactNodeView implements NodeView {
Component.displayName = capitalizeFirstChar(this.extension.config.name)
}
const ReactNodeView: React.FC = (props) => {
const ReactNodeView: React.FunctionComponent = (props) => {
const [isEditable, setIsEditable] = useState(this.editor.isEditable)
const handleEditableChange = () => {
const onDragStart = this.onDragStart.bind(this)
const onViewUpdate = () => {
setIsEditable(this.editor.isEditable)
}
const onDragStart = this.onDragStart.bind(this)
useEffect(() => {
this.editor.on('viewUpdate', handleEditableChange)
this.editor.on('viewUpdate', onViewUpdate)
return () => {
this.editor.off('viewUpdate', handleEditableChange)
this.editor.off('viewUpdate', onViewUpdate)
}
}, [])
return (
<ReactNodeViewContext.Provider
value={{
onDragStart,
isEditable,
}}
>
<ReactNodeViewContext.Provider value={{ onDragStart, isEditable }}>
<Component {...props} />
</ReactNodeViewContext.Provider>
)

View File

@@ -1,28 +1,19 @@
import React from 'react'
import { AnyObject } from '@tiptap/core'
import { Editor } from './Editor'
export interface ReactRendererOptions {
as?: string,
editor: Editor,
props?: { [key: string]: any },
}
function isFunctionalComponent(Component: any) {
return (
typeof Component === 'function' // can be various things
&& !(
Component.prototype // native arrows don't have prototypes
&& Component.prototype.isReactComponent // special property
)
);
}
function isClassComponent(Component: any) {
return !!(
typeof Component === 'function'
&& Component.prototype
&& Component.prototype.isReactComponent
);
)
}
export interface ReactRendererOptions {
as?: string,
editor: Editor,
props?: AnyObject,
}
export class ReactRenderer {
@@ -34,13 +25,13 @@ export class ReactRenderer {
element: Element
props: { [key: string]: any }
props: AnyObject
reactElement: React.ReactNode
ref: React.Component | null = null
constructor(component: any, { props = {}, editor }: ReactRendererOptions) {
constructor(component: React.Component | React.FunctionComponent, { props = {}, editor }: ReactRendererOptions) {
this.id = Math.floor(Math.random() * 0xFFFFFFFF).toString()
this.component = component
this.editor = editor
@@ -50,7 +41,7 @@ export class ReactRenderer {
this.render()
}
render() {
render(): void {
const Component = this.component
const props = this.props
@@ -62,8 +53,6 @@ export class ReactRenderer {
if (this.editor?.contentComponent) {
this.editor.contentComponent.setState({
// TODO
// @ts-ignore
renderers: this.editor.contentComponent.state.renderers.set(
this.id,
this,
@@ -72,7 +61,7 @@ export class ReactRenderer {
}
}
updateProps(props: { [key: string]: any } = {}) {
updateProps(props: AnyObject = {}): void {
this.props = {
...this.props,
...props,
@@ -81,10 +70,8 @@ export class ReactRenderer {
this.render()
}
destroy() {
destroy(): void {
if (this.editor?.contentComponent) {
// TODO
// @ts-ignore
const { renderers } = this.editor.contentComponent.state
renderers.delete(this.id)
@@ -94,5 +81,4 @@ export class ReactRenderer {
})
}
}
}

View File

@@ -1,11 +1,11 @@
import React, { useContext } from 'react'
import { createContext, useContext } from 'react'
export interface ReactNodeViewContextProps {
isEditable: boolean,
onDragStart: (event: DragEvent) => void,
}
export const ReactNodeViewContext = React.createContext<Partial<ReactNodeViewContextProps>>({
export const ReactNodeViewContext = createContext<Partial<ReactNodeViewContextProps>>({
isEditable: undefined,
onDragStart: undefined,
})