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 media-embed/converters
|
8 | */
|
9 |
|
10 | /**
|
11 | * Returns a function that converts the model "url" attribute to the view representation.
|
12 | *
|
13 | * Depending on the configuration, the view representation can be "semantic" (for the data pipeline):
|
14 | *
|
15 | * <figure class="media">
|
16 | * <oembed url="foo"></oembed>
|
17 | * </figure>
|
18 | *
|
19 | * or "non-semantic" (for the editing view pipeline):
|
20 | *
|
21 | * <figure class="media">
|
22 | * <div data-oembed-url="foo">[ non-semantic media preview for "foo" ]</div>
|
23 | * </figure>
|
24 | *
|
25 | * **Note:** Changing the model "url" attribute replaces the entire content of the
|
26 | * `<figure>` in the view.
|
27 | *
|
28 | * @param {module:media-embed/mediaregistry~MediaRegistry} registry The registry providing
|
29 | * the media and their content.
|
30 | * @param {Object} options
|
31 | * @param {String} [options.elementName] When set, overrides the default element name for semantic media embeds.
|
32 | * @param {String} [options.renderMediaPreview] When `true`, the converter will create the view in the non-semantic form.
|
33 | * @param {String} [options.renderForEditingView] When `true`, the converter will create a view specific for the
|
34 | * editing pipeline (e.g. including CSS classes, content placeholders).
|
35 | * @returns {Function}
|
36 | */
|
37 | export function modelToViewUrlAttributeConverter( registry, options ) {
|
38 | return dispatcher => {
|
39 | dispatcher.on( 'attribute:url:media', converter );
|
40 | };
|
41 |
|
42 | function converter( evt, data, conversionApi ) {
|
43 | if ( !conversionApi.consumable.consume( data.item, evt.name ) ) {
|
44 | return;
|
45 | }
|
46 |
|
47 | const url = data.attributeNewValue;
|
48 | const viewWriter = conversionApi.writer;
|
49 | const figure = conversionApi.mapper.toViewElement( data.item );
|
50 | const mediaContentElement = [ ...figure.getChildren() ]
|
51 | .find( child => child.getCustomProperty( 'media-content' ) );
|
52 |
|
53 | // TODO: removing the wrapper and creating it from scratch is a hack. We can do better than that.
|
54 | viewWriter.remove( mediaContentElement );
|
55 |
|
56 | const mediaViewElement = registry.getMediaViewElement( viewWriter, url, options );
|
57 |
|
58 | viewWriter.insert( viewWriter.createPositionAt( figure, 0 ), mediaViewElement );
|
59 | }
|
60 | }
|