PluginProbe
Extendify / 1.17.1
Extendify v1.17.1
3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 0.7.0 All 126 releases
extendify / src / Draft / api / WPApi.js

WPApi.js in Extendify 1.17.1, at src/Draft/api/WPApi.js

132 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import apiFetch from '@wordpress/api-fetch';
2 import { createBlock, insertBlock } from '@wordpress/blocks';
3 import { downloadPing } from '@draft/api/Data';
4 import { loadImage } from '@draft/lib/image';
5
6 export const importImage = async (imageUrl, metadata = {}) => {
7 const image = new Image();
8 image.src = imageUrl;
9 image.crossOrigin = 'anonymous';
10 await loadImage(image);
11
12 const canvas = document.createElement('canvas');
13 canvas.width = image.width;
14 canvas.height = image.height;
15
16 const ctx = canvas.getContext('2d');
17 if (!ctx) return;
18 ctx.drawImage(image, 0, 0);
19
20 const blob = await new Promise((resolve) => {
21 canvas.toBlob((blob) => {
22 blob && resolve(blob);
23 }, 'image/jpeg');
24 });
25
26 const formData = new FormData();
27 formData.append('file', new File([blob], metadata.filename));
28 formData.append('alt_text', metadata.alt ?? '');
29 formData.append('caption', metadata.caption ?? '');
30 formData.append('status', 'publish');
31
32 return await apiFetch({
33 path: 'wp/v2/media',
34 method: 'POST',
35 body: formData,
36 });
37 };
38
39 export const importImageServer = async (src, metadata = {}) => {
40 const formData = new FormData();
41 formData.append('source', src);
42 // Fallback doesn't suppport custom file_name
43 formData.append('alt_text', metadata.alt ?? '');
44 formData.append('caption', metadata.caption ?? '');
45
46 return await apiFetch({
47 path: '/extendify/v1/draft/upload-image',
48 method: 'POST',
49 body: formData,
50 });
51 };
52
53 export const downloadImage = async (
54 id,
55 src,
56 source,
57 unsplashId,
58 metadata = { alt: '', caption: '' },
59 ) => {
60 let image;
61 await downloadPing(id, source, { unsplashId });
62 try {
63 image = await importImage(src, {
64 alt: metadata.alt,
65 filename: 'image.jpg',
66 caption: metadata.caption,
67 });
68 } catch (_e) {
69 image = await importImageServer(src, {
70 alt: metadata.alt,
71 filename: 'image.jpg',
72 caption: metadata.caption,
73 });
74 }
75
76 return image;
77 };
78
79 export const addImageToBlock = (
80 selectedBlock,
81 image,
82 updateBlockAttributes,
83 ) => {
84 if (selectedBlock.name === 'core/image') {
85 updateBlockAttributes(selectedBlock.clientId, {
86 id: image.id,
87 caption: image.caption.raw,
88 url: image.source_url,
89 alt: image.alt_text,
90 });
91 }
92
93 if (selectedBlock.name === 'core/media-text') {
94 updateBlockAttributes(selectedBlock.clientId, {
95 mediaId: image.id,
96 caption: image.caption.raw,
97 mediaUrl: image.source_url,
98 mediaAlt: image.alt_text,
99 mediaType: 'image',
100 });
101 }
102
103 if (selectedBlock.name === 'core/gallery') {
104 const newBlock = createBlock('core/image', {
105 id: image.id,
106 caption: image.caption.raw,
107 url: image.source_url,
108 alt: image.alt_text,
109 });
110
111 insertBlock(newBlock, null, selectedBlock.clientId);
112 }
113
114 if (selectedBlock.name === 'core/cover') {
115 updateBlockAttributes(selectedBlock.clientId, {
116 id: image.id,
117 url: image.source_url,
118 alt: image.alt_text,
119 backgroundType: 'image',
120 dimRatio: 50,
121 hasParallax: false,
122 isDark: true,
123 isRepeated: false,
124 layout: {
125 type: 'constrained',
126 },
127 tagName: 'div',
128 useFeaturedImage: false,
129 });
130 }
131 };
132