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

Commit

Permalink
Created a single DataController#viewDocument. Used the document in Da…
Browse files Browse the repository at this point in the history
…taController#toView.
  • Loading branch information
oleq committed Mar 12, 2020
1 parent 8fb1efa commit 4ab84aa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/controller/datacontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ export default class DataController {
schema: model.schema
} );

/**
* The view document used by the data controller.
*
* @readonly
* @member {module:engine/view/document~Document}
*/
this.viewDocument = new ViewDocument( stylesProcessor );

/**
* The view downcast writer just for data conversion purposes, i.e. to modify
* the {@link #viewDocument}.
*
* @private
* @readonly
* @member {module:engine/view/downcastwriter~DowncastWriter}
*/
this._viewWriter = new ViewDowncastWriter( this.viewDocument );

// Define default converters for text and elements.
//
// Note that if there is no default converter for the element it will be skipped, for instance `<b>foo</b>` will be
Expand Down Expand Up @@ -188,20 +206,19 @@ export default class DataController {
* @returns {module:engine/view/documentfragment~DocumentFragment} Output view DocumentFragment.
*/
toView( modelElementOrFragment ) {
const viewDocument = this.viewDocument;
const viewWriter = this._viewWriter;

// Clear bindings so the call to this method gives correct results.
this.mapper.clearBindings();

// First, convert elements.
const modelRange = ModelRange._createIn( modelElementOrFragment );
const viewDocument = new ViewDocument( this.stylesProcessor );

const viewDocumentFragment = new ViewDocumentFragment( viewDocument );

// Create separate ViewDowncastWriter just for data conversion purposes.
// We have no view controller and rendering do DOM in DataController so view.change() block is not used here.
const viewWriter = new ViewDowncastWriter( viewDocument );
this.mapper.bindElements( modelElementOrFragment, viewDocumentFragment );

// We have no view controller and rendering do DOM in DataController so view.change() block is not used here.
this.downcastDispatcher.convertInsert( modelRange, viewWriter );

if ( !modelElementOrFragment.is( 'documentFragment' ) ) {
Expand Down
14 changes: 14 additions & 0 deletions tests/controller/datacontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ describe( 'DataController', () => {
expect( data.model ).to.equal( model );
expect( data.stylesProcessor ).to.equal( stylesProcessor );
} );

it( 'should create the #viewDocument property', () => {
const stylesProcessor = new StylesProcessor();
const data = new DataController( model, stylesProcessor );

expect( data.viewDocument ).to.be.instanceOf( ViewDocument );
} );
} );

describe( 'parse()', () => {
Expand Down Expand Up @@ -474,6 +481,13 @@ describe( 'DataController', () => {
downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
} );

it( 'should use #viewDocument as a parent for returned document fragments', () => {
const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', schema );
const viewDocumentFragment = data.toView( modelElement );

expect( viewDocumentFragment.document ).to.equal( data.viewDocument );
} );

it( 'should convert a content of an element', () => {
const modelElement = parseModel( '<div><paragraph>foo</paragraph></div>', schema );

Expand Down

0 comments on commit 4ab84aa

Please sign in to comment.