Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Introduced DataController#init method.
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarwrobel committed Mar 9, 2018
1 parent 24fab6e commit 0c06f31
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 9 deletions.
34 changes: 28 additions & 6 deletions src/controller/datacontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import mix from '@ckeditor/ckeditor5-utils/src/mix';
import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';

import Mapper from '../conversion/mapper';

Expand Down Expand Up @@ -100,7 +101,7 @@ export default class DataController {
this.upcastDispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
this.upcastDispatcher.on( 'documentFragment', convertToModelFragment(), { priority: 'lowest' } );

this.decorate( 'set' );
this.decorate( 'init' );
}

/**
Expand Down Expand Up @@ -171,18 +172,39 @@ export default class DataController {
return viewDocumentFragment;
}

/**
* Sets initial input data parsed by the {@link #processor data processor} and
* converted by the {@link #upcastDispatcher view-to-model converters}.
* Initial data can be set only to document that {@link module:engine/model/document~Document#version} is equal 0.
*
* **Note** This method is {@link module:utils/observablemixin~ObservableMixin#decorate decorated} which is
* used by e.g. collaborative editing plugin that syncs remote data on init.
*
* @fires set
* @param {String} data Input data.
* @param {String} [rootName='main'] Root name.
*/
init( data, rootName = 'main' ) {
if ( this.model.document.version ) {
throw new CKEditorError( 'datacontroller-init-document-data-initialized: Trying to set initial data to initialized document.' );
}

const modelRoot = this.model.document.getRoot( rootName );

this.model.enqueueChange( 'transparent', writer => {
writer.insert( this.parse( data, modelRoot ), modelRoot );
} );
}

/**
* Sets input data parsed by the {@link #processor data processor} and
* converted by the {@link #upcastDispatcher view-to-model converters}.
* This method can be used any time to replace existing editor data by the new one without clearing the
* {@link module:engine/model/document~Document#history document history}.
*
* This method also creates a batch with all the changes applied. If all you need is to parse data, use
* the {@link #parse} method.
*
* **Note** This method is {@link module:utils/observablemixin~ObservableMixin#decorate decorated} which is
* used by some plugins to change the behavior of this method. For example, the collaborative editing plugin changes
* this method’s nature to asynchronous by returning a promise.
*
* @fires set
* @param {String} data Input data.
* @param {String} [rootName='main'] Root name.
*/
Expand Down
52 changes: 49 additions & 3 deletions tests/controller/datacontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Model from '../../src/model/model';
import Range from '../../src/model/range';
import DataController from '../../src/controller/datacontroller';
import HtmlDataProcessor from '../../src/dataprocessor/htmldataprocessor';
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';

import ModelDocumentFragment from '../../src/model/documentfragment';
import ViewDocumentFragment from '../../src/view/documentfragment';
Expand Down Expand Up @@ -152,17 +153,62 @@ describe( 'DataController', () => {
} );
} );

describe( 'set()', () => {
describe( 'init()', () => {
it( 'should be decorated', () => {
const spy = sinon.spy();

data.on( 'set', spy );
data.on( 'init', spy );

data.set( 'foo bar' );
data.init( 'foo bar' );

sinon.assert.calledWithExactly( spy, sinon.match.any, [ 'foo bar' ] );
} );

it( 'should throw an error when document data is already initialized', () => {
data.init( '<p>Foo</p>' );

expect( () => {
data.init( '<p>Bar</p>' );
} ).to.throw(
CKEditorError,
'datacontroller-init-document-data-initialized: Trying to set initial data to initialized document.'
);
} );

it( 'should set data to default main root', () => {
schema.extend( '$text', { allowIn: '$root' } );
data.init( 'foo' );

expect( getData( model, { withoutSelection: true } ) ).to.equal( 'foo' );
} );

it( 'should get root name as a parameter', () => {
schema.extend( '$text', { allowIn: '$root' } );
data.init( 'foo', 'title' );

expect( getData( model, { withoutSelection: true, rootName: 'title' } ) ).to.equal( 'foo' );
} );

it( 'should create a batch', () => {
schema.extend( '$text', { allowIn: '$root' } );
data.init( 'foo' );

expect( count( modelDocument.history.getDeltas() ) ).to.equal( 1 );
} );

it( 'should cause firing change event', () => {
const spy = sinon.spy();

schema.extend( '$text', { allowIn: '$root' } );
model.document.on( 'change', spy );

data.init( 'foo' );

expect( spy.calledOnce ).to.be.true;
} );
} );

describe( 'set()', () => {
it( 'should set data to default main root', () => {
schema.extend( '$text', { allowIn: '$root' } );
data.set( 'foo' );
Expand Down

0 comments on commit 0c06f31

Please sign in to comment.