Skip to content

Commit 764c6cd

Browse files
committed
Improved loading editor from CDN.
1 parent 14d4fc2 commit 764c6cd

File tree

3 files changed

+112
-34
lines changed

3 files changed

+112
-34
lines changed

admin/src/components/CKEditorInput/index.js

+13-12
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@ const { ClassicEditor } = window.CKEDITOR;
1414

1515
import sanitize from './utils/utils';
1616

17-
const CKEditorInput = ({
18-
attribute,
19-
onChange,
20-
name,
21-
value,
22-
disabled,
23-
labelAction,
24-
intlLabel,
25-
required,
26-
description,
27-
error
28-
}) => {
17+
const CKEditorInput = ( props ) => {
18+
const {
19+
attribute,
20+
onChange,
21+
name,
22+
value,
23+
disabled,
24+
labelAction,
25+
intlLabel,
26+
required,
27+
description,
28+
error
29+
} = props;
2930
const [ editorInstance, setEditorInstance ] = useState(false);
3031
const { formatMessage } = useIntl();
3132
const { maxLengthCharacters:maxLength , ...options } = attribute.options;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { useState, useEffect } from 'react';
2+
3+
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+
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+
return () => {
61+
const CDNScript = document.querySelector( '#ckeditor5-cdn-script' );
62+
63+
if ( CDNScript ) {
64+
CDNScript.remove();
65+
}
66+
67+
window.CKEditorCDNLoaded = false;
68+
}
69+
}, [] );
70+
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' );
90+
91+
script.src = "https://cdn.ckeditor.com/ckeditor5/43.0.0/ckeditor5.umd.js";
92+
script.async = true;
93+
script.id = 'ckeditor5-cdn-script'
94+
95+
document.body.appendChild( script );
96+
}
97+
98+
export default CKEditorProvider;

admin/src/index.js

+1-22
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ const IconBox = styled( Flex )`
1515
}
1616
`;
1717

18-
// Inject CKEditor 5 and stylesheet from CDN
19-
injectAssetsFromCDN();
20-
2118
export default {
2219
register( app ) {
2320
app.customFields.register( {
@@ -40,7 +37,7 @@ export default {
4037
defaultMessage: 'The rich text editor for every use case'
4138
},
4239
components: {
43-
Input: async () => import( './components/CKEditorInput' ),
40+
Input: async () => import( './components/CKEditorProvider' ),
4441
},
4542
options: {
4643
base: [
@@ -186,21 +183,3 @@ export default {
186183
return Promise.resolve( importedTrads );
187184
},
188185
};
189-
190-
function injectAssetsFromCDN() {
191-
if ( !document.querySelector( '#ckeditor5-cdn-script' ) ) {
192-
const script = document.createElement( 'script' );
193-
const link = document.createElement( 'link' );
194-
195-
script.src = "https://cdn.ckeditor.com/ckeditor5/43.0.0/ckeditor5.umd.js";
196-
script.async = true;
197-
script.id = 'ckeditor5-cdn-script'
198-
199-
link.rel = 'stylesheet';
200-
link.href = 'https://cdn.ckeditor.com/ckeditor5/43.0.0/ckeditor5.css';
201-
link.id = 'ckeditor5-cdn-styles';
202-
203-
document.body.appendChild( script );
204-
document.head.appendChild( link );
205-
}
206-
}

0 commit comments

Comments
 (0)