Skip to content

Commit

Permalink
Feature: Implemented CKEditorInspector#attachToAll(). Closes #56.
Browse files Browse the repository at this point in the history
  • Loading branch information
oleq committed Jul 4, 2019
1 parent cb74731 commit 82db5a0
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
35 changes: 35 additions & 0 deletions src/ckeditorinspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,37 @@ export default class CKEditorInspector {
return Object.keys( editors );
}

/**
* Attaches the inspector to all CKEditor instances discovered in DOM.
*
* Editor instances are named `editor-1`, `editor-2`, etc..
*
* **Note:** This method requires CKEditor 12.3.0 or later.
*
* **Note:** You can pass global configuration options when attaching:
*
* CKEditorInspector.attachToAll( { option: 'value', ... } );
*
* @param {CKEditorInspectorConfig} [options] An object of global configuration options controlling the
* behavior of the inspector.
* @returns {Array.<String>} Names of the editors the inspector attached to. Useful when using `CKEditorInspector.detach()`
* with generated editor names.
*/
static attachToAll( options ) {
const domEditables = document.querySelectorAll( '.ck.ck-content.ck-editor__editable' );
const attachedEditorNames = [];

for ( const domEditable of domEditables ) {
const editor = domEditable.ckeditorInstance;

if ( editor && !CKEditorInspector._isAttachedTo( editor ) ) {
attachedEditorNames.push( ...CKEditorInspector.attach( editor, options ) );
}
}

return attachedEditorNames;
}

/**
* Detaches the inspector from an editor instance.
*
Expand Down Expand Up @@ -138,6 +169,10 @@ export default class CKEditorInspector {
static get _isMounted() {
return !!CKEditorInspector._inspectorRef.current;
}

static _isAttachedTo( editor ) {
return [ ...CKEditorInspector._editors.values() ].indexOf( editor ) > -1;
}
}

CKEditorInspector._editors = new Map();
Expand Down
73 changes: 73 additions & 0 deletions tests/inspector/ckeditorinspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,79 @@ describe( 'CKEditorInspector', () => {
} );
} );

describe( '#attachToAll()', () => {
it( 'attaches to all editors', () => {
return TestEditor.create( element )
.then( anotherEditor => {
document.body.appendChild( editor.ui.view.element );
document.body.appendChild( anotherEditor.ui.view.element );

const editorNames = CKEditorInspector.attachToAll();

inspectorRef = CKEditorInspector._inspectorRef.current;

expect( inspectorRef.state.editors.size ).to.equal( 2 );
expect( inspectorRef.state.editors.get( 'editor-5' ) ).to.equal( editor );
expect( inspectorRef.state.editors.get( 'editor-6' ) ).to.equal( anotherEditor );
expect( editorNames ).to.have.members( [ 'editor-5', 'editor-6' ] );

return anotherEditor.destroy();
} )
.catch( err => {
throw err;
} );
} );

it( 'detects and prevents duplicates', () => {
return TestEditor.create( element )
.then( anotherEditor => {
document.body.appendChild( editor.ui.view.element );
document.body.appendChild( anotherEditor.ui.view.element );

CKEditorInspector.attachToAll();

inspectorRef = CKEditorInspector._inspectorRef.current;

expect( inspectorRef.state.editors.size ).to.equal( 2 );
expect( inspectorRef.state.editors.get( 'editor-7' ) ).to.equal( editor );
expect( inspectorRef.state.editors.get( 'editor-8' ) ).to.equal( anotherEditor );

CKEditorInspector.attachToAll();

inspectorRef = CKEditorInspector._inspectorRef.current;

expect( inspectorRef.state.editors.size ).to.equal( 2 );

return anotherEditor.destroy();
} )
.catch( err => {
throw err;
} );
} );

it( 'passes options to #attach()', () => {
return TestEditor.create( element )
.then( anotherEditor => {
document.body.appendChild( editor.ui.view.element );
document.body.appendChild( anotherEditor.ui.view.element );

const spy = sinon.spy( CKEditorInspector, 'attach' );
const options = { foo: true };
CKEditorInspector.attachToAll( options );

sinon.assert.calledWithExactly( spy.firstCall, editor, options );
sinon.assert.calledWithExactly( spy.secondCall, anotherEditor, options );

spy.restore();

return anotherEditor.destroy();
} )
.catch( err => {
throw err;
} );
} );
} );

describe( '#detach()', () => {
it( 'detaches an editor', () => {
CKEditorInspector.attach( { foo: editor } );
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/testeditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class ClassicTestEditorUI extends EditorUI {
view.render();
view.main.add( view.editable );

this._editableElements.set( 'main', view.editable.element );
this.setEditableElement( 'main', view.editable.element );
this._elementReplacer.replace( element, view.element );

this.editor.commands.add( 'foo', new FooCommand( this.editor ) );
Expand Down

0 comments on commit 82db5a0

Please sign in to comment.