1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import { first } from 'ckeditor5/src/utils';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | export function modelToViewStyleAttribute( styles ) {
|
20 | return ( evt, data, conversionApi ) => {
|
21 | if ( !conversionApi.consumable.consume( data.item, evt.name ) ) {
|
22 | return;
|
23 | }
|
24 |
|
25 |
|
26 | const newStyle = getStyleDefinitionByName( data.attributeNewValue, styles );
|
27 | const oldStyle = getStyleDefinitionByName( data.attributeOldValue, styles );
|
28 |
|
29 | const viewElement = conversionApi.mapper.toViewElement( data.item );
|
30 | const viewWriter = conversionApi.writer;
|
31 |
|
32 | if ( oldStyle ) {
|
33 | viewWriter.removeClass( oldStyle.className, viewElement );
|
34 | }
|
35 |
|
36 | if ( newStyle ) {
|
37 | viewWriter.addClass( newStyle.className, viewElement );
|
38 | }
|
39 | };
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | export function viewToModelStyleAttribute( styles ) {
|
50 |
|
51 | const nonDefaultStyles = {
|
52 | imageInline: styles.filter( style => !style.isDefault && style.modelElements.includes( 'imageInline' ) ),
|
53 | imageBlock: styles.filter( style => !style.isDefault && style.modelElements.includes( 'imageBlock' ) )
|
54 | };
|
55 |
|
56 | return ( evt, data, conversionApi ) => {
|
57 | if ( !data.modelRange ) {
|
58 | return;
|
59 | }
|
60 |
|
61 | const viewElement = data.viewItem;
|
62 | const modelImageElement = first( data.modelRange.getItems() );
|
63 |
|
64 |
|
65 |
|
66 | if ( !modelImageElement ) {
|
67 | return;
|
68 | }
|
69 |
|
70 |
|
71 | if ( !conversionApi.schema.checkAttribute( modelImageElement, 'imageStyle' ) ) {
|
72 | return;
|
73 | }
|
74 |
|
75 |
|
76 | for ( const style of nonDefaultStyles[ modelImageElement.name ] ) {
|
77 |
|
78 | if ( conversionApi.consumable.consume( viewElement, { classes: style.className } ) ) {
|
79 |
|
80 | conversionApi.writer.setAttribute( 'imageStyle', style.name, modelImageElement );
|
81 | }
|
82 | }
|
83 | };
|
84 | }
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | function getStyleDefinitionByName( name, styles ) {
|
92 | for ( const style of styles ) {
|
93 | if ( style.name === name ) {
|
94 | return style;
|
95 | }
|
96 | }
|
97 | }
|