Skip to content
This repository was archived by the owner on Oct 28, 2023. It is now read-only.

Commit 6c4097c

Browse files
committed
Split up files
1 parent 26ea3af commit 6c4097c

File tree

4 files changed

+100
-108
lines changed

4 files changed

+100
-108
lines changed

src/clipboard.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import ClipboardJS from 'clipboard'
2+
import { Config } from './module'
3+
import { Actions } from './types'
4+
5+
export const toClipboard = (
6+
text: string,
7+
action: Actions = 'copy',
8+
): Promise<unknown> => {
9+
return new Promise((resolve, reject) => {
10+
const fakeElement = document.createElement('button')
11+
const clipboard = new ClipboardJS(fakeElement, {
12+
text: () => text,
13+
action: () => action,
14+
})
15+
16+
clipboard.on('success', (e) => {
17+
clipboard.destroy()
18+
resolve(e)
19+
})
20+
21+
clipboard.on('error', (e) => {
22+
clipboard.destroy()
23+
reject(e)
24+
})
25+
26+
if (Config.appendToBody) {
27+
document.body.appendChild(fakeElement)
28+
}
29+
30+
fakeElement.click()
31+
if (Config.appendToBody) {
32+
document.body.removeChild(fakeElement)
33+
}
34+
})
35+
}

src/index.ts

Lines changed: 2 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,2 @@
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'

src/module.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { App } from 'vue'
2+
import ClipboardJS from 'clipboard'
3+
4+
const Config = {
5+
autoSetContainer: false,
6+
appendToBody: true,
7+
}
8+
9+
const install = (app: App): void => {
10+
app.directive('clipboard', {
11+
beforeMount(el, binding) {
12+
if (binding.arg === 'success') {
13+
el._vClipboard_success = binding.value
14+
} else if (binding.arg === 'error') {
15+
el._vClipboard_error = binding.value
16+
} else {
17+
const clipboard = new ClipboardJS(el, {
18+
text: () => binding.value,
19+
action: () => {
20+
return binding.arg === 'cut' ? 'cut' : 'copy'
21+
},
22+
container: Config.autoSetContainer ? el : undefined,
23+
})
24+
clipboard.on('success', (e) => {
25+
const callback = el._vClipboard_success
26+
callback && callback(e)
27+
})
28+
clipboard.on('error', (e) => {
29+
const callback = el._vClipboard_error
30+
callback && callback(e)
31+
})
32+
el._vClipboard = clipboard
33+
}
34+
},
35+
updated(el, binding) {
36+
if (binding.arg === 'success') {
37+
el._vClipboard_success = binding.value
38+
} else if (binding.arg === 'error') {
39+
el._vClipboard_error = binding.value
40+
} else {
41+
el._vClipboard.text = () => {
42+
return binding.value
43+
}
44+
el._vClipboard.action = () => {
45+
return binding.arg === 'cut' ? 'cut' : 'copy'
46+
}
47+
}
48+
},
49+
unmounted(el, binding) {
50+
if (binding.arg === 'success') {
51+
delete el._vClipboard_success
52+
} else if (binding.arg === 'error') {
53+
delete el._vClipboard_error
54+
} else {
55+
el._vClipboard.destroy()
56+
delete el._vClipboard
57+
}
58+
},
59+
})
60+
}
61+
62+
export { install, Config }

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type Actions = 'copy' | 'cut'

0 commit comments

Comments
 (0)