1
1
import { Fun , Optional } from '@ephox/katamari' ;
2
2
import { SugarElement , SugarNode } from '@ephox/sugar' ;
3
3
import * as React from 'react' ;
4
- import * as ReactDOM from 'react-dom' ;
5
4
import { Editor , IAllProps , IProps , Version } from '../../../main/ts/components/Editor' ;
6
5
import { Editor as TinyMCEEditor } from 'tinymce' ;
7
6
import { before , context } from '@ephox/bedrock-client' ;
8
7
import { VersionLoader } from '@tinymce/miniature' ;
8
+ import { createRoot } from 'react-dom/client' ;
9
9
10
10
// @ts -expect-error Remove when dispose polyfill is not needed
11
11
Symbol . dispose ??= Symbol ( 'Symbol.dispose' ) ;
@@ -33,6 +33,7 @@ export const render = async (props: Partial<IAllProps> = {}, container: HTMLElem
33
33
const originalInit = props . init || { } ;
34
34
const originalSetup = originalInit . setup || Fun . noop ;
35
35
const ref = React . createRef < Editor > ( ) ;
36
+ const root = createRoot ( container ) ;
36
37
37
38
const ctx = await new Promise < Context > ( ( resolve , reject ) => {
38
39
const init : IProps [ 'init' ] = {
@@ -43,18 +44,18 @@ export const render = async (props: Partial<IAllProps> = {}, container: HTMLElem
43
44
editor . on ( 'SkinLoaded' , ( ) => {
44
45
setTimeout ( ( ) => {
45
46
Optional . from ( ref . current )
46
- . map ( ReactDOM . findDOMNode )
47
- . bind ( Optional . from )
47
+ . map ( ( editorInstance ) => editorInstance . editor ?. targetElm as HTMLElement )
48
48
. map ( SugarElement . fromDom )
49
49
. filter ( SugarNode . isHTMLElement )
50
50
. map ( ( val ) => val . dom )
51
- . fold ( ( ) => reject ( 'Could not find DOMNode' ) , ( DOMNode ) => {
52
- resolve ( {
53
- ref,
54
- editor,
55
- DOMNode,
56
- } ) ;
57
- } ) ;
51
+ . fold ( ( ) =>
52
+ reject ( 'Could not find editor element' ) ,
53
+ ( DOMNode ) => resolve ( {
54
+ ref : ref as React . RefObject < Editor > ,
55
+ editor,
56
+ DOMNode
57
+ } )
58
+ ) ;
58
59
} , 0 ) ;
59
60
} ) ;
60
61
}
@@ -67,19 +68,20 @@ export const render = async (props: Partial<IAllProps> = {}, container: HTMLElem
67
68
* touch the nodes created by TinyMCE. Since this only seems to be an issue when rendering TinyMCE 4 directly
68
69
* into a root and a fix would be a breaking change, let's just wrap the editor in a <div> here for now.
69
70
*/
70
- ReactDOM . render ( < div > < Editor ref = { ref } apiKey = 'no-api-key' { ...props } init = { init } /> </ div > , container ) ;
71
+ root . render ( < div > < Editor ref = { ref } apiKey = 'no-api-key' { ...props } init = { init } /> </ div > ) ;
71
72
} ) ;
72
73
73
74
const remove = ( ) => {
74
- ReactDOM . unmountComponentAtNode ( container ) ;
75
+ root . unmount ( ) ;
75
76
} ;
76
77
77
78
return {
78
79
...ctx ,
79
80
/** By rendering the Editor into the same root, React will perform a diff and update. */
80
- reRender : ( newProps : IAllProps ) => new Promise < void > ( ( resolve ) =>
81
- ReactDOM . render ( < div > < Editor apiKey = 'no-api-key' ref = { ctx . ref } { ...newProps } /> </ div > , container , resolve )
82
- ) ,
81
+ reRender : ( newProps : IAllProps ) => new Promise < void > ( ( resolve ) => {
82
+ root . render ( < div > < Editor apiKey = 'no-api-key' ref = { ctx . ref } { ...newProps } /> </ div > ) ;
83
+ resolve ( ) ;
84
+ } ) ,
83
85
remove,
84
86
[ Symbol . dispose ] : remove
85
87
} ;
0 commit comments