@ckeditor/ckeditor5-media-embed 41.3.1 → 41.4.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.
- package/build/media-embed.js +1 -1
- package/dist/index-content.css +10 -0
- package/dist/index-editor.css +50 -0
- package/dist/index.css +148 -0
- package/dist/index.css.map +1 -0
- package/dist/index.js +1038 -0
- package/dist/index.js.map +1 -0
- package/dist/translations/ar.d.ts +8 -0
- package/dist/translations/ar.js +5 -0
- package/dist/translations/ar.umd.js +11 -0
- package/dist/translations/az.d.ts +8 -0
- package/dist/translations/az.js +5 -0
- package/dist/translations/az.umd.js +11 -0
- package/dist/translations/bg.d.ts +8 -0
- package/dist/translations/bg.js +5 -0
- package/dist/translations/bg.umd.js +11 -0
- package/dist/translations/bn.d.ts +8 -0
- package/dist/translations/bn.js +5 -0
- package/dist/translations/bn.umd.js +11 -0
- package/dist/translations/ca.d.ts +8 -0
- package/dist/translations/ca.js +5 -0
- package/dist/translations/ca.umd.js +11 -0
- package/dist/translations/cs.d.ts +8 -0
- package/dist/translations/cs.js +5 -0
- package/dist/translations/cs.umd.js +11 -0
- package/dist/translations/da.d.ts +8 -0
- package/dist/translations/da.js +5 -0
- package/dist/translations/da.umd.js +11 -0
- package/dist/translations/de-ch.d.ts +8 -0
- package/dist/translations/de-ch.js +5 -0
- package/dist/translations/de-ch.umd.js +11 -0
- package/dist/translations/de.d.ts +8 -0
- package/dist/translations/de.js +5 -0
- package/dist/translations/de.umd.js +11 -0
- package/dist/translations/el.d.ts +8 -0
- package/dist/translations/el.js +5 -0
- package/dist/translations/el.umd.js +11 -0
- package/dist/translations/en-au.d.ts +8 -0
- package/dist/translations/en-au.js +5 -0
- package/dist/translations/en-au.umd.js +11 -0
- package/dist/translations/en-gb.d.ts +8 -0
- package/dist/translations/en-gb.js +5 -0
- package/dist/translations/en-gb.umd.js +11 -0
- package/dist/translations/en.d.ts +8 -0
- package/dist/translations/en.js +5 -0
- package/dist/translations/en.umd.js +11 -0
- package/dist/translations/es.d.ts +8 -0
- package/dist/translations/es.js +5 -0
- package/dist/translations/es.umd.js +11 -0
- package/dist/translations/et.d.ts +8 -0
- package/dist/translations/et.js +5 -0
- package/dist/translations/et.umd.js +11 -0
- package/dist/translations/fa.d.ts +8 -0
- package/dist/translations/fa.js +5 -0
- package/dist/translations/fa.umd.js +11 -0
- package/dist/translations/fi.d.ts +8 -0
- package/dist/translations/fi.js +5 -0
- package/dist/translations/fi.umd.js +11 -0
- package/dist/translations/fr.d.ts +8 -0
- package/dist/translations/fr.js +5 -0
- package/dist/translations/fr.umd.js +11 -0
- package/dist/translations/gl.d.ts +8 -0
- package/dist/translations/gl.js +5 -0
- package/dist/translations/gl.umd.js +11 -0
- package/dist/translations/he.d.ts +8 -0
- package/dist/translations/he.js +5 -0
- package/dist/translations/he.umd.js +11 -0
- package/dist/translations/hi.d.ts +8 -0
- package/dist/translations/hi.js +5 -0
- package/dist/translations/hi.umd.js +11 -0
- package/dist/translations/hr.d.ts +8 -0
- package/dist/translations/hr.js +5 -0
- package/dist/translations/hr.umd.js +11 -0
- package/dist/translations/hu.d.ts +8 -0
- package/dist/translations/hu.js +5 -0
- package/dist/translations/hu.umd.js +11 -0
- package/dist/translations/id.d.ts +8 -0
- package/dist/translations/id.js +5 -0
- package/dist/translations/id.umd.js +11 -0
- package/dist/translations/it.d.ts +8 -0
- package/dist/translations/it.js +5 -0
- package/dist/translations/it.umd.js +11 -0
- package/dist/translations/ja.d.ts +8 -0
- package/dist/translations/ja.js +5 -0
- package/dist/translations/ja.umd.js +11 -0
- package/dist/translations/ko.d.ts +8 -0
- package/dist/translations/ko.js +5 -0
- package/dist/translations/ko.umd.js +11 -0
- package/dist/translations/ku.d.ts +8 -0
- package/dist/translations/ku.js +5 -0
- package/dist/translations/ku.umd.js +11 -0
- package/dist/translations/lt.d.ts +8 -0
- package/dist/translations/lt.js +5 -0
- package/dist/translations/lt.umd.js +11 -0
- package/dist/translations/lv.d.ts +8 -0
- package/dist/translations/lv.js +5 -0
- package/dist/translations/lv.umd.js +11 -0
- package/dist/translations/ms.d.ts +8 -0
- package/dist/translations/ms.js +5 -0
- package/dist/translations/ms.umd.js +11 -0
- package/dist/translations/ne.d.ts +8 -0
- package/dist/translations/ne.js +5 -0
- package/dist/translations/ne.umd.js +11 -0
- package/dist/translations/nl.d.ts +8 -0
- package/dist/translations/nl.js +5 -0
- package/dist/translations/nl.umd.js +11 -0
- package/dist/translations/no.d.ts +8 -0
- package/dist/translations/no.js +5 -0
- package/dist/translations/no.umd.js +11 -0
- package/dist/translations/pl.d.ts +8 -0
- package/dist/translations/pl.js +5 -0
- package/dist/translations/pl.umd.js +11 -0
- package/dist/translations/pt-br.d.ts +8 -0
- package/dist/translations/pt-br.js +5 -0
- package/dist/translations/pt-br.umd.js +11 -0
- package/dist/translations/pt.d.ts +8 -0
- package/dist/translations/pt.js +5 -0
- package/dist/translations/pt.umd.js +11 -0
- package/dist/translations/ro.d.ts +8 -0
- package/dist/translations/ro.js +5 -0
- package/dist/translations/ro.umd.js +11 -0
- package/dist/translations/ru.d.ts +8 -0
- package/dist/translations/ru.js +5 -0
- package/dist/translations/ru.umd.js +11 -0
- package/dist/translations/sk.d.ts +8 -0
- package/dist/translations/sk.js +5 -0
- package/dist/translations/sk.umd.js +11 -0
- package/dist/translations/sq.d.ts +8 -0
- package/dist/translations/sq.js +5 -0
- package/dist/translations/sq.umd.js +11 -0
- package/dist/translations/sr-latn.d.ts +8 -0
- package/dist/translations/sr-latn.js +5 -0
- package/dist/translations/sr-latn.umd.js +11 -0
- package/dist/translations/sr.d.ts +8 -0
- package/dist/translations/sr.js +5 -0
- package/dist/translations/sr.umd.js +11 -0
- package/dist/translations/sv.d.ts +8 -0
- package/dist/translations/sv.js +5 -0
- package/dist/translations/sv.umd.js +11 -0
- package/dist/translations/th.d.ts +8 -0
- package/dist/translations/th.js +5 -0
- package/dist/translations/th.umd.js +11 -0
- package/dist/translations/tk.d.ts +8 -0
- package/dist/translations/tk.js +5 -0
- package/dist/translations/tk.umd.js +11 -0
- package/dist/translations/tr.d.ts +8 -0
- package/dist/translations/tr.js +5 -0
- package/dist/translations/tr.umd.js +11 -0
- package/dist/translations/uk.d.ts +8 -0
- package/dist/translations/uk.js +5 -0
- package/dist/translations/uk.umd.js +11 -0
- package/dist/translations/ur.d.ts +8 -0
- package/dist/translations/ur.js +5 -0
- package/dist/translations/ur.umd.js +11 -0
- package/dist/translations/uz.d.ts +8 -0
- package/dist/translations/uz.js +5 -0
- package/dist/translations/uz.umd.js +11 -0
- package/dist/translations/vi.d.ts +8 -0
- package/dist/translations/vi.js +5 -0
- package/dist/translations/vi.umd.js +11 -0
- package/dist/translations/zh-cn.d.ts +8 -0
- package/dist/translations/zh-cn.js +5 -0
- package/dist/translations/zh-cn.umd.js +11 -0
- package/dist/translations/zh.d.ts +8 -0
- package/dist/translations/zh.js +5 -0
- package/dist/translations/zh.umd.js +11 -0
- package/dist/types/augmentation.d.ts +29 -0
- package/dist/types/automediaembed.d.ts +55 -0
- package/dist/types/converters.d.ts +42 -0
- package/dist/types/index.d.ts +19 -0
- package/dist/types/mediaembed.d.ts +38 -0
- package/dist/types/mediaembedcommand.d.ts +40 -0
- package/dist/types/mediaembedconfig.d.ts +282 -0
- package/dist/types/mediaembedediting.d.ts +35 -0
- package/dist/types/mediaembedtoolbar.d.ts +34 -0
- package/dist/types/mediaembedui.d.ts +31 -0
- package/dist/types/mediaregistry.d.ts +70 -0
- package/dist/types/ui/mediaformview.d.ts +119 -0
- package/dist/types/utils.d.ts +71 -0
- package/package.json +4 -3
- package/src/mediaembedui.js +4 -0
- package/src/ui/mediaformview.js +0 -1
package/dist/index.js
ADDED
@@ -0,0 +1,1038 @@
|
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2024, 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, Plugin, icons } from '@ckeditor/ckeditor5-core/dist/index.js';
|
6
|
+
import { toWidget, isWidget, findOptimalInsertionRange, Widget, WidgetToolbarRepository } from '@ckeditor/ckeditor5-widget/dist/index.js';
|
7
|
+
import { toArray, logWarning, first, global, FocusTracker, KeystrokeHandler } from '@ckeditor/ckeditor5-utils/dist/index.js';
|
8
|
+
import { IconView, Template, View, submitHandler, LabeledFieldView, createLabeledInputText, ButtonView, ViewCollection, FocusCycler, createDropdown, CssTransitionDisablerMixin } from '@ckeditor/ckeditor5-ui/dist/index.js';
|
9
|
+
import { LivePosition, LiveRange } from '@ckeditor/ckeditor5-engine/dist/index.js';
|
10
|
+
import { Clipboard } from '@ckeditor/ckeditor5-clipboard/dist/index.js';
|
11
|
+
import { Delete } from '@ckeditor/ckeditor5-typing/dist/index.js';
|
12
|
+
import { Undo } from '@ckeditor/ckeditor5-undo/dist/index.js';
|
13
|
+
|
14
|
+
/**
|
15
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
16
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
17
|
+
*/ /**
|
18
|
+
* Returns a function that converts the model "url" attribute to the view representation.
|
19
|
+
*
|
20
|
+
* Depending on the configuration, the view representation can be "semantic" (for the data pipeline):
|
21
|
+
*
|
22
|
+
* ```html
|
23
|
+
* <figure class="media">
|
24
|
+
* <oembed url="foo"></oembed>
|
25
|
+
* </figure>
|
26
|
+
* ```
|
27
|
+
*
|
28
|
+
* or "non-semantic" (for the editing view pipeline):
|
29
|
+
*
|
30
|
+
* ```html
|
31
|
+
* <figure class="media">
|
32
|
+
* <div data-oembed-url="foo">[ non-semantic media preview for "foo" ]</div>
|
33
|
+
* </figure>
|
34
|
+
* ```
|
35
|
+
*
|
36
|
+
* **Note:** Changing the model "url" attribute replaces the entire content of the
|
37
|
+
* `<figure>` in the view.
|
38
|
+
*
|
39
|
+
* @param registry The registry providing
|
40
|
+
* the media and their content.
|
41
|
+
* @param options options object with following properties:
|
42
|
+
* - elementName When set, overrides the default element name for semantic media embeds.
|
43
|
+
* - renderMediaPreview When `true`, the converter will create the view in the non-semantic form.
|
44
|
+
* - renderForEditingView When `true`, the converter will create a view specific for the
|
45
|
+
* editing pipeline (e.g. including CSS classes, content placeholders).
|
46
|
+
*/ function modelToViewUrlAttributeConverter(registry, options) {
|
47
|
+
const converter = (evt, data, conversionApi)=>{
|
48
|
+
if (!conversionApi.consumable.consume(data.item, evt.name)) {
|
49
|
+
return;
|
50
|
+
}
|
51
|
+
const url = data.attributeNewValue;
|
52
|
+
const viewWriter = conversionApi.writer;
|
53
|
+
const figure = conversionApi.mapper.toViewElement(data.item);
|
54
|
+
const mediaContentElement = [
|
55
|
+
...figure.getChildren()
|
56
|
+
].find((child)=>child.getCustomProperty('media-content'));
|
57
|
+
// TODO: removing the wrapper and creating it from scratch is a hack. We can do better than that.
|
58
|
+
viewWriter.remove(mediaContentElement);
|
59
|
+
const mediaViewElement = registry.getMediaViewElement(viewWriter, url, options);
|
60
|
+
viewWriter.insert(viewWriter.createPositionAt(figure, 0), mediaViewElement);
|
61
|
+
};
|
62
|
+
return (dispatcher)=>{
|
63
|
+
dispatcher.on('attribute:url:media', converter);
|
64
|
+
};
|
65
|
+
}
|
66
|
+
|
67
|
+
/**
|
68
|
+
* Converts a given {@link module:engine/view/element~Element} to a media embed widget:
|
69
|
+
* * Adds a {@link module:engine/view/element~Element#_setCustomProperty custom property} allowing to recognize the media widget element.
|
70
|
+
* * Calls the {@link module:widget/utils~toWidget} function with the proper element's label creator.
|
71
|
+
*
|
72
|
+
* @param writer An instance of the view writer.
|
73
|
+
* @param label The element's label.
|
74
|
+
*/ function toMediaWidget(viewElement, writer, label) {
|
75
|
+
writer.setCustomProperty('media', true, viewElement);
|
76
|
+
return toWidget(viewElement, writer, {
|
77
|
+
label
|
78
|
+
});
|
79
|
+
}
|
80
|
+
/**
|
81
|
+
* Returns a media widget editing view element if one is selected.
|
82
|
+
*/ function getSelectedMediaViewWidget(selection) {
|
83
|
+
const viewElement = selection.getSelectedElement();
|
84
|
+
if (viewElement && isMediaWidget(viewElement)) {
|
85
|
+
return viewElement;
|
86
|
+
}
|
87
|
+
return null;
|
88
|
+
}
|
89
|
+
/**
|
90
|
+
* Checks if a given view element is a media widget.
|
91
|
+
*/ function isMediaWidget(viewElement) {
|
92
|
+
return !!viewElement.getCustomProperty('media') && isWidget(viewElement);
|
93
|
+
}
|
94
|
+
/**
|
95
|
+
* Creates a view element representing the media. Either a "semantic" one for the data pipeline:
|
96
|
+
*
|
97
|
+
* ```html
|
98
|
+
* <figure class="media">
|
99
|
+
* <oembed url="foo"></oembed>
|
100
|
+
* </figure>
|
101
|
+
* ```
|
102
|
+
*
|
103
|
+
* or a "non-semantic" (for the editing view pipeline):
|
104
|
+
*
|
105
|
+
* ```html
|
106
|
+
* <figure class="media">
|
107
|
+
* <div data-oembed-url="foo">[ non-semantic media preview for "foo" ]</div>
|
108
|
+
* </figure>
|
109
|
+
* ```
|
110
|
+
*/ function createMediaFigureElement(writer, registry, url, options) {
|
111
|
+
return writer.createContainerElement('figure', {
|
112
|
+
class: 'media'
|
113
|
+
}, [
|
114
|
+
registry.getMediaViewElement(writer, url, options),
|
115
|
+
writer.createSlot()
|
116
|
+
]);
|
117
|
+
}
|
118
|
+
/**
|
119
|
+
* Returns a selected media element in the model, if any.
|
120
|
+
*/ function getSelectedMediaModelWidget(selection) {
|
121
|
+
const selectedElement = selection.getSelectedElement();
|
122
|
+
if (selectedElement && selectedElement.is('element', 'media')) {
|
123
|
+
return selectedElement;
|
124
|
+
}
|
125
|
+
return null;
|
126
|
+
}
|
127
|
+
/**
|
128
|
+
* Creates a media element and inserts it into the model.
|
129
|
+
*
|
130
|
+
* **Note**: This method will use {@link module:engine/model/model~Model#insertContent `model.insertContent()`} logic of inserting content
|
131
|
+
* if no `insertPosition` is passed.
|
132
|
+
*
|
133
|
+
* @param url An URL of an embeddable media.
|
134
|
+
* @param findOptimalPosition If true it will try to find optimal position to insert media without breaking content
|
135
|
+
* in which a selection is.
|
136
|
+
*/ function insertMedia(model, url, selectable, findOptimalPosition) {
|
137
|
+
model.change((writer)=>{
|
138
|
+
const mediaElement = writer.createElement('media', {
|
139
|
+
url
|
140
|
+
});
|
141
|
+
model.insertObject(mediaElement, selectable, null, {
|
142
|
+
setSelection: 'on',
|
143
|
+
findOptimalPosition: findOptimalPosition ? 'auto' : undefined
|
144
|
+
});
|
145
|
+
});
|
146
|
+
}
|
147
|
+
|
148
|
+
class MediaEmbedCommand extends Command {
|
149
|
+
/**
|
150
|
+
* @inheritDoc
|
151
|
+
*/ refresh() {
|
152
|
+
const model = this.editor.model;
|
153
|
+
const selection = model.document.selection;
|
154
|
+
const selectedMedia = getSelectedMediaModelWidget(selection);
|
155
|
+
this.value = selectedMedia ? selectedMedia.getAttribute('url') : undefined;
|
156
|
+
this.isEnabled = isMediaSelected(selection) || isAllowedInParent(selection, model);
|
157
|
+
}
|
158
|
+
/**
|
159
|
+
* Executes the command, which either:
|
160
|
+
*
|
161
|
+
* * updates the URL of the selected media,
|
162
|
+
* * inserts the new media into the editor and puts the selection around it.
|
163
|
+
*
|
164
|
+
* @fires execute
|
165
|
+
* @param url The URL of the media.
|
166
|
+
*/ execute(url) {
|
167
|
+
const model = this.editor.model;
|
168
|
+
const selection = model.document.selection;
|
169
|
+
const selectedMedia = getSelectedMediaModelWidget(selection);
|
170
|
+
if (selectedMedia) {
|
171
|
+
model.change((writer)=>{
|
172
|
+
writer.setAttribute('url', url, selectedMedia);
|
173
|
+
});
|
174
|
+
} else {
|
175
|
+
insertMedia(model, url, selection, true);
|
176
|
+
}
|
177
|
+
}
|
178
|
+
}
|
179
|
+
/**
|
180
|
+
* Checks if the media embed is allowed in the parent.
|
181
|
+
*/ function isAllowedInParent(selection, model) {
|
182
|
+
const insertionRange = findOptimalInsertionRange(selection, model);
|
183
|
+
let parent = insertionRange.start.parent;
|
184
|
+
// The model.insertContent() will remove empty parent (unless it is a $root or a limit).
|
185
|
+
if (parent.isEmpty && !model.schema.isLimit(parent)) {
|
186
|
+
parent = parent.parent;
|
187
|
+
}
|
188
|
+
return model.schema.checkChild(parent, 'media');
|
189
|
+
}
|
190
|
+
/**
|
191
|
+
* Checks if the media object is selected.
|
192
|
+
*/ function isMediaSelected(selection) {
|
193
|
+
const element = selection.getSelectedElement();
|
194
|
+
return !!element && element.name === 'media';
|
195
|
+
}
|
196
|
+
|
197
|
+
var mediaPlaceholderIcon = "<svg viewBox=\"0 0 64 42\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M47.426 17V3.713L63.102 0v19.389h-.001l.001.272c0 1.595-2.032 3.43-4.538 4.098-2.506.668-4.538-.083-4.538-1.678 0-1.594 2.032-3.43 4.538-4.098.914-.244 2.032-.565 2.888-.603V4.516L49.076 7.447v9.556A1.014 1.014 0 0 0 49 17h-1.574zM29.5 17h-8.343a7.073 7.073 0 1 0-4.657 4.06v3.781H3.3a2.803 2.803 0 0 1-2.8-2.804V8.63a2.803 2.803 0 0 1 2.8-2.805h4.082L8.58 2.768A1.994 1.994 0 0 1 10.435 1.5h8.985c.773 0 1.477.448 1.805 1.149l1.488 3.177H26.7c1.546 0 2.8 1.256 2.8 2.805V17zm-11.637 0H17.5a1 1 0 0 0-1 1v.05A4.244 4.244 0 1 1 17.863 17zm29.684 2c.97 0 .953-.048.953.889v20.743c0 .953.016.905-.953.905H19.453c-.97 0-.953.048-.953-.905V19.89c0-.937-.016-.889.97-.889h28.077zm-4.701 19.338V22.183H24.154v16.155h18.692zM20.6 21.375v1.616h1.616v-1.616H20.6zm0 3.231v1.616h1.616v-1.616H20.6zm0 3.231v1.616h1.616v-1.616H20.6zm0 3.231v1.616h1.616v-1.616H20.6zm0 3.231v1.616h1.616v-1.616H20.6zm0 3.231v1.616h1.616V37.53H20.6zm24.233-16.155v1.616h1.615v-1.616h-1.615zm0 3.231v1.616h1.615v-1.616h-1.615zm0 3.231v1.616h1.615v-1.616h-1.615zm0 3.231v1.616h1.615v-1.616h-1.615zm0 3.231v1.616h1.615v-1.616h-1.615zm0 3.231v1.616h1.615V37.53h-1.615zM29.485 25.283a.4.4 0 0 1 .593-.35l9.05 4.977a.4.4 0 0 1 0 .701l-9.05 4.978a.4.4 0 0 1-.593-.35v-9.956z\"/></svg>";
|
198
|
+
|
199
|
+
const mediaPlaceholderIconViewBox = '0 0 64 42';
|
200
|
+
class MediaRegistry {
|
201
|
+
/**
|
202
|
+
* Checks whether the passed URL is representing a certain media type allowed in the editor.
|
203
|
+
*
|
204
|
+
* @param url The URL to be checked
|
205
|
+
*/ hasMedia(url) {
|
206
|
+
return !!this._getMedia(url);
|
207
|
+
}
|
208
|
+
/**
|
209
|
+
* For the given media URL string and options, it returns the {@link module:engine/view/element~Element view element}
|
210
|
+
* representing that media.
|
211
|
+
*
|
212
|
+
* **Note:** If no URL is specified, an empty view element is returned.
|
213
|
+
*
|
214
|
+
* @param writer The view writer used to produce a view element.
|
215
|
+
* @param url The URL to be translated into a view element.
|
216
|
+
*/ getMediaViewElement(writer, url, options) {
|
217
|
+
return this._getMedia(url).getViewElement(writer, options);
|
218
|
+
}
|
219
|
+
/**
|
220
|
+
* Returns a `Media` instance for the given URL.
|
221
|
+
*
|
222
|
+
* @param url The URL of the media.
|
223
|
+
* @returns The `Media` instance or `null` when there is none.
|
224
|
+
*/ _getMedia(url) {
|
225
|
+
if (!url) {
|
226
|
+
return new Media(this.locale);
|
227
|
+
}
|
228
|
+
url = url.trim();
|
229
|
+
for (const definition of this.providerDefinitions){
|
230
|
+
const previewRenderer = definition.html;
|
231
|
+
const pattern = toArray(definition.url);
|
232
|
+
for (const subPattern of pattern){
|
233
|
+
const match = this._getUrlMatches(url, subPattern);
|
234
|
+
if (match) {
|
235
|
+
return new Media(this.locale, url, match, previewRenderer);
|
236
|
+
}
|
237
|
+
}
|
238
|
+
}
|
239
|
+
return null;
|
240
|
+
}
|
241
|
+
/**
|
242
|
+
* Tries to match `url` to `pattern`.
|
243
|
+
*
|
244
|
+
* @param url The URL of the media.
|
245
|
+
* @param pattern The pattern that should accept the media URL.
|
246
|
+
*/ _getUrlMatches(url, pattern) {
|
247
|
+
// 1. Try to match without stripping the protocol and "www" subdomain.
|
248
|
+
let match = url.match(pattern);
|
249
|
+
if (match) {
|
250
|
+
return match;
|
251
|
+
}
|
252
|
+
// 2. Try to match after stripping the protocol.
|
253
|
+
let rawUrl = url.replace(/^https?:\/\//, '');
|
254
|
+
match = rawUrl.match(pattern);
|
255
|
+
if (match) {
|
256
|
+
return match;
|
257
|
+
}
|
258
|
+
// 3. Try to match after stripping the "www" subdomain.
|
259
|
+
rawUrl = rawUrl.replace(/^www\./, '');
|
260
|
+
match = rawUrl.match(pattern);
|
261
|
+
if (match) {
|
262
|
+
return match;
|
263
|
+
}
|
264
|
+
return null;
|
265
|
+
}
|
266
|
+
/**
|
267
|
+
* Creates an instance of the {@link module:media-embed/mediaregistry~MediaRegistry} class.
|
268
|
+
*
|
269
|
+
* @param locale The localization services instance.
|
270
|
+
* @param config The configuration of the media embed feature.
|
271
|
+
*/ constructor(locale, config){
|
272
|
+
const providers = config.providers;
|
273
|
+
const extraProviders = config.extraProviders || [];
|
274
|
+
const removedProviders = new Set(config.removeProviders);
|
275
|
+
const providerDefinitions = providers.concat(extraProviders).filter((provider)=>{
|
276
|
+
const name = provider.name;
|
277
|
+
if (!name) {
|
278
|
+
/**
|
279
|
+
* One of the providers (or extra providers) specified in the media embed configuration
|
280
|
+
* has no name and will not be used by the editor. In order to get this media
|
281
|
+
* provider working, double check your editor configuration.
|
282
|
+
*
|
283
|
+
* @error media-embed-no-provider-name
|
284
|
+
*/ logWarning('media-embed-no-provider-name', {
|
285
|
+
provider
|
286
|
+
});
|
287
|
+
return false;
|
288
|
+
}
|
289
|
+
return !removedProviders.has(name);
|
290
|
+
});
|
291
|
+
this.locale = locale;
|
292
|
+
this.providerDefinitions = providerDefinitions;
|
293
|
+
}
|
294
|
+
}
|
295
|
+
/**
|
296
|
+
* Represents media defined by the provider configuration.
|
297
|
+
*
|
298
|
+
* It can be rendered to the {@link module:engine/view/element~Element view element} and used in the editing or data pipeline.
|
299
|
+
*/ class Media {
|
300
|
+
/**
|
301
|
+
* Returns the view element representation of the media.
|
302
|
+
*
|
303
|
+
* @param writer The view writer used to produce a view element.
|
304
|
+
*/ getViewElement(writer, options) {
|
305
|
+
const attributes = {};
|
306
|
+
let viewElement;
|
307
|
+
if (options.renderForEditingView || options.renderMediaPreview && this.url && this._previewRenderer) {
|
308
|
+
if (this.url) {
|
309
|
+
attributes['data-oembed-url'] = this.url;
|
310
|
+
}
|
311
|
+
if (options.renderForEditingView) {
|
312
|
+
attributes.class = 'ck-media__wrapper';
|
313
|
+
}
|
314
|
+
const mediaHtml = this._getPreviewHtml(options);
|
315
|
+
viewElement = writer.createRawElement('div', attributes, (domElement, domConverter)=>{
|
316
|
+
domConverter.setContentOf(domElement, mediaHtml);
|
317
|
+
});
|
318
|
+
} else {
|
319
|
+
if (this.url) {
|
320
|
+
attributes.url = this.url;
|
321
|
+
}
|
322
|
+
viewElement = writer.createEmptyElement(options.elementName, attributes);
|
323
|
+
}
|
324
|
+
writer.setCustomProperty('media-content', true, viewElement);
|
325
|
+
return viewElement;
|
326
|
+
}
|
327
|
+
/**
|
328
|
+
* Returns the HTML string of the media content preview.
|
329
|
+
*/ _getPreviewHtml(options) {
|
330
|
+
if (this._previewRenderer) {
|
331
|
+
return this._previewRenderer(this._match);
|
332
|
+
} else {
|
333
|
+
// The placeholder only makes sense for editing view and media which have URLs.
|
334
|
+
// Placeholder is never displayed in data and URL-less media have no content.
|
335
|
+
if (this.url && options.renderForEditingView) {
|
336
|
+
return this._getPlaceholderHtml();
|
337
|
+
}
|
338
|
+
return '';
|
339
|
+
}
|
340
|
+
}
|
341
|
+
/**
|
342
|
+
* Returns the placeholder HTML when the media has no content preview.
|
343
|
+
*/ _getPlaceholderHtml() {
|
344
|
+
const icon = new IconView();
|
345
|
+
const t = this._locale.t;
|
346
|
+
icon.content = mediaPlaceholderIcon;
|
347
|
+
icon.viewBox = mediaPlaceholderIconViewBox;
|
348
|
+
const placeholder = new Template({
|
349
|
+
tag: 'div',
|
350
|
+
attributes: {
|
351
|
+
class: 'ck ck-reset_all ck-media__placeholder'
|
352
|
+
},
|
353
|
+
children: [
|
354
|
+
{
|
355
|
+
tag: 'div',
|
356
|
+
attributes: {
|
357
|
+
class: 'ck-media__placeholder__icon'
|
358
|
+
},
|
359
|
+
children: [
|
360
|
+
icon
|
361
|
+
]
|
362
|
+
},
|
363
|
+
{
|
364
|
+
tag: 'a',
|
365
|
+
attributes: {
|
366
|
+
class: 'ck-media__placeholder__url',
|
367
|
+
target: '_blank',
|
368
|
+
rel: 'noopener noreferrer',
|
369
|
+
href: this.url,
|
370
|
+
'data-cke-tooltip-text': t('Open media in new tab')
|
371
|
+
},
|
372
|
+
children: [
|
373
|
+
{
|
374
|
+
tag: 'span',
|
375
|
+
attributes: {
|
376
|
+
class: 'ck-media__placeholder__url__text'
|
377
|
+
},
|
378
|
+
children: [
|
379
|
+
this.url
|
380
|
+
]
|
381
|
+
}
|
382
|
+
]
|
383
|
+
}
|
384
|
+
]
|
385
|
+
}).render();
|
386
|
+
return placeholder.outerHTML;
|
387
|
+
}
|
388
|
+
/**
|
389
|
+
* Returns the full URL to the specified media.
|
390
|
+
*
|
391
|
+
* @param url The URL of the media.
|
392
|
+
*/ _getValidUrl(url) {
|
393
|
+
if (!url) {
|
394
|
+
return null;
|
395
|
+
}
|
396
|
+
if (url.match(/^https?/)) {
|
397
|
+
return url;
|
398
|
+
}
|
399
|
+
return 'https://' + url;
|
400
|
+
}
|
401
|
+
constructor(locale, url, match, previewRenderer){
|
402
|
+
this.url = this._getValidUrl(url);
|
403
|
+
this._locale = locale;
|
404
|
+
this._match = match;
|
405
|
+
this._previewRenderer = previewRenderer;
|
406
|
+
}
|
407
|
+
}
|
408
|
+
|
409
|
+
class MediaEmbedEditing extends Plugin {
|
410
|
+
/**
|
411
|
+
* @inheritDoc
|
412
|
+
*/ static get pluginName() {
|
413
|
+
return 'MediaEmbedEditing';
|
414
|
+
}
|
415
|
+
/**
|
416
|
+
* @inheritDoc
|
417
|
+
*/ init() {
|
418
|
+
const editor = this.editor;
|
419
|
+
const schema = editor.model.schema;
|
420
|
+
const t = editor.t;
|
421
|
+
const conversion = editor.conversion;
|
422
|
+
const renderMediaPreview = editor.config.get('mediaEmbed.previewsInData');
|
423
|
+
const elementName = editor.config.get('mediaEmbed.elementName');
|
424
|
+
const registry = this.registry;
|
425
|
+
editor.commands.add('mediaEmbed', new MediaEmbedCommand(editor));
|
426
|
+
// Configure the schema.
|
427
|
+
schema.register('media', {
|
428
|
+
inheritAllFrom: '$blockObject',
|
429
|
+
allowAttributes: [
|
430
|
+
'url'
|
431
|
+
]
|
432
|
+
});
|
433
|
+
// Model -> Data
|
434
|
+
conversion.for('dataDowncast').elementToStructure({
|
435
|
+
model: 'media',
|
436
|
+
view: (modelElement, { writer })=>{
|
437
|
+
const url = modelElement.getAttribute('url');
|
438
|
+
return createMediaFigureElement(writer, registry, url, {
|
439
|
+
elementName,
|
440
|
+
renderMediaPreview: !!url && renderMediaPreview
|
441
|
+
});
|
442
|
+
}
|
443
|
+
});
|
444
|
+
// Model -> Data (url -> data-oembed-url)
|
445
|
+
conversion.for('dataDowncast').add(modelToViewUrlAttributeConverter(registry, {
|
446
|
+
elementName,
|
447
|
+
renderMediaPreview
|
448
|
+
}));
|
449
|
+
// Model -> View (element)
|
450
|
+
conversion.for('editingDowncast').elementToStructure({
|
451
|
+
model: 'media',
|
452
|
+
view: (modelElement, { writer })=>{
|
453
|
+
const url = modelElement.getAttribute('url');
|
454
|
+
const figure = createMediaFigureElement(writer, registry, url, {
|
455
|
+
elementName,
|
456
|
+
renderForEditingView: true
|
457
|
+
});
|
458
|
+
return toMediaWidget(figure, writer, t('media widget'));
|
459
|
+
}
|
460
|
+
});
|
461
|
+
// Model -> View (url -> data-oembed-url)
|
462
|
+
conversion.for('editingDowncast').add(modelToViewUrlAttributeConverter(registry, {
|
463
|
+
elementName,
|
464
|
+
renderForEditingView: true
|
465
|
+
}));
|
466
|
+
// View -> Model (data-oembed-url -> url)
|
467
|
+
conversion.for('upcast')// Upcast semantic media.
|
468
|
+
.elementToElement({
|
469
|
+
view: (element)=>[
|
470
|
+
'oembed',
|
471
|
+
elementName
|
472
|
+
].includes(element.name) && element.getAttribute('url') ? {
|
473
|
+
name: true
|
474
|
+
} : null,
|
475
|
+
model: (viewMedia, { writer })=>{
|
476
|
+
const url = viewMedia.getAttribute('url');
|
477
|
+
if (registry.hasMedia(url)) {
|
478
|
+
return writer.createElement('media', {
|
479
|
+
url
|
480
|
+
});
|
481
|
+
}
|
482
|
+
return null;
|
483
|
+
}
|
484
|
+
})// Upcast non-semantic media.
|
485
|
+
.elementToElement({
|
486
|
+
view: {
|
487
|
+
name: 'div',
|
488
|
+
attributes: {
|
489
|
+
'data-oembed-url': true
|
490
|
+
}
|
491
|
+
},
|
492
|
+
model: (viewMedia, { writer })=>{
|
493
|
+
const url = viewMedia.getAttribute('data-oembed-url');
|
494
|
+
if (registry.hasMedia(url)) {
|
495
|
+
return writer.createElement('media', {
|
496
|
+
url
|
497
|
+
});
|
498
|
+
}
|
499
|
+
return null;
|
500
|
+
}
|
501
|
+
})// Consume `<figure class="media">` elements, that were left after upcast.
|
502
|
+
.add((dispatcher)=>{
|
503
|
+
const converter = (evt, data, conversionApi)=>{
|
504
|
+
if (!conversionApi.consumable.consume(data.viewItem, {
|
505
|
+
name: true,
|
506
|
+
classes: 'media'
|
507
|
+
})) {
|
508
|
+
return;
|
509
|
+
}
|
510
|
+
const { modelRange, modelCursor } = conversionApi.convertChildren(data.viewItem, data.modelCursor);
|
511
|
+
data.modelRange = modelRange;
|
512
|
+
data.modelCursor = modelCursor;
|
513
|
+
const modelElement = first(modelRange.getItems());
|
514
|
+
if (!modelElement) {
|
515
|
+
// Revert consumed figure so other features can convert it.
|
516
|
+
conversionApi.consumable.revert(data.viewItem, {
|
517
|
+
name: true,
|
518
|
+
classes: 'media'
|
519
|
+
});
|
520
|
+
}
|
521
|
+
};
|
522
|
+
dispatcher.on('element:figure', converter);
|
523
|
+
});
|
524
|
+
}
|
525
|
+
/**
|
526
|
+
* @inheritDoc
|
527
|
+
*/ constructor(editor){
|
528
|
+
super(editor);
|
529
|
+
editor.config.define('mediaEmbed', {
|
530
|
+
elementName: 'oembed',
|
531
|
+
providers: [
|
532
|
+
{
|
533
|
+
name: 'dailymotion',
|
534
|
+
url: [
|
535
|
+
/^dailymotion\.com\/video\/(\w+)/,
|
536
|
+
/^dai.ly\/(\w+)/
|
537
|
+
],
|
538
|
+
html: (match)=>{
|
539
|
+
const id = match[1];
|
540
|
+
return '<div style="position: relative; padding-bottom: 100%; height: 0; ">' + `<iframe src="https://www.dailymotion.com/embed/video/${id}" ` + 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' + 'frameborder="0" width="480" height="270" allowfullscreen allow="autoplay">' + '</iframe>' + '</div>';
|
541
|
+
}
|
542
|
+
},
|
543
|
+
{
|
544
|
+
name: 'spotify',
|
545
|
+
url: [
|
546
|
+
/^open\.spotify\.com\/(artist\/\w+)/,
|
547
|
+
/^open\.spotify\.com\/(album\/\w+)/,
|
548
|
+
/^open\.spotify\.com\/(track\/\w+)/
|
549
|
+
],
|
550
|
+
html: (match)=>{
|
551
|
+
const id = match[1];
|
552
|
+
return '<div style="position: relative; padding-bottom: 100%; height: 0; padding-bottom: 126%;">' + `<iframe src="https://open.spotify.com/embed/${id}" ` + 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' + 'frameborder="0" allowtransparency="true" allow="encrypted-media">' + '</iframe>' + '</div>';
|
553
|
+
}
|
554
|
+
},
|
555
|
+
{
|
556
|
+
name: 'youtube',
|
557
|
+
url: [
|
558
|
+
/^(?:m\.)?youtube\.com\/watch\?v=([\w-]+)(?:&t=(\d+))?/,
|
559
|
+
/^(?:m\.)?youtube\.com\/v\/([\w-]+)(?:\?t=(\d+))?/,
|
560
|
+
/^youtube\.com\/embed\/([\w-]+)(?:\?start=(\d+))?/,
|
561
|
+
/^youtu\.be\/([\w-]+)(?:\?t=(\d+))?/
|
562
|
+
],
|
563
|
+
html: (match)=>{
|
564
|
+
const id = match[1];
|
565
|
+
const time = match[2];
|
566
|
+
return '<div style="position: relative; padding-bottom: 100%; height: 0; padding-bottom: 56.2493%;">' + `<iframe src="https://www.youtube.com/embed/${id}${time ? `?start=${time}` : ''}" ` + 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' + 'frameborder="0" allow="autoplay; encrypted-media" allowfullscreen>' + '</iframe>' + '</div>';
|
567
|
+
}
|
568
|
+
},
|
569
|
+
{
|
570
|
+
name: 'vimeo',
|
571
|
+
url: [
|
572
|
+
/^vimeo\.com\/(\d+)/,
|
573
|
+
/^vimeo\.com\/[^/]+\/[^/]+\/video\/(\d+)/,
|
574
|
+
/^vimeo\.com\/album\/[^/]+\/video\/(\d+)/,
|
575
|
+
/^vimeo\.com\/channels\/[^/]+\/(\d+)/,
|
576
|
+
/^vimeo\.com\/groups\/[^/]+\/videos\/(\d+)/,
|
577
|
+
/^vimeo\.com\/ondemand\/[^/]+\/(\d+)/,
|
578
|
+
/^player\.vimeo\.com\/video\/(\d+)/
|
579
|
+
],
|
580
|
+
html: (match)=>{
|
581
|
+
const id = match[1];
|
582
|
+
return '<div style="position: relative; padding-bottom: 100%; height: 0; padding-bottom: 56.2493%;">' + `<iframe src="https://player.vimeo.com/video/${id}" ` + 'style="position: absolute; width: 100%; height: 100%; top: 0; left: 0;" ' + 'frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen>' + '</iframe>' + '</div>';
|
583
|
+
}
|
584
|
+
},
|
585
|
+
{
|
586
|
+
name: 'instagram',
|
587
|
+
url: /^instagram\.com\/p\/(\w+)/
|
588
|
+
},
|
589
|
+
{
|
590
|
+
name: 'twitter',
|
591
|
+
url: /^twitter\.com/
|
592
|
+
},
|
593
|
+
{
|
594
|
+
name: 'googleMaps',
|
595
|
+
url: [
|
596
|
+
/^google\.com\/maps/,
|
597
|
+
/^goo\.gl\/maps/,
|
598
|
+
/^maps\.google\.com/,
|
599
|
+
/^maps\.app\.goo\.gl/
|
600
|
+
]
|
601
|
+
},
|
602
|
+
{
|
603
|
+
name: 'flickr',
|
604
|
+
url: /^flickr\.com/
|
605
|
+
},
|
606
|
+
{
|
607
|
+
name: 'facebook',
|
608
|
+
url: /^facebook\.com/
|
609
|
+
}
|
610
|
+
]
|
611
|
+
});
|
612
|
+
this.registry = new MediaRegistry(editor.locale, editor.config.get('mediaEmbed'));
|
613
|
+
}
|
614
|
+
}
|
615
|
+
|
616
|
+
const URL_REGEXP = /^(?:http(s)?:\/\/)?[\w-]+\.[\w-.~:/?#[\]@!$&'()*+,;=%]+$/;
|
617
|
+
class AutoMediaEmbed extends Plugin {
|
618
|
+
/**
|
619
|
+
* @inheritDoc
|
620
|
+
*/ static get requires() {
|
621
|
+
return [
|
622
|
+
Clipboard,
|
623
|
+
Delete,
|
624
|
+
Undo
|
625
|
+
];
|
626
|
+
}
|
627
|
+
/**
|
628
|
+
* @inheritDoc
|
629
|
+
*/ static get pluginName() {
|
630
|
+
return 'AutoMediaEmbed';
|
631
|
+
}
|
632
|
+
/**
|
633
|
+
* @inheritDoc
|
634
|
+
*/ init() {
|
635
|
+
const editor = this.editor;
|
636
|
+
const modelDocument = editor.model.document;
|
637
|
+
// We need to listen on `Clipboard#inputTransformation` because we need to save positions of selection.
|
638
|
+
// After pasting, the content between those positions will be checked for a URL that could be transformed
|
639
|
+
// into media.
|
640
|
+
const clipboardPipeline = editor.plugins.get('ClipboardPipeline');
|
641
|
+
this.listenTo(clipboardPipeline, 'inputTransformation', ()=>{
|
642
|
+
const firstRange = modelDocument.selection.getFirstRange();
|
643
|
+
const leftLivePosition = LivePosition.fromPosition(firstRange.start);
|
644
|
+
leftLivePosition.stickiness = 'toPrevious';
|
645
|
+
const rightLivePosition = LivePosition.fromPosition(firstRange.end);
|
646
|
+
rightLivePosition.stickiness = 'toNext';
|
647
|
+
modelDocument.once('change:data', ()=>{
|
648
|
+
this._embedMediaBetweenPositions(leftLivePosition, rightLivePosition);
|
649
|
+
leftLivePosition.detach();
|
650
|
+
rightLivePosition.detach();
|
651
|
+
}, {
|
652
|
+
priority: 'high'
|
653
|
+
});
|
654
|
+
});
|
655
|
+
const undoCommand = editor.commands.get('undo');
|
656
|
+
undoCommand.on('execute', ()=>{
|
657
|
+
if (this._timeoutId) {
|
658
|
+
global.window.clearTimeout(this._timeoutId);
|
659
|
+
this._positionToInsert.detach();
|
660
|
+
this._timeoutId = null;
|
661
|
+
this._positionToInsert = null;
|
662
|
+
}
|
663
|
+
}, {
|
664
|
+
priority: 'high'
|
665
|
+
});
|
666
|
+
}
|
667
|
+
/**
|
668
|
+
* Analyzes the part of the document between provided positions in search for a URL representing media.
|
669
|
+
* When the URL is found, it is automatically converted into media.
|
670
|
+
*
|
671
|
+
* @param leftPosition Left position of the selection.
|
672
|
+
* @param rightPosition Right position of the selection.
|
673
|
+
*/ _embedMediaBetweenPositions(leftPosition, rightPosition) {
|
674
|
+
const editor = this.editor;
|
675
|
+
const mediaRegistry = editor.plugins.get(MediaEmbedEditing).registry;
|
676
|
+
// TODO: Use marker instead of LiveRange & LivePositions.
|
677
|
+
const urlRange = new LiveRange(leftPosition, rightPosition);
|
678
|
+
const walker = urlRange.getWalker({
|
679
|
+
ignoreElementEnd: true
|
680
|
+
});
|
681
|
+
let url = '';
|
682
|
+
for (const node of walker){
|
683
|
+
if (node.item.is('$textProxy')) {
|
684
|
+
url += node.item.data;
|
685
|
+
}
|
686
|
+
}
|
687
|
+
url = url.trim();
|
688
|
+
// If the URL does not match to universal URL regexp, let's skip that.
|
689
|
+
if (!url.match(URL_REGEXP)) {
|
690
|
+
urlRange.detach();
|
691
|
+
return;
|
692
|
+
}
|
693
|
+
// If the URL represents a media, let's use it.
|
694
|
+
if (!mediaRegistry.hasMedia(url)) {
|
695
|
+
urlRange.detach();
|
696
|
+
return;
|
697
|
+
}
|
698
|
+
const mediaEmbedCommand = editor.commands.get('mediaEmbed');
|
699
|
+
// Do not anything if media element cannot be inserted at the current position (#47).
|
700
|
+
if (!mediaEmbedCommand.isEnabled) {
|
701
|
+
urlRange.detach();
|
702
|
+
return;
|
703
|
+
}
|
704
|
+
// Position won't be available in the `setTimeout` function so let's clone it.
|
705
|
+
this._positionToInsert = LivePosition.fromPosition(leftPosition);
|
706
|
+
// This action mustn't be executed if undo was called between pasting and auto-embedding.
|
707
|
+
this._timeoutId = global.window.setTimeout(()=>{
|
708
|
+
editor.model.change((writer)=>{
|
709
|
+
this._timeoutId = null;
|
710
|
+
writer.remove(urlRange);
|
711
|
+
urlRange.detach();
|
712
|
+
let insertionPosition = null;
|
713
|
+
// Check if position where the media element should be inserted is still valid.
|
714
|
+
// Otherwise leave it as undefined to use document.selection - default behavior of model.insertContent().
|
715
|
+
if (this._positionToInsert.root.rootName !== '$graveyard') {
|
716
|
+
insertionPosition = this._positionToInsert;
|
717
|
+
}
|
718
|
+
insertMedia(editor.model, url, insertionPosition, false);
|
719
|
+
this._positionToInsert.detach();
|
720
|
+
this._positionToInsert = null;
|
721
|
+
});
|
722
|
+
editor.plugins.get(Delete).requestUndoOnBackspace();
|
723
|
+
}, 100);
|
724
|
+
}
|
725
|
+
/**
|
726
|
+
* @inheritDoc
|
727
|
+
*/ constructor(editor){
|
728
|
+
super(editor);
|
729
|
+
this._timeoutId = null;
|
730
|
+
this._positionToInsert = null;
|
731
|
+
}
|
732
|
+
}
|
733
|
+
|
734
|
+
class MediaFormView extends View {
|
735
|
+
/**
|
736
|
+
* @inheritDoc
|
737
|
+
*/ render() {
|
738
|
+
super.render();
|
739
|
+
submitHandler({
|
740
|
+
view: this
|
741
|
+
});
|
742
|
+
const childViews = [
|
743
|
+
this.urlInputView,
|
744
|
+
this.saveButtonView,
|
745
|
+
this.cancelButtonView
|
746
|
+
];
|
747
|
+
childViews.forEach((v)=>{
|
748
|
+
// Register the view as focusable.
|
749
|
+
this._focusables.add(v);
|
750
|
+
// Register the view in the focus tracker.
|
751
|
+
this.focusTracker.add(v.element);
|
752
|
+
});
|
753
|
+
// Start listening for the keystrokes coming from #element.
|
754
|
+
this.keystrokes.listenTo(this.element);
|
755
|
+
const stopPropagation = (data)=>data.stopPropagation();
|
756
|
+
// Since the form is in the dropdown panel which is a child of the toolbar, the toolbar's
|
757
|
+
// keystroke handler would take over the key management in the URL input. We need to prevent
|
758
|
+
// this ASAP. Otherwise, the basic caret movement using the arrow keys will be impossible.
|
759
|
+
this.keystrokes.set('arrowright', stopPropagation);
|
760
|
+
this.keystrokes.set('arrowleft', stopPropagation);
|
761
|
+
this.keystrokes.set('arrowup', stopPropagation);
|
762
|
+
this.keystrokes.set('arrowdown', stopPropagation);
|
763
|
+
}
|
764
|
+
/**
|
765
|
+
* @inheritDoc
|
766
|
+
*/ destroy() {
|
767
|
+
super.destroy();
|
768
|
+
this.focusTracker.destroy();
|
769
|
+
this.keystrokes.destroy();
|
770
|
+
}
|
771
|
+
/**
|
772
|
+
* Focuses the fist {@link #_focusables} in the form.
|
773
|
+
*/ focus() {
|
774
|
+
this._focusCycler.focusFirst();
|
775
|
+
}
|
776
|
+
/**
|
777
|
+
* The native DOM `value` of the {@link #urlInputView} element.
|
778
|
+
*
|
779
|
+
* **Note**: Do not confuse it with the {@link module:ui/inputtext/inputtextview~InputTextView#value}
|
780
|
+
* which works one way only and may not represent the actual state of the component in the DOM.
|
781
|
+
*/ get url() {
|
782
|
+
return this.urlInputView.fieldView.element.value.trim();
|
783
|
+
}
|
784
|
+
set url(url) {
|
785
|
+
this.urlInputView.fieldView.element.value = url.trim();
|
786
|
+
}
|
787
|
+
/**
|
788
|
+
* Validates the form and returns `false` when some fields are invalid.
|
789
|
+
*/ isValid() {
|
790
|
+
this.resetFormStatus();
|
791
|
+
for (const validator of this._validators){
|
792
|
+
const errorText = validator(this);
|
793
|
+
// One error per field is enough.
|
794
|
+
if (errorText) {
|
795
|
+
// Apply updated error.
|
796
|
+
this.urlInputView.errorText = errorText;
|
797
|
+
return false;
|
798
|
+
}
|
799
|
+
}
|
800
|
+
return true;
|
801
|
+
}
|
802
|
+
/**
|
803
|
+
* Cleans up the supplementary error and information text of the {@link #urlInputView}
|
804
|
+
* bringing them back to the state when the form has been displayed for the first time.
|
805
|
+
*
|
806
|
+
* See {@link #isValid}.
|
807
|
+
*/ resetFormStatus() {
|
808
|
+
this.urlInputView.errorText = null;
|
809
|
+
this.urlInputView.infoText = this._urlInputViewInfoDefault;
|
810
|
+
}
|
811
|
+
/**
|
812
|
+
* Creates a labeled input view.
|
813
|
+
*
|
814
|
+
* @returns Labeled input view instance.
|
815
|
+
*/ _createUrlInput() {
|
816
|
+
const t = this.locale.t;
|
817
|
+
const labeledInput = new LabeledFieldView(this.locale, createLabeledInputText);
|
818
|
+
const inputField = labeledInput.fieldView;
|
819
|
+
this._urlInputViewInfoDefault = t('Paste the media URL in the input.');
|
820
|
+
this._urlInputViewInfoTip = t('Tip: Paste the URL into the content to embed faster.');
|
821
|
+
labeledInput.label = t('Media URL');
|
822
|
+
labeledInput.infoText = this._urlInputViewInfoDefault;
|
823
|
+
inputField.on('input', ()=>{
|
824
|
+
// Display the tip text only when there is some value. Otherwise fall back to the default info text.
|
825
|
+
labeledInput.infoText = inputField.element.value ? this._urlInputViewInfoTip : this._urlInputViewInfoDefault;
|
826
|
+
this.mediaURLInputValue = inputField.element.value.trim();
|
827
|
+
});
|
828
|
+
return labeledInput;
|
829
|
+
}
|
830
|
+
/**
|
831
|
+
* Creates a button view.
|
832
|
+
*
|
833
|
+
* @param label The button label.
|
834
|
+
* @param icon The button icon.
|
835
|
+
* @param className The additional button CSS class name.
|
836
|
+
* @param eventName An event name that the `ButtonView#execute` event will be delegated to.
|
837
|
+
* @returns The button view instance.
|
838
|
+
*/ _createButton(label, icon, className, eventName) {
|
839
|
+
const button = new ButtonView(this.locale);
|
840
|
+
button.set({
|
841
|
+
label,
|
842
|
+
icon,
|
843
|
+
tooltip: true
|
844
|
+
});
|
845
|
+
button.extendTemplate({
|
846
|
+
attributes: {
|
847
|
+
class: className
|
848
|
+
}
|
849
|
+
});
|
850
|
+
if (eventName) {
|
851
|
+
button.delegate('execute').to(this, eventName);
|
852
|
+
}
|
853
|
+
return button;
|
854
|
+
}
|
855
|
+
/**
|
856
|
+
* @param validators Form validators used by {@link #isValid}.
|
857
|
+
* @param locale The localization services instance.
|
858
|
+
*/ constructor(validators, locale){
|
859
|
+
super(locale);
|
860
|
+
const t = locale.t;
|
861
|
+
this.focusTracker = new FocusTracker();
|
862
|
+
this.keystrokes = new KeystrokeHandler();
|
863
|
+
this.set('mediaURLInputValue', '');
|
864
|
+
this.urlInputView = this._createUrlInput();
|
865
|
+
this.saveButtonView = this._createButton(t('Save'), icons.check, 'ck-button-save');
|
866
|
+
this.saveButtonView.type = 'submit';
|
867
|
+
this.cancelButtonView = this._createButton(t('Cancel'), icons.cancel, 'ck-button-cancel', 'cancel');
|
868
|
+
this._focusables = new ViewCollection();
|
869
|
+
this._focusCycler = new FocusCycler({
|
870
|
+
focusables: this._focusables,
|
871
|
+
focusTracker: this.focusTracker,
|
872
|
+
keystrokeHandler: this.keystrokes,
|
873
|
+
actions: {
|
874
|
+
// Navigate form fields backwards using the <kbd>Shift</kbd> + <kbd>Tab</kbd> keystroke.
|
875
|
+
focusPrevious: 'shift + tab',
|
876
|
+
// Navigate form fields forwards using the <kbd>Tab</kbd> key.
|
877
|
+
focusNext: 'tab'
|
878
|
+
}
|
879
|
+
});
|
880
|
+
this._validators = validators;
|
881
|
+
this.setTemplate({
|
882
|
+
tag: 'form',
|
883
|
+
attributes: {
|
884
|
+
class: [
|
885
|
+
'ck',
|
886
|
+
'ck-media-form',
|
887
|
+
'ck-responsive-form'
|
888
|
+
],
|
889
|
+
tabindex: '-1'
|
890
|
+
},
|
891
|
+
children: [
|
892
|
+
this.urlInputView,
|
893
|
+
this.saveButtonView,
|
894
|
+
this.cancelButtonView
|
895
|
+
]
|
896
|
+
});
|
897
|
+
}
|
898
|
+
}
|
899
|
+
|
900
|
+
var mediaIcon = "<svg viewBox=\"0 0 22 20\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M1.587 1.5c-.612 0-.601-.029-.601.551v14.84c0 .59-.01.559.591.559h18.846c.602 0 .591.03.591-.56V2.052c0-.58.01-.55-.591-.55H1.587Zm.701.971h1.003v1H2.288v-1Zm16.448 0h1.003v1h-1.003v-1Zm-14.24 1h13.008v12H4.467l.029-12Zm-2.208 1h1.003v1H2.288v-1Zm16.448 0h1.003v1h-1.003v-1Zm-16.448 2h1.003v1H2.288v-1Zm16.448 0h1.003v1h-1.003v-1Zm-16.448 2h1.003v1H2.288v-1Zm16.448 0h1.003v1h-1.003v-1Zm-16.448 2h1.003v1H2.288v-1Zm16.448 0h1.003v1h-1.003v-1Zm-16.448 2h1.003l-.029 1h-.974v-1Zm16.448 0h1.003v1h-1.003v-1Zm-16.448 2h.974v1h-.974v-1Zm16.448 0h1.003v1h-1.003v-1Z\"/><path d=\"M8.374 6.648a.399.399 0 0 1 .395-.4.402.402 0 0 1 .2.049l5.148 2.824a.4.4 0 0 1 0 .7l-5.148 2.824a.403.403 0 0 1-.595-.35V6.648Z\"/></svg>";
|
901
|
+
|
902
|
+
class MediaEmbedUI extends Plugin {
|
903
|
+
/**
|
904
|
+
* @inheritDoc
|
905
|
+
*/ static get requires() {
|
906
|
+
return [
|
907
|
+
MediaEmbedEditing
|
908
|
+
];
|
909
|
+
}
|
910
|
+
/**
|
911
|
+
* @inheritDoc
|
912
|
+
*/ static get pluginName() {
|
913
|
+
return 'MediaEmbedUI';
|
914
|
+
}
|
915
|
+
/**
|
916
|
+
* @inheritDoc
|
917
|
+
*/ init() {
|
918
|
+
const editor = this.editor;
|
919
|
+
const command = editor.commands.get('mediaEmbed');
|
920
|
+
editor.ui.componentFactory.add('mediaEmbed', (locale)=>{
|
921
|
+
const dropdown = createDropdown(locale);
|
922
|
+
this._setUpDropdown(dropdown, command);
|
923
|
+
return dropdown;
|
924
|
+
});
|
925
|
+
}
|
926
|
+
_setUpDropdown(dropdown, command) {
|
927
|
+
const editor = this.editor;
|
928
|
+
const t = editor.t;
|
929
|
+
const button = dropdown.buttonView;
|
930
|
+
const registry = editor.plugins.get(MediaEmbedEditing).registry;
|
931
|
+
dropdown.once('change:isOpen', ()=>{
|
932
|
+
const form = new (CssTransitionDisablerMixin(MediaFormView))(getFormValidators(editor.t, registry), editor.locale);
|
933
|
+
dropdown.panelView.children.add(form);
|
934
|
+
// Note: Use the low priority to make sure the following listener starts working after the
|
935
|
+
// default action of the drop-down is executed (i.e. the panel showed up). Otherwise, the
|
936
|
+
// invisible form/input cannot be focused/selected.
|
937
|
+
button.on('open', ()=>{
|
938
|
+
form.disableCssTransitions();
|
939
|
+
// Make sure that each time the panel shows up, the URL field remains in sync with the value of
|
940
|
+
// the command. If the user typed in the input, then canceled (`urlInputView#fieldView#value` stays
|
941
|
+
// unaltered) and re-opened it without changing the value of the media command (e.g. because they
|
942
|
+
// didn't change the selection), they would see the old value instead of the actual value of the
|
943
|
+
// command.
|
944
|
+
form.url = command.value || '';
|
945
|
+
form.urlInputView.fieldView.select();
|
946
|
+
form.enableCssTransitions();
|
947
|
+
}, {
|
948
|
+
priority: 'low'
|
949
|
+
});
|
950
|
+
dropdown.on('submit', ()=>{
|
951
|
+
if (form.isValid()) {
|
952
|
+
editor.execute('mediaEmbed', form.url);
|
953
|
+
editor.editing.view.focus();
|
954
|
+
}
|
955
|
+
});
|
956
|
+
dropdown.on('change:isOpen', ()=>form.resetFormStatus());
|
957
|
+
dropdown.on('cancel', ()=>{
|
958
|
+
editor.editing.view.focus();
|
959
|
+
});
|
960
|
+
form.delegate('submit', 'cancel').to(dropdown);
|
961
|
+
form.urlInputView.fieldView.bind('value').to(command, 'value');
|
962
|
+
// Update balloon position when form error changes.
|
963
|
+
form.urlInputView.on('change:errorText', ()=>{
|
964
|
+
editor.ui.update();
|
965
|
+
});
|
966
|
+
// Form elements should be read-only when corresponding commands are disabled.
|
967
|
+
form.urlInputView.bind('isEnabled').to(command, 'isEnabled');
|
968
|
+
});
|
969
|
+
dropdown.bind('isEnabled').to(command);
|
970
|
+
button.set({
|
971
|
+
label: t('Insert media'),
|
972
|
+
icon: mediaIcon,
|
973
|
+
tooltip: true
|
974
|
+
});
|
975
|
+
}
|
976
|
+
}
|
977
|
+
function getFormValidators(t, registry) {
|
978
|
+
return [
|
979
|
+
(form)=>{
|
980
|
+
if (!form.url.length) {
|
981
|
+
return t('The URL must not be empty.');
|
982
|
+
}
|
983
|
+
},
|
984
|
+
(form)=>{
|
985
|
+
if (!registry.hasMedia(form.url)) {
|
986
|
+
return t('This media URL is not supported.');
|
987
|
+
}
|
988
|
+
}
|
989
|
+
];
|
990
|
+
}
|
991
|
+
|
992
|
+
class MediaEmbed extends Plugin {
|
993
|
+
/**
|
994
|
+
* @inheritDoc
|
995
|
+
*/ static get requires() {
|
996
|
+
return [
|
997
|
+
MediaEmbedEditing,
|
998
|
+
MediaEmbedUI,
|
999
|
+
AutoMediaEmbed,
|
1000
|
+
Widget
|
1001
|
+
];
|
1002
|
+
}
|
1003
|
+
/**
|
1004
|
+
* @inheritDoc
|
1005
|
+
*/ static get pluginName() {
|
1006
|
+
return 'MediaEmbed';
|
1007
|
+
}
|
1008
|
+
}
|
1009
|
+
|
1010
|
+
class MediaEmbedToolbar extends Plugin {
|
1011
|
+
/**
|
1012
|
+
* @inheritDoc
|
1013
|
+
*/ static get requires() {
|
1014
|
+
return [
|
1015
|
+
WidgetToolbarRepository
|
1016
|
+
];
|
1017
|
+
}
|
1018
|
+
/**
|
1019
|
+
* @inheritDoc
|
1020
|
+
*/ static get pluginName() {
|
1021
|
+
return 'MediaEmbedToolbar';
|
1022
|
+
}
|
1023
|
+
/**
|
1024
|
+
* @inheritDoc
|
1025
|
+
*/ afterInit() {
|
1026
|
+
const editor = this.editor;
|
1027
|
+
const t = editor.t;
|
1028
|
+
const widgetToolbarRepository = editor.plugins.get(WidgetToolbarRepository);
|
1029
|
+
widgetToolbarRepository.register('mediaEmbed', {
|
1030
|
+
ariaLabel: t('Media toolbar'),
|
1031
|
+
items: editor.config.get('mediaEmbed.toolbar') || [],
|
1032
|
+
getRelatedElement: getSelectedMediaViewWidget
|
1033
|
+
});
|
1034
|
+
}
|
1035
|
+
}
|
1036
|
+
|
1037
|
+
export { AutoMediaEmbed, MediaEmbed, MediaEmbedEditing, MediaEmbedToolbar, MediaEmbedUI };
|
1038
|
+
//# sourceMappingURL=index.js.map
|