@@ -54,43 +54,50 @@ export function createClipboardItem(
54
54
}
55
55
56
56
const ClipboardStubControl = Symbol ( 'Manage ClipboardSub' )
57
+ type ClipboardStubControlInstance = {
58
+ resetClipboardStub : ( ) => void
59
+ detachClipboardStub : ( ) => void
60
+ }
57
61
58
- function createClipboardStub ( window : Window & typeof globalThis ) {
59
- return new ( class Clipboard extends window . EventTarget {
60
- private items : ClipboardItem [ ] = [ ]
62
+ function createClipboardStub (
63
+ window : Window & typeof globalThis ,
64
+ control : ClipboardStubControlInstance ,
65
+ ) {
66
+ return Object . assign (
67
+ new ( class Clipboard extends window . EventTarget {
68
+ private items : ClipboardItem [ ] = [ ]
61
69
62
- async read ( ) {
63
- return Array . from ( this . items )
64
- }
70
+ async read ( ) {
71
+ return Array . from ( this . items )
72
+ }
65
73
66
- async readText ( ) {
67
- let text = ''
68
- for ( const item of this . items ) {
69
- const type = item . types . includes ( 'text/plain' )
70
- ? 'text/plain'
71
- : item . types . find ( t => t . startsWith ( 'text/' ) )
72
- if ( type ) {
73
- text += await item
74
- . getType ( type )
75
- . then ( b => readBlobText ( b , window . FileReader ) )
74
+ async readText ( ) {
75
+ let text = ''
76
+ for ( const item of this . items ) {
77
+ const type = item . types . includes ( 'text/plain' )
78
+ ? 'text/plain'
79
+ : item . types . find ( t => t . startsWith ( 'text/' ) )
80
+ if ( type ) {
81
+ text += await item
82
+ . getType ( type )
83
+ . then ( b => readBlobText ( b , window . FileReader ) )
84
+ }
76
85
}
86
+ return text
77
87
}
78
- return text
79
- }
80
-
81
- async write ( data : ClipboardItem [ ] ) {
82
- this . items = data
83
- }
84
88
85
- async writeText ( text : string ) {
86
- this . items = [ createClipboardItem ( window , text ) ]
87
- }
89
+ async write ( data : ClipboardItem [ ] ) {
90
+ this . items = data
91
+ }
88
92
89
- [ ClipboardStubControl ] : {
90
- resetClipboardStub : ( ) => void
91
- detachClipboardStub : ( ) => void
92
- }
93
- } ) ( )
93
+ async writeText ( text : string ) {
94
+ this . items = [ createClipboardItem ( window , text ) ]
95
+ }
96
+ } ) ( ) ,
97
+ {
98
+ [ ClipboardStubControl ] : control ,
99
+ } ,
100
+ )
94
101
}
95
102
type ClipboardStub = ReturnType < typeof createClipboardStub >
96
103
@@ -112,11 +119,10 @@ export function attachClipboardStubToView(window: Window & typeof globalThis) {
112
119
'clipboard' ,
113
120
)
114
121
115
- let stub = createClipboardStub ( window )
116
- const control = {
122
+ let stub : ClipboardStub
123
+ const control : ClipboardStubControlInstance = {
117
124
resetClipboardStub : ( ) => {
118
- stub = createClipboardStub ( window )
119
- stub [ ClipboardStubControl ] = control
125
+ stub = createClipboardStub ( window , control )
120
126
} ,
121
127
detachClipboardStub : ( ) => {
122
128
/* istanbul ignore if */
@@ -130,7 +136,7 @@ export function attachClipboardStubToView(window: Window & typeof globalThis) {
130
136
}
131
137
} ,
132
138
}
133
- stub [ ClipboardStubControl ] = control
139
+ stub = createClipboardStub ( window , control )
134
140
135
141
Object . defineProperty ( window . navigator , 'clipboard' , {
136
142
get : ( ) => stub ,
0 commit comments