-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
mediaregistry.ts
318 lines (270 loc) · 8.33 KB
/
mediaregistry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module media-embed/mediaregistry
*/
import type { DowncastWriter, ViewElement } from 'ckeditor5/src/engine.js';
import { IconView, Template } from 'ckeditor5/src/ui.js';
import { type Locale, logWarning, toArray } from 'ckeditor5/src/utils.js';
import mediaPlaceholderIcon from '../theme/icons/media-placeholder.svg';
import type { MediaEmbedConfig, MediaEmbedProvider } from './mediaembedconfig.js';
import type { MediaOptions } from './utils.js';
const mediaPlaceholderIconViewBox = '0 0 64 42';
/**
* A bridge between the raw media content provider definitions and the editor view content.
*
* It helps translating media URLs to corresponding {@link module:engine/view/element~Element view elements}.
*
* Mostly used by the {@link module:media-embed/mediaembedediting~MediaEmbedEditing} plugin.
*/
export default class MediaRegistry {
/**
* The {@link module:utils/locale~Locale} instance.
*/
public locale: Locale;
/**
* The media provider definitions available for the registry. Usually corresponding with the
* {@link module:media-embed/mediaembedconfig~MediaEmbedConfig media configuration}.
*/
public providerDefinitions: Array<MediaEmbedProvider>;
/**
* Creates an instance of the {@link module:media-embed/mediaregistry~MediaRegistry} class.
*
* @param locale The localization services instance.
* @param config The configuration of the media embed feature.
*/
constructor( locale: Locale, config: MediaEmbedConfig ) {
const providers = config.providers!;
const extraProviders = config.extraProviders || [];
const removedProviders = new Set( config.removeProviders );
const providerDefinitions = providers
.concat( extraProviders )
.filter( provider => {
const name = provider.name;
if ( !name ) {
/**
* One of the providers (or extra providers) specified in the media embed configuration
* has no name and will not be used by the editor. In order to get this media
* provider working, double check your editor configuration.
*
* @error media-embed-no-provider-name
*/
logWarning( 'media-embed-no-provider-name', { provider } );
return false;
}
return !removedProviders.has( name );
} );
this.locale = locale;
this.providerDefinitions = providerDefinitions;
}
/**
* Checks whether the passed URL is representing a certain media type allowed in the editor.
*
* @param url The URL to be checked
*/
public hasMedia( url: string ): boolean {
return !!this._getMedia( url );
}
/**
* For the given media URL string and options, it returns the {@link module:engine/view/element~Element view element}
* representing that media.
*
* **Note:** If no URL is specified, an empty view element is returned.
*
* @param writer The view writer used to produce a view element.
* @param url The URL to be translated into a view element.
*/
public getMediaViewElement(
writer: DowncastWriter,
url: string,
options: MediaOptions
): ViewElement {
return this._getMedia( url )!.getViewElement( writer, options );
}
/**
* Returns a `Media` instance for the given URL.
*
* @param url The URL of the media.
* @returns The `Media` instance or `null` when there is none.
*/
private _getMedia( url: string ): Media | null {
if ( !url ) {
return new Media( this.locale );
}
url = url.trim();
for ( const definition of this.providerDefinitions ) {
const previewRenderer = definition.html!;
const pattern = toArray( definition.url );
for ( const subPattern of pattern ) {
const match = this._getUrlMatches( url, subPattern );
if ( match ) {
return new Media( this.locale, url, match, previewRenderer );
}
}
}
return null;
}
/**
* Tries to match `url` to `pattern`.
*
* @param url The URL of the media.
* @param pattern The pattern that should accept the media URL.
*/
private _getUrlMatches( url: string, pattern: RegExp ): RegExpMatchArray | null {
// 1. Try to match without stripping the protocol and "www" subdomain.
let match = url.match( pattern );
if ( match ) {
return match;
}
// 2. Try to match after stripping the protocol.
let rawUrl = url.replace( /^https?:\/\//, '' );
match = rawUrl.match( pattern );
if ( match ) {
return match;
}
// 3. Try to match after stripping the "www" subdomain.
rawUrl = rawUrl.replace( /^www\./, '' );
match = rawUrl.match( pattern );
if ( match ) {
return match;
}
return null;
}
}
/**
* Represents media defined by the provider configuration.
*
* It can be rendered to the {@link module:engine/view/element~Element view element} and used in the editing or data pipeline.
*/
class Media {
/**
* The URL this Media instance represents.
*/
public url: string | null;
/**
* Shorthand for {@link module:utils/locale~Locale#t}.
*
* @see module:utils/locale~Locale#t
*/
private _locale: Locale;
/**
* The output of the `RegExp.match` which validated the {@link #url} of this media.
*/
private _match?: RegExpMatchArray;
/**
* The function returning the HTML string preview of this media.
*/
private _previewRenderer?: ( match: RegExpMatchArray ) => string;
constructor( locale: Locale, url?: string, match?: RegExpMatchArray, previewRenderer?: ( match: RegExpMatchArray ) => string ) {
this.url = this._getValidUrl( url );
this._locale = locale;
this._match = match;
this._previewRenderer = previewRenderer;
}
/**
* Returns the view element representation of the media.
*
* @param writer The view writer used to produce a view element.
*/
public getViewElement(
writer: DowncastWriter,
options: MediaOptions
): ViewElement {
const attributes: Record<string, unknown> = {};
let viewElement;
if ( options.renderForEditingView || ( options.renderMediaPreview && this.url && this._previewRenderer ) ) {
if ( this.url ) {
attributes[ 'data-oembed-url' ] = this.url;
}
if ( options.renderForEditingView ) {
attributes.class = 'ck-media__wrapper';
}
const mediaHtml = this._getPreviewHtml( options );
viewElement = writer.createRawElement( 'div', attributes, ( domElement, domConverter ) => {
domConverter!.setContentOf( domElement, mediaHtml );
} );
} else {
if ( this.url ) {
attributes.url = this.url;
}
viewElement = writer.createEmptyElement( options.elementName, attributes );
}
writer.setCustomProperty( 'media-content', true, viewElement );
return viewElement;
}
/**
* Returns the HTML string of the media content preview.
*/
private _getPreviewHtml( options: { renderForEditingView?: boolean } ): string {
if ( this._previewRenderer ) {
return this._previewRenderer( this._match! );
} else {
// The placeholder only makes sense for editing view and media which have URLs.
// Placeholder is never displayed in data and URL-less media have no content.
if ( this.url && options.renderForEditingView ) {
return this._getPlaceholderHtml();
}
return '';
}
}
/**
* Returns the placeholder HTML when the media has no content preview.
*/
private _getPlaceholderHtml(): string {
const icon = new IconView();
const t = this._locale.t;
icon.content = mediaPlaceholderIcon;
icon.viewBox = mediaPlaceholderIconViewBox;
const placeholder = new Template( {
tag: 'div',
attributes: {
class: 'ck ck-reset_all ck-media__placeholder'
},
children: [
{
tag: 'div',
attributes: {
class: 'ck-media__placeholder__icon'
},
children: [ icon ]
},
{
tag: 'a',
attributes: {
class: 'ck-media__placeholder__url',
target: '_blank',
rel: 'noopener noreferrer',
href: this.url,
'data-cke-tooltip-text': t( 'Open media in new tab' )
},
children: [
{
tag: 'span',
attributes: {
class: 'ck-media__placeholder__url__text'
},
children: [ this.url ]
}
]
}
]
} ).render() as HTMLElement;
return placeholder.outerHTML;
}
/**
* Returns the full URL to the specified media.
*
* @param url The URL of the media.
*/
private _getValidUrl( url: string | undefined ): string | null {
if ( !url ) {
return null;
}
if ( url.match( /^https?/ ) ) {
return url;
}
return 'https://' + url;
}
}