1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | import { first } from 'ckeditor5/src/utils';
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | export function upcastImageFigure( imageUtils ) {
|
29 | return dispatcher => {
|
30 | dispatcher.on( 'element:figure', converter );
|
31 | };
|
32 |
|
33 | function converter( evt, data, conversionApi ) {
|
34 |
|
35 | if ( !conversionApi.consumable.test( data.viewItem, { name: true, classes: 'image' } ) ) {
|
36 | return;
|
37 | }
|
38 |
|
39 |
|
40 | const viewImage = imageUtils.findViewImgElement( data.viewItem );
|
41 |
|
42 |
|
43 | if ( !viewImage || !conversionApi.consumable.test( viewImage, { name: true } ) ) {
|
44 | return;
|
45 | }
|
46 |
|
47 |
|
48 | conversionApi.consumable.consume( data.viewItem, { name: true, classes: 'image' } );
|
49 |
|
50 |
|
51 | const conversionResult = conversionApi.convertItem( viewImage, data.modelCursor );
|
52 |
|
53 |
|
54 | const modelImage = first( conversionResult.modelRange.getItems() );
|
55 |
|
56 |
|
57 | if ( !modelImage ) {
|
58 |
|
59 | conversionApi.consumable.revert( data.viewItem, { name: true, classes: 'image' } );
|
60 |
|
61 | return;
|
62 | }
|
63 |
|
64 |
|
65 | conversionApi.convertChildren( data.viewItem, modelImage );
|
66 |
|
67 | conversionApi.updateConversionResult( modelImage, data );
|
68 | }
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | export function upcastPicture( imageUtils ) {
|
85 | const sourceAttributeNames = [ 'srcset', 'media', 'type', 'sizes' ];
|
86 |
|
87 | return dispatcher => {
|
88 | dispatcher.on( 'element:picture', converter );
|
89 | };
|
90 |
|
91 | function converter( evt, data, conversionApi ) {
|
92 | const pictureViewElement = data.viewItem;
|
93 |
|
94 |
|
95 | if ( !conversionApi.consumable.test( pictureViewElement, { name: true } ) ) {
|
96 | return;
|
97 | }
|
98 |
|
99 | const sources = new Map();
|
100 |
|
101 |
|
102 | for ( const childSourceElement of pictureViewElement.getChildren() ) {
|
103 | if ( childSourceElement.is( 'element', 'source' ) ) {
|
104 | const attributes = {};
|
105 |
|
106 | for ( const name of sourceAttributeNames ) {
|
107 | if ( childSourceElement.hasAttribute( name ) ) {
|
108 |
|
109 | if ( conversionApi.consumable.test( childSourceElement, { attributes: name } ) ) {
|
110 | attributes[ name ] = childSourceElement.getAttribute( name );
|
111 | }
|
112 | }
|
113 | }
|
114 |
|
115 | if ( Object.keys( attributes ).length ) {
|
116 | sources.set( childSourceElement, attributes );
|
117 | }
|
118 | }
|
119 | }
|
120 |
|
121 | const imgViewElement = imageUtils.findViewImgElement( pictureViewElement );
|
122 |
|
123 |
|
124 | if ( !imgViewElement ) {
|
125 | return;
|
126 | }
|
127 |
|
128 | let modelImage = data.modelCursor.parent;
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | if ( !modelImage.is( 'element', 'imageBlock' ) ) {
|
136 | const conversionResult = conversionApi.convertItem( imgViewElement, data.modelCursor );
|
137 |
|
138 |
|
139 | data.modelRange = conversionResult.modelRange;
|
140 |
|
141 |
|
142 | data.modelCursor = conversionResult.modelCursor;
|
143 |
|
144 | modelImage = first( conversionResult.modelRange.getItems() );
|
145 | }
|
146 |
|
147 | conversionApi.consumable.consume( pictureViewElement, { name: true } );
|
148 |
|
149 |
|
150 |
|
151 | for ( const [ sourceElement, attributes ] of sources ) {
|
152 | conversionApi.consumable.consume( sourceElement, { attributes: Object.keys( attributes ) } );
|
153 | }
|
154 |
|
155 | if ( sources.size ) {
|
156 | conversionApi.writer.setAttribute( 'sources', Array.from( sources.values() ), modelImage );
|
157 | }
|
158 |
|
159 |
|
160 | conversionApi.convertChildren( pictureViewElement, modelImage );
|
161 | }
|
162 | }
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | export function downcastSrcsetAttribute( imageUtils, imageType ) {
|
173 | return dispatcher => {
|
174 | dispatcher.on( `attribute:srcset:${ imageType }`, converter );
|
175 | };
|
176 |
|
177 | function converter( evt, data, conversionApi ) {
|
178 | if ( !conversionApi.consumable.consume( data.item, evt.name ) ) {
|
179 | return;
|
180 | }
|
181 |
|
182 | const writer = conversionApi.writer;
|
183 | const element = conversionApi.mapper.toViewElement( data.item );
|
184 | const img = imageUtils.findViewImgElement( element );
|
185 |
|
186 | if ( data.attributeNewValue === null ) {
|
187 | const srcset = data.attributeOldValue;
|
188 |
|
189 | if ( srcset.data ) {
|
190 | writer.removeAttribute( 'srcset', img );
|
191 | writer.removeAttribute( 'sizes', img );
|
192 |
|
193 | if ( srcset.width ) {
|
194 | writer.removeAttribute( 'width', img );
|
195 | }
|
196 | }
|
197 | } else {
|
198 | const srcset = data.attributeNewValue;
|
199 |
|
200 | if ( srcset.data ) {
|
201 | writer.setAttribute( 'srcset', srcset.data, img );
|
202 |
|
203 | writer.setAttribute( 'sizes', '100vw', img );
|
204 |
|
205 | if ( srcset.width ) {
|
206 | writer.setAttribute( 'width', srcset.width, img );
|
207 | }
|
208 | }
|
209 | }
|
210 | }
|
211 | }
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | export function downcastSourcesAttribute( imageUtils ) {
|
222 | return dispatcher => {
|
223 | dispatcher.on( 'attribute:sources:imageBlock', converter );
|
224 | dispatcher.on( 'attribute:sources:imageInline', converter );
|
225 | };
|
226 |
|
227 | function converter( evt, data, conversionApi ) {
|
228 | if ( !conversionApi.consumable.consume( data.item, evt.name ) ) {
|
229 | return;
|
230 | }
|
231 |
|
232 | const viewWriter = conversionApi.writer;
|
233 | const element = conversionApi.mapper.toViewElement( data.item );
|
234 | const imgElement = imageUtils.findViewImgElement( element );
|
235 |
|
236 | if ( data.attributeNewValue && data.attributeNewValue.length ) {
|
237 |
|
238 | const pictureElement = viewWriter.createContainerElement( 'picture', null,
|
239 | data.attributeNewValue.map( sourceAttributes => {
|
240 | return viewWriter.createEmptyElement( 'source', sourceAttributes );
|
241 | } )
|
242 | );
|
243 |
|
244 |
|
245 | const attributeElements = [];
|
246 | let viewElement = imgElement.parent;
|
247 |
|
248 | while ( viewElement && viewElement.is( 'attributeElement' ) ) {
|
249 | const parentElement = viewElement.parent;
|
250 |
|
251 | viewWriter.unwrap( viewWriter.createRangeOn( imgElement ), viewElement );
|
252 |
|
253 | attributeElements.unshift( viewElement );
|
254 | viewElement = parentElement;
|
255 | }
|
256 |
|
257 |
|
258 | viewWriter.insert( viewWriter.createPositionBefore( imgElement ), pictureElement );
|
259 | viewWriter.move( viewWriter.createRangeOn( imgElement ), viewWriter.createPositionAt( pictureElement, 'end' ) );
|
260 |
|
261 |
|
262 | for ( const attributeElement of attributeElements ) {
|
263 | viewWriter.wrap( viewWriter.createRangeOn( pictureElement ), attributeElement );
|
264 | }
|
265 | }
|
266 |
|
267 |
|
268 | else if ( imgElement.parent.is( 'element', 'picture' ) ) {
|
269 | const pictureElement = imgElement.parent;
|
270 |
|
271 | viewWriter.move( viewWriter.createRangeOn( imgElement ), viewWriter.createPositionBefore( pictureElement ) );
|
272 | viewWriter.remove( pictureElement );
|
273 | }
|
274 | }
|
275 | }
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 | export function downcastImageAttribute( imageUtils, imageType, attributeKey ) {
|
287 | return dispatcher => {
|
288 | dispatcher.on( `attribute:${ attributeKey }:${ imageType }`, converter );
|
289 | };
|
290 |
|
291 | function converter( evt, data, conversionApi ) {
|
292 | if ( !conversionApi.consumable.consume( data.item, evt.name ) ) {
|
293 | return;
|
294 | }
|
295 |
|
296 | const viewWriter = conversionApi.writer;
|
297 | const element = conversionApi.mapper.toViewElement( data.item );
|
298 | const img = imageUtils.findViewImgElement( element );
|
299 |
|
300 | viewWriter.setAttribute( data.attributeKey, data.attributeNewValue || '', img );
|
301 | }
|
302 | }
|
303 |
|