|
1 |
| -import { App } from 'vue' |
2 |
| -import ClipboardJS from 'clipboard' |
3 |
| - |
4 |
| -enum Events { |
5 |
| - SUCCESS = 'success', |
6 |
| - ERROR = 'error', |
7 |
| -} |
8 |
| - |
9 |
| -export enum ClipboardActions { |
10 |
| - COPY = 'copy', |
11 |
| - CUT = 'cut', // CUT only works on inputs and textareas |
12 |
| -} |
13 |
| - |
14 |
| -const Config = { |
15 |
| - autoSetContainer: false, |
16 |
| - appendToBody: true, |
17 |
| -} |
18 |
| - |
19 |
| -export const toClipboard = ( |
20 |
| - text: string, |
21 |
| - action = ClipboardActions.COPY, |
22 |
| -): Promise<unknown> => { |
23 |
| - return new Promise((resolve, reject) => { |
24 |
| - const fakeElement = document.createElement('button') |
25 |
| - const clipboard = new ClipboardJS(fakeElement, { |
26 |
| - text: () => text, |
27 |
| - action: () => action, |
28 |
| - }) |
29 |
| - |
30 |
| - clipboard.on(Events.SUCCESS, (e) => { |
31 |
| - clipboard.destroy() |
32 |
| - resolve(e) |
33 |
| - }) |
34 |
| - |
35 |
| - clipboard.on(Events.ERROR, (e) => { |
36 |
| - clipboard.destroy() |
37 |
| - reject(e) |
38 |
| - }) |
39 |
| - |
40 |
| - if (Config.appendToBody) { |
41 |
| - document.body.appendChild(fakeElement) |
42 |
| - } |
43 |
| - |
44 |
| - fakeElement.click() |
45 |
| - if (Config.appendToBody) { |
46 |
| - document.body.removeChild(fakeElement) |
47 |
| - } |
48 |
| - }) |
49 |
| -} |
50 |
| - |
51 |
| -export const install = (app: App): void => { |
52 |
| - app.directive('clipboard', { |
53 |
| - beforeMount(el, binding) { |
54 |
| - if (binding.arg === Events.SUCCESS) { |
55 |
| - el._vClipboard_success = binding.value |
56 |
| - } else if (binding.arg === Events.ERROR) { |
57 |
| - el._vClipboard_error = binding.value |
58 |
| - } else { |
59 |
| - const clipboard = new ClipboardJS(el, { |
60 |
| - text: () => binding.value, |
61 |
| - action: () => { |
62 |
| - return binding.arg === ClipboardActions.CUT |
63 |
| - ? ClipboardActions.CUT |
64 |
| - : ClipboardActions.COPY |
65 |
| - }, |
66 |
| - container: Config.autoSetContainer ? el : undefined, |
67 |
| - }) |
68 |
| - clipboard.on(Events.SUCCESS, (e) => { |
69 |
| - const callback = el._vClipboard_success |
70 |
| - callback && callback(e) |
71 |
| - }) |
72 |
| - clipboard.on(Events.ERROR, (e) => { |
73 |
| - const callback = el._vClipboard_error |
74 |
| - callback && callback(e) |
75 |
| - }) |
76 |
| - el._vClipboard = clipboard |
77 |
| - } |
78 |
| - }, |
79 |
| - updated(el, binding) { |
80 |
| - if (binding.arg === Events.SUCCESS) { |
81 |
| - el._vClipboard_success = binding.value |
82 |
| - } else if (binding.arg === Events.ERROR) { |
83 |
| - el._vClipboard_error = binding.value |
84 |
| - } else { |
85 |
| - el._vClipboard.text = () => { |
86 |
| - return binding.value |
87 |
| - } |
88 |
| - el._vClipboard.action = () => { |
89 |
| - return binding.arg === ClipboardActions.CUT |
90 |
| - ? ClipboardActions.CUT |
91 |
| - : ClipboardActions.COPY |
92 |
| - } |
93 |
| - } |
94 |
| - }, |
95 |
| - unmounted(el, binding) { |
96 |
| - if (binding.arg === Events.SUCCESS) { |
97 |
| - delete el._vClipboard_success |
98 |
| - } else if (binding.arg === Events.ERROR) { |
99 |
| - delete el._vClipboard_error |
100 |
| - } else { |
101 |
| - el._vClipboard.destroy() |
102 |
| - delete el._vClipboard |
103 |
| - } |
104 |
| - }, |
105 |
| - }) |
106 |
| -} |
107 |
| - |
108 |
| -export default install |
| 1 | +export * from './clipboard' |
| 2 | +export { install as VueClipboard, Config } from './module' |
0 commit comments