From cdd8870de5eaea0f13c6dc189a6fa62cb5bd64af Mon Sep 17 00:00:00 2001 From: Bolarinwa Balogun Date: Wed, 17 Jul 2019 11:05:21 -0400 Subject: [PATCH] Add copy button to markdown image upload (#2957) --- .../stylesheets/preact/article-form.scss | 25 +++++++ .../article-form/elements/imageManagement.jsx | 73 +++++++++++++++++-- app/views/articles/edit.html.erb | 2 + app/views/articles/new.html.erb | 2 + 4 files changed, 95 insertions(+), 7 deletions(-) diff --git a/app/assets/stylesheets/preact/article-form.scss b/app/assets/stylesheets/preact/article-form.scss index 39f00ca02..2084c10a4 100644 --- a/app/assets/stylesheets/preact/article-form.scss +++ b/app/assets/stylesheets/preact/article-form.scss @@ -542,6 +542,31 @@ text-align: center; border-radius: 3px; overflow-y: scroll; + clipboard-copy { + cursor: pointer; + img { + display: inline-block; + height: initial; + max-width: 24px; + margin: -10px 5px; + position: relative; + } + input { + cursor: pointer; + } + + #image-markdown-copy-link-input { + margin-left: 24px; + } + #image-markdown-copy-link-announcer { + display: block; + padding-top: 10px; + + &[hidden] { + display: none; + } + } + } h2 { color: $green; font-size: 1.9em; diff --git a/app/javascript/article-form/elements/imageManagement.jsx b/app/javascript/article-form/elements/imageManagement.jsx index 1b7320f6c..dc2c76e96 100644 --- a/app/javascript/article-form/elements/imageManagement.jsx +++ b/app/javascript/article-form/elements/imageManagement.jsx @@ -1,5 +1,6 @@ import { h, Component } from 'preact'; import PropTypes from 'prop-types'; +import linkCopyIcon from '../../../assets/images/content-copy.svg'; import { generateMainImage } from '../actions'; export default class ImageManagement extends Component { @@ -65,6 +66,35 @@ export default class ImageManagement extends Component { }); }; + copyText = () => { + this.imageMarkdownAnnouncer = document.getElementById( + 'image-markdown-copy-link-announcer', + ); + this.imageMarkdownInput = document.getElementById( + 'image-markdown-copy-link-input', + ); + + const isIOSDevice = + navigator.userAgent.match(/iPhone|iPad/i) || + navigator.userAgent.match('CriOS') || + navigator.userAgent === 'DEV-Native-ios'; + + if (isIOSDevice) { + this.imageMarkdownInput.setSelectionRange( + 0, + this.imageMarkdownInput.value.length, + ); + document.execCommand('copy'); + } else { + this.imageMarkdownInput.focus(); + this.imageMarkdownInput.setSelectionRange( + 0, + this.imageMarkdownInput.value.length, + ); + } + this.imageMarkdownAnnouncer.hidden = false; + }; + render() { const { onExit, mainImage, version } = this.props; const { insertionImageUrl, uploadError, uploadErrorMessage } = this.state; @@ -92,9 +122,32 @@ export default class ImageManagement extends Component { insertionImageArea = (

Markdown Image:

- + + + Copy to Clipboard + +

Direct URL:

- +
); } else { @@ -111,10 +164,16 @@ export default class ImageManagement extends Component {

Upload an Image

{insertionImageArea}
-

To add a cover image for the post, add cover_image: direct_url_to_image.jpg to the frontmatter

+

+ + To add a cover image for the post, add + cover_image: direct_url_to_image.jpg + to the frontmatter + +

- ) + ); } else { imageOptions = (
@@ -123,7 +182,7 @@ export default class ImageManagement extends Component {

Body Images

{insertionImageArea}
- ) + ); } return (
@@ -157,5 +216,5 @@ ImageManagement.propTypes = { onExit: PropTypes.func.isRequired, onMainImageUrlChange: PropTypes.func.isRequired, mainImage: PropTypes.string.isRequired, - version: PropTypes.string.isRequired -}; \ No newline at end of file + version: PropTypes.string.isRequired, +}; diff --git a/app/views/articles/edit.html.erb b/app/views/articles/edit.html.erb index bbef9eec5..b5d8d2f15 100644 --- a/app/views/articles/edit.html.erb +++ b/app/views/articles/edit.html.erb @@ -27,5 +27,7 @@
<% end %> +<%= javascript_include_tag "https://unpkg.com/@webcomponents/webcomponentsjs@2.2.10/webcomponents-loader.js", defer: true %> +<%= javascript_pack_tag "clipboardCopy", defer: true %> <%= javascript_pack_tag "articleForm", defer: true %> <%= render "articles/v2_form", article: @article, organizations: @organizations, version: @version %> diff --git a/app/views/articles/new.html.erb b/app/views/articles/new.html.erb index 07abb0d64..b01df17eb 100644 --- a/app/views/articles/new.html.erb +++ b/app/views/articles/new.html.erb @@ -1,6 +1,8 @@ <% title "New Post" %> <% if user_signed_in? %> + <%= javascript_include_tag "https://unpkg.com/@webcomponents/webcomponentsjs@2.2.10/webcomponents-loader.js", defer: true %> + <%= javascript_pack_tag "clipboardCopy", defer: true %> <%= javascript_pack_tag "articleForm", defer: true %> <%= render "articles/v2_form", article: @article, organizations: @organizations, version: @version %> <% else %>