mirror of
https://github.com/zenorocha/clipboard.js.git
synced 2023-08-10 21:12:48 +03:00
support custom trigger event
This commit is contained in:
17
dist/clipboard.js
vendored
17
dist/clipboard.js
vendored
@@ -621,9 +621,9 @@ var Clipboard = (function (_Emitter) {
|
|||||||
_classCallCheck(this, Clipboard);
|
_classCallCheck(this, Clipboard);
|
||||||
|
|
||||||
_Emitter.call(this);
|
_Emitter.call(this);
|
||||||
|
this.defaultTriggerEvent = 'click';
|
||||||
this.resolveOptions(options);
|
this.resolveOptions(options);
|
||||||
this.listenClick(trigger);
|
this.listenEvent(trigger);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -644,27 +644,28 @@ var Clipboard = (function (_Emitter) {
|
|||||||
this.action = typeof options.action === 'function' ? options.action : this.defaultAction;
|
this.action = typeof options.action === 'function' ? options.action : this.defaultAction;
|
||||||
this.target = typeof options.target === 'function' ? options.target : this.defaultTarget;
|
this.target = typeof options.target === 'function' ? options.target : this.defaultTarget;
|
||||||
this.text = typeof options.text === 'function' ? options.text : this.defaultText;
|
this.text = typeof options.text === 'function' ? options.text : this.defaultText;
|
||||||
|
this.triggerEvent = typeof options.triggerEvent === 'string' ? options.triggerEvent : this.defaultTriggerEvent;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a click event listener to the passed trigger.
|
* Adds a event listener to the passed trigger.
|
||||||
* @param {String|HTMLElement|HTMLCollection|NodeList} trigger
|
* @param {String|HTMLElement|HTMLCollection|NodeList} trigger
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Clipboard.prototype.listenClick = function listenClick(trigger) {
|
Clipboard.prototype.listenEvent = function listenEvent(trigger) {
|
||||||
var _this = this;
|
var _this = this;
|
||||||
|
|
||||||
this.listener = _goodListener2['default'](trigger, 'click', function (e) {
|
this.listener = _goodListener2['default'](trigger, this.triggerEvent, function (e) {
|
||||||
return _this.onClick(e);
|
return _this.handleEvent(e);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines a new `ClipboardAction` on each click event.
|
* Defines a new `ClipboardAction` on each trigger event.
|
||||||
* @param {Event} e
|
* @param {Event} e
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Clipboard.prototype.onClick = function onClick(e) {
|
Clipboard.prototype.handleEvent = function handleEvent(e) {
|
||||||
if (this.clipboardAction) {
|
if (this.clipboardAction) {
|
||||||
this.clipboardAction = null;
|
this.clipboardAction = null;
|
||||||
}
|
}
|
||||||
|
|||||||
2
dist/clipboard.min.js
vendored
2
dist/clipboard.min.js
vendored
File diff suppressed because one or more lines are too long
14
readme.md
14
readme.md
@@ -102,6 +102,20 @@ Truth is, you don't even need another element to copy its content from. You can
|
|||||||
</button>
|
</button>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Use custom event trigger
|
||||||
|
|
||||||
|
you can listen other event, the default event is 'click'.
|
||||||
|
|
||||||
|
```html
|
||||||
|
<label class="triggerLabel" data-clipboard-text="Just because you can doesn't mean you should — clipboard.js">
|
||||||
|
Double click, then copy to clipboard
|
||||||
|
</label>
|
||||||
|
```
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
var clipboard = new Clipboard('.triggerLabel', {'triggerEvent' : 'dblclick'});
|
||||||
|
```
|
||||||
|
|
||||||
## Events
|
## Events
|
||||||
|
|
||||||
There are cases where you'd like to show some user feedback or capture what has been selected after a copy/cut operation.
|
There are cases where you'd like to show some user feedback or capture what has been selected after a copy/cut operation.
|
||||||
|
|||||||
@@ -13,9 +13,9 @@ class Clipboard extends Emitter {
|
|||||||
*/
|
*/
|
||||||
constructor(trigger, options) {
|
constructor(trigger, options) {
|
||||||
super();
|
super();
|
||||||
|
this.defaultTriggerEvent = 'click';
|
||||||
this.resolveOptions(options);
|
this.resolveOptions(options);
|
||||||
this.listenClick(trigger);
|
this.listenEvent(trigger);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -27,21 +27,22 @@ class Clipboard extends Emitter {
|
|||||||
this.action = (typeof options.action === 'function') ? options.action : this.defaultAction;
|
this.action = (typeof options.action === 'function') ? options.action : this.defaultAction;
|
||||||
this.target = (typeof options.target === 'function') ? options.target : this.defaultTarget;
|
this.target = (typeof options.target === 'function') ? options.target : this.defaultTarget;
|
||||||
this.text = (typeof options.text === 'function') ? options.text : this.defaultText;
|
this.text = (typeof options.text === 'function') ? options.text : this.defaultText;
|
||||||
|
this.triggerEvent = (typeof options.triggerEvent === 'string') ? options.triggerEvent : this.defaultTriggerEvent;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a click event listener to the passed trigger.
|
* Adds a event listener to the passed trigger.
|
||||||
* @param {String|HTMLElement|HTMLCollection|NodeList} trigger
|
* @param {String|HTMLElement|HTMLCollection|NodeList} trigger
|
||||||
*/
|
*/
|
||||||
listenClick(trigger) {
|
listenEvent(trigger) {
|
||||||
this.listener = listen(trigger, 'click', (e) => this.onClick(e));
|
this.listener = listen(trigger, this.triggerEvent, (e) => this.handleEvent(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines a new `ClipboardAction` on each click event.
|
* Defines a new `ClipboardAction` on each trigger event.
|
||||||
* @param {Event} e
|
* @param {Event} e
|
||||||
*/
|
*/
|
||||||
onClick(e) {
|
handleEvent(e) {
|
||||||
if (this.clipboardAction) {
|
if (this.clipboardAction) {
|
||||||
this.clipboardAction = null;
|
this.clipboardAction = null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user