1 | import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
2 | import _objectSpread from "@babel/runtime/helpers/esm/objectSpread";
|
3 | import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
4 | import { createElement } from "@wordpress/element";
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | import { common, others } from './core-embeds';
|
10 | import { DEFAULT_EMBED_BLOCK, WORDPRESS_EMBED_BLOCK, ASPECT_RATIOS } from './constants';
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | import { includes } from 'lodash';
|
16 | import classnames from 'classnames/dedupe';
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | import { renderToString } from '@wordpress/element';
|
22 | import { createBlock } from '@wordpress/blocks';
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | export var matchesPatterns = function matchesPatterns(url) {
|
32 | var patterns = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
33 | return patterns.some(function (pattern) {
|
34 | return url.match(pattern);
|
35 | });
|
36 | };
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | export var findBlock = function findBlock(url) {
|
46 | var _arr = [].concat(_toConsumableArray(common), _toConsumableArray(others));
|
47 |
|
48 | for (var _i = 0; _i < _arr.length; _i++) {
|
49 | var block = _arr[_i];
|
50 |
|
51 | if (matchesPatterns(url, block.patterns)) {
|
52 | return block.name;
|
53 | }
|
54 | }
|
55 |
|
56 | return DEFAULT_EMBED_BLOCK;
|
57 | };
|
58 | export var isFromWordPress = function isFromWordPress(html) {
|
59 | return includes(html, 'class="wp-embedded-content"');
|
60 | };
|
61 | export var getPhotoHtml = function getPhotoHtml(photo) {
|
62 |
|
63 |
|
64 | var imageUrl = photo.thumbnail_url ? photo.thumbnail_url : photo.url;
|
65 | var photoPreview = createElement("p", null, createElement("img", {
|
66 | src: imageUrl,
|
67 | alt: photo.title,
|
68 | width: "100%"
|
69 | }));
|
70 | return renderToString(photoPreview);
|
71 | };
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | export var createUpgradedEmbedBlock = function createUpgradedEmbedBlock(props, attributesFromPreview) {
|
88 | var preview = props.preview,
|
89 | name = props.name;
|
90 | var url = props.attributes.url;
|
91 |
|
92 | if (!url) {
|
93 | return;
|
94 | }
|
95 |
|
96 | var matchingBlock = findBlock(url);
|
97 |
|
98 |
|
99 | if (WORDPRESS_EMBED_BLOCK !== name && DEFAULT_EMBED_BLOCK !== matchingBlock) {
|
100 |
|
101 | if (name !== matchingBlock) {
|
102 | return createBlock(matchingBlock, {
|
103 | url: url
|
104 | });
|
105 | }
|
106 | }
|
107 |
|
108 | if (preview) {
|
109 | var html = preview.html;
|
110 |
|
111 | if (isFromWordPress(html)) {
|
112 |
|
113 | if (WORDPRESS_EMBED_BLOCK !== name) {
|
114 | return createBlock(WORDPRESS_EMBED_BLOCK, _objectSpread({
|
115 | url: url
|
116 | }, attributesFromPreview));
|
117 | }
|
118 | }
|
119 | }
|
120 | };
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | export function getClassNames(html) {
|
131 | var existingClassNames = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
132 | var allowResponsive = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
133 |
|
134 | if (!allowResponsive) {
|
135 |
|
136 | var aspectRatioClassNames = {
|
137 | 'wp-has-aspect-ratio': false
|
138 | };
|
139 |
|
140 | for (var ratioIndex = 0; ratioIndex < ASPECT_RATIOS.length; ratioIndex++) {
|
141 | var aspectRatioToRemove = ASPECT_RATIOS[ratioIndex];
|
142 | aspectRatioClassNames[aspectRatioToRemove.className] = false;
|
143 | }
|
144 |
|
145 | return classnames(existingClassNames, aspectRatioClassNames);
|
146 | }
|
147 |
|
148 | var previewDocument = document.implementation.createHTMLDocument('');
|
149 | previewDocument.body.innerHTML = html;
|
150 | var iframe = previewDocument.body.querySelector('iframe');
|
151 |
|
152 | if (iframe && iframe.height && iframe.width) {
|
153 | var aspectRatio = (iframe.width / iframe.height).toFixed(2);
|
154 |
|
155 | for (var _ratioIndex = 0; _ratioIndex < ASPECT_RATIOS.length; _ratioIndex++) {
|
156 | var potentialRatio = ASPECT_RATIOS[_ratioIndex];
|
157 |
|
158 | if (aspectRatio >= potentialRatio.ratio) {
|
159 | var _classnames;
|
160 |
|
161 | return classnames(existingClassNames, (_classnames = {}, _defineProperty(_classnames, potentialRatio.className, allowResponsive), _defineProperty(_classnames, 'wp-has-aspect-ratio', allowResponsive), _classnames));
|
162 | }
|
163 | }
|
164 | }
|
165 |
|
166 | return existingClassNames;
|
167 | }
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | export function fallback(url, onReplace) {
|
177 | var link = createElement("a", {
|
178 | href: url
|
179 | }, url);
|
180 | onReplace(createBlock('core/paragraph', {
|
181 | content: renderToString(link)
|
182 | }));
|
183 | }
|
184 |
|
\ | No newline at end of file |