Skip to content

Instantly share code, notes, and snippets.

@robertknight
Created August 8, 2017 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertknight/10fbffb58aa967d87ba92d163d99f168 to your computer and use it in GitHub Desktop.
Save robertknight/10fbffb58aa967d87ba92d163d99f168 to your computer and use it in GitHub Desktop.
Add copy button to share dialog
diff --git a/src/sidebar/components/share-dialog.js b/src/sidebar/components/share-dialog.js
index 652d1b8d..534597e4 100644
--- a/src/sidebar/components/share-dialog.js
+++ b/src/sidebar/components/share-dialog.js
@@ -2,6 +2,21 @@
var VIA_PREFIX = 'https://via.hypothes.is/';
+function copyToClipboard(text) {
+ // Temporarily add a hidden input element and populate it with the text we
+ // want to copy.
+ var inputEl = document.createElement('textarea');
+ document.body.appendChild(inputEl);
+ inputEl.textContent = text;
+ inputEl.focus();
+ inputEl.selectionStart = 0;
+ inputEl.selectionEnd = text.length;
+
+ document.execCommand('copy');
+
+ inputEl.remove();
+}
+
// @ngInject
function ShareDialogController($scope, $element, analytics, annotationUI) {
var self = this;
@@ -32,6 +47,13 @@ function ShareDialogController($scope, $element, analytics, annotationUI) {
analytics.track(analytics.events.DOCUMENT_SHARED, target);
}
};
+
+ $scope.copyAnnotations = function () {
+ var annotations = annotationUI.getState().annotations;
+ var content = annotations.map(ann => ann.text).join('\n\n');
+
+ copyToClipboard(content);
+ };
}
module.exports = {
diff --git a/src/sidebar/templates/share-dialog.html b/src/sidebar/templates/share-dialog.html
index 1bf4438c..af497bd7 100644
--- a/src/sidebar/templates/share-dialog.html
+++ b/src/sidebar/templates/share-dialog.html
@@ -35,6 +35,7 @@
class="share-link-icon h-icon-mail"
ng-click="onShareClick('email')"></a>
</p>
+ <button ng-click="copyAnnotations()">Copy annotations</button>
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment