@ckeditor/ckeditor5-html-support 33.0.0 → 34.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +2 -2
- package/README.md +2 -1
- package/build/html-support.js +1 -1
- package/build/translations/en-au.js +1 -0
- package/build/translations/hr.js +1 -0
- package/build/translations/jv.js +1 -0
- package/build/translations/lv.js +1 -0
- package/build/translations/ur.js +1 -0
- package/build/translations/zh-cn.js +1 -0
- package/lang/translations/en-au.po +21 -0
- package/lang/translations/es.po +1 -1
- package/lang/translations/hr.po +21 -0
- package/lang/translations/it.po +1 -1
- package/lang/translations/jv.po +21 -0
- package/lang/translations/lv.po +21 -0
- package/lang/translations/pt-br.po +1 -1
- package/lang/translations/ur.po +21 -0
- package/lang/translations/zh-cn.po +21 -0
- package/package.json +31 -31
- package/src/conversionutils.js +48 -5
- package/src/converters.js +33 -16
- package/src/datafilter.js +134 -14
- package/src/dataschema.js +3 -0
- package/src/generalhtmlsupport.js +231 -1
- package/src/integrations/codeblock.js +13 -4
- package/src/integrations/customelement.js +162 -0
- package/src/integrations/documentlist.js +207 -0
- package/src/integrations/dualcontent.js +19 -3
- package/src/integrations/heading.js +7 -0
- package/src/integrations/image.js +64 -27
- package/src/integrations/mediaembed.js +45 -8
- package/src/integrations/script.js +7 -0
- package/src/integrations/style.js +7 -0
- package/src/integrations/table.js +36 -7
- package/src/schemadefinitions.js +66 -66
- package/theme/datafilter.css +5 -0
- package/build/html-support.js.map +0 -1
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { Plugin } from 'ckeditor5/src/core';
|
|
11
|
+
import { toArray } from 'ckeditor5/src/utils';
|
|
11
12
|
|
|
12
13
|
import DataFilter from './datafilter';
|
|
13
14
|
import CodeBlockElementSupport from './integrations/codeblock';
|
|
@@ -18,6 +19,8 @@ import MediaEmbedElementSupport from './integrations/mediaembed';
|
|
|
18
19
|
import ScriptElementSupport from './integrations/script';
|
|
19
20
|
import TableElementSupport from './integrations/table';
|
|
20
21
|
import StyleElementSupport from './integrations/style';
|
|
22
|
+
import DocumentListElementSupport from './integrations/documentlist';
|
|
23
|
+
import CustomElementSupport from './integrations/customelement';
|
|
21
24
|
|
|
22
25
|
/**
|
|
23
26
|
* The General HTML Support feature.
|
|
@@ -48,7 +51,9 @@ export default class GeneralHtmlSupport extends Plugin {
|
|
|
48
51
|
MediaEmbedElementSupport,
|
|
49
52
|
ScriptElementSupport,
|
|
50
53
|
TableElementSupport,
|
|
51
|
-
StyleElementSupport
|
|
54
|
+
StyleElementSupport,
|
|
55
|
+
DocumentListElementSupport,
|
|
56
|
+
CustomElementSupport
|
|
52
57
|
];
|
|
53
58
|
}
|
|
54
59
|
|
|
@@ -63,6 +68,231 @@ export default class GeneralHtmlSupport extends Plugin {
|
|
|
63
68
|
dataFilter.loadAllowedConfig( editor.config.get( 'htmlSupport.allow' ) || [] );
|
|
64
69
|
dataFilter.loadDisallowedConfig( editor.config.get( 'htmlSupport.disallow' ) || [] );
|
|
65
70
|
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Returns a GHS model attribute name related to a given view element name.
|
|
74
|
+
*
|
|
75
|
+
* @protected
|
|
76
|
+
* @param {String} viewElementName A view element name.
|
|
77
|
+
* @returns {String}
|
|
78
|
+
*/
|
|
79
|
+
getGhsAttributeNameForElement( viewElementName ) {
|
|
80
|
+
const dataSchema = this.editor.plugins.get( 'DataSchema' );
|
|
81
|
+
const definitions = Array.from( dataSchema.getDefinitionsForView( viewElementName, false ) );
|
|
82
|
+
|
|
83
|
+
if ( definitions && definitions.length && definitions[ 0 ].isInline && !definitions[ 0 ].isObject ) {
|
|
84
|
+
return definitions[ 0 ].model;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return 'htmlAttributes';
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Updates GHS model attribute for a specified view element name, so it includes the given class name.
|
|
92
|
+
*
|
|
93
|
+
* @protected
|
|
94
|
+
* @param {String} viewElementName A view element name.
|
|
95
|
+
* @param {String|Array.<String>} className The css class to add.
|
|
96
|
+
* @param {module:engine/model/selection~Selectable} selectable The selection or element to update.
|
|
97
|
+
*/
|
|
98
|
+
addModelHtmlClass( viewElementName, className, selectable ) {
|
|
99
|
+
const model = this.editor.model;
|
|
100
|
+
const ghsAttributeName = this.getGhsAttributeNameForElement( viewElementName );
|
|
101
|
+
|
|
102
|
+
model.change( writer => {
|
|
103
|
+
for ( const item of getItemsToUpdateGhsAttribute( model, selectable, ghsAttributeName ) ) {
|
|
104
|
+
modifyGhsAttribute( writer, item, ghsAttributeName, 'classes', classes => {
|
|
105
|
+
for ( const value of toArray( className ) ) {
|
|
106
|
+
classes.add( value );
|
|
107
|
+
}
|
|
108
|
+
} );
|
|
109
|
+
}
|
|
110
|
+
} );
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Updates GHS model attribute for a specified view element name, so it does not include the given class name.
|
|
115
|
+
*
|
|
116
|
+
* @protected
|
|
117
|
+
* @param {String} viewElementName A view element name.
|
|
118
|
+
* @param {String|Array.<String>} className The css class to remove.
|
|
119
|
+
* @param {module:engine/model/selection~Selectable} selectable The selection or element to update.
|
|
120
|
+
*/
|
|
121
|
+
removeModelHtmlClass( viewElementName, className, selectable ) {
|
|
122
|
+
const model = this.editor.model;
|
|
123
|
+
const ghsAttributeName = this.getGhsAttributeNameForElement( viewElementName );
|
|
124
|
+
|
|
125
|
+
model.change( writer => {
|
|
126
|
+
for ( const item of getItemsToUpdateGhsAttribute( model, selectable, ghsAttributeName ) ) {
|
|
127
|
+
modifyGhsAttribute( writer, item, ghsAttributeName, 'classes', classes => {
|
|
128
|
+
for ( const value of toArray( className ) ) {
|
|
129
|
+
classes.delete( value );
|
|
130
|
+
}
|
|
131
|
+
} );
|
|
132
|
+
}
|
|
133
|
+
} );
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Updates GHS model attribute for a specified view element name, so it includes the given attribute.
|
|
138
|
+
*
|
|
139
|
+
* @protected
|
|
140
|
+
* @param {String} viewElementName A view element name.
|
|
141
|
+
* @param {Object} attributes The object with attributes to set.
|
|
142
|
+
* @param {module:engine/model/selection~Selectable} selectable The selection or element to update.
|
|
143
|
+
*/
|
|
144
|
+
setModelHtmlAttributes( viewElementName, attributes, selectable ) {
|
|
145
|
+
const model = this.editor.model;
|
|
146
|
+
const ghsAttributeName = this.getGhsAttributeNameForElement( viewElementName );
|
|
147
|
+
|
|
148
|
+
model.change( writer => {
|
|
149
|
+
for ( const item of getItemsToUpdateGhsAttribute( model, selectable, ghsAttributeName ) ) {
|
|
150
|
+
modifyGhsAttribute( writer, item, ghsAttributeName, 'attributes', attributesMap => {
|
|
151
|
+
for ( const [ key, value ] of Object.entries( attributes ) ) {
|
|
152
|
+
attributesMap.set( key, value );
|
|
153
|
+
}
|
|
154
|
+
} );
|
|
155
|
+
}
|
|
156
|
+
} );
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Updates GHS model attribute for a specified view element name, so it does not include the given attribute.
|
|
161
|
+
*
|
|
162
|
+
* @protected
|
|
163
|
+
* @param {String} viewElementName A view element name.
|
|
164
|
+
* @param {String|Array.<String>} attributeName The attribute name (or names) to remove.
|
|
165
|
+
* @param {module:engine/model/selection~Selectable} selectable The selection or element to update.
|
|
166
|
+
*/
|
|
167
|
+
removeModelHtmlAttributes( viewElementName, attributeName, selectable ) {
|
|
168
|
+
const model = this.editor.model;
|
|
169
|
+
const ghsAttributeName = this.getGhsAttributeNameForElement( viewElementName );
|
|
170
|
+
|
|
171
|
+
model.change( writer => {
|
|
172
|
+
for ( const item of getItemsToUpdateGhsAttribute( model, selectable, ghsAttributeName ) ) {
|
|
173
|
+
modifyGhsAttribute( writer, item, ghsAttributeName, 'attributes', attributesMap => {
|
|
174
|
+
for ( const key of toArray( attributeName ) ) {
|
|
175
|
+
attributesMap.delete( key );
|
|
176
|
+
}
|
|
177
|
+
} );
|
|
178
|
+
}
|
|
179
|
+
} );
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Updates GHS model attribute for a specified view element name, so it includes a given style.
|
|
184
|
+
*
|
|
185
|
+
* @protected
|
|
186
|
+
* @param {String} viewElementName A view element name.
|
|
187
|
+
* @param {Object} styles The object with styles to set.
|
|
188
|
+
* @param {module:engine/model/selection~Selectable} selectable The selection or element to update.
|
|
189
|
+
*/
|
|
190
|
+
setModelHtmlStyles( viewElementName, styles, selectable ) {
|
|
191
|
+
const model = this.editor.model;
|
|
192
|
+
const ghsAttributeName = this.getGhsAttributeNameForElement( viewElementName );
|
|
193
|
+
|
|
194
|
+
model.change( writer => {
|
|
195
|
+
for ( const item of getItemsToUpdateGhsAttribute( model, selectable, ghsAttributeName ) ) {
|
|
196
|
+
modifyGhsAttribute( writer, item, ghsAttributeName, 'styles', stylesMap => {
|
|
197
|
+
for ( const [ key, value ] of Object.entries( styles ) ) {
|
|
198
|
+
stylesMap.set( key, value );
|
|
199
|
+
}
|
|
200
|
+
} );
|
|
201
|
+
}
|
|
202
|
+
} );
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Updates GHS model attribute for a specified view element name, so it does not include a given style.
|
|
207
|
+
*
|
|
208
|
+
* @protected
|
|
209
|
+
* @param {String} viewElementName A view element name.
|
|
210
|
+
* @param {String|Array.<String>} properties The style (or styles list) to remove.
|
|
211
|
+
* @param {module:engine/model/selection~Selectable} selectable The selection or element to update.
|
|
212
|
+
*/
|
|
213
|
+
removeModelHtmlStyles( viewElementName, properties, selectable ) {
|
|
214
|
+
const model = this.editor.model;
|
|
215
|
+
const ghsAttributeName = this.getGhsAttributeNameForElement( viewElementName );
|
|
216
|
+
|
|
217
|
+
model.change( writer => {
|
|
218
|
+
for ( const item of getItemsToUpdateGhsAttribute( model, selectable, ghsAttributeName ) ) {
|
|
219
|
+
modifyGhsAttribute( writer, item, ghsAttributeName, 'styles', stylesMap => {
|
|
220
|
+
for ( const key of toArray( properties ) ) {
|
|
221
|
+
stylesMap.delete( key );
|
|
222
|
+
}
|
|
223
|
+
} );
|
|
224
|
+
}
|
|
225
|
+
} );
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Returns an iterator over an items in the selectable that accept given GHS attribute.
|
|
230
|
+
function* getItemsToUpdateGhsAttribute( model, selectable, ghsAttributeName ) {
|
|
231
|
+
if ( selectable.is( 'documentSelection' ) && selectable.isCollapsed ) {
|
|
232
|
+
if ( model.schema.checkAttributeInSelection( selectable, ghsAttributeName ) ) {
|
|
233
|
+
yield selectable;
|
|
234
|
+
}
|
|
235
|
+
} else {
|
|
236
|
+
for ( const range of getValidRangesForSelectable( model, selectable, ghsAttributeName ) ) {
|
|
237
|
+
yield* range.getItems( { shallow: true } );
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Translates a given selectable to an iterable of ranges.
|
|
243
|
+
function getValidRangesForSelectable( model, selectable, ghsAttributeName ) {
|
|
244
|
+
if ( selectable.is( 'node' ) || selectable.is( '$text' ) || selectable.is( '$textProxy' ) ) {
|
|
245
|
+
if ( model.schema.checkAttribute( selectable, ghsAttributeName ) ) {
|
|
246
|
+
return [ model.createRangeOn( selectable ) ];
|
|
247
|
+
} else {
|
|
248
|
+
return [];
|
|
249
|
+
}
|
|
250
|
+
} else {
|
|
251
|
+
return model.schema.getValidRanges( model.createSelection( selectable ).getRanges(), ghsAttributeName );
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Updates a GHS attribute on a specified item.
|
|
256
|
+
// @param {module:engine/model/writer~Writer} writer
|
|
257
|
+
// @param {module:engine/model/item~Item|module:engine/model/documentselection~DocumentSelection} item
|
|
258
|
+
// @param {String} ghsAttributeName
|
|
259
|
+
// @param {'classes'|'attributes'|'styles'} subject
|
|
260
|
+
// @param {Function} callback That receives a map or set as an argument and should modify it (add or remove entries).
|
|
261
|
+
function modifyGhsAttribute( writer, item, ghsAttributeName, subject, callback ) {
|
|
262
|
+
const oldValue = item.getAttribute( ghsAttributeName );
|
|
263
|
+
const newValue = {};
|
|
264
|
+
|
|
265
|
+
for ( const kind of [ 'attributes', 'styles', 'classes' ] ) {
|
|
266
|
+
if ( kind != subject ) {
|
|
267
|
+
if ( oldValue && oldValue[ kind ] ) {
|
|
268
|
+
newValue[ kind ] = oldValue[ kind ];
|
|
269
|
+
}
|
|
270
|
+
} else {
|
|
271
|
+
const values = kind == 'classes' ?
|
|
272
|
+
new Set( oldValue && oldValue[ kind ] || [] ) :
|
|
273
|
+
new Map( Object.entries( oldValue && oldValue[ kind ] || {} ) );
|
|
274
|
+
|
|
275
|
+
callback( values );
|
|
276
|
+
|
|
277
|
+
if ( values.size ) {
|
|
278
|
+
newValue[ kind ] = kind == 'classes' ? Array.from( values ) : Object.fromEntries( values );
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if ( Object.keys( newValue ).length ) {
|
|
284
|
+
if ( item.is( 'documentSelection' ) ) {
|
|
285
|
+
writer.setSelectionAttribute( ghsAttributeName, newValue );
|
|
286
|
+
} else {
|
|
287
|
+
writer.setAttribute( ghsAttributeName, newValue, item );
|
|
288
|
+
}
|
|
289
|
+
} else if ( oldValue ) {
|
|
290
|
+
if ( item.is( 'documentSelection' ) ) {
|
|
291
|
+
writer.removeSelectionAttribute( ghsAttributeName );
|
|
292
|
+
} else {
|
|
293
|
+
writer.removeAttribute( ghsAttributeName, item );
|
|
294
|
+
}
|
|
295
|
+
}
|
|
66
296
|
}
|
|
67
297
|
|
|
68
298
|
/**
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { Plugin } from 'ckeditor5/src/core';
|
|
11
|
-
import {
|
|
11
|
+
import { updateViewAttributes } from '../conversionutils.js';
|
|
12
12
|
|
|
13
13
|
import DataFilter from '../datafilter';
|
|
14
14
|
|
|
@@ -25,6 +25,13 @@ export default class CodeBlockElementSupport extends Plugin {
|
|
|
25
25
|
return [ DataFilter ];
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
/**
|
|
29
|
+
* @inheritDoc
|
|
30
|
+
*/
|
|
31
|
+
static get pluginName() {
|
|
32
|
+
return 'CodeBlockElementSupport';
|
|
33
|
+
}
|
|
34
|
+
|
|
28
35
|
/**
|
|
29
36
|
* @inheritDoc
|
|
30
37
|
*/
|
|
@@ -79,7 +86,7 @@ function viewToModelCodeBlockAttributeConverter( dataFilter ) {
|
|
|
79
86
|
preserveElementAttributes( viewCodeElement, 'htmlContentAttributes' );
|
|
80
87
|
|
|
81
88
|
function preserveElementAttributes( viewElement, attributeName ) {
|
|
82
|
-
const viewAttributes = dataFilter.
|
|
89
|
+
const viewAttributes = dataFilter.processViewAttributes( viewElement, conversionApi );
|
|
83
90
|
|
|
84
91
|
if ( viewAttributes ) {
|
|
85
92
|
conversionApi.writer.setAttribute( attributeName, viewAttributes, data.modelRange );
|
|
@@ -101,10 +108,11 @@ function modelToViewCodeBlockAttributeConverter() {
|
|
|
101
108
|
return;
|
|
102
109
|
}
|
|
103
110
|
|
|
111
|
+
const { attributeOldValue, attributeNewValue } = data;
|
|
104
112
|
const viewCodeElement = conversionApi.mapper.toViewElement( data.item );
|
|
105
113
|
const viewPreElement = viewCodeElement.parent;
|
|
106
114
|
|
|
107
|
-
|
|
115
|
+
updateViewAttributes( conversionApi.writer, attributeOldValue, attributeNewValue, viewPreElement );
|
|
108
116
|
} );
|
|
109
117
|
|
|
110
118
|
dispatcher.on( 'attribute:htmlContentAttributes:codeBlock', ( evt, data, conversionApi ) => {
|
|
@@ -112,9 +120,10 @@ function modelToViewCodeBlockAttributeConverter() {
|
|
|
112
120
|
return;
|
|
113
121
|
}
|
|
114
122
|
|
|
123
|
+
const { attributeOldValue, attributeNewValue } = data;
|
|
115
124
|
const viewCodeElement = conversionApi.mapper.toViewElement( data.item );
|
|
116
125
|
|
|
117
|
-
|
|
126
|
+
updateViewAttributes( conversionApi.writer, attributeOldValue, attributeNewValue, viewCodeElement );
|
|
118
127
|
} );
|
|
119
128
|
};
|
|
120
129
|
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @module html-support/integrations/customelement
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { Plugin } from 'ckeditor5/src/core';
|
|
11
|
+
import { UpcastWriter } from 'ckeditor5/src/engine';
|
|
12
|
+
|
|
13
|
+
import DataSchema from '../dataschema';
|
|
14
|
+
import DataFilter from '../datafilter';
|
|
15
|
+
import { setViewAttributes } from '../conversionutils';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Provides the General HTML Support for custom elements (not registered in the {@link module:html-support/dataschema~DataSchema}).
|
|
19
|
+
*
|
|
20
|
+
* @extends module:core/plugin~Plugin
|
|
21
|
+
*/
|
|
22
|
+
export default class CustomElementSupport extends Plugin {
|
|
23
|
+
/**
|
|
24
|
+
* @inheritDoc
|
|
25
|
+
*/
|
|
26
|
+
static get requires() {
|
|
27
|
+
return [ DataFilter, DataSchema ];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @inheritDoc
|
|
32
|
+
*/
|
|
33
|
+
static get pluginName() {
|
|
34
|
+
return 'CustomElementSupport';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @inheritDoc
|
|
39
|
+
*/
|
|
40
|
+
init() {
|
|
41
|
+
const dataFilter = this.editor.plugins.get( DataFilter );
|
|
42
|
+
const dataSchema = this.editor.plugins.get( DataSchema );
|
|
43
|
+
|
|
44
|
+
dataFilter.on( 'register:$customElement', ( evt, definition ) => {
|
|
45
|
+
evt.stop();
|
|
46
|
+
|
|
47
|
+
const editor = this.editor;
|
|
48
|
+
const schema = editor.model.schema;
|
|
49
|
+
const conversion = editor.conversion;
|
|
50
|
+
const unsafeElements = editor.editing.view.domConverter.unsafeElements;
|
|
51
|
+
const preLikeElements = editor.data.htmlProcessor.domConverter.preElements;
|
|
52
|
+
|
|
53
|
+
schema.register( definition.model, definition.modelSchema );
|
|
54
|
+
schema.extend( definition.model, {
|
|
55
|
+
allowAttributes: [ 'htmlElementName', 'htmlAttributes', 'htmlContent' ],
|
|
56
|
+
isContent: true
|
|
57
|
+
} );
|
|
58
|
+
|
|
59
|
+
// Being executed on the low priority, it will catch all elements that were not caught by other converters.
|
|
60
|
+
conversion.for( 'upcast' ).elementToElement( {
|
|
61
|
+
view: /.*/,
|
|
62
|
+
model: ( viewElement, conversionApi ) => {
|
|
63
|
+
// Do not try to convert $comment fake element.
|
|
64
|
+
if ( viewElement.name == '$comment' ) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Allow for fallback only if this element is not defined in data schema to make sure
|
|
69
|
+
// that this will handle only custom elements not registered in the data schema.
|
|
70
|
+
if ( dataSchema.getDefinitionsForView( viewElement.name ).size ) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Make sure that this element will not render in the editing view.
|
|
75
|
+
if ( !unsafeElements.includes( viewElement.name ) ) {
|
|
76
|
+
unsafeElements.push( viewElement.name );
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Make sure that whitespaces will not be trimmed or replaced by nbsps while stringify content.
|
|
80
|
+
if ( !preLikeElements.includes( viewElement.name ) ) {
|
|
81
|
+
preLikeElements.push( viewElement.name );
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const modelElement = conversionApi.writer.createElement( definition.model, {
|
|
85
|
+
htmlElementName: viewElement.name
|
|
86
|
+
} );
|
|
87
|
+
|
|
88
|
+
const htmlAttributes = dataFilter.processViewAttributes( viewElement, conversionApi );
|
|
89
|
+
|
|
90
|
+
if ( htmlAttributes ) {
|
|
91
|
+
conversionApi.writer.setAttribute( 'htmlAttributes', htmlAttributes, modelElement );
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Store the whole element in the attribute so that DomConverter will be able to use the pre like element context.
|
|
95
|
+
const viewWriter = new UpcastWriter( viewElement.document );
|
|
96
|
+
const documentFragment = viewWriter.createDocumentFragment( viewElement );
|
|
97
|
+
const htmlContent = editor.data.processor.toData( documentFragment );
|
|
98
|
+
|
|
99
|
+
conversionApi.writer.setAttribute( 'htmlContent', htmlContent, modelElement );
|
|
100
|
+
|
|
101
|
+
// Consume the content of the element.
|
|
102
|
+
for ( const { item } of editor.editing.view.createRangeIn( viewElement ) ) {
|
|
103
|
+
conversionApi.consumable.consume( item, { name: true } );
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return modelElement;
|
|
107
|
+
},
|
|
108
|
+
converterPriority: 'low'
|
|
109
|
+
} );
|
|
110
|
+
|
|
111
|
+
// Because this element is unsafe (DomConverter#unsafeElements), it will render as a transparent <span> but it must
|
|
112
|
+
// be rendered anyway for the mapping between the model and the view to exist.
|
|
113
|
+
conversion.for( 'editingDowncast' ).elementToElement( {
|
|
114
|
+
model: {
|
|
115
|
+
name: definition.model,
|
|
116
|
+
attributes: [ 'htmlElementName', 'htmlAttributes', 'htmlContent' ]
|
|
117
|
+
},
|
|
118
|
+
view: ( modelElement, { writer } ) => {
|
|
119
|
+
const viewName = modelElement.getAttribute( 'htmlElementName' );
|
|
120
|
+
const viewElement = writer.createRawElement( viewName );
|
|
121
|
+
|
|
122
|
+
if ( modelElement.hasAttribute( 'htmlAttributes' ) ) {
|
|
123
|
+
setViewAttributes( writer, modelElement.getAttribute( 'htmlAttributes' ), viewElement );
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return viewElement;
|
|
127
|
+
}
|
|
128
|
+
} );
|
|
129
|
+
|
|
130
|
+
conversion.for( 'dataDowncast' ).elementToElement( {
|
|
131
|
+
model: {
|
|
132
|
+
name: definition.model,
|
|
133
|
+
attributes: [ 'htmlElementName', 'htmlAttributes', 'htmlContent' ]
|
|
134
|
+
},
|
|
135
|
+
view: ( modelElement, { writer } ) => {
|
|
136
|
+
const viewName = modelElement.getAttribute( 'htmlElementName' );
|
|
137
|
+
const htmlContent = modelElement.getAttribute( 'htmlContent' );
|
|
138
|
+
|
|
139
|
+
const viewElement = writer.createRawElement( viewName, null, ( domElement, domConverter ) => {
|
|
140
|
+
domConverter.setContentOf( domElement, htmlContent );
|
|
141
|
+
|
|
142
|
+
// Unwrap the custom element content (it was stored in the attribute as the whole custom element).
|
|
143
|
+
// See the upcast conversion for the "htmlContent" attribute to learn more.
|
|
144
|
+
const customElement = domElement.firstChild;
|
|
145
|
+
|
|
146
|
+
customElement.remove();
|
|
147
|
+
|
|
148
|
+
while ( customElement.firstChild ) {
|
|
149
|
+
domElement.appendChild( customElement.firstChild );
|
|
150
|
+
}
|
|
151
|
+
} );
|
|
152
|
+
|
|
153
|
+
if ( modelElement.hasAttribute( 'htmlAttributes' ) ) {
|
|
154
|
+
setViewAttributes( writer, modelElement.getAttribute( 'htmlAttributes' ), viewElement );
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return viewElement;
|
|
158
|
+
}
|
|
159
|
+
} );
|
|
160
|
+
} );
|
|
161
|
+
}
|
|
162
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @module html-support/integrations/documentlist
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { isEqual } from 'lodash-es';
|
|
11
|
+
import { Plugin } from 'ckeditor5/src/core';
|
|
12
|
+
import { setViewAttributes } from '../conversionutils.js';
|
|
13
|
+
|
|
14
|
+
import DataFilter from '../datafilter';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Provides the General HTML Support integration with the {@link module:list/documentlist~DocumentList Document List} feature.
|
|
18
|
+
*
|
|
19
|
+
* @extends module:core/plugin~Plugin
|
|
20
|
+
*/
|
|
21
|
+
export default class DocumentListElementSupport extends Plugin {
|
|
22
|
+
/**
|
|
23
|
+
* @inheritDoc
|
|
24
|
+
*/
|
|
25
|
+
static get requires() {
|
|
26
|
+
return [ DataFilter ];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @inheritDoc
|
|
31
|
+
*/
|
|
32
|
+
static get pluginName() {
|
|
33
|
+
return 'DocumentListElementSupport';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @inheritDoc
|
|
38
|
+
*/
|
|
39
|
+
init() {
|
|
40
|
+
const editor = this.editor;
|
|
41
|
+
|
|
42
|
+
if ( !editor.plugins.has( 'DocumentListEditing' ) ) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const schema = editor.model.schema;
|
|
47
|
+
const conversion = editor.conversion;
|
|
48
|
+
const dataFilter = editor.plugins.get( DataFilter );
|
|
49
|
+
const documentListEditing = editor.plugins.get( 'DocumentListEditing' );
|
|
50
|
+
|
|
51
|
+
// Register downcast strategy.
|
|
52
|
+
// Note that this must be done before document list editing registers conversion in afterInit.
|
|
53
|
+
documentListEditing.registerDowncastStrategy( {
|
|
54
|
+
scope: 'item',
|
|
55
|
+
attributeName: 'htmlLiAttributes',
|
|
56
|
+
|
|
57
|
+
setAttributeOnDowncast( writer, attributeValue, viewElement ) {
|
|
58
|
+
setViewAttributes( writer, attributeValue, viewElement );
|
|
59
|
+
}
|
|
60
|
+
} );
|
|
61
|
+
|
|
62
|
+
documentListEditing.registerDowncastStrategy( {
|
|
63
|
+
scope: 'list',
|
|
64
|
+
attributeName: 'htmlListAttributes',
|
|
65
|
+
|
|
66
|
+
setAttributeOnDowncast( writer, viewAttributes, viewElement ) {
|
|
67
|
+
setViewAttributes( writer, viewAttributes, viewElement );
|
|
68
|
+
}
|
|
69
|
+
} );
|
|
70
|
+
|
|
71
|
+
dataFilter.on( 'register', ( evt, definition ) => {
|
|
72
|
+
if ( ![ 'ul', 'ol', 'li' ].includes( definition.view ) ) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
evt.stop();
|
|
77
|
+
|
|
78
|
+
// Do not register same converters twice.
|
|
79
|
+
if ( schema.checkAttribute( '$block', 'htmlListAttributes' ) ) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
schema.extend( '$block', { allowAttributes: [ 'htmlListAttributes', 'htmlLiAttributes' ] } );
|
|
84
|
+
schema.extend( '$blockObject', { allowAttributes: [ 'htmlListAttributes', 'htmlLiAttributes' ] } );
|
|
85
|
+
schema.extend( '$container', { allowAttributes: [ 'htmlListAttributes', 'htmlLiAttributes' ] } );
|
|
86
|
+
|
|
87
|
+
conversion.for( 'upcast' ).add( dispatcher => {
|
|
88
|
+
dispatcher.on( 'element:ul', viewToModelListAttributeConverter( 'htmlListAttributes', dataFilter ), { priority: 'low' } );
|
|
89
|
+
dispatcher.on( 'element:ol', viewToModelListAttributeConverter( 'htmlListAttributes', dataFilter ), { priority: 'low' } );
|
|
90
|
+
dispatcher.on( 'element:li', viewToModelListAttributeConverter( 'htmlLiAttributes', dataFilter ), { priority: 'low' } );
|
|
91
|
+
} );
|
|
92
|
+
} );
|
|
93
|
+
|
|
94
|
+
// Make sure that all items in a single list (items at the same level & listType) have the same properties.
|
|
95
|
+
// Note: This is almost an exact copy from DocumentListPropertiesEditing.
|
|
96
|
+
documentListEditing.on( 'postFixer', ( evt, { listNodes, writer } ) => {
|
|
97
|
+
const previousNodesByIndent = []; // Last seen nodes of lower indented lists.
|
|
98
|
+
|
|
99
|
+
for ( const { node, previous } of listNodes ) {
|
|
100
|
+
// For the first list block there is nothing to compare with.
|
|
101
|
+
if ( !previous ) {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const nodeIndent = node.getAttribute( 'listIndent' );
|
|
106
|
+
const previousNodeIndent = previous.getAttribute( 'listIndent' );
|
|
107
|
+
|
|
108
|
+
let previousNodeInList = null; // It's like `previous` but has the same indent as current node.
|
|
109
|
+
|
|
110
|
+
// Let's find previous node for the same indent.
|
|
111
|
+
// We're going to need that when we get back to previous indent.
|
|
112
|
+
if ( nodeIndent > previousNodeIndent ) {
|
|
113
|
+
previousNodesByIndent[ previousNodeIndent ] = previous;
|
|
114
|
+
}
|
|
115
|
+
// Restore the one for given indent.
|
|
116
|
+
else if ( nodeIndent < previousNodeIndent ) {
|
|
117
|
+
previousNodeInList = previousNodesByIndent[ nodeIndent ];
|
|
118
|
+
previousNodesByIndent.length = nodeIndent;
|
|
119
|
+
}
|
|
120
|
+
// Same indent.
|
|
121
|
+
else {
|
|
122
|
+
previousNodeInList = previous;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// This is a first item of a nested list.
|
|
126
|
+
if ( !previousNodeInList ) {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if ( previousNodeInList.getAttribute( 'listType' ) == node.getAttribute( 'listType' ) ) {
|
|
131
|
+
const value = previousNodeInList.getAttribute( 'htmlListAttributes' );
|
|
132
|
+
|
|
133
|
+
if ( !isEqual( node.getAttribute( 'htmlListAttributes' ), value ) ) {
|
|
134
|
+
writer.setAttribute( 'htmlListAttributes', value, node );
|
|
135
|
+
evt.return = true;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if ( previousNodeInList.getAttribute( 'listItemId' ) == node.getAttribute( 'listItemId' ) ) {
|
|
140
|
+
const value = previousNodeInList.getAttribute( 'htmlLiAttributes' );
|
|
141
|
+
|
|
142
|
+
if ( !isEqual( node.getAttribute( 'htmlLiAttributes' ), value ) ) {
|
|
143
|
+
writer.setAttribute( 'htmlLiAttributes', value, node );
|
|
144
|
+
evt.return = true;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
} );
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* @inheritDoc
|
|
153
|
+
*/
|
|
154
|
+
afterInit() {
|
|
155
|
+
const editor = this.editor;
|
|
156
|
+
|
|
157
|
+
if ( !editor.commands.get( 'indentList' ) ) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Reset list attributes after indenting list items.
|
|
162
|
+
this.listenTo( editor.commands.get( 'indentList' ), 'afterExecute', ( evt, changedBlocks ) => {
|
|
163
|
+
editor.model.change( writer => {
|
|
164
|
+
for ( const node of changedBlocks ) {
|
|
165
|
+
// Just reset the attribute.
|
|
166
|
+
// If there is a previous indented list that this node should be merged into,
|
|
167
|
+
// the postfixer will unify all the attributes of both sub-lists.
|
|
168
|
+
writer.setAttribute( 'htmlListAttributes', {}, node );
|
|
169
|
+
}
|
|
170
|
+
} );
|
|
171
|
+
} );
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// View-to-model conversion helper preserving allowed attributes on {@link TODO}
|
|
176
|
+
// feature model element.
|
|
177
|
+
//
|
|
178
|
+
// @private
|
|
179
|
+
// @param {String} attributeName
|
|
180
|
+
// @param {module:html-support/datafilter~DataFilter} dataFilter
|
|
181
|
+
// @returns {Function} Returns a conversion callback.
|
|
182
|
+
function viewToModelListAttributeConverter( attributeName, dataFilter ) {
|
|
183
|
+
return ( evt, data, conversionApi ) => {
|
|
184
|
+
const viewElement = data.viewItem;
|
|
185
|
+
|
|
186
|
+
if ( !data.modelRange ) {
|
|
187
|
+
Object.assign( data, conversionApi.convertChildren( data.viewItem, data.modelCursor ) );
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const viewAttributes = dataFilter.processViewAttributes( viewElement, conversionApi );
|
|
191
|
+
|
|
192
|
+
for ( const item of data.modelRange.getItems( { shallow: true } ) ) {
|
|
193
|
+
// Apply only to list item blocks.
|
|
194
|
+
if ( !item.hasAttribute( 'listItemId' ) ) {
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Set list attributes only on same level items, those nested deeper are already handled
|
|
199
|
+
// by the recursive conversion.
|
|
200
|
+
if ( item.hasAttribute( attributeName ) ) {
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
conversionApi.writer.setAttribute( attributeName, viewAttributes || {}, item );
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
}
|