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

Commit fd128a1

Browse files
author
Piotr Jasiun
authored
Merge pull request #1274 from ckeditor/t/1236
Other: Conversion utilities refactor. Closes #1236. --- ### Additional information * `ModelConverterBuilder` and `ViewConverterBuilder` are removed, * `definition-based-converters` are removed, * `conversion.Conversion` class is introduced, * new converter functions, basing on declarative config, are introduced, * other related changes.
2 parents 7849d1d + 38abf3d commit fd128a1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+4732
-4358
lines changed

src/controller/datacontroller.js

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
1212

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

15-
import ModelConversionDispatcher from '../conversion/modelconversiondispatcher';
16-
import { insertText } from '../conversion/model-to-view-converters';
15+
import DowncastDispatcher from '../conversion/downcastdispatcher';
16+
import { insertText } from '../conversion/downcast-converters';
1717

18-
import ViewConversionDispatcher from '../conversion/viewconversiondispatcher';
19-
import { convertText, convertToModelFragment } from '../conversion/view-to-model-converters';
18+
import UpcastDispatcher from '../conversion/upcastdispatcher';
19+
import { convertText, convertToModelFragment } from '../conversion/upcast-converters';
2020

2121
import ViewDocumentFragment from '../view/documentfragment';
2222

@@ -26,11 +26,11 @@ import ModelRange from '../model/range';
2626
* Controller for the data pipeline. The data pipeline controls how data is retrieved from the document
2727
* and set inside it. Hence, the controller features two methods which allow to {@link ~DataController#get get}
2828
* and {@link ~DataController#set set} data of the {@link ~DataController#model model}
29-
* using the given:
29+
* using given:
3030
*
3131
* * {@link module:engine/dataprocessor/dataprocessor~DataProcessor data processor},
32-
* * {@link module:engine/conversion/modelconversiondispatcher~ModelConversionDispatcher model to view} and
33-
* * {@link module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher view to model} converters.
32+
* * downcast converters,
33+
* * upcast converters.
3434
*
3535
* @mixes module:utils/observablemixin~ObservableMixin
3636
*/
@@ -62,44 +62,30 @@ export default class DataController {
6262
/**
6363
* Mapper used for the conversion. It has no permanent bindings, because they are created when getting data and
6464
* cleared directly after the data are converted. However, the mapper is defined as a class property, because
65-
* it needs to be passed to the `ModelConversionDispatcher` as a conversion API.
65+
* it needs to be passed to the `DowncastDispatcher` as a conversion API.
6666
*
6767
* @member {module:engine/conversion/mapper~Mapper}
6868
*/
6969
this.mapper = new Mapper();
7070

7171
/**
72-
* Model-to-view conversion dispatcher used by the {@link #get get method}.
73-
* To attach the model-to-view converter to the data pipeline you need to add a listener to this property:
74-
*
75-
* data.modelToView( 'insert:$element', customInsertConverter );
76-
*
77-
* Or use {@link module:engine/conversion/buildmodelconverter~ModelConverterBuilder}:
78-
*
79-
* buildModelConverter().for( data.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
72+
* Downcast dispatcher used by the {@link #get get method}. Downcast converters should be attached to it.
8073
*
8174
* @readonly
82-
* @member {module:engine/conversion/modelconversiondispatcher~ModelConversionDispatcher}
75+
* @member {module:engine/conversion/downcastdispatcher~DowncastDispatcher}
8376
*/
84-
this.modelToView = new ModelConversionDispatcher( this.model, {
77+
this.downcastDispatcher = new DowncastDispatcher( this.model, {
8578
mapper: this.mapper
8679
} );
87-
this.modelToView.on( 'insert:$text', insertText(), { priority: 'lowest' } );
80+
this.downcastDispatcher.on( 'insert:$text', insertText(), { priority: 'lowest' } );
8881

8982
/**
90-
* View-to-model conversion dispatcher used by the {@link #set set method}.
91-
* To attach the view-to-model converter to the data pipeline you need to add a listener to this property:
92-
*
93-
* data.viewToModel( 'element', customElementConverter );
94-
*
95-
* Or use {@link module:engine/conversion/buildviewconverter~ViewConverterBuilder}:
96-
*
97-
* buildViewConverter().for( data.viewToModel ).fromElement( 'b' ).toAttribute( 'bold', 'true' );
83+
* Upcast dispatcher used by the {@link #set set method}. Upcast converters should be attached to it.
9884
*
9985
* @readonly
100-
* @member {module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher}
86+
* @member {module:engine/conversion/upcastdispatcher~UpcastDispatcher}
10187
*/
102-
this.viewToModel = new ViewConversionDispatcher( this.model, {
88+
this.upcastDispatcher = new UpcastDispatcher( this.model, {
10389
schema: model.schema
10490
} );
10591

@@ -108,13 +94,13 @@ export default class DataController {
10894
// Note that if there is no default converter for the element it will be skipped, for instance `<b>foo</b>` will be
10995
// converted to nothing. We add `convertToModelFragment` as a last converter so it converts children of that
11096
// element to the document fragment so `<b>foo</b>` will be converted to `foo` if there is no converter for `<b>`.
111-
this.viewToModel.on( 'text', convertText(), { priority: 'lowest' } );
112-
this.viewToModel.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
113-
this.viewToModel.on( 'documentFragment', convertToModelFragment(), { priority: 'lowest' } );
97+
this.upcastDispatcher.on( 'text', convertText(), { priority: 'lowest' } );
98+
this.upcastDispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
99+
this.upcastDispatcher.on( 'documentFragment', convertToModelFragment(), { priority: 'lowest' } );
114100
}
115101

116102
/**
117-
* Returns the model's data converted by the {@link #modelToView model-to-view converters} and
103+
* Returns the model's data converted by downcast dispatchers attached to {@link #downcastDispatcher} and
118104
* formatted by the {@link #processor data processor}.
119105
*
120106
* @param {String} [rootName='main'] Root name.
@@ -127,9 +113,8 @@ export default class DataController {
127113

128114
/**
129115
* Returns the content of the given {@link module:engine/model/element~Element model's element} or
130-
* {@link module:engine/model/documentfragment~DocumentFragment model document fragment} converted by the
131-
* {@link #modelToView model-to-view converters} and formatted by the
132-
* {@link #processor data processor}.
116+
* {@link module:engine/model/documentfragment~DocumentFragment model document fragment} converted by the downcast converters
117+
* attached to {@link #downcastDispatcher} and formatted by the {@link #processor data processor}.
133118
*
134119
* @param {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment} modelElementOrFragment
135120
* Element whose content will be stringified.
@@ -145,8 +130,8 @@ export default class DataController {
145130

146131
/**
147132
* Returns the content of the given {@link module:engine/model/element~Element model element} or
148-
* {@link module:engine/model/documentfragment~DocumentFragment model document fragment} converted by the
149-
* {@link #modelToView model-to-view converters} to a
133+
* {@link module:engine/model/documentfragment~DocumentFragment model document fragment} converted by the downcast
134+
* converters attached to {@link #downcastDispatcher} to a
150135
* {@link module:engine/view/documentfragment~DocumentFragment view document fragment}.
151136
*
152137
* @param {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment} modelElementOrFragment
@@ -160,15 +145,15 @@ export default class DataController {
160145
const viewDocumentFragment = new ViewDocumentFragment();
161146
this.mapper.bindElements( modelElementOrFragment, viewDocumentFragment );
162147

163-
this.modelToView.convertInsert( modelRange );
148+
this.downcastDispatcher.convertInsert( modelRange );
164149

165150
if ( !modelElementOrFragment.is( 'documentFragment' ) ) {
166151
// Then, if a document element is converted, convert markers.
167152
// From all document markers, get those, which "intersect" with the converter element.
168153
const markers = _getMarkersRelativeToElement( modelElementOrFragment );
169154

170155
for ( const [ name, range ] of markers ) {
171-
this.modelToView.convertMarkerAdd( name, range );
156+
this.downcastDispatcher.convertMarkerAdd( name, range );
172157
}
173158
}
174159

@@ -180,7 +165,7 @@ export default class DataController {
180165

181166
/**
182167
* Sets input data parsed by the {@link #processor data processor} and
183-
* converted by the {@link #viewToModel view-to-model converters}.
168+
* converted by the {@link #upcastDispatcher view-to-model converters}.
184169
*
185170
* This method also creates a batch with all the changes applied. If all you need is to parse data, use
186171
* the {@link #parse} method.
@@ -204,13 +189,13 @@ export default class DataController {
204189
}
205190

206191
/**
207-
* Returns the data parsed by the {@link #processor data processor} and then
208-
* converted by the {@link #viewToModel view-to-model converters}.
192+
* Returns the data parsed by the {@link #processor data processor} and then converted by upcast converters
193+
* attached to the {@link #upcastDispatcher}.
209194
*
210195
* @see #set
211196
* @param {String} data Data to parse.
212197
* @param {module:engine/model/schema~SchemaContextDefinition} [context='$root'] Base context in which the view will
213-
* be converted to the model. See: {@link module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher#convert}.
198+
* be converted to the model. See: {@link module:engine/conversion/upcastdispatcher~UpcastDispatcher#convert}.
214199
* @returns {module:engine/model/documentfragment~DocumentFragment} Parsed data.
215200
*/
216201
parse( data, context = '$root' ) {
@@ -224,19 +209,19 @@ export default class DataController {
224209
/**
225210
* Returns the result of the given {@link module:engine/view/element~Element view element} or
226211
* {@link module:engine/view/documentfragment~DocumentFragment view document fragment} converted by the
227-
* {@link #viewToModel view-to-model converters}, wrapped by {module:engine/model/documentfragment~DocumentFragment}.
212+
* {@link #upcastDispatcher view-to-model converters}, wrapped by {module:engine/model/documentfragment~DocumentFragment}.
228213
*
229214
* When marker elements were converted during the conversion process, it will be set as a document fragment's
230215
* {@link module:engine/model/documentfragment~DocumentFragment#markers static markers map}.
231216
*
232217
* @param {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment} viewElementOrFragment
233218
* Element or document fragment whose content will be converted.
234219
* @param {module:engine/model/schema~SchemaContextDefinition} [context='$root'] Base context in which the view will
235-
* be converted to the model. See: {@link module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher#convert}.
220+
* be converted to the model. See: {@link module:engine/conversion/upcastdispatcher~UpcastDispatcher#convert}.
236221
* @returns {module:engine/model/documentfragment~DocumentFragment} Output document fragment.
237222
*/
238223
toModel( viewElementOrFragment, context = '$root' ) {
239-
return this.viewToModel.convert( viewElementOrFragment, context );
224+
return this.upcastDispatcher.convert( viewElementOrFragment, context );
240225
}
241226

242227
/**
@@ -247,7 +232,7 @@ export default class DataController {
247232

248233
mix( DataController, ObservableMixin );
249234

250-
// Helper function for converting part of a model to view.
235+
// Helper function for downcast conversion.
251236
//
252237
// Takes a document element (element that is added to a model document) and checks which markers are inside it
253238
// and which markers are containing it. If the marker is intersecting with element, the intersection is returned.

src/controller/editingcontroller.js

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010
import RootEditableElement from '../view/rooteditableelement';
1111
import ViewDocument from '../view/document';
1212
import Mapper from '../conversion/mapper';
13-
import ModelConversionDispatcher from '../conversion/modelconversiondispatcher';
13+
import DowncastDispatcher from '../conversion/downcastdispatcher';
1414
import {
1515
insertText,
1616
remove
17-
} from '../conversion/model-to-view-converters';
18-
import { convertSelectionChange } from '../conversion/view-selection-to-model-converters';
17+
} from '../conversion/downcast-converters';
18+
import { convertSelectionChange } from '../conversion/upcast-selection-converters';
1919
import {
2020
convertRangeSelection,
2121
convertCollapsedSelection,
2222
clearAttributes,
2323
clearFakeSelection
24-
} from '../conversion/model-selection-to-view-converters';
24+
} from '../conversion/downcast-selection-converters';
2525

2626
import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
2727
import mix from '@ckeditor/ckeditor5-utils/src/mix';
@@ -65,47 +65,39 @@ export default class EditingController {
6565
this.mapper = new Mapper();
6666

6767
/**
68-
* Model-to-view conversion dispatcher that converts changes from the model to {@link #view the editing view}.
69-
*
70-
* To attach the model-to-view converter to the editing pipeline you need to add a listener to this dispatcher:
71-
*
72-
* editing.modelToView( 'insert:$element', customInsertConverter );
73-
*
74-
* Or use {@link module:engine/conversion/buildmodelconverter~ModelConverterBuilder}:
75-
*
76-
* buildModelConverter().for( editing.modelToView ).fromAttribute( 'bold' ).toElement( 'b' );
68+
* Downcast dispatcher that converts changes from the model to {@link #view the editing view}.
7769
*
7870
* @readonly
79-
* @member {module:engine/conversion/modelconversiondispatcher~ModelConversionDispatcher} #modelToView
71+
* @member {module:engine/conversion/downcastdispatcher~DowncastDispatcher} #downcastDispatcher
8072
*/
81-
this.modelToView = new ModelConversionDispatcher( this.model, {
73+
this.downcastDispatcher = new DowncastDispatcher( this.model, {
8274
mapper: this.mapper,
8375
viewSelection: this.view.selection
8476
} );
8577

8678
const doc = this.model.document;
8779

8880
this.listenTo( doc, 'change', () => {
89-
this.modelToView.convertChanges( doc.differ );
81+
this.downcastDispatcher.convertChanges( doc.differ );
9082
}, { priority: 'low' } );
9183

9284
this.listenTo( model, '_change', () => {
93-
this.modelToView.convertSelection( doc.selection );
85+
this.downcastDispatcher.convertSelection( doc.selection );
9486
this.view.render();
9587
}, { priority: 'low' } );
9688

9789
// Convert selection from view to model.
9890
this.listenTo( this.view, 'selectionChange', convertSelectionChange( this.model, this.mapper ) );
9991

10092
// Attach default model converters.
101-
this.modelToView.on( 'insert:$text', insertText(), { priority: 'lowest' } );
102-
this.modelToView.on( 'remove', remove(), { priority: 'low' } );
93+
this.downcastDispatcher.on( 'insert:$text', insertText(), { priority: 'lowest' } );
94+
this.downcastDispatcher.on( 'remove', remove(), { priority: 'low' } );
10395

10496
// Attach default model selection converters.
105-
this.modelToView.on( 'selection', clearAttributes(), { priority: 'low' } );
106-
this.modelToView.on( 'selection', clearFakeSelection(), { priority: 'low' } );
107-
this.modelToView.on( 'selection', convertRangeSelection(), { priority: 'low' } );
108-
this.modelToView.on( 'selection', convertCollapsedSelection(), { priority: 'low' } );
97+
this.downcastDispatcher.on( 'selection', clearAttributes(), { priority: 'low' } );
98+
this.downcastDispatcher.on( 'selection', clearFakeSelection(), { priority: 'low' } );
99+
this.downcastDispatcher.on( 'selection', convertRangeSelection(), { priority: 'low' } );
100+
this.downcastDispatcher.on( 'selection', convertCollapsedSelection(), { priority: 'low' } );
109101

110102
// Convert markers removal.
111103
//
@@ -130,7 +122,7 @@ export default class EditingController {
130122
if ( _operationAffectsMarker( operation, marker ) ) {
131123
// And if the operation in any way modifies the marker, remove the marker from the view.
132124
removedMarkers.add( marker.name );
133-
this.modelToView.convertMarkerRemove( marker.name, markerRange );
125+
this.downcastDispatcher.convertMarkerRemove( marker.name, markerRange );
134126

135127
// TODO: This stinks but this is the safest place to have this code.
136128
this.model.document.differ.bufferMarkerChange( marker.name, markerRange, markerRange );
@@ -143,7 +135,7 @@ export default class EditingController {
143135
this.listenTo( model.markers, 'remove', ( evt, marker ) => {
144136
if ( !removedMarkers.has( marker.name ) ) {
145137
removedMarkers.add( marker.name );
146-
this.modelToView.convertMarkerRemove( marker.name, marker.getRange() );
138+
this.downcastDispatcher.convertMarkerRemove( marker.name, marker.getRange() );
147139
}
148140
} );
149141

0 commit comments

Comments
 (0)