UNPKG

5.66 kBJavaScriptView Raw
1import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2import _objectSpread from "@babel/runtime/helpers/esm/objectSpread";
3import { createElement } from "@wordpress/element";
4
5/**
6 * Internal dependencies
7 */
8import { getEmbedEditComponent } from './edit';
9/**
10 * External dependencies
11 */
12
13import classnames from 'classnames/dedupe';
14/**
15 * WordPress dependencies
16 */
17
18import { __ } from '@wordpress/i18n';
19import { compose } from '@wordpress/compose';
20import { RichText } from '@wordpress/block-editor';
21import { withSelect, withDispatch } from '@wordpress/data';
22var embedAttributes = {
23 url: {
24 type: 'string'
25 },
26 caption: {
27 type: 'string',
28 source: 'html',
29 selector: 'figcaption'
30 },
31 type: {
32 type: 'string'
33 },
34 providerNameSlug: {
35 type: 'string'
36 },
37 allowResponsive: {
38 type: 'boolean',
39 default: true
40 }
41};
42export function getEmbedBlockSettings(_ref) {
43 var title = _ref.title,
44 description = _ref.description,
45 icon = _ref.icon,
46 _ref$category = _ref.category,
47 category = _ref$category === void 0 ? 'embed' : _ref$category,
48 transforms = _ref.transforms,
49 _ref$keywords = _ref.keywords,
50 keywords = _ref$keywords === void 0 ? [] : _ref$keywords,
51 _ref$supports = _ref.supports,
52 supports = _ref$supports === void 0 ? {} : _ref$supports,
53 _ref$responsive = _ref.responsive,
54 responsive = _ref$responsive === void 0 ? true : _ref$responsive;
55
56 var blockDescription = description || __('Add a block that displays content pulled from other sites, like Twitter, Instagram or YouTube.');
57
58 var edit = getEmbedEditComponent(title, icon, responsive);
59 return {
60 title: title,
61 description: blockDescription,
62 icon: icon,
63 category: category,
64 keywords: keywords,
65 attributes: embedAttributes,
66 supports: _objectSpread({
67 align: true
68 }, supports),
69 transforms: transforms,
70 edit: compose(withSelect(function (select, ownProps) {
71 var url = ownProps.attributes.url;
72 var core = select('core');
73 var getEmbedPreview = core.getEmbedPreview,
74 isPreviewEmbedFallback = core.isPreviewEmbedFallback,
75 isRequestingEmbedPreview = core.isRequestingEmbedPreview,
76 getThemeSupports = core.getThemeSupports;
77 var preview = undefined !== url && getEmbedPreview(url);
78 var previewIsFallback = undefined !== url && isPreviewEmbedFallback(url);
79 var fetching = undefined !== url && isRequestingEmbedPreview(url);
80 var themeSupports = getThemeSupports(); // The external oEmbed provider does not exist. We got no type info and no html.
81
82 var badEmbedProvider = !!preview && undefined === preview.type && false === preview.html; // Some WordPress URLs that can't be embedded will cause the API to return
83 // a valid JSON response with no HTML and `data.status` set to 404, rather
84 // than generating a fallback response as other embeds do.
85
86 var wordpressCantEmbed = !!preview && preview.data && preview.data.status === 404;
87 var validPreview = !!preview && !badEmbedProvider && !wordpressCantEmbed;
88 var cannotEmbed = undefined !== url && (!validPreview || previewIsFallback);
89 return {
90 preview: validPreview ? preview : undefined,
91 fetching: fetching,
92 themeSupportsResponsive: themeSupports['responsive-embeds'],
93 cannotEmbed: cannotEmbed
94 };
95 }), withDispatch(function (dispatch, ownProps) {
96 var url = ownProps.attributes.url;
97 var coreData = dispatch('core/data');
98
99 var tryAgain = function tryAgain() {
100 coreData.invalidateResolution('core', 'getEmbedPreview', [url]);
101 };
102
103 return {
104 tryAgain: tryAgain
105 };
106 }))(edit),
107 save: function save(_ref2) {
108 var _classnames;
109
110 var attributes = _ref2.attributes;
111 var url = attributes.url,
112 caption = attributes.caption,
113 type = attributes.type,
114 providerNameSlug = attributes.providerNameSlug;
115
116 if (!url) {
117 return null;
118 }
119
120 var embedClassName = classnames('wp-block-embed', (_classnames = {}, _defineProperty(_classnames, "is-type-".concat(type), type), _defineProperty(_classnames, "is-provider-".concat(providerNameSlug), providerNameSlug), _classnames));
121 return createElement("figure", {
122 className: embedClassName
123 }, createElement("div", {
124 className: "wp-block-embed__wrapper"
125 }, "\n".concat(url, "\n")
126 /* URL needs to be on its own line. */
127 ), !RichText.isEmpty(caption) && createElement(RichText.Content, {
128 tagName: "figcaption",
129 value: caption
130 }));
131 },
132 deprecated: [{
133 attributes: embedAttributes,
134 save: function save(_ref3) {
135 var _classnames2;
136
137 var attributes = _ref3.attributes;
138 var url = attributes.url,
139 caption = attributes.caption,
140 type = attributes.type,
141 providerNameSlug = attributes.providerNameSlug;
142
143 if (!url) {
144 return null;
145 }
146
147 var embedClassName = classnames('wp-block-embed', (_classnames2 = {}, _defineProperty(_classnames2, "is-type-".concat(type), type), _defineProperty(_classnames2, "is-provider-".concat(providerNameSlug), providerNameSlug), _classnames2));
148 return createElement("figure", {
149 className: embedClassName
150 }, "\n".concat(url, "\n")
151 /* URL needs to be on its own line. */
152 , !RichText.isEmpty(caption) && createElement(RichText.Content, {
153 tagName: "figcaption",
154 value: caption
155 }));
156 }
157 }]
158 };
159}
160//# sourceMappingURL=settings.js.map
\No newline at end of file