Skip to content

Commit

Permalink
Merge pull request #73 from ckeditor/t/72
Browse files Browse the repository at this point in the history
Fix: Handled element refs to be properly recognized by the Editor. Closes #72.
  • Loading branch information
pomek committed Mar 19, 2019
2 parents 650b9ca + 7a09982 commit a31de76
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/ckeditor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ckeditor.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -17,6 +17,7 @@
"ckeditor 5"
],
"dependencies": {
"lodash-es": "^4.17.11",
"prop-types": "^15.6.1"
},
"devDependencies": {
Expand Down
11 changes: 10 additions & 1 deletion src/ckeditor.jsx
Expand Up @@ -5,6 +5,7 @@

import React from 'react';
import PropTypes from 'prop-types';
import { cloneDeepWith, isPlainObject, isElement } from 'lodash-es';

export default class CKEditor extends React.Component {
constructor( props ) {
Expand Down Expand Up @@ -52,7 +53,7 @@ export default class CKEditor extends React.Component {

_initializeEditor() {
this.props.editor
.create( this.domContainer.current , this.props.config )
.create( this.domContainer.current , parseConfig( this.props.config ) )
.then( editor => {
this.editor = editor;

Expand Down Expand Up @@ -120,3 +121,11 @@ CKEditor.defaultProps = {
config: {}
};

function parseConfig( config ) {
// Replaces all DOM references created using React.createRef() with the "current" DOM node.
return cloneDeepWith( config, value => {
if ( isPlainObject( value ) && isElement( value.current ) && Object.keys( value ).length === 1 ) {
return value.current;
}
} );
}
60 changes: 60 additions & 0 deletions tests/ckeditor.jsx
Expand Up @@ -161,6 +161,66 @@ describe( 'CKEditor Component', () => {
expect( editorInstance.setData.called ).to.be.false;
} );

describe( '#config', () => {
it( 'should replace all react DOM references with the `current` DOM element', done => {
const spy = sinon.spy( Editor, 'create' );

const domElement = document.createElement( 'div' );
const domElementRef = React.createRef();
const similarToDomElementRef = {
current: domElement,
foo: 'bar'
};

const config = {
domElement,
domElementRef,
nested: {
domElementRef
},
similarToDomElementRef,
array: [
domElementRef, domElement, similarToDomElementRef
]
};

wrapper = mount(
<div>
<div ref={ domElementRef }></div>
<CKEditor editor={ Editor } config={ config } />
</div>
);

setTimeout( () => {
const parsedConfig = spy.lastCall.args[ 1 ];

// Not changed.
expect( parsedConfig.domElement ).to.equal( domElement ).to.instanceof( HTMLElement );

// Changed.
expect( parsedConfig.domElementRef ).to.equal( domElementRef.current ).to.instanceof( HTMLElement );

// Changed.
expect( parsedConfig.nested.domElementRef ).to.equal( domElementRef.current ).to.instanceof( HTMLElement );

// Not changed.
expect( parsedConfig.similarToDomElementRef ).to.deep.equal( similarToDomElementRef );
expect( parsedConfig.similarToDomElementRef.current ).to.equal( domElement );

// Changed.
expect( parsedConfig.array[ 0 ] ).to.equal( domElementRef.current ).to.instanceof( HTMLElement );

// Not changed.
expect( parsedConfig.array[ 1 ] ).to.equal( domElement );
expect( parsedConfig.array[ 2 ] ).to.deep.equal( similarToDomElementRef );
expect( parsedConfig.array[ 2 ].current ).to.equal( domElement );

spy.restore();
done();
} );
} );
} );

describe( '#onInit', () => {
it( 'calls "onInit" callback if specified when the editor is ready to use', done => {
const editorInstance = new Editor();
Expand Down

0 comments on commit a31de76

Please sign in to comment.