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