UNPKG

11.5 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.getEmbedEditComponent = getEmbedEditComponent;
9
10var _element = require("@wordpress/element");
11
12var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
13
14var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
15
16var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
17
18var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
19
20var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
21
22var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
23
24var _util = require("./util");
25
26var _embedControls = _interopRequireDefault(require("./embed-controls"));
27
28var _embedLoading = _interopRequireDefault(require("./embed-loading"));
29
30var _embedPlaceholder = _interopRequireDefault(require("./embed-placeholder"));
31
32var _embedPreview = _interopRequireDefault(require("./embed-preview"));
33
34var _lodash = require("lodash");
35
36var _i18n = require("@wordpress/i18n");
37
38/**
39 * Internal dependencies
40 */
41
42/**
43 * External dependencies
44 */
45
46/**
47 * WordPress dependencies
48 */
49function getEmbedEditComponent(title, icon) {
50 var responsive = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
51 return (
52 /*#__PURE__*/
53 function (_Component) {
54 (0, _inherits2.default)(_class, _Component);
55
56 function _class() {
57 var _this;
58
59 (0, _classCallCheck2.default)(this, _class);
60 _this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(_class).apply(this, arguments));
61 _this.switchBackToURLInput = _this.switchBackToURLInput.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
62 _this.setUrl = _this.setUrl.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
63 _this.getAttributesFromPreview = _this.getAttributesFromPreview.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
64 _this.setAttributesFromPreview = _this.setAttributesFromPreview.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
65 _this.getResponsiveHelp = _this.getResponsiveHelp.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
66 _this.toggleResponsive = _this.toggleResponsive.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
67 _this.handleIncomingPreview = _this.handleIncomingPreview.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
68 _this.state = {
69 editingURL: false,
70 url: _this.props.attributes.url
71 };
72
73 if (_this.props.preview) {
74 _this.handleIncomingPreview();
75 }
76
77 return _this;
78 }
79
80 (0, _createClass2.default)(_class, [{
81 key: "handleIncomingPreview",
82 value: function handleIncomingPreview() {
83 var allowResponsive = this.props.attributes.allowResponsive;
84 this.setAttributesFromPreview();
85 var upgradedBlock = (0, _util.createUpgradedEmbedBlock)(this.props, this.getAttributesFromPreview(this.props.preview, allowResponsive));
86
87 if (upgradedBlock) {
88 this.props.onReplace(upgradedBlock);
89 }
90 }
91 }, {
92 key: "componentDidUpdate",
93 value: function componentDidUpdate(prevProps) {
94 var hasPreview = undefined !== this.props.preview;
95 var hadPreview = undefined !== prevProps.preview;
96 var previewChanged = prevProps.preview && this.props.preview && this.props.preview.html !== prevProps.preview.html;
97 var switchedPreview = previewChanged || hasPreview && !hadPreview;
98 var switchedURL = this.props.attributes.url !== prevProps.attributes.url;
99
100 if (switchedPreview || switchedURL) {
101 if (this.props.cannotEmbed) {
102 // We either have a new preview or a new URL, but we can't embed it.
103 if (!this.props.fetching) {
104 // If we're not fetching the preview, then we know it can't be embedded, so try
105 // removing any trailing slash, and resubmit.
106 this.resubmitWithoutTrailingSlash();
107 }
108
109 return;
110 }
111
112 this.handleIncomingPreview();
113 }
114 }
115 }, {
116 key: "resubmitWithoutTrailingSlash",
117 value: function resubmitWithoutTrailingSlash() {
118 this.setState(function (prevState) {
119 return {
120 url: prevState.url.replace(/\/$/, '')
121 };
122 }, this.setUrl);
123 }
124 }, {
125 key: "setUrl",
126 value: function setUrl(event) {
127 if (event) {
128 event.preventDefault();
129 }
130
131 var url = this.state.url;
132 var setAttributes = this.props.setAttributes;
133 this.setState({
134 editingURL: false
135 });
136 setAttributes({
137 url: url
138 });
139 }
140 /***
141 * Gets block attributes based on the preview and responsive state.
142 *
143 * @param {string} preview The preview data.
144 * @param {boolean} allowResponsive Apply responsive classes to fixed size content.
145 * @return {Object} Attributes and values.
146 */
147
148 }, {
149 key: "getAttributesFromPreview",
150 value: function getAttributesFromPreview(preview) {
151 var allowResponsive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
152 var attributes = {}; // Some plugins only return HTML with no type info, so default this to 'rich'.
153
154 var _preview$type = preview.type,
155 type = _preview$type === void 0 ? 'rich' : _preview$type; // If we got a provider name from the API, use it for the slug, otherwise we use the title,
156 // because not all embed code gives us a provider name.
157
158 var html = preview.html,
159 providerName = preview.provider_name;
160 var providerNameSlug = (0, _lodash.kebabCase)((0, _lodash.toLower)('' !== providerName ? providerName : title));
161
162 if ((0, _util.isFromWordPress)(html)) {
163 type = 'wp-embed';
164 }
165
166 if (html || 'photo' === type) {
167 attributes.type = type;
168 attributes.providerNameSlug = providerNameSlug;
169 }
170
171 attributes.className = (0, _util.getClassNames)(html, this.props.attributes.className, responsive && allowResponsive);
172 return attributes;
173 }
174 /***
175 * Sets block attributes based on the preview data.
176 */
177
178 }, {
179 key: "setAttributesFromPreview",
180 value: function setAttributesFromPreview() {
181 var _this$props = this.props,
182 setAttributes = _this$props.setAttributes,
183 preview = _this$props.preview;
184 var allowResponsive = this.props.attributes.allowResponsive;
185 setAttributes(this.getAttributesFromPreview(preview, allowResponsive));
186 }
187 }, {
188 key: "switchBackToURLInput",
189 value: function switchBackToURLInput() {
190 this.setState({
191 editingURL: true
192 });
193 }
194 }, {
195 key: "getResponsiveHelp",
196 value: function getResponsiveHelp(checked) {
197 return checked ? (0, _i18n.__)('This embed will preserve its aspect ratio when the browser is resized.') : (0, _i18n.__)('This embed may not preserve its aspect ratio when the browser is resized.');
198 }
199 }, {
200 key: "toggleResponsive",
201 value: function toggleResponsive() {
202 var _this$props$attribute = this.props.attributes,
203 allowResponsive = _this$props$attribute.allowResponsive,
204 className = _this$props$attribute.className;
205 var html = this.props.preview.html;
206 var newAllowResponsive = !allowResponsive;
207 this.props.setAttributes({
208 allowResponsive: newAllowResponsive,
209 className: (0, _util.getClassNames)(html, className, responsive && newAllowResponsive)
210 });
211 }
212 }, {
213 key: "render",
214 value: function render() {
215 var _this2 = this;
216
217 var _this$state = this.state,
218 url = _this$state.url,
219 editingURL = _this$state.editingURL;
220 var _this$props$attribute2 = this.props.attributes,
221 caption = _this$props$attribute2.caption,
222 type = _this$props$attribute2.type,
223 allowResponsive = _this$props$attribute2.allowResponsive;
224 var _this$props2 = this.props,
225 fetching = _this$props2.fetching,
226 setAttributes = _this$props2.setAttributes,
227 isSelected = _this$props2.isSelected,
228 className = _this$props2.className,
229 preview = _this$props2.preview,
230 cannotEmbed = _this$props2.cannotEmbed,
231 themeSupportsResponsive = _this$props2.themeSupportsResponsive,
232 tryAgain = _this$props2.tryAgain;
233
234 if (fetching) {
235 return (0, _element.createElement)(_embedLoading.default, null);
236 } // translators: %s: type of embed e.g: "YouTube", "Twitter", etc. "Embed" is used when no specific type exists
237
238
239 var label = (0, _i18n.sprintf)((0, _i18n.__)('%s URL'), title); // No preview, or we can't embed the current URL, or we've clicked the edit button.
240
241 if (!preview || cannotEmbed || editingURL) {
242 return (0, _element.createElement)(_embedPlaceholder.default, {
243 icon: icon,
244 label: label,
245 onSubmit: this.setUrl,
246 value: url,
247 cannotEmbed: cannotEmbed,
248 onChange: function onChange(event) {
249 return _this2.setState({
250 url: event.target.value
251 });
252 },
253 fallback: function fallback() {
254 return (0, _util.fallback)(url, _this2.props.onReplace);
255 },
256 tryAgain: tryAgain
257 });
258 }
259
260 return (0, _element.createElement)(_element.Fragment, null, (0, _element.createElement)(_embedControls.default, {
261 showEditButton: preview && !cannotEmbed,
262 themeSupportsResponsive: themeSupportsResponsive,
263 blockSupportsResponsive: responsive,
264 allowResponsive: allowResponsive,
265 getResponsiveHelp: this.getResponsiveHelp,
266 toggleResponsive: this.toggleResponsive,
267 switchBackToURLInput: this.switchBackToURLInput
268 }), (0, _element.createElement)(_embedPreview.default, {
269 preview: preview,
270 className: className,
271 url: url,
272 type: type,
273 caption: caption,
274 onCaptionChange: function onCaptionChange(value) {
275 return setAttributes({
276 caption: value
277 });
278 },
279 isSelected: isSelected,
280 icon: icon,
281 label: label
282 }));
283 }
284 }]);
285 return _class;
286 }(_element.Component)
287 );
288}
289//# sourceMappingURL=edit.js.map
\No newline at end of file