-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #283 from Automattic/update/sketch-upload-to-media…
…-gallery Sketch: upload to media gallery
- Loading branch information
Showing
5 changed files
with
147 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
export default function svgDomToBlob( svgNode, fn ) { | ||
if ( ! svgNode ) { | ||
return; | ||
} | ||
|
||
// Get SVG Blob. | ||
const svgString = new XMLSerializer().serializeToString( svgNode ); | ||
const DOMURL = self.URL || self.webkitURL || self; | ||
const svgBlob = new Blob( [ svgString ], { type: "image/svg+xml;charset=utf-8" } ); | ||
|
||
// Start to convert SVG string to base64. | ||
const canvas = document.createElement( 'canvas' ); | ||
const ctx = canvas.getContext( "2d" ); | ||
ctx.canvas.width = svgNode.width.baseVal.value; | ||
ctx.canvas.height = svgNode.height.baseVal.value; | ||
|
||
// Create an image based on the SVG string. | ||
const img = new Image(); | ||
const url = DOMURL.createObjectURL( svgBlob ); | ||
img.onload = function() { | ||
// Draw the image on the canvas. | ||
ctx.drawImage( img, 0, 0 ); | ||
DOMURL.revokeObjectURL( canvas.toDataURL( 'image/png' ) ); | ||
canvas.toBlob( fn ); | ||
}; | ||
img.src = url; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
import { select } from '@wordpress/data'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
export default function uploadBlobToMediaLibrary( | ||
imageBlob, | ||
{ | ||
title = __( 'Image generated via Sketch block', 'a8c-sketch' ), | ||
caption = '', | ||
description = '', | ||
}, | ||
fn | ||
) { | ||
const { getSettings } = select( blockEditorStore ); | ||
const mediaUpload = getSettings().mediaUpload; | ||
|
||
if ( ! imageBlob ) { | ||
return fn( __( 'No valid image', 'a8c-sketch' ) ); | ||
} | ||
|
||
const reader = new window.FileReader(); | ||
reader.readAsDataURL( imageBlob ); | ||
reader.onloadend = () => { | ||
mediaUpload( { | ||
additionalData: { | ||
title, | ||
caption, | ||
description, | ||
}, | ||
allowedTypes: [ 'image' ], | ||
filesList: [ imageBlob ], | ||
onFileChange: ( images ) => { | ||
if ( ! images?.length ) { | ||
return; | ||
} | ||
|
||
const image = images[ 0 ]; | ||
if ( ! image?.id ) { | ||
return; | ||
} | ||
fn( null, image ); | ||
}, | ||
onError: fn, | ||
} ); | ||
}; | ||
} |