1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | import { Matcher, UpcastWriter } from 'ckeditor5/src/engine.js';
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | export function replaceImagesSourceWithBase64(documentFragment, rtfData) {
|
18 | if (!documentFragment.childCount) {
|
19 | return;
|
20 | }
|
21 | const upcastWriter = new UpcastWriter(documentFragment.document);
|
22 | const shapesIds = findAllShapesIds(documentFragment, upcastWriter);
|
23 | removeAllImgElementsRepresentingShapes(shapesIds, documentFragment, upcastWriter);
|
24 | insertMissingImgs(shapesIds, documentFragment, upcastWriter);
|
25 | removeAllShapeElements(documentFragment, upcastWriter);
|
26 | const images = findAllImageElementsWithLocalSource(documentFragment, upcastWriter);
|
27 | if (images.length) {
|
28 | replaceImagesFileSourceWithInlineRepresentation(images, extractImageDataFromRtf(rtfData), upcastWriter);
|
29 | }
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | export function _convertHexToBase64(hexString) {
|
39 | return btoa(hexString.match(/\w{2}/g).map(char => {
|
40 | return String.fromCharCode(parseInt(char, 16));
|
41 | }).join(''));
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | function findAllShapesIds(documentFragment, writer) {
|
51 | const range = writer.createRangeIn(documentFragment);
|
52 | const shapeElementsMatcher = new Matcher({
|
53 | name: /v:(.+)/
|
54 | });
|
55 | const shapesIds = [];
|
56 | for (const value of range) {
|
57 | if (value.type != 'elementStart') {
|
58 | continue;
|
59 | }
|
60 | const el = value.item;
|
61 | const previousSibling = el.previousSibling;
|
62 | const prevSiblingName = previousSibling && previousSibling.is('element') ? previousSibling.name : null;
|
63 |
|
64 |
|
65 | const exceptionIds = ['Chart'];
|
66 | const isElementAShape = shapeElementsMatcher.match(el);
|
67 | const hasElementGfxdataAttribute = el.getAttribute('o:gfxdata');
|
68 | const isPreviousSiblingAShapeType = prevSiblingName === 'v:shapetype';
|
69 | const isElementIdInExceptionsArray = hasElementGfxdataAttribute &&
|
70 | exceptionIds.some(item => el.getAttribute('id').includes(item));
|
71 |
|
72 |
|
73 | if (isElementAShape &&
|
74 | hasElementGfxdataAttribute &&
|
75 | !isPreviousSiblingAShapeType &&
|
76 | !isElementIdInExceptionsArray) {
|
77 | shapesIds.push(value.item.getAttribute('id'));
|
78 | }
|
79 | }
|
80 | return shapesIds;
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | function removeAllImgElementsRepresentingShapes(shapesIds, documentFragment, writer) {
|
89 | const range = writer.createRangeIn(documentFragment);
|
90 | const imageElementsMatcher = new Matcher({
|
91 | name: 'img'
|
92 | });
|
93 | const imgs = [];
|
94 | for (const value of range) {
|
95 | if (value.item.is('element') && imageElementsMatcher.match(value.item)) {
|
96 | const el = value.item;
|
97 | const shapes = el.getAttribute('v:shapes') ? el.getAttribute('v:shapes').split(' ') : [];
|
98 | if (shapes.length && shapes.every(shape => shapesIds.indexOf(shape) > -1)) {
|
99 | imgs.push(el);
|
100 |
|
101 | }
|
102 | else if (!el.getAttribute('src')) {
|
103 | imgs.push(el);
|
104 | }
|
105 | }
|
106 | }
|
107 | for (const img of imgs) {
|
108 | writer.remove(img);
|
109 | }
|
110 | }
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | function removeAllShapeElements(documentFragment, writer) {
|
117 | const range = writer.createRangeIn(documentFragment);
|
118 | const shapeElementsMatcher = new Matcher({
|
119 | name: /v:(.+)/
|
120 | });
|
121 | const shapes = [];
|
122 | for (const value of range) {
|
123 | if (value.type == 'elementStart' && shapeElementsMatcher.match(value.item)) {
|
124 | shapes.push(value.item);
|
125 | }
|
126 | }
|
127 | for (const shape of shapes) {
|
128 | writer.remove(shape);
|
129 | }
|
130 | }
|
131 |
|
132 |
|
133 |
|
134 | function insertMissingImgs(shapeIds, documentFragment, writer) {
|
135 | const range = writer.createRangeIn(documentFragment);
|
136 | const shapes = [];
|
137 | for (const value of range) {
|
138 | if (value.type == 'elementStart' && value.item.is('element', 'v:shape')) {
|
139 | const id = value.item.getAttribute('id');
|
140 | if (shapeIds.includes(id)) {
|
141 | continue;
|
142 | }
|
143 | if (!containsMatchingImg(value.item.parent.getChildren(), id)) {
|
144 | shapes.push(value.item);
|
145 | }
|
146 | }
|
147 | }
|
148 | for (const shape of shapes) {
|
149 | const attrs = {
|
150 | src: findSrc(shape)
|
151 | };
|
152 | if (shape.hasAttribute('alt')) {
|
153 | attrs.alt = shape.getAttribute('alt');
|
154 | }
|
155 | const img = writer.createElement('img', attrs);
|
156 | writer.insertChild(shape.index + 1, img, shape.parent);
|
157 | }
|
158 | function containsMatchingImg(nodes, id) {
|
159 | for (const node of nodes) {
|
160 |
|
161 | if (node.is('element')) {
|
162 | if (node.name == 'img' && node.getAttribute('v:shapes') == id) {
|
163 | return true;
|
164 | }
|
165 | if (containsMatchingImg(node.getChildren(), id)) {
|
166 | return true;
|
167 | }
|
168 | }
|
169 | }
|
170 | return false;
|
171 | }
|
172 | function findSrc(shape) {
|
173 | for (const child of shape.getChildren()) {
|
174 |
|
175 | if (child.is('element') && child.getAttribute('src')) {
|
176 | return child.getAttribute('src');
|
177 | }
|
178 | }
|
179 | }
|
180 | }
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | function findAllImageElementsWithLocalSource(documentFragment, writer) {
|
188 | const range = writer.createRangeIn(documentFragment);
|
189 | const imageElementsMatcher = new Matcher({
|
190 | name: 'img'
|
191 | });
|
192 | const imgs = [];
|
193 | for (const value of range) {
|
194 | if (value.item.is('element') && imageElementsMatcher.match(value.item)) {
|
195 | if (value.item.getAttribute('src').startsWith('file://')) {
|
196 | imgs.push(value.item);
|
197 | }
|
198 | }
|
199 | }
|
200 | return imgs;
|
201 | }
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | function extractImageDataFromRtf(rtfData) {
|
212 | if (!rtfData) {
|
213 | return [];
|
214 | }
|
215 | const regexPictureHeader = /{\\pict[\s\S]+?\\bliptag-?\d+(\\blipupi-?\d+)?({\\\*\\blipuid\s?[\da-fA-F]+)?[\s}]*?/;
|
216 | const regexPicture = new RegExp('(?:(' + regexPictureHeader.source + '))([\\da-fA-F\\s]+)\\}', 'g');
|
217 | const images = rtfData.match(regexPicture);
|
218 | const result = [];
|
219 | if (images) {
|
220 | for (const image of images) {
|
221 | let imageType = false;
|
222 | if (image.includes('\\pngblip')) {
|
223 | imageType = 'image/png';
|
224 | }
|
225 | else if (image.includes('\\jpegblip')) {
|
226 | imageType = 'image/jpeg';
|
227 | }
|
228 | if (imageType) {
|
229 | result.push({
|
230 | hex: image.replace(regexPictureHeader, '').replace(/[^\da-fA-F]/g, ''),
|
231 | type: imageType
|
232 | });
|
233 | }
|
234 | }
|
235 | }
|
236 | return result;
|
237 | }
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 | function replaceImagesFileSourceWithInlineRepresentation(imageElements, imagesHexSources, writer) {
|
246 |
|
247 | if (imageElements.length === imagesHexSources.length) {
|
248 | for (let i = 0; i < imageElements.length; i++) {
|
249 | const newSrc = `data:${imagesHexSources[i].type};base64,${_convertHexToBase64(imagesHexSources[i].hex)}`;
|
250 | writer.setAttribute('src', newSrc, imageElements[i]);
|
251 | }
|
252 | }
|
253 | }
|