2021-07-06 15:56:36 +00:00
|
|
|
import Modifier from 'ember-modifier';
|
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import { runInDebug } from '@ember/debug';
|
|
|
|
|
|
|
|
const typeAssertion = (type, value, withDefault) => {
|
|
|
|
return typeof value === type ? value : withDefault;
|
|
|
|
};
|
|
|
|
export default class WithCopyableModifier extends Modifier {
|
|
|
|
@service('clipboard/os') clipboard;
|
|
|
|
|
|
|
|
hash = null;
|
|
|
|
source = null;
|
|
|
|
|
|
|
|
connect([value], _hash) {
|
|
|
|
value = typeAssertion('string', value, this.element.innerText);
|
|
|
|
const hash = {
|
2022-09-15 08:43:17 +00:00
|
|
|
success: (e) => {
|
|
|
|
runInDebug((_) => console.info(`with-copyable: Copied \`${value}\``));
|
2021-07-06 15:56:36 +00:00
|
|
|
return typeAssertion('function', _hash.success, () => {})(e);
|
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
error: (e) => {
|
|
|
|
runInDebug((_) => console.info(`with-copyable: Error copying \`${value}\``));
|
2021-07-06 15:56:36 +00:00
|
|
|
return typeAssertion('function', _hash.error, () => {})(e);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
this.source = this.clipboard
|
|
|
|
.execute(this.element, {
|
2022-09-15 08:43:17 +00:00
|
|
|
text: (_) => value,
|
2021-07-06 15:56:36 +00:00
|
|
|
...hash.options,
|
|
|
|
})
|
|
|
|
.on('success', hash.success)
|
|
|
|
.on('error', hash.error);
|
|
|
|
this.hash = hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
|
|
|
if (this.source && this.hash) {
|
|
|
|
this.source.off('success', this.hash.success).off('error', this.hash.error);
|
|
|
|
|
|
|
|
this.source.destroy();
|
|
|
|
this.hash = null;
|
|
|
|
this.source = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// lifecycle hooks
|
|
|
|
didReceiveArguments() {
|
|
|
|
this.disconnect();
|
|
|
|
this.connect(this.args.positional, this.args.named);
|
|
|
|
}
|
|
|
|
|
|
|
|
willRemove() {
|
|
|
|
this.disconnect();
|
|
|
|
}
|
|
|
|
}
|