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