@tiptap/react 2.0.0-beta.2 → 2.0.0-beta.200

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/README.md +2 -2
  2. package/dist/packages/react/src/BubbleMenu.d.ts +9 -0
  3. package/dist/packages/react/src/Editor.d.ts +3 -2
  4. package/dist/packages/react/src/EditorContent.d.ts +11 -5
  5. package/dist/packages/react/src/FloatingMenu.d.ts +9 -0
  6. package/dist/packages/react/src/NodeViewContent.d.ts +6 -0
  7. package/dist/packages/react/src/NodeViewWrapper.d.ts +6 -0
  8. package/dist/packages/react/src/ReactNodeViewRenderer.d.ts +15 -0
  9. package/dist/packages/react/src/ReactRenderer.d.ts +24 -0
  10. package/dist/packages/react/src/index.d.ts +8 -2
  11. package/dist/packages/react/src/useEditor.d.ts +2 -1
  12. package/dist/packages/react/src/useReactNodeView.d.ts +7 -0
  13. package/dist/tiptap-react.cjs.js +346 -32
  14. package/dist/tiptap-react.cjs.js.map +1 -1
  15. package/dist/tiptap-react.esm.js +337 -28
  16. package/dist/tiptap-react.esm.js.map +1 -1
  17. package/dist/tiptap-react.umd.js +347 -36
  18. package/dist/tiptap-react.umd.js.map +1 -1
  19. package/package.json +18 -9
  20. package/src/BubbleMenu.tsx +52 -0
  21. package/src/Editor.ts +4 -2
  22. package/src/EditorContent.tsx +75 -12
  23. package/src/FloatingMenu.tsx +52 -0
  24. package/src/NodeViewContent.tsx +25 -0
  25. package/src/NodeViewWrapper.tsx +26 -0
  26. package/src/ReactNodeViewRenderer.tsx +192 -0
  27. package/src/ReactRenderer.tsx +119 -0
  28. package/src/index.ts +8 -5
  29. package/src/useEditor.ts +16 -4
  30. package/src/useReactNodeView.ts +12 -0
  31. package/CHANGELOG.md +0 -24
  32. package/LICENSE.md +0 -21
  33. package/dist/tiptap-react.bundle.umd.min.js +0 -15
  34. package/dist/tiptap-react.bundle.umd.min.js.map +0 -1
  35. package/src/ReactNodeViewRenderer.ts +0 -208
  36. package/src/ReactRenderer.ts +0 -58
@@ -1,11 +1,32 @@
1
- import React from 'react'
1
+ import React, { HTMLProps } from 'react'
2
+ import ReactDOM from 'react-dom'
3
+
2
4
  import { Editor } from './Editor'
5
+ import { ReactRenderer } from './ReactRenderer'
6
+
7
+ const Portals: React.FC<{ renderers: Map<string, ReactRenderer> }> = ({ renderers }) => {
8
+ return (
9
+ <>
10
+ {Array.from(renderers).map(([key, renderer]) => {
11
+ return ReactDOM.createPortal(
12
+ renderer.reactElement,
13
+ renderer.element,
14
+ key,
15
+ )
16
+ })}
17
+ </>
18
+ )
19
+ }
3
20
 
4
- type EditorContentProps = {
5
- editor: Editor | null
21
+ export interface EditorContentProps extends HTMLProps<HTMLDivElement> {
22
+ editor: Editor | null,
6
23
  }
7
24
 
8
- export class PureEditorContent extends React.Component<EditorContentProps, EditorContentProps> {
25
+ export interface EditorContentState {
26
+ renderers: Map<string, ReactRenderer>
27
+ }
28
+
29
+ export class PureEditorContent extends React.Component<EditorContentProps, EditorContentState> {
9
30
  editorContentRef: React.RefObject<any>
10
31
 
11
32
  constructor(props: EditorContentProps) {
@@ -13,17 +34,29 @@ export class PureEditorContent extends React.Component<EditorContentProps, Edito
13
34
  this.editorContentRef = React.createRef()
14
35
 
15
36
  this.state = {
16
- editor: this.props.editor
37
+ renderers: new Map(),
17
38
  }
18
39
  }
19
40
 
41
+ componentDidMount() {
42
+ this.init()
43
+ }
44
+
20
45
  componentDidUpdate() {
46
+ this.init()
47
+ }
48
+
49
+ init() {
21
50
  const { editor } = this.props
22
51
 
23
52
  if (editor && editor.options.element) {
53
+ if (editor.contentComponent) {
54
+ return
55
+ }
56
+
24
57
  const element = this.editorContentRef.current
25
58
 
26
- element.appendChild(editor.options.element.firstChild)
59
+ element.append(...editor.options.element.childNodes)
27
60
 
28
61
  editor.setOptions({
29
62
  element,
@@ -31,18 +64,48 @@ export class PureEditorContent extends React.Component<EditorContentProps, Edito
31
64
 
32
65
  editor.contentComponent = this
33
66
 
34
- // TODO: why setTimeout?
35
- setTimeout(() => {
36
- editor.createNodeViews()
37
- }, 0)
67
+ editor.createNodeViews()
38
68
  }
39
69
  }
40
70
 
71
+ componentWillUnmount() {
72
+ const { editor } = this.props
73
+
74
+ if (!editor) {
75
+ return
76
+ }
77
+
78
+ if (!editor.isDestroyed) {
79
+ editor.view.setProps({
80
+ nodeViews: {},
81
+ })
82
+ }
83
+
84
+ editor.contentComponent = null
85
+
86
+ if (!editor.options.element.firstChild) {
87
+ return
88
+ }
89
+
90
+ const newElement = document.createElement('div')
91
+
92
+ newElement.append(...editor.options.element.childNodes)
93
+
94
+ editor.setOptions({
95
+ element: newElement,
96
+ })
97
+ }
98
+
41
99
  render() {
100
+ const { editor, ...rest } = this.props
101
+
42
102
  return (
43
- <div ref={this.editorContentRef} />
103
+ <>
104
+ <div ref={this.editorContentRef} {...rest} />
105
+ <Portals renderers={this.state.renderers} />
106
+ </>
44
107
  )
45
108
  }
46
109
  }
47
110
 
48
- export const EditorContent = React.memo(PureEditorContent);
111
+ export const EditorContent = React.memo(PureEditorContent)
@@ -0,0 +1,52 @@
1
+ import { FloatingMenuPlugin, FloatingMenuPluginProps } from '@tiptap/extension-floating-menu'
2
+ import React, {
3
+ useEffect, useState,
4
+ } from 'react'
5
+
6
+ type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>
7
+
8
+ export type FloatingMenuProps = Omit<Optional<FloatingMenuPluginProps, 'pluginKey'>, 'element'> & {
9
+ className?: string,
10
+ children: React.ReactNode
11
+ }
12
+
13
+ export const FloatingMenu = (props: FloatingMenuProps) => {
14
+ const [element, setElement] = useState<HTMLDivElement | null>(null)
15
+
16
+ useEffect(() => {
17
+ if (!element) {
18
+ return
19
+ }
20
+
21
+ if (props.editor.isDestroyed) {
22
+ return
23
+ }
24
+
25
+ const {
26
+ pluginKey = 'floatingMenu',
27
+ editor,
28
+ tippyOptions = {},
29
+ shouldShow = null,
30
+ } = props
31
+
32
+ const plugin = FloatingMenuPlugin({
33
+ pluginKey,
34
+ editor,
35
+ element,
36
+ tippyOptions,
37
+ shouldShow,
38
+ })
39
+
40
+ editor.registerPlugin(plugin)
41
+ return () => editor.unregisterPlugin(pluginKey)
42
+ }, [
43
+ props.editor,
44
+ element,
45
+ ])
46
+
47
+ return (
48
+ <div ref={setElement} className={props.className} style={{ visibility: 'hidden' }}>
49
+ {props.children}
50
+ </div>
51
+ )
52
+ }
@@ -0,0 +1,25 @@
1
+ import React from 'react'
2
+
3
+ import { useReactNodeView } from './useReactNodeView'
4
+
5
+ export interface NodeViewContentProps {
6
+ [key: string]: any,
7
+ as?: React.ElementType,
8
+ }
9
+
10
+ export const NodeViewContent: React.FC<NodeViewContentProps> = props => {
11
+ const Tag = props.as || 'div'
12
+ const { nodeViewContentRef } = useReactNodeView()
13
+
14
+ return (
15
+ <Tag
16
+ {...props}
17
+ ref={nodeViewContentRef}
18
+ data-node-view-content=""
19
+ style={{
20
+ whiteSpace: 'pre-wrap',
21
+ ...props.style,
22
+ }}
23
+ />
24
+ )
25
+ }
@@ -0,0 +1,26 @@
1
+ import React from 'react'
2
+
3
+ import { useReactNodeView } from './useReactNodeView'
4
+
5
+ export interface NodeViewWrapperProps {
6
+ [key: string]: any,
7
+ as?: React.ElementType,
8
+ }
9
+
10
+ export const NodeViewWrapper: React.FC<NodeViewWrapperProps> = React.forwardRef((props, ref) => {
11
+ const { onDragStart } = useReactNodeView()
12
+ const Tag = props.as || 'div'
13
+
14
+ return (
15
+ <Tag
16
+ {...props}
17
+ ref={ref}
18
+ data-node-view-wrapper=""
19
+ onDragStart={onDragStart}
20
+ style={{
21
+ whiteSpace: 'normal',
22
+ ...props.style,
23
+ }}
24
+ />
25
+ )
26
+ })
@@ -0,0 +1,192 @@
1
+ import {
2
+ NodeView,
3
+ NodeViewProps,
4
+ NodeViewRenderer,
5
+ NodeViewRendererOptions,
6
+ NodeViewRendererProps,
7
+ } from '@tiptap/core'
8
+ import { Node as ProseMirrorNode } from 'prosemirror-model'
9
+ import { Decoration, NodeView as ProseMirrorNodeView } from 'prosemirror-view'
10
+ import React from 'react'
11
+
12
+ import { Editor } from './Editor'
13
+ import { ReactRenderer } from './ReactRenderer'
14
+ import { ReactNodeViewContext, ReactNodeViewContextProps } from './useReactNodeView'
15
+
16
+ export interface ReactNodeViewRendererOptions extends NodeViewRendererOptions {
17
+ update:
18
+ | ((props: {
19
+ oldNode: ProseMirrorNode;
20
+ oldDecorations: Decoration[];
21
+ newNode: ProseMirrorNode;
22
+ newDecorations: Decoration[];
23
+ updateProps: () => void;
24
+ }) => boolean)
25
+ | null;
26
+ as?: string;
27
+ className?: string;
28
+ }
29
+
30
+ class ReactNodeView extends NodeView<
31
+ React.FunctionComponent,
32
+ Editor,
33
+ ReactNodeViewRendererOptions
34
+ > {
35
+ renderer!: ReactRenderer
36
+
37
+ contentDOMElement!: HTMLElement | null
38
+
39
+ mount() {
40
+ const props: NodeViewProps = {
41
+ editor: this.editor,
42
+ node: this.node,
43
+ decorations: this.decorations,
44
+ selected: false,
45
+ extension: this.extension,
46
+ getPos: () => this.getPos(),
47
+ updateAttributes: (attributes = {}) => this.updateAttributes(attributes),
48
+ deleteNode: () => this.deleteNode(),
49
+ }
50
+
51
+ if (!(this.component as any).displayName) {
52
+ const capitalizeFirstChar = (string: string): string => {
53
+ return string.charAt(0).toUpperCase() + string.substring(1)
54
+ }
55
+
56
+ this.component.displayName = capitalizeFirstChar(this.extension.name)
57
+ }
58
+
59
+ const ReactNodeViewProvider: React.FunctionComponent = componentProps => {
60
+ const Component = this.component
61
+ const onDragStart = this.onDragStart.bind(this)
62
+ const nodeViewContentRef: ReactNodeViewContextProps['nodeViewContentRef'] = element => {
63
+ if (element && this.contentDOMElement && element.firstChild !== this.contentDOMElement) {
64
+ element.appendChild(this.contentDOMElement)
65
+ }
66
+ }
67
+
68
+ return (
69
+ <ReactNodeViewContext.Provider value={{ onDragStart, nodeViewContentRef }}>
70
+ <Component {...componentProps} />
71
+ </ReactNodeViewContext.Provider>
72
+ )
73
+ }
74
+
75
+ ReactNodeViewProvider.displayName = 'ReactNodeView'
76
+
77
+ this.contentDOMElement = this.node.isLeaf
78
+ ? null
79
+ : document.createElement(this.node.isInline ? 'span' : 'div')
80
+
81
+ if (this.contentDOMElement) {
82
+ // For some reason the whiteSpace prop is not inherited properly in Chrome and Safari
83
+ // With this fix it seems to work fine
84
+ // See: https://github.com/ueberdosis/tiptap/issues/1197
85
+ this.contentDOMElement.style.whiteSpace = 'inherit'
86
+ }
87
+
88
+ let as = this.node.isInline ? 'span' : 'div'
89
+
90
+ if (this.options.as) {
91
+ as = this.options.as
92
+ }
93
+
94
+ const { className = '' } = this.options
95
+
96
+ this.renderer = new ReactRenderer(ReactNodeViewProvider, {
97
+ editor: this.editor,
98
+ props,
99
+ as,
100
+ className: `node-${this.node.type.name} ${className}`.trim(),
101
+ })
102
+ }
103
+
104
+ get dom() {
105
+ if (
106
+ this.renderer.element.firstElementChild
107
+ && !this.renderer.element.firstElementChild?.hasAttribute('data-node-view-wrapper')
108
+ ) {
109
+ throw Error('Please use the NodeViewWrapper component for your node view.')
110
+ }
111
+
112
+ return this.renderer.element as HTMLElement
113
+ }
114
+
115
+ get contentDOM() {
116
+ if (this.node.isLeaf) {
117
+ return null
118
+ }
119
+
120
+ return this.contentDOMElement
121
+ }
122
+
123
+ update(node: ProseMirrorNode, decorations: Decoration[]) {
124
+ const updateProps = (props?: Record<string, any>) => {
125
+ this.renderer.updateProps(props)
126
+ }
127
+
128
+ if (node.type !== this.node.type) {
129
+ return false
130
+ }
131
+
132
+ if (typeof this.options.update === 'function') {
133
+ const oldNode = this.node
134
+ const oldDecorations = this.decorations
135
+
136
+ this.node = node
137
+ this.decorations = decorations
138
+
139
+ return this.options.update({
140
+ oldNode,
141
+ oldDecorations,
142
+ newNode: node,
143
+ newDecorations: decorations,
144
+ updateProps: () => updateProps({ node, decorations }),
145
+ })
146
+ }
147
+
148
+ if (node === this.node && this.decorations === decorations) {
149
+ return true
150
+ }
151
+
152
+ this.node = node
153
+ this.decorations = decorations
154
+
155
+ updateProps({ node, decorations })
156
+
157
+ return true
158
+ }
159
+
160
+ selectNode() {
161
+ this.renderer.updateProps({
162
+ selected: true,
163
+ })
164
+ }
165
+
166
+ deselectNode() {
167
+ this.renderer.updateProps({
168
+ selected: false,
169
+ })
170
+ }
171
+
172
+ destroy() {
173
+ this.renderer.destroy()
174
+ this.contentDOMElement = null
175
+ }
176
+ }
177
+
178
+ export function ReactNodeViewRenderer(
179
+ component: any,
180
+ options?: Partial<ReactNodeViewRendererOptions>,
181
+ ): NodeViewRenderer {
182
+ return (props: NodeViewRendererProps) => {
183
+ // try to get the parent component
184
+ // this is important for vue devtools to show the component hierarchy correctly
185
+ // maybe it’s `undefined` because <editor-content> isn’t rendered yet
186
+ if (!(props.editor as Editor).contentComponent) {
187
+ return {}
188
+ }
189
+
190
+ return new ReactNodeView(component, props, options) as unknown as ProseMirrorNodeView
191
+ }
192
+ }
@@ -0,0 +1,119 @@
1
+ import { Editor } from '@tiptap/core'
2
+ import React from 'react'
3
+ import { flushSync } from 'react-dom'
4
+
5
+ import { Editor as ExtendedEditor } from './Editor'
6
+
7
+ function isClassComponent(Component: any) {
8
+ return !!(
9
+ typeof Component === 'function'
10
+ && Component.prototype
11
+ && Component.prototype.isReactComponent
12
+ )
13
+ }
14
+
15
+ function isForwardRefComponent(Component: any) {
16
+ return !!(
17
+ typeof Component === 'object'
18
+ && Component.$$typeof?.toString() === 'Symbol(react.forward_ref)'
19
+ )
20
+ }
21
+
22
+ export interface ReactRendererOptions {
23
+ editor: Editor,
24
+ props?: Record<string, any>,
25
+ as?: string,
26
+ className?: string,
27
+ }
28
+
29
+ type ComponentType<R, P> =
30
+ React.ComponentClass<P> |
31
+ React.FunctionComponent<P> |
32
+ React.ForwardRefExoticComponent<React.PropsWithoutRef<P> & React.RefAttributes<R>>;
33
+
34
+ export class ReactRenderer<R = unknown, P = unknown> {
35
+ id: string
36
+
37
+ editor: ExtendedEditor
38
+
39
+ component: any
40
+
41
+ element: Element
42
+
43
+ props: Record<string, any>
44
+
45
+ reactElement: React.ReactNode
46
+
47
+ ref: R | null = null
48
+
49
+ constructor(component: ComponentType<R, P>, {
50
+ editor,
51
+ props = {},
52
+ as = 'div',
53
+ className = '',
54
+ }: ReactRendererOptions) {
55
+ this.id = Math.floor(Math.random() * 0xFFFFFFFF).toString()
56
+ this.component = component
57
+ this.editor = editor as ExtendedEditor
58
+ this.props = props
59
+ this.element = document.createElement(as)
60
+ this.element.classList.add('react-renderer')
61
+
62
+ if (className) {
63
+ this.element.classList.add(...className.split(' '))
64
+ }
65
+
66
+ this.render()
67
+ }
68
+
69
+ render(): void {
70
+ const Component = this.component
71
+ const props = this.props
72
+
73
+ if (isClassComponent(Component) || isForwardRefComponent(Component)) {
74
+ props.ref = (ref: R) => {
75
+ this.ref = ref
76
+ }
77
+ }
78
+
79
+ this.reactElement = <Component {...props } />
80
+
81
+ queueMicrotask(() => {
82
+ flushSync(() => {
83
+ if (this.editor?.contentComponent) {
84
+ this.editor.contentComponent.setState({
85
+ renderers: this.editor.contentComponent.state.renderers.set(
86
+ this.id,
87
+ this,
88
+ ),
89
+ })
90
+ }
91
+ })
92
+ })
93
+ }
94
+
95
+ updateProps(props: Record<string, any> = {}): void {
96
+ this.props = {
97
+ ...this.props,
98
+ ...props,
99
+ }
100
+
101
+ this.render()
102
+ }
103
+
104
+ destroy(): void {
105
+ queueMicrotask(() => {
106
+ flushSync(() => {
107
+ if (this.editor?.contentComponent) {
108
+ const { renderers } = this.editor.contentComponent.state
109
+
110
+ renderers.delete(this.id)
111
+
112
+ this.editor.contentComponent.setState({
113
+ renderers,
114
+ })
115
+ }
116
+ })
117
+ })
118
+ }
119
+ }
package/src/index.ts CHANGED
@@ -1,7 +1,10 @@
1
- // @ts-nocheck
2
- export * from '@tiptap/core'
1
+ export * from './BubbleMenu'
3
2
  export { Editor } from './Editor'
4
- export * from './useEditor'
5
- // export * from './ReactRenderer'
6
- // export * from './ReactNodeViewRenderer'
7
3
  export * from './EditorContent'
4
+ export * from './FloatingMenu'
5
+ export * from './NodeViewContent'
6
+ export * from './NodeViewWrapper'
7
+ export * from './ReactNodeViewRenderer'
8
+ export * from './ReactRenderer'
9
+ export * from './useEditor'
10
+ export * from '@tiptap/core'
package/src/useEditor.ts CHANGED
@@ -1,5 +1,6 @@
1
- import { useState, useEffect } from 'react'
2
1
  import { EditorOptions } from '@tiptap/core'
2
+ import { DependencyList, useEffect, useState } from 'react'
3
+
3
4
  import { Editor } from './Editor'
4
5
 
5
6
  function useForceUpdate() {
@@ -8,21 +9,32 @@ function useForceUpdate() {
8
9
  return () => setValue(value => value + 1)
9
10
  }
10
11
 
11
- export const useEditor = (options: Partial<EditorOptions> = {}) => {
12
+ export const useEditor = (options: Partial<EditorOptions> = {}, deps: DependencyList = []) => {
12
13
  const [editor, setEditor] = useState<Editor | null>(null)
13
14
  const forceUpdate = useForceUpdate()
14
15
 
15
16
  useEffect(() => {
17
+ let isMounted = true
18
+
16
19
  const instance = new Editor(options)
17
20
 
18
21
  setEditor(instance)
19
22
 
20
- instance.on('transaction', forceUpdate)
23
+ instance.on('transaction', () => {
24
+ requestAnimationFrame(() => {
25
+ requestAnimationFrame(() => {
26
+ if (isMounted) {
27
+ forceUpdate()
28
+ }
29
+ })
30
+ })
31
+ })
21
32
 
22
33
  return () => {
23
34
  instance.destroy()
35
+ isMounted = false
24
36
  }
25
- }, [])
37
+ }, deps)
26
38
 
27
39
  return editor
28
40
  }
@@ -0,0 +1,12 @@
1
+ import { createContext, useContext } from 'react'
2
+
3
+ export interface ReactNodeViewContextProps {
4
+ onDragStart: (event: DragEvent) => void,
5
+ nodeViewContentRef: (element: HTMLElement | null) => void,
6
+ }
7
+
8
+ export const ReactNodeViewContext = createContext<Partial<ReactNodeViewContextProps>>({
9
+ onDragStart: undefined,
10
+ })
11
+
12
+ export const useReactNodeView = () => useContext(ReactNodeViewContext)
package/CHANGELOG.md DELETED
@@ -1,24 +0,0 @@
1
- # Change Log
2
-
3
- All notable changes to this project will be documented in this file.
4
- See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
-
6
- # [2.0.0-beta.2](https://github.com/ueberdosis/tiptap-next/compare/@tiptap/react@2.0.0-beta.1...@tiptap/react@2.0.0-beta.2) (2021-03-09)
7
-
8
- **Note:** Version bump only for package @tiptap/react
9
-
10
-
11
-
12
-
13
-
14
- # [2.0.0-beta.1](https://github.com/ueberdosis/tiptap-next/compare/@tiptap/react@2.0.0-alpha.2...@tiptap/react@2.0.0-beta.1) (2021-03-05)
15
-
16
- **Note:** Version bump only for package @tiptap/react
17
-
18
-
19
-
20
-
21
-
22
- # 2.0.0-alpha.2 (2021-02-26)
23
-
24
- **Note:** Version bump only for package @tiptap/react
package/LICENSE.md DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2020, überdosis GbR
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.