1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | import { Matcher, UpcastWriter } from 'ckeditor5/src/engine';
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | export function transformListItemLikeElementsIntoLists( documentFragment, stylesString ) {
|
24 | if ( !documentFragment.childCount ) {
|
25 | return;
|
26 | }
|
27 |
|
28 | const writer = new UpcastWriter( documentFragment.document );
|
29 | const itemLikeElements = findAllItemLikeElements( documentFragment, writer );
|
30 |
|
31 | if ( !itemLikeElements.length ) {
|
32 | return;
|
33 | }
|
34 |
|
35 | let currentList = null;
|
36 | let currentIndentation = 1;
|
37 |
|
38 | itemLikeElements.forEach( ( itemLikeElement, i ) => {
|
39 | const isDifferentList = isNewListNeeded( itemLikeElements[ i - 1 ], itemLikeElement );
|
40 | const previousItemLikeElement = isDifferentList ? null : itemLikeElements[ i - 1 ];
|
41 | const indentationDifference = getIndentationDifference( previousItemLikeElement, itemLikeElement );
|
42 |
|
43 | if ( isDifferentList ) {
|
44 | currentList = null;
|
45 | currentIndentation = 1;
|
46 | }
|
47 |
|
48 | if ( !currentList || indentationDifference !== 0 ) {
|
49 | const listStyle = detectListStyle( itemLikeElement, stylesString );
|
50 |
|
51 | if ( !currentList ) {
|
52 | currentList = insertNewEmptyList( listStyle, itemLikeElement.element, writer );
|
53 | } else if ( itemLikeElement.indent > currentIndentation ) {
|
54 | const lastListItem = currentList.getChild( currentList.childCount - 1 );
|
55 | const lastListItemChild = lastListItem.getChild( lastListItem.childCount - 1 );
|
56 |
|
57 | currentList = insertNewEmptyList( listStyle, lastListItemChild, writer );
|
58 | currentIndentation += 1;
|
59 | } else if ( itemLikeElement.indent < currentIndentation ) {
|
60 | const differentIndentation = currentIndentation - itemLikeElement.indent;
|
61 |
|
62 | currentList = findParentListAtLevel( currentList, differentIndentation );
|
63 | currentIndentation = parseInt( itemLikeElement.indent );
|
64 | }
|
65 |
|
66 | if ( itemLikeElement.indent <= currentIndentation ) {
|
67 | if ( !currentList.is( 'element', listStyle.type ) ) {
|
68 | currentList = writer.rename( listStyle.type, currentList );
|
69 | }
|
70 | }
|
71 | }
|
72 |
|
73 | const listItem = transformElementIntoListItem( itemLikeElement.element, writer );
|
74 |
|
75 | writer.appendChild( listItem, currentList );
|
76 | } );
|
77 | }
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | export function unwrapParagraphInListItem( documentFragment, writer ) {
|
86 | for ( const value of writer.createRangeIn( documentFragment ) ) {
|
87 | const element = value.item;
|
88 |
|
89 | if ( element.is( 'element', 'li' ) ) {
|
90 |
|
91 | const firstChild = element.getChild( 0 );
|
92 |
|
93 | if ( firstChild && firstChild.is( 'element', 'p' ) ) {
|
94 | writer.unwrapElement( firstChild );
|
95 | }
|
96 | }
|
97 | }
|
98 | }
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | function findAllItemLikeElements( documentFragment, writer ) {
|
112 | const range = writer.createRangeIn( documentFragment );
|
113 |
|
114 |
|
115 | const itemLikeElementsMatcher = new Matcher( {
|
116 | name: /^p|h\d+$/,
|
117 | styles: {
|
118 | 'mso-list': /.*/
|
119 | }
|
120 | } );
|
121 |
|
122 | const itemLikeElements = [];
|
123 |
|
124 | for ( const value of range ) {
|
125 | if ( value.type === 'elementStart' && itemLikeElementsMatcher.match( value.item ) ) {
|
126 | const itemData = getListItemData( value.item );
|
127 |
|
128 | itemLikeElements.push( {
|
129 | element: value.item,
|
130 | id: itemData.id,
|
131 | order: itemData.order,
|
132 | indent: itemData.indent
|
133 | } );
|
134 | }
|
135 | }
|
136 |
|
137 | return itemLikeElements;
|
138 | }
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | function detectListStyle( listLikeItem, stylesString ) {
|
163 | const listStyleRegexp = new RegExp( `@list l${ listLikeItem.id }:level${ listLikeItem.indent }\\s*({[^}]*)`, 'gi' );
|
164 | const listStyleTypeRegex = /mso-level-number-format:([^;]{0,100});/gi;
|
165 | const listStartIndexRegex = /mso-level-start-at:\s{0,100}([0-9]{0,10})\s{0,100};/gi;
|
166 |
|
167 | const listStyleMatch = listStyleRegexp.exec( stylesString );
|
168 |
|
169 | let listStyleType = 'decimal';
|
170 | let type = 'ol';
|
171 | let startIndex = null;
|
172 |
|
173 | if ( listStyleMatch && listStyleMatch[ 1 ] ) {
|
174 | const listStyleTypeMatch = listStyleTypeRegex.exec( listStyleMatch[ 1 ] );
|
175 |
|
176 | if ( listStyleTypeMatch && listStyleTypeMatch[ 1 ] ) {
|
177 | listStyleType = listStyleTypeMatch[ 1 ].trim();
|
178 | type = listStyleType !== 'bullet' && listStyleType !== 'image' ? 'ol' : 'ul';
|
179 | }
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | if ( listStyleType === 'bullet' ) {
|
186 | const bulletedStyle = findBulletedListStyle( listLikeItem.element );
|
187 |
|
188 | if ( bulletedStyle ) {
|
189 | listStyleType = bulletedStyle;
|
190 | }
|
191 | } else {
|
192 | const listStartIndexMatch = listStartIndexRegex.exec( listStyleMatch[ 1 ] );
|
193 |
|
194 | if ( listStartIndexMatch && listStartIndexMatch[ 1 ] ) {
|
195 | startIndex = parseInt( listStartIndexMatch[ 1 ] );
|
196 | }
|
197 | }
|
198 | }
|
199 |
|
200 | return {
|
201 | type,
|
202 | startIndex,
|
203 | style: mapListStyleDefinition( listStyleType )
|
204 | };
|
205 | }
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | function findBulletedListStyle( element ) {
|
212 | const listMarkerElement = findListMarkerNode( element );
|
213 |
|
214 | if ( !listMarkerElement ) {
|
215 | return null;
|
216 | }
|
217 |
|
218 | const listMarker = listMarkerElement._data;
|
219 |
|
220 | if ( listMarker === 'o' ) {
|
221 | return 'circle';
|
222 | } else if ( listMarker === '·' ) {
|
223 | return 'disc';
|
224 | }
|
225 |
|
226 | else if ( listMarker === '§' ) {
|
227 | return 'square';
|
228 | }
|
229 |
|
230 | return null;
|
231 | }
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | function findListMarkerNode( element ) {
|
238 |
|
239 |
|
240 | if ( element.getChild( 0 ).is( '$text' ) ) {
|
241 | return null;
|
242 | }
|
243 |
|
244 | for ( const childNode of element.getChildren() ) {
|
245 |
|
246 |
|
247 | if ( !childNode.is( 'element', 'span' ) ) {
|
248 | continue;
|
249 | }
|
250 |
|
251 | const textNodeOrElement = childNode.getChild( 0 );
|
252 |
|
253 |
|
254 | if ( textNodeOrElement.is( '$text' ) ) {
|
255 | return textNodeOrElement;
|
256 | }
|
257 |
|
258 | return textNodeOrElement.getChild( 0 );
|
259 | }
|
260 | }
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 | function mapListStyleDefinition( value ) {
|
267 | if ( value.startsWith( 'arabic-leading-zero' ) ) {
|
268 | return 'decimal-leading-zero';
|
269 | }
|
270 |
|
271 | switch ( value ) {
|
272 | case 'alpha-upper':
|
273 | return 'upper-alpha';
|
274 | case 'alpha-lower':
|
275 | return 'lower-alpha';
|
276 | case 'roman-upper':
|
277 | return 'upper-roman';
|
278 | case 'roman-lower':
|
279 | return 'lower-roman';
|
280 | case 'circle':
|
281 | case 'disc':
|
282 | case 'square':
|
283 | return value;
|
284 | default:
|
285 | return null;
|
286 | }
|
287 | }
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 | function insertNewEmptyList( listStyle, element, writer ) {
|
298 | const parent = element.parent;
|
299 | const list = writer.createElement( listStyle.type );
|
300 | const position = parent.getChildIndex( element ) + 1;
|
301 |
|
302 | writer.insertChild( position, list, parent );
|
303 |
|
304 |
|
305 |
|
306 | if ( listStyle.style ) {
|
307 | writer.setStyle( 'list-style-type', listStyle.style, list );
|
308 | }
|
309 |
|
310 | if ( listStyle.startIndex && listStyle.startIndex > 1 ) {
|
311 | writer.setAttribute( 'start', listStyle.startIndex, list );
|
312 | }
|
313 |
|
314 | return list;
|
315 | }
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 | function transformElementIntoListItem( element, writer ) {
|
325 | removeBulletElement( element, writer );
|
326 |
|
327 | return writer.rename( 'li', element );
|
328 | }
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 | function getListItemData( element ) {
|
346 | const data = {};
|
347 | const listStyle = element.getStyle( 'mso-list' );
|
348 |
|
349 | if ( listStyle ) {
|
350 | const idMatch = listStyle.match( /(^|\s{1,100})l(\d+)/i );
|
351 | const orderMatch = listStyle.match( /\s{0,100}lfo(\d+)/i );
|
352 | const indentMatch = listStyle.match( /\s{0,100}level(\d+)/i );
|
353 |
|
354 | if ( idMatch && orderMatch && indentMatch ) {
|
355 | data.id = idMatch[ 2 ];
|
356 | data.order = orderMatch[ 1 ];
|
357 | data.indent = indentMatch[ 1 ];
|
358 | }
|
359 | }
|
360 |
|
361 | return data;
|
362 | }
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 | function removeBulletElement( element, writer ) {
|
369 |
|
370 | const bulletMatcher = new Matcher( {
|
371 | name: 'span',
|
372 | styles: {
|
373 | 'mso-list': 'Ignore'
|
374 | }
|
375 | } );
|
376 |
|
377 | const range = writer.createRangeIn( element );
|
378 |
|
379 | for ( const value of range ) {
|
380 | if ( value.type === 'elementStart' && bulletMatcher.match( value.item ) ) {
|
381 | writer.remove( value.item );
|
382 | }
|
383 | }
|
384 | }
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 | function isNewListNeeded( previousItem, currentItem ) {
|
398 | if ( !previousItem ) {
|
399 | return true;
|
400 | }
|
401 |
|
402 | if ( previousItem.id !== currentItem.id ) {
|
403 |
|
404 |
|
405 |
|
406 |
|
407 | if ( currentItem.indent - previousItem.indent === 1 ) {
|
408 | return false;
|
409 | }
|
410 |
|
411 | return true;
|
412 | }
|
413 |
|
414 | const previousSibling = currentItem.element.previousSibling;
|
415 |
|
416 | if ( !previousSibling ) {
|
417 | return true;
|
418 | }
|
419 |
|
420 |
|
421 | return !isList( previousSibling );
|
422 | }
|
423 |
|
424 | function isList( element ) {
|
425 | return element.is( 'element', 'ol' ) || element.is( 'element', 'ul' );
|
426 | }
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 | function getIndentationDifference( previousItem, currentItem ) {
|
435 | return previousItem ? currentItem.indent - previousItem.indent : currentItem.indent - 1;
|
436 | }
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 | function findParentListAtLevel( listElement, indentationDifference ) {
|
444 | const ancestors = listElement.getAncestors( { parentFirst: true } );
|
445 |
|
446 | let parentList = null;
|
447 | let levelChange = 0;
|
448 |
|
449 | for ( const ancestor of ancestors ) {
|
450 | if ( ancestor.name === 'ul' || ancestor.name === 'ol' ) {
|
451 | levelChange++;
|
452 | }
|
453 |
|
454 | if ( levelChange === indentationDifference ) {
|
455 | parentList = ancestor;
|
456 | break;
|
457 | }
|
458 | }
|
459 |
|
460 | return parentList;
|
461 | }
|