Skip to content

Commit

Permalink
Fix required prop in CKEditor - editor to work with IDE.
Browse files Browse the repository at this point in the history
  • Loading branch information
martnpaneq committed Mar 28, 2023
1 parent 9d8a8de commit 0797efd
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion demo-react-16/package.json
Expand Up @@ -9,7 +9,7 @@
"preview": "vite preview"
},
"dependencies": {
"@ckeditor/ckeditor5-react": "link:..",
"@ckeditor/ckeditor5-react": "file:..",
"@ckeditor/ckeditor5-build-classic": "37.0.0-alpha.3",
"react": "^16.13.1",
"react-dom": "^16.13.1"
Expand Down
19 changes: 10 additions & 9 deletions demo-react-16/yarn.lock
Expand Up @@ -407,9 +407,10 @@
dependencies:
ckeditor5 "^37.0.0-alpha.3"

"@ckeditor/ckeditor5-react@link:..":
version "0.0.0"
uid ""
"@ckeditor/ckeditor5-react@file:..":
version "5.1.0"
dependencies:
prop-types "^15.7.2"

"@ckeditor/ckeditor5-select-all@^37.0.0-alpha.3":
version "37.0.0-alpha.3"
Expand Down Expand Up @@ -655,9 +656,9 @@
"@types/react" "^16"

"@types/react@^16", "@types/react@^16.14.35":
version "16.14.35"
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.14.35.tgz#9d3cf047d85aca8006c4776693124a5be90ee429"
integrity sha512-NUEiwmSS1XXtmBcsm1NyRRPYjoZF2YTE89/5QiLt5mlGffYK9FQqOKuOLuXNrjPQV04oQgaZG+Yq02ZfHoFyyg==
version "16.14.36"
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.14.36.tgz#05d6239d2e61f9e81e9e87596e6f69a99765f199"
integrity sha512-tVq2ZJ5ZYoBYdRX4v18Ki4VCMPmy4dXC3eYMmHkApa1+4wgnpd2tQS4Dy7q7qBf9jMXOICnZXEE6ttvYIgNIjg==
dependencies:
"@types/prop-types" "*"
"@types/scheduler" "*"
Expand Down Expand Up @@ -759,9 +760,9 @@ debug@^4.1.0:
ms "2.1.2"

electron-to-chromium@^1.4.284:
version "1.4.340"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.340.tgz#3a6d7414c1fc2dbf84b6f7af3ec24270606c85b8"
integrity sha512-zx8hqumOqltKsv/MF50yvdAlPF9S/4PXbyfzJS6ZGhbddGkRegdwImmfSVqCkEziYzrIGZ/TlrzBND4FysfkDg==
version "1.4.341"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.341.tgz#ab31e9e57ef7758a14c7a7977a1978d599514470"
integrity sha512-R4A8VfUBQY9WmAhuqY5tjHRf5fH2AAf6vqitBOE0y6u2PgHgqHSrhZmu78dIX3fVZtjqlwJNX1i2zwC3VpHtQQ==

esbuild@^0.17.5:
version "0.17.14"
Expand Down
2 changes: 1 addition & 1 deletion demo-react-18/package.json
Expand Up @@ -9,7 +9,7 @@
"preview": "vite preview"
},
"dependencies": {
"@ckeditor/ckeditor5-react": "link:..",
"@ckeditor/ckeditor5-react": "file:..",
"@ckeditor/ckeditor5-build-classic": "37.0.0-alpha.3"
},
"devDependencies": {
Expand Down
7 changes: 4 additions & 3 deletions demo-react-18/yarn.lock
Expand Up @@ -407,9 +407,10 @@
dependencies:
ckeditor5 "^37.0.0-alpha.3"

"@ckeditor/ckeditor5-react@link:..":
version "0.0.0"
uid ""
"@ckeditor/ckeditor5-react@file:..":
version "5.1.0"
dependencies:
prop-types "^15.7.2"

"@ckeditor/ckeditor5-select-all@^37.0.0-alpha.3":
version "37.0.0-alpha.3"
Expand Down
24 changes: 11 additions & 13 deletions src/ckeditor.tsx
Expand Up @@ -132,6 +132,8 @@ export default class CKEditor<TEditor extends Editor> extends React.Component<Pr
* Initializes the editor by creating a proper watchdog and initializing it with the editor's configuration.
*/
private async _initializeEditor(): Promise<unknown> {
const onError = this.props.onError || ( ( error, details ) => console.error( error, details ) );

await this.editorDestructionInProgress;

/* istanbul ignore next */
Expand All @@ -148,11 +150,11 @@ export default class CKEditor<TEditor extends Editor> extends React.Component<Pr
this.watchdog.setCreator( ( el, config ) => this._createEditor( el, config ) );

this.watchdog.on( 'error', ( _, { error, causesRestart } ) => {
this.props.onError( error, { phase: 'runtime', willEditorRestart: causesRestart } );
onError( error, { phase: 'runtime', willEditorRestart: causesRestart } );
} );

await this.watchdog.create( this.domContainer.current!, this._getConfig() )
.catch( error => this.props.onError( error, { phase: 'initialization', willEditorRestart: false } ) );
.catch( error => onError( error, { phase: 'initialization', willEditorRestart: false } ) );
}

/**
Expand Down Expand Up @@ -261,7 +263,9 @@ export default class CKEditor<TEditor extends Editor> extends React.Component<Pr
* Returns the editor configuration.
*/
private _getConfig(): EditorConfig {
if ( this.props.data && this.props.config.initialData ) {
const config = this.props.config || {};

if ( this.props.data && config.initialData ) {
console.warn(
'Editor data should be provided either using `config.initialData` or `data` properties. ' +
'The config property is over the data value and the first one will be used when specified both.'
Expand All @@ -270,8 +274,8 @@ export default class CKEditor<TEditor extends Editor> extends React.Component<Pr

// Merge two possible ways of providing data into the `config.initialData` field.
return {
...this.props.config,
initialData: this.props.config.initialData || this.props.data || ''
...config,
initialData: config.initialData || this.props.data || ''
};
}

Expand Down Expand Up @@ -300,12 +304,6 @@ export default class CKEditor<TEditor extends Editor> extends React.Component<Pr
}
};

// Default values for non-required properties.
public static defaultProps: Partial<Props<Editor>> = {
config: {},
onError: ( error, details ) => console.error( error, details )
};

// Store the API in the static property to easily overwrite it in tests.
// Too bad dependency injection does not work in Webpack + ES 6 (const) + Babel.
public static _EditorWatchdog = EditorWatchdog;
Expand All @@ -316,10 +314,10 @@ export default class CKEditor<TEditor extends Editor> extends React.Component<Pr
*/
interface Props<TEditor extends Editor> extends InferProps<typeof CKEditor.propTypes> {
editor: { create( ...args: any ): Promise<TEditor> };
config: EditorConfig;
config?: EditorConfig;
watchdogConfig?: WatchdogConfig;
onReady?: ( editor: TEditor ) => void;
onError: ( error: Error, details: ErrorDetails ) => void;
onError?: ( error: Error, details: ErrorDetails ) => void;
onChange?: ( event: EventInfo, editor: TEditor ) => void;
onFocus?: ( event: EventInfo, editor: TEditor ) => void;
onBlur?: ( event: EventInfo, editor: TEditor ) => void;
Expand Down

0 comments on commit 0797efd

Please sign in to comment.