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/utils
|
8 | */
|
9 |
|
10 | import { isWidget, toWidget } from 'ckeditor5/src/widget';
|
11 |
|
12 | /**
|
13 | * Converts a given {@link module:engine/view/element~Element} to a media embed widget:
|
14 | * * Adds a {@link module:engine/view/element~Element#_setCustomProperty custom property} allowing to recognize the media widget element.
|
15 | * * Calls the {@link module:widget/utils~toWidget} function with the proper element's label creator.
|
16 | *
|
17 | * @param {module:engine/view/element~Element} viewElement
|
18 | * @param {module:engine/view/downcastwriter~DowncastWriter} writer An instance of the view writer.
|
19 | * @param {String} label The element's label.
|
20 | * @returns {module:engine/view/element~Element}
|
21 | */
|
22 | export function toMediaWidget( viewElement, writer, label ) {
|
23 | writer.setCustomProperty( 'media', true, viewElement );
|
24 |
|
25 | return toWidget( viewElement, writer, { label } );
|
26 | }
|
27 |
|
28 | /**
|
29 | * Returns a media widget editing view element if one is selected.
|
30 | *
|
31 | * @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} selection
|
32 | * @returns {module:engine/view/element~Element|null}
|
33 | */
|
34 | export function getSelectedMediaViewWidget( selection ) {
|
35 | const viewElement = selection.getSelectedElement();
|
36 |
|
37 | if ( viewElement && isMediaWidget( viewElement ) ) {
|
38 | return viewElement;
|
39 | }
|
40 |
|
41 | return null;
|
42 | }
|
43 |
|
44 | /**
|
45 | * Checks if a given view element is a media widget.
|
46 | *
|
47 | * @param {module:engine/view/element~Element} viewElement
|
48 | * @returns {Boolean}
|
49 | */
|
50 | export function isMediaWidget( viewElement ) {
|
51 | return !!viewElement.getCustomProperty( 'media' ) && isWidget( viewElement );
|
52 | }
|
53 |
|
54 | /**
|
55 | * Creates a view element representing the media. Either a "semantic" one for the data pipeline:
|
56 | *
|
57 | * <figure class="media">
|
58 | * <oembed url="foo"></oembed>
|
59 | * </figure>
|
60 | *
|
61 | * or a "non-semantic" (for the editing view pipeline):
|
62 | *
|
63 | * <figure class="media">
|
64 | * <div data-oembed-url="foo">[ non-semantic media preview for "foo" ]</div>
|
65 | * </figure>
|
66 | *
|
67 | * @param {module:engine/view/downcastwriter~DowncastWriter} writer
|
68 | * @param {module:media-embed/mediaregistry~MediaRegistry} registry
|
69 | * @param {String} url
|
70 | * @param {Object} options
|
71 | * @param {String} [options.elementName]
|
72 | * @param {Boolean} [options.useSemanticWrapper]
|
73 | * @param {Boolean} [options.renderForEditingView]
|
74 | * @returns {module:engine/view/containerelement~ContainerElement}
|
75 | */
|
76 | export function createMediaFigureElement( writer, registry, url, options ) {
|
77 | return writer.createContainerElement( 'figure', { class: 'media' }, [
|
78 | registry.getMediaViewElement( writer, url, options ),
|
79 | writer.createSlot()
|
80 | ] );
|
81 | }
|
82 |
|
83 | /**
|
84 | * Returns a selected media element in the model, if any.
|
85 | *
|
86 | * @param {module:engine/model/selection~Selection} selection
|
87 | * @returns {module:engine/model/element~Element|null}
|
88 | */
|
89 | export function getSelectedMediaModelWidget( selection ) {
|
90 | const selectedElement = selection.getSelectedElement();
|
91 |
|
92 | if ( selectedElement && selectedElement.is( 'element', 'media' ) ) {
|
93 | return selectedElement;
|
94 | }
|
95 |
|
96 | return null;
|
97 | }
|
98 |
|
99 | /**
|
100 | * Creates a media element and inserts it into the model.
|
101 | *
|
102 | * **Note**: This method will use {@link module:engine/model/model~Model#insertContent `model.insertContent()`} logic of inserting content
|
103 | * if no `insertPosition` is passed.
|
104 | *
|
105 | * @param {module:engine/model/model~Model} model
|
106 | * @param {String} url An URL of an embeddable media.
|
107 | * @param {module:engine/model/range~Range} [insertRange] The range to insert the media. If not specified,
|
108 | * the default behavior of {@link module:engine/model/model~Model#insertContent `model.insertContent()`} will
|
109 | * be applied.
|
110 | * @param {Boolean} findOptimalPosition If true it will try to find optimal position to insert media without breaking content
|
111 | * in which a selection is.
|
112 | */
|
113 | export function insertMedia( model, url, selectable, findOptimalPosition ) {
|
114 | model.change( writer => {
|
115 | const mediaElement = writer.createElement( 'media', { url } );
|
116 |
|
117 | model.insertObject( mediaElement, selectable, null, {
|
118 | setSelection: 'on',
|
119 | findOptimalPosition
|
120 | } );
|
121 | } );
|
122 | }
|