Skip to content

Commit

Permalink
Revert fix for default props - failing test is blocking the fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
martnpaneq committed Mar 28, 2023
1 parent 0797efd commit ea77c72
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/ckeditor.tsx
Expand Up @@ -131,9 +131,7 @@ 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 ) );

private async _initializeEditor(): Promise<void> {
await this.editorDestructionInProgress;

/* istanbul ignore next */
Expand All @@ -150,11 +148,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 } ) => {
onError( error, { phase: 'runtime', willEditorRestart: causesRestart } );
this.props.onError( error, { phase: 'runtime', willEditorRestart: causesRestart } );
} );

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

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

if ( this.props.data && config.initialData ) {
if ( this.props.data && this.props.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 @@ -274,16 +270,16 @@ export default class CKEditor<TEditor extends Editor> extends React.Component<Pr

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

public static override contextType = ContextWatchdogContext;

// Properties definition.
public static propTypes = {
editor: PropTypes.func.isRequired as unknown as Validator<{ create( ...args: any ): Promise<any> }>,
editor: PropTypes.func.isRequired,
data: PropTypes.string,
config: PropTypes.object,
watchdogConfig: PropTypes.object,
Expand All @@ -304,20 +300,23 @@ 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;
}

/**
* TODO this is type space definition for props, the CKEditor.propTypes is a run-time props validation that should match.
*/
interface Props<TEditor extends Editor> extends InferProps<typeof CKEditor.propTypes> {
interface Props<TEditor extends Editor> extends Pick<InferProps<typeof CKEditor.propTypes>, 'data' | 'disabled' | 'id'> {
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 ea77c72

Please sign in to comment.