Skip to content

Commit

Permalink
Fix Event API. Update demos. Update tests (#793)
Browse files Browse the repository at this point in the history
  • Loading branch information
obetomuniz authored Feb 2, 2022
1 parent 9698b11 commit 7d675f5
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 26 deletions.
8 changes: 6 additions & 2 deletions demo/constructor-node.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
var clipboard = new ClipboardJS(btn);

clipboard.on('success', function (e) {
console.log(e);
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
});

clipboard.on('error', function (e) {
console.log(e);
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
});
</script>
</body>
Expand Down
8 changes: 6 additions & 2 deletions demo/constructor-nodelist.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
var clipboard = new ClipboardJS(btns);

clipboard.on('success', function (e) {
console.log(e);
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
});

clipboard.on('error', function (e) {
console.log(e);
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
});
</script>
</body>
Expand Down
8 changes: 6 additions & 2 deletions demo/constructor-selector.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
var clipboard = new ClipboardJS('.btn');

clipboard.on('success', function (e) {
console.log(e);
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
});

clipboard.on('error', function (e) {
console.log(e);
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
});
</script>
</body>
Expand Down
8 changes: 6 additions & 2 deletions demo/function-target.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
});

clipboard.on('success', function (e) {
console.log(e);
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
});

clipboard.on('error', function (e) {
console.log(e);
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
});
</script>
</body>
Expand Down
8 changes: 6 additions & 2 deletions demo/function-text.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@
});

clipboard.on('success', function (e) {
console.log(e);
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
});

clipboard.on('error', function (e) {
console.log(e);
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
});
</script>
</body>
Expand Down
8 changes: 6 additions & 2 deletions demo/target-div.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@
var clipboard = new ClipboardJS('.btn');

clipboard.on('success', function (e) {
console.log(e);
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
});

clipboard.on('error', function (e) {
console.log(e);
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
});
</script>
</body>
Expand Down
4 changes: 3 additions & 1 deletion demo/target-input.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
var clipboard = new ClipboardJS('.btn');

clipboard.on('success', function (e) {
console.log(e);
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
});

clipboard.on('error', function (e) {
Expand Down
8 changes: 6 additions & 2 deletions demo/target-textarea.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@
var clipboard = new ClipboardJS('.btn');

clipboard.on('success', function (e) {
console.log(e);
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
});

clipboard.on('error', function (e) {
console.log(e);
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
});
</script>
</body>
Expand Down
11 changes: 6 additions & 5 deletions dist/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,16 +287,17 @@ var Clipboard = /*#__PURE__*/function (_Emitter) {
key: "onClick",
value: function onClick(e) {
var trigger = e.delegateTarget || e.currentTarget;
var selectedText = actions_default({
action: this.action(trigger),
var action = this.action(trigger) || 'copy';
var text = actions_default({
action: action,
container: this.container,
target: this.target(trigger),
text: this.text(trigger)
}); // Fires an event based on the copy operation result.

this.emit(selectedText ? 'success' : 'error', {
action: this.action,
text: selectedText,
this.emit(text ? 'success' : 'error', {
action: action,
text: text,
trigger: trigger,
clearSelection: function clearSelection() {
if (trigger) {
Expand Down
2 changes: 1 addition & 1 deletion dist/clipboard.min.js

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions src/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,18 @@ class Clipboard extends Emitter {
*/
onClick(e) {
const trigger = e.delegateTarget || e.currentTarget;
const selectedText = ClipboardActionDefault({
action: this.action(trigger),
const action = this.action(trigger) || 'copy';
const text = ClipboardActionDefault({
action,
container: this.container,
target: this.target(trigger),
text: this.text(trigger),
});

// Fires an event based on the copy operation result.
this.emit(selectedText ? 'success' : 'error', {
action: this.action,
text: selectedText,
this.emit(text ? 'success' : 'error', {
action,
text,
trigger,
clearSelection() {
if (trigger) {
Expand Down
1 change: 1 addition & 0 deletions test/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ describe('Clipboard', () => {

clipboard.on('success', (e) => {
assert.property(e, 'action');
assert.equal(e.action, 'copy');
assert.property(e, 'text');
assert.property(e, 'trigger');
assert.property(e, 'clearSelection');
Expand Down

0 comments on commit 7d675f5

Please sign in to comment.