This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
mediaregistry.js
342 lines (295 loc) · 8.86 KB
/
mediaregistry.js
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module media-embed/mediaregistry
*/
/* globals console */
import mediaPlaceholderIcon from '../theme/icons/media-placeholder.svg';
import TooltipView from '@ckeditor/ckeditor5-ui/src/tooltip/tooltipview';
import IconView from '@ckeditor/ckeditor5-ui/src/icon/iconview';
import Template from '@ckeditor/ckeditor5-ui/src/template';
import { attachLinkToDocumentation } from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
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 {
/**
* Creates an instance of the {@link module:media-embed/mediaregistry~MediaRegistry} class.
*
* @param {module:utils/locale~Locale} locale The localization services instance.
* @param {module:media-embed/mediaembed~MediaEmbedConfig} config The configuration of the media embed feature.
*/
constructor( locale, config ) {
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.
*
* @warning media-embed-no-provider-name
*/
console.warn( attachLinkToDocumentation(
'media-embed-no-provider-name: The configured media provider has no name and cannot be used.'
), { provider } );
return false;
}
return !removedProviders.has( name );
} );
/**
* The locale {@link module:utils/locale~Locale} instance.
*
* @member {module:utils/locale~Locale}
*/
this.locale = locale;
/**
* The media provider definitions available for the registry. Usually corresponding with the
* {@link module:media-embed/mediaembed~MediaEmbedConfig media configuration}.
*
* @member {Array}
*/
this.providerDefinitions = providerDefinitions;
}
/**
* Checks whether the passed URL is representing a certain media type allowed in the editor.
*
* @param {String} url The URL to be checked
* @returns {Boolean}
*/
hasMedia( url ) {
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 {module:engine/view/downcastwriter~DowncastWriter} writer The view writer used to produce a view element.
* @param {String} url The URL to be translated into a view element.
* @param {Object} options
* @param {String} [options.renderMediaPreview]
* @param {String} [options.renderForEditingView]
* @returns {module:engine/view/element~Element}
*/
getMediaViewElement( writer, url, options ) {
return this._getMedia( url ).getViewElement( writer, options );
}
/**
* Returns a `Media` instance for the given URL.
*
* @protected
* @param {String} url The URL of the media.
* @returns {module:media-embed/mediaregistry~Media|null} The `Media` instance or `null` when there is none.
*/
_getMedia( url ) {
if ( !url ) {
return new Media( this.locale );
}
url = url.trim();
for ( const definition of this.providerDefinitions ) {
const previewRenderer = definition.html;
let pattern = definition.url;
if ( !Array.isArray( pattern ) ) {
pattern = [ pattern ];
}
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`.
*
* @private
* @param {String} url The URL of the media.
* @param {RegExp} pattern The pattern that should accept the media URL.
* @returns {Array|null}
*/
_getUrlMatches( url, pattern ) {
// 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.
*
* @private
*/
class Media {
constructor( locale, url, match, previewRenderer ) {
/**
* The URL this Media instance represents.
*
* @member {String}
*/
this.url = this._getValidUrl( url );
/**
* Shorthand for {@link module:utils/locale~Locale#t}.
*
* @see module:utils/locale~Locale#t
* @method
*/
this._t = locale.t;
/**
* The output of the `RegExp.match` which validated the {@link #url} of this media.
*
* @member {Object}
*/
this._match = match;
/**
* The function returning the HTML string preview of this media.
*
* @member {Function}
*/
this._previewRenderer = previewRenderer;
}
/**
* Returns the view element representation of the media.
*
* @param {module:engine/view/downcastwriter~DowncastWriter} writer The view writer used to produce a view element.
* @param {Object} options
* @param {String} [options.renderMediaPreview]
* @param {String} [options.renderForEditingView]
* @returns {module:engine/view/element~Element}
*/
getViewElement( writer, options ) {
const attributes = {};
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 );
return writer.createUIElement( 'div', attributes, function( domDocument ) {
const domElement = this.toDomElement( domDocument );
domElement.innerHTML = mediaHtml;
return domElement;
} );
} else {
if ( this.url ) {
attributes.url = this.url;
}
return writer.createEmptyElement( 'oembed', attributes );
}
}
/**
* Returns the HTML string of the media content preview.
*
* @param {module:engine/view/downcastwriter~DowncastWriter} writer The view writer used to produce a view element.
* @param {Object} options
* @param {String} [options.renderForEditingView]
* @returns {String}
*/
_getPreviewHtml( options ) {
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.
*
* @returns {String}
*/
_getPlaceholderHtml() {
const tooltip = new TooltipView();
const icon = new IconView();
tooltip.text = this._t( 'Open media in new tab' );
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
},
children: [
{
tag: 'span',
attributes: {
class: 'ck-media__placeholder__url__text'
},
children: [ this.url ]
},
tooltip
]
}
]
} ).render();
return placeholder.outerHTML;
}
/**
* Returns the full URL to the specified media.
*
* @param {String} url The URL of the media.
* @returns {String|null}
*/
_getValidUrl( url ) {
if ( !url ) {
return null;
}
if ( url.match( /^https?/ ) ) {
return url;
}
return 'https://' + url;
}
}