|
1 | | -import { useState, useEffect } from 'react'; |
| 1 | +import { memo, useEffect } from 'react'; |
| 2 | +import { useCKEditorCloud } from '@ckeditor/ckeditor5-react'; |
2 | 3 |
|
3 | 4 | const CKEditorProvider = ( { |
4 | | - attribute, |
5 | | - onChange, |
6 | | - name, |
7 | | - value, |
8 | | - disabled = false, |
9 | | - labelAction = null, |
10 | | - intlLabel, |
11 | | - required = false, |
12 | | - description = null, |
13 | | - error = null |
14 | | - } ) => { |
15 | | - const [ importedEditor, setImportedEditor ] = useState( null ); |
16 | | - |
| 5 | + attribute, |
| 6 | + onChange, |
| 7 | + name, |
| 8 | + value, |
| 9 | + disabled = false, |
| 10 | + labelAction = null, |
| 11 | + intlLabel, |
| 12 | + required = false, |
| 13 | + description = null, |
| 14 | + error = null } ) => { |
| 15 | + |
| 16 | + // Clean up CDN scripts after unmounting the component. |
17 | 17 | useEffect( () => { |
18 | | - const importEditor = async () => { |
19 | | - const module = await import( '../CKEditorInput' ); |
20 | | - const CKEditorInput = module.default; |
21 | | - |
22 | | - setImportedEditor(<CKEditorInput |
23 | | - attribute={ attribute } |
24 | | - onChange={ onChange } |
25 | | - name={ name } |
26 | | - value={ value } |
27 | | - disabled={ disabled } |
28 | | - labelAction={ labelAction } |
29 | | - required={ required } |
30 | | - description={ description } |
31 | | - error={ error } |
32 | | - intlLabel={ intlLabel } |
33 | | - /> ); |
34 | | - }; |
35 | | - |
36 | | - const injectAssetsFromCDN = setInterval( () => { |
37 | | - const CDNScript = document.querySelector( '#ckeditor5-cdn-script' ); |
38 | | - const CDNStyles = document.querySelector( '#ckeditor5-cdn-styles' ); |
39 | | - |
40 | | - if ( !CDNStyles ) { |
41 | | - _injectStylesFromCDN(); |
42 | | - } |
43 | | - |
44 | | - if ( window.CKEDITOR ) { |
45 | | - window.CKEditorCDNLoaded = true; |
46 | | - |
47 | | - importEditor(); |
48 | | - |
49 | | - clearInterval( injectAssetsFromCDN ); |
50 | | - |
51 | | - return; |
52 | | - } |
53 | | - |
54 | | - if ( !CDNScript ) { |
55 | | - _injectScriptFromCDN(); |
56 | | - |
57 | | - } |
58 | | - }, 100 ) |
59 | | - |
60 | 18 | return () => { |
61 | | - const CDNScript = document.querySelector( '#ckeditor5-cdn-script' ); |
| 19 | + const assets = document.querySelectorAll( '[data-injected-by="ckeditor-integration"]' ); |
62 | 20 |
|
63 | | - if ( CDNScript ) { |
64 | | - CDNScript.remove(); |
65 | | - } |
| 21 | + assets.forEach( asset => asset.remove() ); |
66 | 22 |
|
67 | | - window.CKEditorCDNLoaded = false; |
| 23 | + window.CKEDITOR_VERSION = null; |
68 | 24 | } |
69 | | - }, [] ); |
| 25 | + }, [] ) |
70 | 26 |
|
71 | | - return ( |
72 | | - <> |
73 | | - { window.CKEditorCDNLoaded && importedEditor } |
74 | | - </> |
75 | | - ) |
76 | | -} |
77 | | - |
78 | | -function _injectStylesFromCDN() { |
79 | | - const link = document.createElement( 'link' ); |
80 | | - |
81 | | - link.rel = 'stylesheet'; |
82 | | - link.href = 'https://cdn.ckeditor.com/ckeditor5/43.0.0/ckeditor5.css'; |
83 | | - link.id = 'ckeditor5-cdn-styles'; |
84 | | - |
85 | | - document.head.appendChild( link ); |
86 | | -} |
87 | | - |
88 | | -function _injectScriptFromCDN() { |
89 | | - const script = document.createElement( 'script' ); |
| 27 | + const cloud = useCKEditorCloud( { |
| 28 | + version: '44.0.0', |
| 29 | + plugins: { |
| 30 | + CKEditorInput: async () => ( await import('../CKEditorInput') ).CKEditorInput |
| 31 | + } |
| 32 | + } ); |
90 | 33 |
|
91 | | - script.src = "https://cdn.ckeditor.com/ckeditor5/43.0.0/ckeditor5.umd.js"; |
92 | | - script.async = true; |
93 | | - script.id = 'ckeditor5-cdn-script' |
| 34 | + if ( cloud.status !== 'success' ) { |
| 35 | + return null; |
| 36 | + } |
94 | 37 |
|
95 | | - document.body.appendChild( script ); |
| 38 | + return ( |
| 39 | + <cloud.loadedPlugins.CKEditorInput |
| 40 | + attribute={ attribute } |
| 41 | + onChange={ onChange } |
| 42 | + name={ name } |
| 43 | + value={ value } |
| 44 | + disabled={ disabled } |
| 45 | + labelAction={ labelAction } |
| 46 | + required={ required } |
| 47 | + description={ description } |
| 48 | + error={ error } |
| 49 | + intlLabel={ intlLabel } |
| 50 | + /> |
| 51 | + ) |
96 | 52 | } |
97 | 53 |
|
98 | | -export default CKEditorProvider; |
| 54 | +export default memo( CKEditorProvider ); |
0 commit comments