From ecd8e631b7f2ee8ea8d5fee6a9634c844b929669 Mon Sep 17 00:00:00 2001 From: Amit Keinan Date: Sun, 7 Feb 2021 02:45:43 +0200 Subject: [PATCH] Add export to imgur (#1158) * add button and functionality * Create a share menu and for twitter and imgur * follow 'tweet' patterns * remove unused carbonRef prop * remove unused exportImgur * share menu * remove Share SVG * combine tweet and imgur button components * delete unused tweet and imgur button components * remove options.filename * onClickImgur * change to async await to fix loading state * NEXT_PUBLIC_IMGUR_CLIENT_ID * return promise to fix loading state (remove async/await) Co-authored-by: Mike Fix Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com> --- components/Editor.js | 12 ++++-- components/ShareMenu.js | 83 +++++++++++++++++++++++++++++++++++++++ components/TweetButton.js | 37 ----------------- lib/api.js | 22 +++++++++++ 4 files changed, 114 insertions(+), 40 deletions(-) create mode 100644 components/ShareMenu.js delete mode 100644 components/TweetButton.js diff --git a/components/Editor.js b/components/Editor.js index 346dc69..640910f 100644 --- a/components/Editor.js +++ b/components/Editor.js @@ -13,9 +13,9 @@ import Overlay from './Overlay' import BackgroundSelect from './BackgroundSelect' import Carbon from './Carbon' import ExportMenu from './ExportMenu' +import ShareMenu from './ShareMenu' import CopyMenu from './CopyMenu' import Themes from './Themes' -import TweetButton from './TweetButton' import FontFace from './FontFace' import LanguageIcon from './svg/Language' import { @@ -180,7 +180,7 @@ class Editor extends React.Component { return domtoimage.toBlob(node, config) } - // Twitter needs regular dataURLs + // Twitter and Imgur needs regular dataURLs return domtoimage.toPng(node, config) } @@ -190,6 +190,12 @@ class Editor extends React.Component { ) } + imgur = () => { + const prefix = this.state.name || 'carbon' + + return this.getCarbonImage({ format: 'png' }).then(data => this.context.imgur(data, prefix)) + } + exportImage = (format = 'png', options = {}) => { const link = document.createElement('a') @@ -378,7 +384,7 @@ class Editor extends React.Component {
- + +
+ + +
+ + +
+ ) +} + +export default managePopout(React.memo(ShareMenu)) diff --git a/components/TweetButton.js b/components/TweetButton.js deleted file mode 100644 index bd4d2db..0000000 --- a/components/TweetButton.js +++ /dev/null @@ -1,37 +0,0 @@ -import React from 'react' -import { useAsyncCallback, useOnline as useOnlineListener } from 'actionsack' - -import { useAPI } from './ApiContext' -import Button from './Button' - -import { COLORS } from '../lib/constants' - -function TweetButton(props) { - const api = useAPI() - const online = useOnlineListener() - const [onClick, { loading }] = useAsyncCallback(props.onClick) - - if (!api || !api.tweet) { - return null - } - - if (!online) { - return null - } - - return ( - - ) -} - -export default React.memo(TweetButton) diff --git a/lib/api.js b/lib/api.js index 75878de..3b991cf 100644 --- a/lib/api.js +++ b/lib/api.js @@ -73,6 +73,27 @@ const unsplash = { }, } +const imgur = (data, title) => { + const image = data.split(',')[1] + + return axios + .post( + 'https://api.imgur.com/3/image', + { image, title }, + { + headers: { + Authorization: `Client-ID ${process.env.NEXT_PUBLIC_IMGUR_CLIENT_ID}`, + }, + } + ) + .then(res => res.data.data.link) + .then(link => window.open(link, '_blank')) + .catch(e => { + console.error(e) + return null + }) +} + function getSnippet(uid = '', { host, filename } = {}) { return client .get(`/snippets/${uid}`, { @@ -165,5 +186,6 @@ export default { tweet: debounce(tweet, ms('5s'), { leading: true, trailing: false }), image: debounce(image, ms('5s'), { leading: true, trailing: false }), unsplash, + imgur, downloadThumbnailImage, }