@ckeditor/ckeditor5-engine 30.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +17 -0
- package/README.md +30 -0
- package/package.json +70 -0
- package/src/controller/datacontroller.js +563 -0
- package/src/controller/editingcontroller.js +149 -0
- package/src/conversion/conversion.js +644 -0
- package/src/conversion/conversionhelpers.js +40 -0
- package/src/conversion/downcastdispatcher.js +914 -0
- package/src/conversion/downcasthelpers.js +1706 -0
- package/src/conversion/mapper.js +696 -0
- package/src/conversion/modelconsumable.js +329 -0
- package/src/conversion/upcastdispatcher.js +807 -0
- package/src/conversion/upcasthelpers.js +997 -0
- package/src/conversion/viewconsumable.js +623 -0
- package/src/dataprocessor/basichtmlwriter.js +32 -0
- package/src/dataprocessor/dataprocessor.jsdoc +64 -0
- package/src/dataprocessor/htmldataprocessor.js +159 -0
- package/src/dataprocessor/htmlwriter.js +22 -0
- package/src/dataprocessor/xmldataprocessor.js +161 -0
- package/src/dev-utils/model.js +482 -0
- package/src/dev-utils/operationreplayer.js +140 -0
- package/src/dev-utils/utils.js +103 -0
- package/src/dev-utils/view.js +1091 -0
- package/src/index.js +52 -0
- package/src/model/batch.js +82 -0
- package/src/model/differ.js +1282 -0
- package/src/model/document.js +483 -0
- package/src/model/documentfragment.js +390 -0
- package/src/model/documentselection.js +1261 -0
- package/src/model/element.js +438 -0
- package/src/model/history.js +138 -0
- package/src/model/item.jsdoc +14 -0
- package/src/model/liveposition.js +182 -0
- package/src/model/liverange.js +221 -0
- package/src/model/markercollection.js +553 -0
- package/src/model/model.js +934 -0
- package/src/model/node.js +507 -0
- package/src/model/nodelist.js +217 -0
- package/src/model/operation/attributeoperation.js +202 -0
- package/src/model/operation/detachoperation.js +103 -0
- package/src/model/operation/insertoperation.js +188 -0
- package/src/model/operation/markeroperation.js +154 -0
- package/src/model/operation/mergeoperation.js +216 -0
- package/src/model/operation/moveoperation.js +209 -0
- package/src/model/operation/nooperation.js +58 -0
- package/src/model/operation/operation.js +139 -0
- package/src/model/operation/operationfactory.js +49 -0
- package/src/model/operation/renameoperation.js +155 -0
- package/src/model/operation/rootattributeoperation.js +211 -0
- package/src/model/operation/splitoperation.js +254 -0
- package/src/model/operation/transform.js +2389 -0
- package/src/model/operation/utils.js +292 -0
- package/src/model/position.js +1164 -0
- package/src/model/range.js +1049 -0
- package/src/model/rootelement.js +111 -0
- package/src/model/schema.js +1851 -0
- package/src/model/selection.js +902 -0
- package/src/model/text.js +138 -0
- package/src/model/textproxy.js +279 -0
- package/src/model/treewalker.js +414 -0
- package/src/model/utils/autoparagraphing.js +77 -0
- package/src/model/utils/deletecontent.js +528 -0
- package/src/model/utils/getselectedcontent.js +150 -0
- package/src/model/utils/insertcontent.js +824 -0
- package/src/model/utils/modifyselection.js +229 -0
- package/src/model/utils/selection-post-fixer.js +297 -0
- package/src/model/writer.js +1574 -0
- package/src/view/attributeelement.js +274 -0
- package/src/view/containerelement.js +123 -0
- package/src/view/document.js +221 -0
- package/src/view/documentfragment.js +273 -0
- package/src/view/documentselection.js +387 -0
- package/src/view/domconverter.js +1437 -0
- package/src/view/downcastwriter.js +2121 -0
- package/src/view/editableelement.js +118 -0
- package/src/view/element.js +945 -0
- package/src/view/elementdefinition.jsdoc +59 -0
- package/src/view/emptyelement.js +119 -0
- package/src/view/filler.js +161 -0
- package/src/view/item.jsdoc +14 -0
- package/src/view/matcher.js +776 -0
- package/src/view/node.js +391 -0
- package/src/view/observer/arrowkeysobserver.js +58 -0
- package/src/view/observer/bubblingemittermixin.js +307 -0
- package/src/view/observer/bubblingeventinfo.js +71 -0
- package/src/view/observer/clickobserver.js +46 -0
- package/src/view/observer/compositionobserver.js +79 -0
- package/src/view/observer/domeventdata.js +82 -0
- package/src/view/observer/domeventobserver.js +99 -0
- package/src/view/observer/fakeselectionobserver.js +118 -0
- package/src/view/observer/focusobserver.js +106 -0
- package/src/view/observer/inputobserver.js +44 -0
- package/src/view/observer/keyobserver.js +83 -0
- package/src/view/observer/mouseobserver.js +56 -0
- package/src/view/observer/mutationobserver.js +345 -0
- package/src/view/observer/observer.js +118 -0
- package/src/view/observer/selectionobserver.js +242 -0
- package/src/view/placeholder.js +285 -0
- package/src/view/position.js +426 -0
- package/src/view/range.js +533 -0
- package/src/view/rawelement.js +148 -0
- package/src/view/renderer.js +1037 -0
- package/src/view/rooteditableelement.js +107 -0
- package/src/view/selection.js +718 -0
- package/src/view/styles/background.js +73 -0
- package/src/view/styles/border.js +362 -0
- package/src/view/styles/margin.js +41 -0
- package/src/view/styles/padding.js +40 -0
- package/src/view/styles/utils.js +277 -0
- package/src/view/stylesmap.js +938 -0
- package/src/view/text.js +147 -0
- package/src/view/textproxy.js +199 -0
- package/src/view/treewalker.js +496 -0
- package/src/view/uielement.js +238 -0
- package/src/view/upcastwriter.js +484 -0
- package/src/view/view.js +721 -0
- package/theme/placeholder.css +27 -0
|
@@ -0,0 +1,914 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @module engine/conversion/downcastdispatcher
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import Consumable from './modelconsumable';
|
|
11
|
+
import Range from '../model/range';
|
|
12
|
+
import Position, { getNodeAfterPosition, getTextNodeAtPosition } from '../model/position';
|
|
13
|
+
|
|
14
|
+
import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
|
|
15
|
+
import mix from '@ckeditor/ckeditor5-utils/src/mix';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The downcast dispatcher is a central point of downcasting (conversion from the model to the view), which is a process of reacting
|
|
19
|
+
* to changes in the model and firing a set of events. Callbacks listening to these events are called converters. The
|
|
20
|
+
* converters' role is to convert the model changes to changes in view (for example, adding view nodes or
|
|
21
|
+
* changing attributes on view elements).
|
|
22
|
+
*
|
|
23
|
+
* During the conversion process, downcast dispatcher fires events basing on the state of the model and prepares
|
|
24
|
+
* data for these events. It is important to understand that the events are connected with the changes done on the model,
|
|
25
|
+
* for example: "a node has been inserted" or "an attribute has changed". This is in contrary to upcasting (a view-to-model conversion)
|
|
26
|
+
* where you convert the view state (view nodes) to a model tree.
|
|
27
|
+
*
|
|
28
|
+
* The events are prepared basing on a diff created by {@link module:engine/model/differ~Differ Differ}, which buffers them
|
|
29
|
+
* and then passes to the downcast dispatcher as a diff between the old model state and the new model state.
|
|
30
|
+
*
|
|
31
|
+
* Note that because the changes are converted, there is a need to have a mapping between the model structure and the view structure.
|
|
32
|
+
* To map positions and elements during the downcast (a model-to-view conversion), use {@link module:engine/conversion/mapper~Mapper}.
|
|
33
|
+
*
|
|
34
|
+
* Downcast dispatcher fires the following events for model tree changes:
|
|
35
|
+
*
|
|
36
|
+
* * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:insert `insert`} –
|
|
37
|
+
* If a range of nodes was inserted to the model tree.
|
|
38
|
+
* * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:remove `remove`} –
|
|
39
|
+
* If a range of nodes was removed from the model tree.
|
|
40
|
+
* * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:attribute `attribute`} –
|
|
41
|
+
* If an attribute was added, changed or removed from a model node.
|
|
42
|
+
*
|
|
43
|
+
* For {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:insert `insert`}
|
|
44
|
+
* and {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:attribute `attribute`},
|
|
45
|
+
* downcast dispatcher generates {@link module:engine/conversion/modelconsumable~ModelConsumable consumables}.
|
|
46
|
+
* These are used to have control over which changes have already been consumed. It is useful when some converters
|
|
47
|
+
* overwrite others or convert multiple changes (for example, it converts an insertion of an element and also converts that
|
|
48
|
+
* element's attributes during the insertion).
|
|
49
|
+
*
|
|
50
|
+
* Additionally, downcast dispatcher fires events for {@link module:engine/model/markercollection~Marker marker} changes:
|
|
51
|
+
*
|
|
52
|
+
* * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:addMarker} – If a marker was added.
|
|
53
|
+
* * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:removeMarker} – If a marker was removed.
|
|
54
|
+
*
|
|
55
|
+
* Note that changing a marker is done through removing the marker from the old range and adding it to the new range,
|
|
56
|
+
* so both events are fired.
|
|
57
|
+
*
|
|
58
|
+
* Finally, downcast dispatcher also handles firing events for the {@link module:engine/model/selection model selection}
|
|
59
|
+
* conversion:
|
|
60
|
+
*
|
|
61
|
+
* * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:selection}
|
|
62
|
+
* – Converts the selection from the model to the view.
|
|
63
|
+
* * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:attribute}
|
|
64
|
+
* – Fired for every selection attribute.
|
|
65
|
+
* * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:addMarker}
|
|
66
|
+
* – Fired for every marker that contains a selection.
|
|
67
|
+
*
|
|
68
|
+
* Unlike the model tree and the markers, the events for selection are not fired for changes but for a selection state.
|
|
69
|
+
*
|
|
70
|
+
* When providing custom listeners for a downcast dispatcher, remember to check whether a given change has not been
|
|
71
|
+
* {@link module:engine/conversion/modelconsumable~ModelConsumable#consume consumed} yet.
|
|
72
|
+
*
|
|
73
|
+
* When providing custom listeners for downcast dispatcher, keep in mind that any callback that has
|
|
74
|
+
* {@link module:engine/conversion/modelconsumable~ModelConsumable#consume consumed} a value from a consumable and
|
|
75
|
+
* converted the change should also stop the event (for efficiency purposes).
|
|
76
|
+
*
|
|
77
|
+
* When providing custom listeners for downcast dispatcher, remember to use the provided
|
|
78
|
+
* {@link module:engine/view/downcastwriter~DowncastWriter view downcast writer} to apply changes to the view document.
|
|
79
|
+
*
|
|
80
|
+
* You can read more about conversion in the following guides:
|
|
81
|
+
*
|
|
82
|
+
* * {@glink framework/guides/deep-dive/conversion/conversion-introduction Advanced conversion concepts — attributes}
|
|
83
|
+
* * {@glink framework/guides/deep-dive/conversion/conversion-extending-output Extending the editor output }
|
|
84
|
+
* * {@glink framework/guides/deep-dive/conversion/custom-element-conversion Custom element conversion}
|
|
85
|
+
*
|
|
86
|
+
* An example of a custom converter for the downcast dispatcher:
|
|
87
|
+
*
|
|
88
|
+
* // You will convert inserting a "paragraph" model element into the model.
|
|
89
|
+
* downcastDispatcher.on( 'insert:paragraph', ( evt, data, conversionApi ) => {
|
|
90
|
+
* // Remember to check whether the change has not been consumed yet and consume it.
|
|
91
|
+
* if ( !conversionApi.consumable.consume( data.item, 'insert' ) ) {
|
|
92
|
+
* return;
|
|
93
|
+
* }
|
|
94
|
+
*
|
|
95
|
+
* // Translate the position in the model to a position in the view.
|
|
96
|
+
* const viewPosition = conversionApi.mapper.toViewPosition( data.range.start );
|
|
97
|
+
*
|
|
98
|
+
* // Create a <p> element that will be inserted into the view at the `viewPosition`.
|
|
99
|
+
* const viewElement = conversionApi.writer.createContainerElement( 'p' );
|
|
100
|
+
*
|
|
101
|
+
* // Bind the newly created view element to the model element so positions will map accordingly in the future.
|
|
102
|
+
* conversionApi.mapper.bindElements( data.item, viewElement );
|
|
103
|
+
*
|
|
104
|
+
* // Add the newly created view element to the view.
|
|
105
|
+
* conversionApi.writer.insert( viewPosition, viewElement );
|
|
106
|
+
*
|
|
107
|
+
* // Remember to stop the event propagation.
|
|
108
|
+
* evt.stop();
|
|
109
|
+
* } );
|
|
110
|
+
*/
|
|
111
|
+
export default class DowncastDispatcher {
|
|
112
|
+
/**
|
|
113
|
+
* Creates a downcast dispatcher instance.
|
|
114
|
+
*
|
|
115
|
+
* @see module:engine/conversion/downcastdispatcher~DowncastConversionApi
|
|
116
|
+
* @param {Object} conversionApi Additional properties for an interface that will be passed to events fired
|
|
117
|
+
* by the downcast dispatcher.
|
|
118
|
+
*/
|
|
119
|
+
constructor( conversionApi ) {
|
|
120
|
+
/**
|
|
121
|
+
* An interface passed by the dispatcher to the event callbacks.
|
|
122
|
+
*
|
|
123
|
+
* @member {module:engine/conversion/downcastdispatcher~DowncastConversionApi}
|
|
124
|
+
*/
|
|
125
|
+
this.conversionApi = Object.assign( { dispatcher: this }, conversionApi );
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Maps conversion event names that will trigger element reconversion for a given element name.
|
|
129
|
+
*
|
|
130
|
+
* @type {Map<String, String>}
|
|
131
|
+
* @private
|
|
132
|
+
*/
|
|
133
|
+
this._reconversionEventsMapping = new Map();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Takes a {@link module:engine/model/differ~Differ model differ} object with buffered changes and fires conversion basing on it.
|
|
138
|
+
*
|
|
139
|
+
* @param {module:engine/model/differ~Differ} differ The differ object with buffered changes.
|
|
140
|
+
* @param {module:engine/model/markercollection~MarkerCollection} markers Markers connected with the converted model.
|
|
141
|
+
* @param {module:engine/view/downcastwriter~DowncastWriter} writer The view writer that should be used to modify the view document.
|
|
142
|
+
*/
|
|
143
|
+
convertChanges( differ, markers, writer ) {
|
|
144
|
+
// Before the view is updated, remove markers which have changed.
|
|
145
|
+
for ( const change of differ.getMarkersToRemove() ) {
|
|
146
|
+
this.convertMarkerRemove( change.name, change.range, writer );
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const changes = this._mapChangesWithAutomaticReconversion( differ );
|
|
150
|
+
|
|
151
|
+
// Convert changes that happened on model tree.
|
|
152
|
+
for ( const entry of changes ) {
|
|
153
|
+
if ( entry.type === 'insert' ) {
|
|
154
|
+
this.convertInsert( Range._createFromPositionAndShift( entry.position, entry.length ), writer );
|
|
155
|
+
} else if ( entry.type === 'remove' ) {
|
|
156
|
+
this.convertRemove( entry.position, entry.length, entry.name, writer );
|
|
157
|
+
} else if ( entry.type === 'reconvert' ) {
|
|
158
|
+
this.reconvertElement( entry.element, writer );
|
|
159
|
+
} else {
|
|
160
|
+
// Defaults to 'attribute' change.
|
|
161
|
+
this.convertAttribute( entry.range, entry.attributeKey, entry.attributeOldValue, entry.attributeNewValue, writer );
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
for ( const markerName of this.conversionApi.mapper.flushUnboundMarkerNames() ) {
|
|
166
|
+
const markerRange = markers.get( markerName ).getRange();
|
|
167
|
+
|
|
168
|
+
this.convertMarkerRemove( markerName, markerRange, writer );
|
|
169
|
+
this.convertMarkerAdd( markerName, markerRange, writer );
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// After the view is updated, convert markers which have changed.
|
|
173
|
+
for ( const change of differ.getMarkersToAdd() ) {
|
|
174
|
+
this.convertMarkerAdd( change.name, change.range, writer );
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Starts a conversion of a range insertion.
|
|
180
|
+
*
|
|
181
|
+
* For each node in the range, {@link #event:insert `insert` event is fired}. For each attribute on each node,
|
|
182
|
+
* {@link #event:attribute `attribute` event is fired}.
|
|
183
|
+
*
|
|
184
|
+
* @fires insert
|
|
185
|
+
* @fires attribute
|
|
186
|
+
* @param {module:engine/model/range~Range} range The inserted range.
|
|
187
|
+
* @param {module:engine/view/downcastwriter~DowncastWriter} writer The view writer that should be used to modify the view document.
|
|
188
|
+
*/
|
|
189
|
+
convertInsert( range, writer ) {
|
|
190
|
+
this.conversionApi.writer = writer;
|
|
191
|
+
|
|
192
|
+
// Create a list of things that can be consumed, consisting of nodes and their attributes.
|
|
193
|
+
this.conversionApi.consumable = this._createInsertConsumable( range );
|
|
194
|
+
|
|
195
|
+
// Fire a separate insert event for each node and text fragment contained in the range.
|
|
196
|
+
for ( const data of Array.from( range ).map( walkerValueToEventData ) ) {
|
|
197
|
+
this._convertInsertWithAttributes( data );
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
this._clearConversionApi();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Fires conversion of a single node removal. Fires {@link #event:remove remove event} with provided data.
|
|
205
|
+
*
|
|
206
|
+
* @param {module:engine/model/position~Position} position Position from which node was removed.
|
|
207
|
+
* @param {Number} length Offset size of removed node.
|
|
208
|
+
* @param {String} name Name of removed node.
|
|
209
|
+
* @param {module:engine/view/downcastwriter~DowncastWriter} writer View writer that should be used to modify view document.
|
|
210
|
+
*/
|
|
211
|
+
convertRemove( position, length, name, writer ) {
|
|
212
|
+
this.conversionApi.writer = writer;
|
|
213
|
+
|
|
214
|
+
this.fire( 'remove:' + name, { position, length }, this.conversionApi );
|
|
215
|
+
|
|
216
|
+
this._clearConversionApi();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Starts a conversion of an attribute change on a given `range`.
|
|
221
|
+
*
|
|
222
|
+
* For each node in the given `range`, {@link #event:attribute attribute event} is fired with the passed data.
|
|
223
|
+
*
|
|
224
|
+
* @fires attribute
|
|
225
|
+
* @param {module:engine/model/range~Range} range Changed range.
|
|
226
|
+
* @param {String} key Key of the attribute that has changed.
|
|
227
|
+
* @param {*} oldValue Attribute value before the change or `null` if the attribute has not been set before.
|
|
228
|
+
* @param {*} newValue New attribute value or `null` if the attribute has been removed.
|
|
229
|
+
* @param {module:engine/view/downcastwriter~DowncastWriter} writer View writer that should be used to modify view document.
|
|
230
|
+
*/
|
|
231
|
+
convertAttribute( range, key, oldValue, newValue, writer ) {
|
|
232
|
+
this.conversionApi.writer = writer;
|
|
233
|
+
|
|
234
|
+
// Create a list with attributes to consume.
|
|
235
|
+
this.conversionApi.consumable = this._createConsumableForRange( range, `attribute:${ key }` );
|
|
236
|
+
|
|
237
|
+
// Create a separate attribute event for each node in the range.
|
|
238
|
+
for ( const value of range ) {
|
|
239
|
+
const item = value.item;
|
|
240
|
+
const itemRange = Range._createFromPositionAndShift( value.previousPosition, value.length );
|
|
241
|
+
const data = {
|
|
242
|
+
item,
|
|
243
|
+
range: itemRange,
|
|
244
|
+
attributeKey: key,
|
|
245
|
+
attributeOldValue: oldValue,
|
|
246
|
+
attributeNewValue: newValue
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
this._testAndFire( `attribute:${ key }`, data );
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
this._clearConversionApi();
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Starts the reconversion of an element. It will:
|
|
257
|
+
*
|
|
258
|
+
* * Fire an {@link #event:insert `insert` event} for the element to reconvert.
|
|
259
|
+
* * Fire an {@link #event:attribute `attribute` event} for element attributes.
|
|
260
|
+
*
|
|
261
|
+
* This will not reconvert children of the element if they have existing (already converted) views. For newly inserted child elements
|
|
262
|
+
* it will behave the same as {@link #convertInsert}.
|
|
263
|
+
*
|
|
264
|
+
* Element reconversion is defined by the `triggerBy` configuration for the
|
|
265
|
+
* {@link module:engine/conversion/downcasthelpers~DowncastHelpers#elementToElement `elementToElement()`} conversion helper.
|
|
266
|
+
*
|
|
267
|
+
* @fires insert
|
|
268
|
+
* @fires attribute
|
|
269
|
+
* @param {module:engine/model/element~Element} element The element to be reconverted.
|
|
270
|
+
* @param {module:engine/view/downcastwriter~DowncastWriter} writer The view writer that should be used to modify the view document.
|
|
271
|
+
*/
|
|
272
|
+
reconvertElement( element, writer ) {
|
|
273
|
+
const elementRange = Range._createOn( element );
|
|
274
|
+
|
|
275
|
+
this.conversionApi.writer = writer;
|
|
276
|
+
|
|
277
|
+
// Create a list of things that can be consumed, consisting of nodes and their attributes.
|
|
278
|
+
this.conversionApi.consumable = this._createInsertConsumable( elementRange );
|
|
279
|
+
|
|
280
|
+
const mapper = this.conversionApi.mapper;
|
|
281
|
+
const currentView = mapper.toViewElement( element );
|
|
282
|
+
|
|
283
|
+
// Remove the old view but do not remove mapper mappings - those will be used to revive existing elements.
|
|
284
|
+
writer.remove( currentView );
|
|
285
|
+
|
|
286
|
+
// Convert the element - without converting children.
|
|
287
|
+
this._convertInsertWithAttributes( {
|
|
288
|
+
item: element,
|
|
289
|
+
range: elementRange
|
|
290
|
+
} );
|
|
291
|
+
|
|
292
|
+
const convertedViewElement = mapper.toViewElement( element );
|
|
293
|
+
|
|
294
|
+
// Iterate over children of reconverted element in order to...
|
|
295
|
+
for ( const value of Range._createIn( element ) ) {
|
|
296
|
+
const { item } = value;
|
|
297
|
+
|
|
298
|
+
const view = elementOrTextProxyToView( item, mapper );
|
|
299
|
+
|
|
300
|
+
// ...either bring back previously converted view...
|
|
301
|
+
if ( view ) {
|
|
302
|
+
// Do not move views that are already in converted element - those might be created by the main element converter in case
|
|
303
|
+
// when main element converts also its direct children.
|
|
304
|
+
if ( view.root !== convertedViewElement.root ) {
|
|
305
|
+
writer.move(
|
|
306
|
+
writer.createRangeOn( view ),
|
|
307
|
+
mapper.toViewPosition( Position._createBefore( item ) )
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
// ... or by converting newly inserted elements.
|
|
312
|
+
else {
|
|
313
|
+
this._convertInsertWithAttributes( walkerValueToEventData( value ) );
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// After reconversion is done we can unbind the old view.
|
|
318
|
+
mapper.unbindViewElement( currentView );
|
|
319
|
+
|
|
320
|
+
this._clearConversionApi();
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Starts the model selection conversion.
|
|
325
|
+
*
|
|
326
|
+
* Fires events for a given {@link module:engine/model/selection~Selection selection} to start the selection conversion.
|
|
327
|
+
*
|
|
328
|
+
* @fires selection
|
|
329
|
+
* @fires addMarker
|
|
330
|
+
* @fires attribute
|
|
331
|
+
* @param {module:engine/model/selection~Selection} selection The selection to convert.
|
|
332
|
+
* @param {module:engine/model/markercollection~MarkerCollection} markers Markers connected with the converted model.
|
|
333
|
+
* @param {module:engine/view/downcastwriter~DowncastWriter} writer View writer that should be used to modify the view document.
|
|
334
|
+
*/
|
|
335
|
+
convertSelection( selection, markers, writer ) {
|
|
336
|
+
const markersAtSelection = Array.from( markers.getMarkersAtPosition( selection.getFirstPosition() ) );
|
|
337
|
+
|
|
338
|
+
this.conversionApi.writer = writer;
|
|
339
|
+
this.conversionApi.consumable = this._createSelectionConsumable( selection, markersAtSelection );
|
|
340
|
+
|
|
341
|
+
this.fire( 'selection', { selection }, this.conversionApi );
|
|
342
|
+
|
|
343
|
+
if ( !selection.isCollapsed ) {
|
|
344
|
+
this._clearConversionApi();
|
|
345
|
+
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
for ( const marker of markersAtSelection ) {
|
|
350
|
+
const markerRange = marker.getRange();
|
|
351
|
+
|
|
352
|
+
if ( !shouldMarkerChangeBeConverted( selection.getFirstPosition(), marker, this.conversionApi.mapper ) ) {
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const data = {
|
|
357
|
+
item: selection,
|
|
358
|
+
markerName: marker.name,
|
|
359
|
+
markerRange
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
if ( this.conversionApi.consumable.test( selection, 'addMarker:' + marker.name ) ) {
|
|
363
|
+
this.fire( 'addMarker:' + marker.name, data, this.conversionApi );
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
for ( const key of selection.getAttributeKeys() ) {
|
|
368
|
+
const data = {
|
|
369
|
+
item: selection,
|
|
370
|
+
range: selection.getFirstRange(),
|
|
371
|
+
attributeKey: key,
|
|
372
|
+
attributeOldValue: null,
|
|
373
|
+
attributeNewValue: selection.getAttribute( key )
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
// Do not fire event if the attribute has been consumed.
|
|
377
|
+
if ( this.conversionApi.consumable.test( selection, 'attribute:' + data.attributeKey ) ) {
|
|
378
|
+
this.fire( 'attribute:' + data.attributeKey + ':$text', data, this.conversionApi );
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
this._clearConversionApi();
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Converts the added marker. Fires the {@link #event:addMarker `addMarker`} event for each item
|
|
387
|
+
* in the marker's range. If the range is collapsed, a single event is dispatched. See the event description for more details.
|
|
388
|
+
*
|
|
389
|
+
* @fires addMarker
|
|
390
|
+
* @param {String} markerName Marker name.
|
|
391
|
+
* @param {module:engine/model/range~Range} markerRange The marker range.
|
|
392
|
+
* @param {module:engine/view/downcastwriter~DowncastWriter} writer View writer that should be used to modify the view document.
|
|
393
|
+
*/
|
|
394
|
+
convertMarkerAdd( markerName, markerRange, writer ) {
|
|
395
|
+
// Do not convert if range is in graveyard.
|
|
396
|
+
if ( markerRange.root.rootName == '$graveyard' ) {
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
this.conversionApi.writer = writer;
|
|
401
|
+
|
|
402
|
+
// In markers' case, event name == consumable name.
|
|
403
|
+
const eventName = 'addMarker:' + markerName;
|
|
404
|
+
|
|
405
|
+
//
|
|
406
|
+
// First, fire an event for the whole marker.
|
|
407
|
+
//
|
|
408
|
+
const consumable = new Consumable();
|
|
409
|
+
consumable.add( markerRange, eventName );
|
|
410
|
+
|
|
411
|
+
this.conversionApi.consumable = consumable;
|
|
412
|
+
|
|
413
|
+
this.fire( eventName, { markerName, markerRange }, this.conversionApi );
|
|
414
|
+
|
|
415
|
+
//
|
|
416
|
+
// Do not fire events for each item inside the range if the range got consumed.
|
|
417
|
+
//
|
|
418
|
+
if ( !consumable.test( markerRange, eventName ) ) {
|
|
419
|
+
this._clearConversionApi();
|
|
420
|
+
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
//
|
|
425
|
+
// Then, fire an event for each item inside the marker range.
|
|
426
|
+
//
|
|
427
|
+
this.conversionApi.consumable = this._createConsumableForRange( markerRange, eventName );
|
|
428
|
+
|
|
429
|
+
for ( const item of markerRange.getItems() ) {
|
|
430
|
+
// Do not fire event for already consumed items.
|
|
431
|
+
if ( !this.conversionApi.consumable.test( item, eventName ) ) {
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const data = { item, range: Range._createOn( item ), markerName, markerRange };
|
|
436
|
+
|
|
437
|
+
this.fire( eventName, data, this.conversionApi );
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
this._clearConversionApi();
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Fires the conversion of the marker removal. Fires the {@link #event:removeMarker `removeMarker`} event with the provided data.
|
|
445
|
+
*
|
|
446
|
+
* @fires removeMarker
|
|
447
|
+
* @param {String} markerName Marker name.
|
|
448
|
+
* @param {module:engine/model/range~Range} markerRange The marker range.
|
|
449
|
+
* @param {module:engine/view/downcastwriter~DowncastWriter} writer View writer that should be used to modify the view document.
|
|
450
|
+
*/
|
|
451
|
+
convertMarkerRemove( markerName, markerRange, writer ) {
|
|
452
|
+
// Do not convert if range is in graveyard.
|
|
453
|
+
if ( markerRange.root.rootName == '$graveyard' ) {
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
this.conversionApi.writer = writer;
|
|
458
|
+
|
|
459
|
+
this.fire( 'removeMarker:' + markerName, { markerName, markerRange }, this.conversionApi );
|
|
460
|
+
|
|
461
|
+
this._clearConversionApi();
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Maps the model element "insert" reconversion for given event names. The event names must be fully specified:
|
|
466
|
+
*
|
|
467
|
+
* * For "attribute" change event, it should include the main element name, i.e: `'attribute:attributeName:elementName'`.
|
|
468
|
+
* * For child node change events, these should use the child event name as well, i.e:
|
|
469
|
+
* * For adding a node: `'insert:childElementName'`.
|
|
470
|
+
* * For removing a node: `'remove:childElementName'`.
|
|
471
|
+
*
|
|
472
|
+
* **Note**: This method should not be used directly. The reconversion is defined by the `triggerBy()` configuration of the
|
|
473
|
+
* `elementToElement()` conversion helper.
|
|
474
|
+
*
|
|
475
|
+
* @protected
|
|
476
|
+
* @param {String} modelName The name of the main model element for which the events will trigger the reconversion.
|
|
477
|
+
* @param {String} eventName The name of an event that would trigger conversion for a given model element.
|
|
478
|
+
*/
|
|
479
|
+
_mapReconversionTriggerEvent( modelName, eventName ) {
|
|
480
|
+
this._reconversionEventsMapping.set( eventName, modelName );
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Creates {@link module:engine/conversion/modelconsumable~ModelConsumable} with values to consume from a given range,
|
|
485
|
+
* assuming that the range has just been inserted to the model.
|
|
486
|
+
*
|
|
487
|
+
* @private
|
|
488
|
+
* @param {module:engine/model/range~Range} range The inserted range.
|
|
489
|
+
* @returns {module:engine/conversion/modelconsumable~ModelConsumable} The values to consume.
|
|
490
|
+
*/
|
|
491
|
+
_createInsertConsumable( range ) {
|
|
492
|
+
const consumable = new Consumable();
|
|
493
|
+
|
|
494
|
+
for ( const value of range ) {
|
|
495
|
+
const item = value.item;
|
|
496
|
+
|
|
497
|
+
consumable.add( item, 'insert' );
|
|
498
|
+
|
|
499
|
+
for ( const key of item.getAttributeKeys() ) {
|
|
500
|
+
consumable.add( item, 'attribute:' + key );
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
return consumable;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* Creates {@link module:engine/conversion/modelconsumable~ModelConsumable} with values to consume for a given range.
|
|
509
|
+
*
|
|
510
|
+
* @private
|
|
511
|
+
* @param {module:engine/model/range~Range} range The affected range.
|
|
512
|
+
* @param {String} type Consumable type.
|
|
513
|
+
* @returns {module:engine/conversion/modelconsumable~ModelConsumable} The values to consume.
|
|
514
|
+
*/
|
|
515
|
+
_createConsumableForRange( range, type ) {
|
|
516
|
+
const consumable = new Consumable();
|
|
517
|
+
|
|
518
|
+
for ( const item of range.getItems() ) {
|
|
519
|
+
consumable.add( item, type );
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
return consumable;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* Creates {@link module:engine/conversion/modelconsumable~ModelConsumable} with selection consumable values.
|
|
527
|
+
*
|
|
528
|
+
* @private
|
|
529
|
+
* @param {module:engine/model/selection~Selection} selection The selection to create the consumable from.
|
|
530
|
+
* @param {Iterable.<module:engine/model/markercollection~Marker>} markers Markers that contain the selection.
|
|
531
|
+
* @returns {module:engine/conversion/modelconsumable~ModelConsumable} The values to consume.
|
|
532
|
+
*/
|
|
533
|
+
_createSelectionConsumable( selection, markers ) {
|
|
534
|
+
const consumable = new Consumable();
|
|
535
|
+
|
|
536
|
+
consumable.add( selection, 'selection' );
|
|
537
|
+
|
|
538
|
+
for ( const marker of markers ) {
|
|
539
|
+
consumable.add( selection, 'addMarker:' + marker.name );
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
for ( const key of selection.getAttributeKeys() ) {
|
|
543
|
+
consumable.add( selection, 'attribute:' + key );
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
return consumable;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* Tests passed `consumable` to check whether given event can be fired and if so, fires it.
|
|
551
|
+
*
|
|
552
|
+
* @private
|
|
553
|
+
* @fires insert
|
|
554
|
+
* @fires attribute
|
|
555
|
+
* @param {String} type Event type.
|
|
556
|
+
* @param {Object} data Event data.
|
|
557
|
+
*/
|
|
558
|
+
_testAndFire( type, data ) {
|
|
559
|
+
if ( !this.conversionApi.consumable.test( data.item, type ) ) {
|
|
560
|
+
// Do not fire event if the item was consumed.
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
this.fire( getEventName( type, data ), data, this.conversionApi );
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* Clears the conversion API object.
|
|
569
|
+
*
|
|
570
|
+
* @private
|
|
571
|
+
*/
|
|
572
|
+
_clearConversionApi() {
|
|
573
|
+
delete this.conversionApi.writer;
|
|
574
|
+
delete this.conversionApi.consumable;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* Internal method for converting element insertion. It will fire events for the inserted element and events for its attributes.
|
|
579
|
+
*
|
|
580
|
+
* @private
|
|
581
|
+
* @fires insert
|
|
582
|
+
* @fires attribute
|
|
583
|
+
* @param {Object} data Event data.
|
|
584
|
+
*/
|
|
585
|
+
_convertInsertWithAttributes( data ) {
|
|
586
|
+
this._testAndFire( 'insert', data );
|
|
587
|
+
|
|
588
|
+
// Fire a separate addAttribute event for each attribute that was set on inserted items.
|
|
589
|
+
// This is important because most attributes converters will listen only to add/change/removeAttribute events.
|
|
590
|
+
// If we would not add this part, attributes on inserted nodes would not be converted.
|
|
591
|
+
for ( const key of data.item.getAttributeKeys() ) {
|
|
592
|
+
data.attributeKey = key;
|
|
593
|
+
data.attributeOldValue = null;
|
|
594
|
+
data.attributeNewValue = data.item.getAttribute( key );
|
|
595
|
+
|
|
596
|
+
this._testAndFire( `attribute:${ key }`, data );
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/**
|
|
601
|
+
* Returns differ changes together with added "reconvert" type changes for {@link #reconvertElement}. These are defined by
|
|
602
|
+
* a the `triggerBy()` configuration for the
|
|
603
|
+
* {@link module:engine/conversion/downcasthelpers~DowncastHelpers#elementToElement `elementToElement()`} conversion helper.
|
|
604
|
+
*
|
|
605
|
+
* This method will remove every mapped insert or remove change with a single "reconvert" change.
|
|
606
|
+
*
|
|
607
|
+
* For instance: Having a `triggerBy()` configuration defined for the `<complex>` element that issues this element reconversion on
|
|
608
|
+
* `foo` and `bar` attributes change, and a set of changes for this element:
|
|
609
|
+
*
|
|
610
|
+
* const differChanges = [
|
|
611
|
+
* { type: 'attribute', attributeKey: 'foo', ... },
|
|
612
|
+
* { type: 'attribute', attributeKey: 'bar', ... },
|
|
613
|
+
* { type: 'attribute', attributeKey: 'baz', ... }
|
|
614
|
+
* ];
|
|
615
|
+
*
|
|
616
|
+
* This method will return:
|
|
617
|
+
*
|
|
618
|
+
* const updatedChanges = [
|
|
619
|
+
* { type: 'reconvert', element: complexElementInstance },
|
|
620
|
+
* { type: 'attribute', attributeKey: 'baz', ... }
|
|
621
|
+
* ];
|
|
622
|
+
*
|
|
623
|
+
* In the example above, the `'baz'` attribute change will fire an {@link #event:attribute attribute event}
|
|
624
|
+
*
|
|
625
|
+
* @param {module:engine/model/differ~Differ} differ The differ object with buffered changes.
|
|
626
|
+
* @returns {Array.<Object>} Updated set of changes.
|
|
627
|
+
* @private
|
|
628
|
+
*/
|
|
629
|
+
_mapChangesWithAutomaticReconversion( differ ) {
|
|
630
|
+
const itemsToReconvert = new Set();
|
|
631
|
+
const updated = [];
|
|
632
|
+
|
|
633
|
+
for ( const entry of differ.getChanges() ) {
|
|
634
|
+
const position = entry.position || entry.range.start;
|
|
635
|
+
// Cached parent - just in case. See https://github.com/ckeditor/ckeditor5/issues/6579.
|
|
636
|
+
const positionParent = position.parent;
|
|
637
|
+
const textNode = getTextNodeAtPosition( position, positionParent );
|
|
638
|
+
|
|
639
|
+
// Reconversion is done only on elements so skip text changes.
|
|
640
|
+
if ( textNode ) {
|
|
641
|
+
updated.push( entry );
|
|
642
|
+
|
|
643
|
+
continue;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
const element = entry.type === 'attribute' ? getNodeAfterPosition( position, positionParent, null ) : positionParent;
|
|
647
|
+
|
|
648
|
+
// Case of text node set directly in root. For now used only in tests but can be possible when enabled in paragraph-like roots.
|
|
649
|
+
// See: https://github.com/ckeditor/ckeditor5/issues/762.
|
|
650
|
+
if ( element.is( '$text' ) ) {
|
|
651
|
+
updated.push( entry );
|
|
652
|
+
|
|
653
|
+
continue;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
let eventName;
|
|
657
|
+
|
|
658
|
+
if ( entry.type === 'attribute' ) {
|
|
659
|
+
eventName = `attribute:${ entry.attributeKey }:${ element.name }`;
|
|
660
|
+
} else {
|
|
661
|
+
eventName = `${ entry.type }:${ entry.name }`;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
if ( this._isReconvertTriggerEvent( eventName, element.name ) ) {
|
|
665
|
+
if ( itemsToReconvert.has( element ) ) {
|
|
666
|
+
// Element is already reconverted, so skip this change.
|
|
667
|
+
continue;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
itemsToReconvert.add( element );
|
|
671
|
+
|
|
672
|
+
// Add special "reconvert" change.
|
|
673
|
+
updated.push( { type: 'reconvert', element } );
|
|
674
|
+
} else {
|
|
675
|
+
updated.push( entry );
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
return updated;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* Checks if the resulting change should trigger element reconversion.
|
|
684
|
+
*
|
|
685
|
+
* These are defined by a `triggerBy()` configuration for the
|
|
686
|
+
* {@link module:engine/conversion/downcasthelpers~DowncastHelpers#elementToElement `elementToElement()`} conversion helper.
|
|
687
|
+
*
|
|
688
|
+
* @private
|
|
689
|
+
* @param {String} eventName The event name to check.
|
|
690
|
+
* @param {String} elementName The element name to check.
|
|
691
|
+
* @returns {Boolean}
|
|
692
|
+
*/
|
|
693
|
+
_isReconvertTriggerEvent( eventName, elementName ) {
|
|
694
|
+
return this._reconversionEventsMapping.get( eventName ) === elementName;
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/**
|
|
698
|
+
* Fired for inserted nodes.
|
|
699
|
+
*
|
|
700
|
+
* `insert` is a namespace for a class of events. Names of actually called events follow this pattern:
|
|
701
|
+
* `insert:name`. `name` is either `'$text'`, when {@link module:engine/model/text~Text a text node} has been inserted,
|
|
702
|
+
* or {@link module:engine/model/element~Element#name name} of inserted element.
|
|
703
|
+
*
|
|
704
|
+
* This way listeners can either listen to a general `insert` event or specific event (for example `insert:paragraph`).
|
|
705
|
+
*
|
|
706
|
+
* @event insert
|
|
707
|
+
* @param {Object} data Additional information about the change.
|
|
708
|
+
* @param {module:engine/model/item~Item} data.item Inserted item.
|
|
709
|
+
* @param {module:engine/model/range~Range} data.range Range spanning over inserted item.
|
|
710
|
+
* @param {module:engine/conversion/downcastdispatcher~DowncastConversionApi} conversionApi Conversion interface
|
|
711
|
+
* to be used by callback, passed in `DowncastDispatcher` constructor.
|
|
712
|
+
*/
|
|
713
|
+
|
|
714
|
+
/**
|
|
715
|
+
* Fired for removed nodes.
|
|
716
|
+
*
|
|
717
|
+
* `remove` is a namespace for a class of events. Names of actually called events follow this pattern:
|
|
718
|
+
* `remove:name`. `name` is either `'$text'`, when {@link module:engine/model/text~Text a text node} has been removed,
|
|
719
|
+
* or the {@link module:engine/model/element~Element#name name} of removed element.
|
|
720
|
+
*
|
|
721
|
+
* This way listeners can either listen to a general `remove` event or specific event (for example `remove:paragraph`).
|
|
722
|
+
*
|
|
723
|
+
* @event remove
|
|
724
|
+
* @param {Object} data Additional information about the change.
|
|
725
|
+
* @param {module:engine/model/position~Position} data.position Position from which the node has been removed.
|
|
726
|
+
* @param {Number} data.length Offset size of the removed node.
|
|
727
|
+
* @param {module:engine/conversion/downcastdispatcher~DowncastConversionApi} conversionApi Conversion interface
|
|
728
|
+
* to be used by callback, passed in `DowncastDispatcher` constructor.
|
|
729
|
+
*/
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* Fired in the following cases:
|
|
733
|
+
*
|
|
734
|
+
* * when an attribute has been added, changed, or removed from a node,
|
|
735
|
+
* * when a node with an attribute is inserted,
|
|
736
|
+
* * when collapsed model selection attribute is converted.
|
|
737
|
+
*
|
|
738
|
+
* `attribute` is a namespace for a class of events. Names of actually called events follow this pattern:
|
|
739
|
+
* `attribute:attributeKey:name`. `attributeKey` is the key of added/changed/removed attribute.
|
|
740
|
+
* `name` is either `'$text'` if change was on {@link module:engine/model/text~Text a text node},
|
|
741
|
+
* or the {@link module:engine/model/element~Element#name name} of element which attribute has changed.
|
|
742
|
+
*
|
|
743
|
+
* This way listeners can either listen to a general `attribute:bold` event or specific event (for example `attribute:src:imageBlock`).
|
|
744
|
+
*
|
|
745
|
+
* @event attribute
|
|
746
|
+
* @param {Object} data Additional information about the change.
|
|
747
|
+
* @param {module:engine/model/item~Item|module:engine/model/documentselection~DocumentSelection} data.item Changed item
|
|
748
|
+
* or converted selection.
|
|
749
|
+
* @param {module:engine/model/range~Range} data.range Range spanning over changed item or selection range.
|
|
750
|
+
* @param {String} data.attributeKey Attribute key.
|
|
751
|
+
* @param {*} data.attributeOldValue Attribute value before the change. This is `null` when selection attribute is converted.
|
|
752
|
+
* @param {*} data.attributeNewValue New attribute value.
|
|
753
|
+
* @param {module:engine/conversion/downcastdispatcher~DowncastConversionApi} conversionApi Conversion interface
|
|
754
|
+
* to be used by callback, passed in `DowncastDispatcher` constructor.
|
|
755
|
+
*/
|
|
756
|
+
|
|
757
|
+
/**
|
|
758
|
+
* Fired for {@link module:engine/model/selection~Selection selection} changes.
|
|
759
|
+
*
|
|
760
|
+
* @event selection
|
|
761
|
+
* @param {module:engine/model/selection~Selection} selection Selection that is converted.
|
|
762
|
+
* @param {module:engine/conversion/downcastdispatcher~DowncastConversionApi} conversionApi Conversion interface
|
|
763
|
+
* to be used by callback, passed in `DowncastDispatcher` constructor.
|
|
764
|
+
*/
|
|
765
|
+
|
|
766
|
+
/**
|
|
767
|
+
* Fired when a new marker is added to the model. Also fired when a collapsed model selection that is inside a marker is converted.
|
|
768
|
+
*
|
|
769
|
+
* `addMarker` is a namespace for a class of events. Names of actually called events follow this pattern:
|
|
770
|
+
* `addMarker:markerName`. By specifying certain marker names, you can make the events even more gradual. For example,
|
|
771
|
+
* if markers are named `foo:abc`, `foo:bar`, then it is possible to listen to `addMarker:foo` or `addMarker:foo:abc` and
|
|
772
|
+
* `addMarker:foo:bar` events.
|
|
773
|
+
*
|
|
774
|
+
* If the marker range is not collapsed:
|
|
775
|
+
*
|
|
776
|
+
* * the event is fired for each item in the marker range one by one,
|
|
777
|
+
* * `conversionApi.consumable` includes each item of the marker range and the consumable value is same as the event name.
|
|
778
|
+
*
|
|
779
|
+
* If the marker range is collapsed:
|
|
780
|
+
*
|
|
781
|
+
* * there is only one event,
|
|
782
|
+
* * `conversionApi.consumable` includes marker range with the event name.
|
|
783
|
+
*
|
|
784
|
+
* If the selection inside a marker is converted:
|
|
785
|
+
*
|
|
786
|
+
* * there is only one event,
|
|
787
|
+
* * `conversionApi.consumable` includes the selection instance with the event name.
|
|
788
|
+
*
|
|
789
|
+
* @event addMarker
|
|
790
|
+
* @param {Object} data Additional information about the change.
|
|
791
|
+
* @param {module:engine/model/item~Item|module:engine/model/selection~Selection} data.item Item inside the new marker or
|
|
792
|
+
* the selection that is being converted.
|
|
793
|
+
* @param {module:engine/model/range~Range} [data.range] Range spanning over converted item. Available only in marker conversion, if
|
|
794
|
+
* the marker range was not collapsed.
|
|
795
|
+
* @param {module:engine/model/range~Range} data.markerRange Marker range.
|
|
796
|
+
* @param {String} data.markerName Marker name.
|
|
797
|
+
* @param {module:engine/conversion/downcastdispatcher~DowncastConversionApi} conversionApi Conversion interface
|
|
798
|
+
* to be used by callback, passed in `DowncastDispatcher` constructor.
|
|
799
|
+
*/
|
|
800
|
+
|
|
801
|
+
/**
|
|
802
|
+
* Fired when a marker is removed from the model.
|
|
803
|
+
*
|
|
804
|
+
* `removeMarker` is a namespace for a class of events. Names of actually called events follow this pattern:
|
|
805
|
+
* `removeMarker:markerName`. By specifying certain marker names, you can make the events even more gradual. For example,
|
|
806
|
+
* if markers are named `foo:abc`, `foo:bar`, then it is possible to listen to `removeMarker:foo` or `removeMarker:foo:abc` and
|
|
807
|
+
* `removeMarker:foo:bar` events.
|
|
808
|
+
*
|
|
809
|
+
* @event removeMarker
|
|
810
|
+
* @param {Object} data Additional information about the change.
|
|
811
|
+
* @param {module:engine/model/range~Range} data.markerRange Marker range.
|
|
812
|
+
* @param {String} data.markerName Marker name.
|
|
813
|
+
* @param {module:engine/conversion/downcastdispatcher~DowncastConversionApi} conversionApi Conversion interface
|
|
814
|
+
* to be used by callback, passed in `DowncastDispatcher` constructor.
|
|
815
|
+
*/
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
mix( DowncastDispatcher, EmitterMixin );
|
|
819
|
+
|
|
820
|
+
// Helper function, checks whether change of `marker` at `modelPosition` should be converted. Marker changes are not
|
|
821
|
+
// converted if they happen inside an element with custom conversion method.
|
|
822
|
+
//
|
|
823
|
+
// @param {module:engine/model/position~Position} modelPosition
|
|
824
|
+
// @param {module:engine/model/markercollection~Marker} marker
|
|
825
|
+
// @param {module:engine/conversion/mapper~Mapper} mapper
|
|
826
|
+
// @returns {Boolean}
|
|
827
|
+
function shouldMarkerChangeBeConverted( modelPosition, marker, mapper ) {
|
|
828
|
+
const range = marker.getRange();
|
|
829
|
+
const ancestors = Array.from( modelPosition.getAncestors() );
|
|
830
|
+
ancestors.shift(); // Remove root element. It cannot be passed to `model.Range#containsItem`.
|
|
831
|
+
ancestors.reverse();
|
|
832
|
+
|
|
833
|
+
const hasCustomHandling = ancestors.some( element => {
|
|
834
|
+
if ( range.containsItem( element ) ) {
|
|
835
|
+
const viewElement = mapper.toViewElement( element );
|
|
836
|
+
|
|
837
|
+
return !!viewElement.getCustomProperty( 'addHighlight' );
|
|
838
|
+
}
|
|
839
|
+
} );
|
|
840
|
+
|
|
841
|
+
return !hasCustomHandling;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
function getEventName( type, data ) {
|
|
845
|
+
const name = data.item.name || '$text';
|
|
846
|
+
|
|
847
|
+
return `${ type }:${ name }`;
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
function walkerValueToEventData( value ) {
|
|
851
|
+
const item = value.item;
|
|
852
|
+
const itemRange = Range._createFromPositionAndShift( value.previousPosition, value.length );
|
|
853
|
+
|
|
854
|
+
return {
|
|
855
|
+
item,
|
|
856
|
+
range: itemRange
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
function elementOrTextProxyToView( item, mapper ) {
|
|
861
|
+
if ( item.is( 'textProxy' ) ) {
|
|
862
|
+
const mappedPosition = mapper.toViewPosition( Position._createBefore( item ) );
|
|
863
|
+
const positionParent = mappedPosition.parent;
|
|
864
|
+
|
|
865
|
+
return positionParent.is( '$text' ) ? positionParent : null;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
return mapper.toViewElement( item );
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
/**
|
|
872
|
+
* Conversion interface that is registered for given {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher}
|
|
873
|
+
* and is passed as one of parameters when {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher dispatcher}
|
|
874
|
+
* fires its events.
|
|
875
|
+
*
|
|
876
|
+
* @interface module:engine/conversion/downcastdispatcher~DowncastConversionApi
|
|
877
|
+
*/
|
|
878
|
+
|
|
879
|
+
/**
|
|
880
|
+
* The {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher} instance.
|
|
881
|
+
*
|
|
882
|
+
* @member {module:engine/conversion/downcastdispatcher~DowncastDispatcher} #dispatcher
|
|
883
|
+
*/
|
|
884
|
+
|
|
885
|
+
/**
|
|
886
|
+
* Stores the information about what parts of a processed model item are still waiting to be handled. After a piece of a model item was
|
|
887
|
+
* converted, an appropriate consumable value should be {@link module:engine/conversion/modelconsumable~ModelConsumable#consume consumed}.
|
|
888
|
+
*
|
|
889
|
+
* @member {module:engine/conversion/modelconsumable~ModelConsumable} #consumable
|
|
890
|
+
*/
|
|
891
|
+
|
|
892
|
+
/**
|
|
893
|
+
* The {@link module:engine/conversion/mapper~Mapper} instance.
|
|
894
|
+
*
|
|
895
|
+
* @member {module:engine/conversion/mapper~Mapper} #mapper
|
|
896
|
+
*/
|
|
897
|
+
|
|
898
|
+
/**
|
|
899
|
+
* The {@link module:engine/model/schema~Schema} instance set for the model that is downcast.
|
|
900
|
+
*
|
|
901
|
+
* @member {module:engine/model/schema~Schema} #schema
|
|
902
|
+
*/
|
|
903
|
+
|
|
904
|
+
/**
|
|
905
|
+
* The {@link module:engine/view/downcastwriter~DowncastWriter} instance used to manipulate the data during conversion.
|
|
906
|
+
*
|
|
907
|
+
* @member {module:engine/view/downcastwriter~DowncastWriter} #writer
|
|
908
|
+
*/
|
|
909
|
+
|
|
910
|
+
/**
|
|
911
|
+
* An object with an additional configuration which can be used during the conversion process. Available only for data downcast conversion.
|
|
912
|
+
*
|
|
913
|
+
* @member {Object} #options
|
|
914
|
+
*/
|