@ckeditor/ckeditor5-media-embed 36.0.0 → 37.0.0-alpha.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.
@@ -0,0 +1,38 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ import type { DowncastDispatcher } from 'ckeditor5/src/engine';
6
+ import type MediaRegistry from './mediaregistry';
7
+ import type { MediaOptions } from './utils';
8
+ /**
9
+ * Returns a function that converts the model "url" attribute to the view representation.
10
+ *
11
+ * Depending on the configuration, the view representation can be "semantic" (for the data pipeline):
12
+ *
13
+ * ```html
14
+ * <figure class="media">
15
+ * <oembed url="foo"></oembed>
16
+ * </figure>
17
+ * ```
18
+ *
19
+ * or "non-semantic" (for the editing view pipeline):
20
+ *
21
+ * ```html
22
+ * <figure class="media">
23
+ * <div data-oembed-url="foo">[ non-semantic media preview for "foo" ]</div>
24
+ * </figure>
25
+ * ```
26
+ *
27
+ * **Note:** Changing the model "url" attribute replaces the entire content of the
28
+ * `<figure>` in the view.
29
+ *
30
+ * @param registry The registry providing
31
+ * the media and their content.
32
+ * @param options options object with following properties:
33
+ * - elementName When set, overrides the default element name for semantic media embeds.
34
+ * - renderMediaPreview When `true`, the converter will create the view in the non-semantic form.
35
+ * - renderForEditingView When `true`, the converter will create a view specific for the
36
+ * editing pipeline (e.g. including CSS classes, content placeholders).
37
+ */
38
+ export declare function modelToViewUrlAttributeConverter(registry: MediaRegistry, options: MediaOptions): (dispatcher: DowncastDispatcher) => void;
package/src/converters.js CHANGED
@@ -2,59 +2,52 @@
2
2
  * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
- /**
7
- * @module media-embed/converters
8
- */
9
-
10
5
  /**
11
6
  * Returns a function that converts the model "url" attribute to the view representation.
12
7
  *
13
8
  * Depending on the configuration, the view representation can be "semantic" (for the data pipeline):
14
9
  *
15
- * <figure class="media">
16
- * <oembed url="foo"></oembed>
17
- * </figure>
10
+ * ```html
11
+ * <figure class="media">
12
+ * <oembed url="foo"></oembed>
13
+ * </figure>
14
+ * ```
18
15
  *
19
16
  * or "non-semantic" (for the editing view pipeline):
20
17
  *
21
- * <figure class="media">
22
- * <div data-oembed-url="foo">[ non-semantic media preview for "foo" ]</div>
23
- * </figure>
18
+ * ```html
19
+ * <figure class="media">
20
+ * <div data-oembed-url="foo">[ non-semantic media preview for "foo" ]</div>
21
+ * </figure>
22
+ * ```
24
23
  *
25
24
  * **Note:** Changing the model "url" attribute replaces the entire content of the
26
25
  * `<figure>` in the view.
27
26
  *
28
- * @param {module:media-embed/mediaregistry~MediaRegistry} registry The registry providing
27
+ * @param registry The registry providing
29
28
  * 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
29
+ * @param options options object with following properties:
30
+ * - elementName When set, overrides the default element name for semantic media embeds.
31
+ * - renderMediaPreview When `true`, the converter will create the view in the non-semantic form.
32
+ * - renderForEditingView When `true`, the converter will create a view specific for the
34
33
  * editing pipeline (e.g. including CSS classes, content placeholders).
35
- * @returns {Function}
36
34
  */
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
- }
35
+ export function modelToViewUrlAttributeConverter(registry, options) {
36
+ const converter = (evt, data, conversionApi) => {
37
+ if (!conversionApi.consumable.consume(data.item, evt.name)) {
38
+ return;
39
+ }
40
+ const url = data.attributeNewValue;
41
+ const viewWriter = conversionApi.writer;
42
+ const figure = conversionApi.mapper.toViewElement(data.item);
43
+ const mediaContentElement = [...figure.getChildren()]
44
+ .find(child => child.getCustomProperty('media-content'));
45
+ // TODO: removing the wrapper and creating it from scratch is a hack. We can do better than that.
46
+ viewWriter.remove(mediaContentElement);
47
+ const mediaViewElement = registry.getMediaViewElement(viewWriter, url, options);
48
+ viewWriter.insert(viewWriter.createPositionAt(figure, 0), mediaViewElement);
49
+ };
50
+ return dispatcher => {
51
+ dispatcher.on('attribute:url:media', converter);
52
+ };
60
53
  }
package/src/index.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ * @module media-embed
7
+ */
8
+ export { default as MediaEmbed } from './mediaembed';
9
+ export { default as MediaEmbedEditing } from './mediaembedediting';
10
+ export { default as MediaEmbedUI } from './mediaembedui';
11
+ export { default as AutoMediaEmbed } from './automediaembed';
12
+ export { default as MediaEmbedToolbar } from './mediaembedtoolbar';
package/src/index.js CHANGED
@@ -2,11 +2,9 @@
2
2
  * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module media-embed
8
7
  */
9
-
10
8
  export { default as MediaEmbed } from './mediaembed';
11
9
  export { default as MediaEmbedEditing } from './mediaembedediting';
12
10
  export { default as MediaEmbedUI } from './mediaembedui';
@@ -0,0 +1,35 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ * @module media-embed/mediaembed
7
+ */
8
+ import { Plugin, type PluginDependencies } from 'ckeditor5/src/core';
9
+ import '../theme/mediaembed.css';
10
+ /**
11
+ * The media embed plugin.
12
+ *
13
+ * For a detailed overview, check the {@glink features/media-embed Media Embed feature documentation}.
14
+ *
15
+ * This is a "glue" plugin which loads the following plugins:
16
+ *
17
+ * * The {@link module:media-embed/mediaembedediting~MediaEmbedEditing media embed editing feature},
18
+ * * The {@link module:media-embed/mediaembedui~MediaEmbedUI media embed UI feature} and
19
+ * * The {@link module:media-embed/automediaembed~AutoMediaEmbed auto-media embed feature}.
20
+ */
21
+ export default class MediaEmbed extends Plugin {
22
+ /**
23
+ * @inheritDoc
24
+ */
25
+ static get requires(): PluginDependencies;
26
+ /**
27
+ * @inheritDoc
28
+ */
29
+ static get pluginName(): 'MediaEmbed';
30
+ }
31
+ declare module '@ckeditor/ckeditor5-core' {
32
+ interface PluginsMap {
33
+ [MediaEmbed.pluginName]: MediaEmbed;
34
+ }
35
+ }
package/src/mediaembed.js CHANGED
@@ -2,20 +2,15 @@
2
2
  * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module media-embed/mediaembed
8
7
  */
9
-
10
8
  import { Plugin } from 'ckeditor5/src/core';
11
9
  import { Widget } from 'ckeditor5/src/widget';
12
-
13
10
  import MediaEmbedEditing from './mediaembedediting';
14
11
  import AutoMediaEmbed from './automediaembed';
15
12
  import MediaEmbedUI from './mediaembedui';
16
-
17
13
  import '../theme/mediaembed.css';
18
-
19
14
  /**
20
15
  * The media embed plugin.
21
16
  *
@@ -26,267 +21,18 @@ import '../theme/mediaembed.css';
26
21
  * * The {@link module:media-embed/mediaembedediting~MediaEmbedEditing media embed editing feature},
27
22
  * * The {@link module:media-embed/mediaembedui~MediaEmbedUI media embed UI feature} and
28
23
  * * The {@link module:media-embed/automediaembed~AutoMediaEmbed auto-media embed feature}.
29
- *
30
- * @extends module:core/plugin~Plugin
31
24
  */
32
25
  export default class MediaEmbed extends Plugin {
33
- /**
34
- * @inheritDoc
35
- */
36
- static get requires() {
37
- return [ MediaEmbedEditing, MediaEmbedUI, AutoMediaEmbed, Widget ];
38
- }
39
-
40
- /**
41
- * @inheritDoc
42
- */
43
- static get pluginName() {
44
- return 'MediaEmbed';
45
- }
26
+ /**
27
+ * @inheritDoc
28
+ */
29
+ static get requires() {
30
+ return [MediaEmbedEditing, MediaEmbedUI, AutoMediaEmbed, Widget];
31
+ }
32
+ /**
33
+ * @inheritDoc
34
+ */
35
+ static get pluginName() {
36
+ return 'MediaEmbed';
37
+ }
46
38
  }
47
-
48
- /**
49
- * The media embed provider descriptor. Used in
50
- * {@link module:media-embed/mediaembed~MediaEmbedConfig#providers `config.mediaEmbed.providers`} and
51
- * {@link module:media-embed/mediaembed~MediaEmbedConfig#extraProviders `config.mediaEmbed.extraProviders`}.
52
- *
53
- * See {@link module:media-embed/mediaembed~MediaEmbedConfig} to learn more.
54
- *
55
- * {
56
- * name: 'example',
57
- *
58
- * // The following RegExp matches https://www.example.com/media/{media id},
59
- * // (either with "http(s)://" and "www" or without), so the valid URLs are:
60
- * //
61
- * // * https://www.example.com/media/{media id},
62
- * // * http://www.example.com/media/{media id},
63
- * // * www.example.com/media/{media id},
64
- * // * example.com/media/{media id}
65
- * url: /^example\.com\/media\/(\w+)/,
66
- *
67
- * // The rendering function of the provider.
68
- * // Used to represent the media when editing the content (i.e. in the view)
69
- * // and also in the data output of the editor if semantic data output is disabled.
70
- * html: match => `The HTML representing the media with ID=${ match[ 1 ] }.`
71
- * }
72
- *
73
- * You can allow any sort of media in the editor using the "allow–all" `RegExp`.
74
- * But mind that, since URLs are processed in the order of configuration, if one of the previous
75
- * `RegExps` matches the URL, it will have a precedence over this one.
76
- *
77
- * {
78
- * name: 'allow-all',
79
- * url: /^.+/
80
- * }
81
- *
82
- * To implement responsive media, you can use the following HTML structure:
83
- *
84
- * {
85
- * ...
86
- * html: match =>
87
- * '<div style="position:relative; padding-bottom:100%; height:0">' +
88
- * '<iframe src="..." frameborder="0" ' +
89
- * 'style="position:absolute; width:100%; height:100%; top:0; left:0">' +
90
- * '</iframe>' +
91
- * '</div>'
92
- * }
93
- *
94
- * @typedef {Object} module:media-embed/mediaembed~MediaEmbedProvider
95
- * @property {String} name The name of the provider. Used e.g. when
96
- * {@link module:media-embed/mediaembed~MediaEmbedConfig#removeProviders removing providers}.
97
- * @property {RegExp|Array.<RegExp>} url The `RegExp` object (or array of objects) defining the URL of the media.
98
- * If any URL matches the `RegExp`, it becomes the media in the editor model, as defined by the provider. The result
99
- * of matching (output of `String.prototype.match()`) is passed to the `html` rendering function of the media.
100
- *
101
- * **Note:** You do not need to include the protocol (`http://`, `https://`) and `www` subdomain in your `RegExps`,
102
- * they are stripped from the URLs before matching anyway.
103
- * @property {Function} [html] (optional) The rendering function of the media. The function receives the entire matching
104
- * array from the corresponding `url` `RegExp` as an argument, allowing rendering a dedicated
105
- * preview of the media identified by a certain ID or a hash. When not defined, the media embed feature
106
- * will use a generic media representation in the view and output data.
107
- * Note that when
108
- * {@link module:media-embed/mediaembed~MediaEmbedConfig#previewsInData `config.mediaEmbed.previewsInData`}
109
- * is `true`, the rendering function **will always** be used for the media in the editor data output.
110
- */
111
-
112
- /**
113
- * The configuration of the {@link module:media-embed/mediaembed~MediaEmbed} feature.
114
- *
115
- * Read more in {@link module:media-embed/mediaembed~MediaEmbedConfig}.
116
- *
117
- * @member {module:media-embed/mediaembed~MediaEmbedConfig} module:core/editor/editorconfig~EditorConfig#mediaEmbed
118
- */
119
-
120
- /**
121
- * The configuration of the media embed features.
122
- *
123
- * Read more about {@glink features/media-embed#configuration configuring the media embed feature}.
124
- *
125
- * ClassicEditor
126
- * .create( editorElement, {
127
- * mediaEmbed: ... // Media embed feature options.
128
- * } )
129
- * .then( ... )
130
- * .catch( ... );
131
- *
132
- * See {@link module:core/editor/editorconfig~EditorConfig all editor options}.
133
- *
134
- * @interface MediaEmbedConfig
135
- */
136
-
137
- /**
138
- * The default media providers supported by the editor.
139
- *
140
- * The names of providers with rendering functions (previews):
141
- *
142
- * * "dailymotion",
143
- * * "spotify",
144
- * * "youtube",
145
- * * "vimeo"
146
- *
147
- * The names of providers without rendering functions:
148
- *
149
- * * "instagram",
150
- * * "twitter",
151
- * * "googleMaps",
152
- * * "flickr",
153
- * * "facebook"
154
- *
155
- * See the {@link module:media-embed/mediaembed~MediaEmbedProvider provider syntax} to learn more about
156
- * different kinds of media and media providers.
157
- *
158
- * **Note**: The default media provider configuration may not support all possible media URLs,
159
- * only the most common are included.
160
- *
161
- * Media without rendering functions are always represented in the data using the "semantic" markup. See
162
- * {@link module:media-embed/mediaembed~MediaEmbedConfig#previewsInData `config.mediaEmbed.previewsInData`} to
163
- * learn more about possible data outputs.
164
- *
165
- * The priority of media providers corresponds to the order of configuration. The first provider
166
- * to match the URL is always used, even if there are other providers that support a particular URL.
167
- * The URL is never matched against the remaining providers.
168
- *
169
- * To discard **all** default media providers, simply override this configuration with your own
170
- * {@link module:media-embed/mediaembed~MediaEmbedProvider definitions}:
171
- *
172
- * ClassicEditor
173
- * .create( editorElement, {
174
- * plugins: [ MediaEmbed, ... ],
175
- * mediaEmbed: {
176
- * providers: [
177
- * {
178
- * name: 'myProvider',
179
- * url: /^example\.com\/media\/(\w+)/,
180
- * html: match => '...'
181
- * },
182
- * ...
183
- * ]
184
- * }
185
- * } )
186
- * .then( ... )
187
- * .catch( ... );
188
- *
189
- * You can take inspiration from the default configuration of this feature which you can find in:
190
- * https://github.com/ckeditor/ckeditor5-media-embed/blob/master/src/mediaembedediting.js
191
- *
192
- * To **extend** the list of default providers, use
193
- * {@link module:media-embed/mediaembed~MediaEmbedConfig#extraProviders `config.mediaEmbed.extraProviders`}.
194
- *
195
- * To **remove** certain providers, use
196
- * {@link module:media-embed/mediaembed~MediaEmbedConfig#removeProviders `config.mediaEmbed.removeProviders`}.
197
- *
198
- * @member {Array.<module:media-embed/mediaembed~MediaEmbedProvider>} module:media-embed/mediaembed~MediaEmbedConfig#providers
199
- */
200
-
201
- /**
202
- * The additional media providers supported by the editor. This configuration helps extend the default
203
- * {@link module:media-embed/mediaembed~MediaEmbedConfig#providers}.
204
- *
205
- * ClassicEditor
206
- * .create( editorElement, {
207
- * plugins: [ MediaEmbed, ... ],
208
- * mediaEmbed: {
209
- * extraProviders: [
210
- * {
211
- * name: 'extraProvider',
212
- * url: /^example\.com\/media\/(\w+)/,
213
- * html: match => '...'
214
- * },
215
- * ...
216
- * ]
217
- * }
218
- * } )
219
- * .then( ... )
220
- * .catch( ... );
221
- *
222
- * See the {@link module:media-embed/mediaembed~MediaEmbedProvider provider syntax} to learn more.
223
- *
224
- * @member {Array.<module:media-embed/mediaembed~MediaEmbedProvider>} module:media-embed/mediaembed~MediaEmbedConfig#extraProviders
225
- */
226
-
227
- /**
228
- * The list of media providers that should not be used despite being available in
229
- * {@link module:media-embed/mediaembed~MediaEmbedConfig#providers `config.mediaEmbed.providers`} and
230
- * {@link module:media-embed/mediaembed~MediaEmbedConfig#extraProviders `config.mediaEmbed.extraProviders`}
231
- *
232
- * mediaEmbed: {
233
- * removeProviders: [ 'youtube', 'twitter' ]
234
- * }
235
- *
236
- * @member {Array.<String>} module:media-embed/mediaembed~MediaEmbedConfig#removeProviders
237
- */
238
-
239
- /**
240
- * Overrides the element name used for "semantic" data.
241
- *
242
- * This is not relevant if {@link module:media-embed/mediaembed~MediaEmbedConfig#previewsInData `config.mediaEmbed.previewsInData`}
243
- * is set to `true`.
244
- *
245
- * When not set, the feature produces the `<oembed>` tag:
246
- *
247
- * <figure class="media">
248
- * <oembed url="https://url"></oembed>
249
- * </figure>
250
- *
251
- * To override the element name with, for instance, the `o-embed` name:
252
- *
253
- * mediaEmbed: {
254
- * elementName: 'o-embed'
255
- * }
256
- *
257
- * This will produce semantic data with the `<o-embed>` tag:
258
- *
259
- * <figure class="media">
260
- * <o-embed url="https://url"></o-embed>
261
- * </figure>
262
- *
263
- * @default 'oembed'
264
- * @member {String} [module:media-embed/mediaembed~MediaEmbedConfig#elementName]
265
- */
266
-
267
- /**
268
- * Controls the data format produced by the feature.
269
- *
270
- * When `false` (default), the feature produces "semantic" data, i.e. it does not include the preview of
271
- * the media, just the `<oembed>` tag with the `url` attribute:
272
- *
273
- * <figure class="media">
274
- * <oembed url="https://url"></oembed>
275
- * </figure>
276
- *
277
- * When `true`, the media is represented in the output in the same way it looks in the editor,
278
- * i.e. the media preview is saved to the database:
279
- *
280
- * <figure class="media">
281
- * <div data-oembed-url="https://url">
282
- * <iframe src="https://preview"></iframe>
283
- * </div>
284
- * </figure>
285
- *
286
- * **Note:** Media without preview are always represented in the data using the "semantic" markup
287
- * regardless of the value of the `previewsInData`. Learn more about different kinds of media
288
- * in the {@link module:media-embed/mediaembed~MediaEmbedConfig#providers `config.mediaEmbed.providers`}
289
- * configuration description.
290
- *
291
- * @member {Boolean} [module:media-embed/mediaembed~MediaEmbedConfig#previewsInData=false]
292
- */
@@ -0,0 +1,41 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ import { Command } from 'ckeditor5/src/core';
6
+ /**
7
+ * The insert media command.
8
+ *
9
+ * The command is registered by the {@link module:media-embed/mediaembedediting~MediaEmbedEditing} as `'mediaEmbed'`.
10
+ *
11
+ * To insert media at the current selection, execute the command and specify the URL:
12
+ *
13
+ * ```ts
14
+ * editor.execute( 'mediaEmbed', 'http://url.to.the/media' );
15
+ * ```
16
+ */
17
+ export default class MediaEmbedCommand extends Command {
18
+ /**
19
+ * Media url.
20
+ */
21
+ value: string | undefined;
22
+ /**
23
+ * @inheritDoc
24
+ */
25
+ refresh(): void;
26
+ /**
27
+ * Executes the command, which either:
28
+ *
29
+ * * updates the URL of the selected media,
30
+ * * inserts the new media into the editor and puts the selection around it.
31
+ *
32
+ * @fires execute
33
+ * @param url The URL of the media.
34
+ */
35
+ execute(url: string): void;
36
+ }
37
+ declare module '@ckeditor/ckeditor5-core' {
38
+ interface CommandsMap {
39
+ mediaEmbed: MediaEmbedCommand;
40
+ }
41
+ }