mirror of
https://github.com/discourse/discourse.git
synced 2025-02-23 04:33:43 +08:00
Merge pull request #3620 from riking/copy-fix
FIX: Do automatic copy on share in click handler
This commit is contained in:
commit
46c8d0d7e2
34
app/assets/javascripts/discourse/lib/copy-text.js.es6
Normal file
34
app/assets/javascripts/discourse/lib/copy-text.js.es6
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/**
|
||||||
|
* Copy text to the clipboard. Must be called from within a user gesture (Chrome).
|
||||||
|
*/
|
||||||
|
export default function(text, element) {
|
||||||
|
let supported = false;
|
||||||
|
try {
|
||||||
|
// Chrome: This only returns true within a user gesture.
|
||||||
|
// Chrome: queryCommandEnabled() only returns true if a selection is
|
||||||
|
// present, so we use queryCommandSupported() instead for the fail-fast.
|
||||||
|
if (document.queryCommandSupported('copy')) {
|
||||||
|
supported = true;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
if (!supported) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let newRange = document.createRange();
|
||||||
|
newRange.selectNode(element);
|
||||||
|
const selection = window.getSelection();
|
||||||
|
selection.removeAllRanges();
|
||||||
|
selection.addRange(newRange);
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (document.execCommand("copy")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
@ -19,3 +19,4 @@
|
|||||||
<a href {{action "close"}} aria-label='{{i18n 'share.close'}}' title='{{i18n 'share.close'}}'>{{fa-icon "times-circle"}}</a>
|
<a href {{action "close"}} aria-label='{{i18n 'share.close'}}' title='{{i18n 'share.close'}}'>{{fa-icon "times-circle"}}</a>
|
||||||
</div>
|
</div>
|
||||||
{{/if}}
|
{{/if}}
|
||||||
|
<span class="hidden" id="copy-target"></span>
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import copyText from 'discourse/lib/copy-text';
|
||||||
|
|
||||||
export default Discourse.View.extend({
|
export default Discourse.View.extend({
|
||||||
templateName: 'share',
|
templateName: 'share',
|
||||||
elementId: 'share-link',
|
elementId: 'share-link',
|
||||||
@ -18,34 +20,13 @@ export default Discourse.View.extend({
|
|||||||
return null;
|
return null;
|
||||||
}.property('controller.link'),
|
}.property('controller.link'),
|
||||||
|
|
||||||
copyLink($element) {
|
|
||||||
const element = $element[0];
|
|
||||||
try {
|
|
||||||
if (document.queryCommandSupported('copy')) {
|
|
||||||
let newRange = document.createRange();
|
|
||||||
newRange.selectNode(element);
|
|
||||||
const selection = window.getSelection();
|
|
||||||
selection.removeAllRanges();
|
|
||||||
selection.addRange(newRange);
|
|
||||||
|
|
||||||
if (document.execCommand("copy")) {
|
|
||||||
this.set('controller.copied', true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
// Ignore
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
linkChanged: function() {
|
linkChanged: function() {
|
||||||
const self = this;
|
const self = this;
|
||||||
this.set('controller.copied', false);
|
|
||||||
if (this.present('controller.link')) {
|
if (this.present('controller.link')) {
|
||||||
Em.run.next(function() {
|
Em.run.next(function() {
|
||||||
if (!self.capabilities.touch) {
|
if (!self.capabilities.touch) {
|
||||||
var $linkInput = $('#share-link input');
|
var $linkInput = $('#share-link input');
|
||||||
$linkInput.val(self.get('controller.link'));
|
$linkInput.val(self.get('controller.link'));
|
||||||
self.copyLink($linkInput);
|
|
||||||
|
|
||||||
// Wait for the fade-in transition to finish before selecting the link:
|
// Wait for the fade-in transition to finish before selecting the link:
|
||||||
window.setTimeout(function() {
|
window.setTimeout(function() {
|
||||||
@ -55,7 +36,6 @@ export default Discourse.View.extend({
|
|||||||
var $linkForTouch = $('#share-link .share-for-touch a');
|
var $linkForTouch = $('#share-link .share-for-touch a');
|
||||||
$linkForTouch.attr('href',self.get('controller.link'));
|
$linkForTouch.attr('href',self.get('controller.link'));
|
||||||
$linkForTouch.html(self.get('controller.link'));
|
$linkForTouch.html(self.get('controller.link'));
|
||||||
self.copyLink($linkForTouch);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -83,6 +63,7 @@ export default Discourse.View.extend({
|
|||||||
var $currentTarget = $(e.currentTarget),
|
var $currentTarget = $(e.currentTarget),
|
||||||
$currentTargetOffset = $currentTarget.offset(),
|
$currentTargetOffset = $currentTarget.offset(),
|
||||||
$shareLink = $('#share-link'),
|
$shareLink = $('#share-link'),
|
||||||
|
copyElement = document.getElementById('copy-target'),
|
||||||
url = $currentTarget.data('share-url'),
|
url = $currentTarget.data('share-url'),
|
||||||
postNumber = $currentTarget.data('post-number'),
|
postNumber = $currentTarget.data('post-number'),
|
||||||
date = $currentTarget.children().data('time');
|
date = $currentTarget.children().data('time');
|
||||||
@ -113,10 +94,18 @@ export default Discourse.View.extend({
|
|||||||
$shareLink.css({left: "" + x + "px"});
|
$shareLink.css({left: "" + x + "px"});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.set('controller.copied', false);
|
||||||
|
|
||||||
|
const copySuccess = copyText(url, copyElement);
|
||||||
|
|
||||||
self.set('controller.link', url);
|
self.set('controller.link', url);
|
||||||
self.set('controller.postNumber', postNumber);
|
self.set('controller.postNumber', postNumber);
|
||||||
self.set('controller.date', date);
|
self.set('controller.date', date);
|
||||||
|
|
||||||
|
Ember.run.later(null, function() {
|
||||||
|
self.set('controller.copied', copySuccess);
|
||||||
|
}, 50);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user