-
Notifications
You must be signed in to change notification settings - Fork 94
/
paste.coffee
267 lines (250 loc) · 9.96 KB
/
paste.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
###
paste.js is an interface to read data ( text / image ) from clipboard in different browsers. It also contains several hacks.
https://github.com/layerssss/paste.js
###
$ = window.jQuery
$.paste = (pasteContainer) ->
console?.log "DEPRECATED: This method is deprecated. Please use $.fn.pastableNonInputable() instead."
pm = Paste.mountNonInputable pasteContainer
pm._container
$.fn.pastableNonInputable = ->
for el in @
continue if el._pastable || $(el).is('textarea, input:text, [contenteditable]')
Paste.mountNonInputable el
el._pastable = true
@
$.fn.pastableTextarea = ->
for el in @
continue if el._pastable || $(el).is(':not(textarea, input:text)')
Paste.mountTextarea el
el._pastable = true
@
$.fn.pastableContenteditable = ->
for el in @
continue if el._pastable || $(el).is(':not([contenteditable])')
Paste.mountContenteditable el
el._pastable = true
@
dataURLtoBlob = (dataURL, sliceSize=512) ->
return null unless m = dataURL.match /^data\:([^\;]+)\;base64\,(.+)$/
[m, contentType, b64Data] = m
byteCharacters = atob(b64Data)
byteArrays = []
offset = 0
while offset < byteCharacters.length
slice = byteCharacters.slice(offset, offset + sliceSize)
byteNumbers = new Array(slice.length)
i = 0
while i < slice.length
byteNumbers[i] = slice.charCodeAt(i)
i++
byteArray = new Uint8Array(byteNumbers)
byteArrays.push byteArray
offset += sliceSize
new Blob byteArrays,
type: contentType
createHiddenEditable = ->
$(document.createElement 'div')
.attr 'contenteditable', true
.attr 'aria-hidden', true
.attr 'tabindex', -1
.css
width: 1
height: 1
position: 'fixed'
left: -100
overflow: 'hidden'
opacity: 1e-17
isFocusable = (element, hasTabindex) ->
# https://github.com/jquery/jquery-ui/blob/master/ui/focusable.js
#
# * Copyright jQuery Foundation and other contributors
# * Released under the MIT license.
# * http://jquery.org/license
#
map = undefined
mapName = undefined
img = undefined
focusableIfVisible = undefined
fieldset = undefined
nodeName = element.nodeName.toLowerCase()
if 'area' == nodeName
map = element.parentNode
mapName = map.name
if !element.href or !mapName or map.nodeName.toLowerCase() != 'map'
return false
img = $('img[usemap=\'#' + mapName + '\']')
return img.length > 0 and img.is(':visible')
if /^(input|select|textarea|button|object)$/.test(nodeName)
focusableIfVisible = !element.disabled
if focusableIfVisible
# Form controls within a disabled fieldset are disabled.
# However, controls within the fieldset's legend do not get disabled.
# Since controls generally aren't placed inside legends, we skip
# this portion of the check.
fieldset = $(element).closest('fieldset')[0]
if fieldset
focusableIfVisible = !fieldset.disabled
else if 'a' == nodeName
focusableIfVisible = element.href or hasTabindex
else
focusableIfVisible = hasTabindex
focusableIfVisible = focusableIfVisible or $(element).is('[contenteditable]')
focusableIfVisible and $(element).is(':visible')
class Paste
# Element to receive final events.
_target: null
# Actual element to do pasting.
_container: null
@mountNonInputable: (nonInputable)->
paste = new Paste createHiddenEditable().appendTo(nonInputable), nonInputable
$(nonInputable).on 'click', (ev)=>
paste._container.focus() unless isFocusable(ev.target, false) or window.getSelection().toString()
paste._container.on 'focus', => $(nonInputable).addClass 'pastable-focus'
paste._container.on 'blur', => $(nonInputable).removeClass 'pastable-focus'
@mountTextarea: (textarea)->
# Firefox & IE
return @mountContenteditable textarea if DataTransfer?.prototype && Object.getOwnPropertyDescriptor?.call(Object, DataTransfer.prototype, 'items')?.get
paste = new Paste createHiddenEditable().insertBefore(textarea), textarea
ctlDown = false
$(textarea).on 'keyup', (ev)->
ctlDown = false if ev.keyCode in [17, 224]
null
$(textarea).on 'keydown', (ev)->
ctlDown = true if ev.keyCode in [17, 224]
ctlDown = ev.ctrlKey || ev.metaKey if ev.ctrlKey? && ev.metaKey?
if ctlDown && ev.keyCode == 86
paste._textarea_focus_stolen = true
paste._container.focus()
paste._paste_event_fired = false
setTimeout =>
unless paste._paste_event_fired
$(textarea).focus()
paste._textarea_focus_stolen = false
, 1
null
$(textarea).on 'paste', =>
$(textarea).on 'focus', =>
$(textarea).addClass 'pastable-focus' unless paste._textarea_focus_stolen
$(textarea).on 'blur', =>
$(textarea).removeClass 'pastable-focus' unless paste._textarea_focus_stolen
$(paste._target).on '_pasteCheckContainerDone', =>
$(textarea).focus()
paste._textarea_focus_stolen = false
$(paste._target).on 'pasteText', (ev, data)=>
curStart = $(textarea).prop('selectionStart')
curEnd = $(textarea).prop('selectionEnd')
content = $(textarea).val()
$(textarea).val "#{content[0...curStart]}#{data.text}#{content[curEnd...]}"
$(textarea)[0].setSelectionRange curStart + data.text.length, curStart + data.text.length
$(textarea).trigger 'change'
@mountContenteditable: (contenteditable)->
paste = new Paste contenteditable, contenteditable
$(contenteditable).on 'focus', => $(contenteditable).addClass 'pastable-focus'
$(contenteditable).on 'blur', => $(contenteditable).removeClass 'pastable-focus'
constructor: (@_container, @_target)->
@_container = $ @_container
@_target = $ @_target
.addClass 'pastable'
@_container.on 'paste', (ev)=>
# return ev.preventDefault() unless ev.currentTarget == ev.target
@originalEvent = (if ev.originalEvent != null then ev.originalEvent else null)
@_paste_event_fired = true
if ev.originalEvent?.clipboardData?
clipboardData = ev.originalEvent.clipboardData
if clipboardData.items
pastedFilename = null
# Chrome or any other browsers with DataTransfer.items implemented
@originalEvent.pastedTypes = []
for item in clipboardData.items
if item.type.match(/^text\/(plain|rtf|html)/)
@originalEvent.pastedTypes.push(item.type)
for item, _i in clipboardData.items
if item.type.match /^image\//
reader = new FileReader()
reader.onload = (event)=>
@_handleImage event.target.result, @originalEvent, pastedFilename
try
reader.readAsDataURL item.getAsFile()
ev.preventDefault()
break
if item.type == 'text/plain'
if _i == 0 && clipboardData.items.length > 1 && clipboardData.items[1].type.match /^image\//
stringIsFilename = true
fileType = clipboardData.items[1].type
item.getAsString (string)=>
if stringIsFilename
pastedFilename = string
@_target.trigger 'pasteText', text: string, isFilename: true, fileType: fileType, originalEvent: @originalEvent
else
@_target.trigger 'pasteText', text: string, originalEvent: @originalEvent
if item.type == 'text/rtf'
item.getAsString (string)=>
@_target.trigger 'pasteTextRich', text: string, originalEvent: @originalEvent
if item.type == 'text/html'
item.getAsString (string)=>
@_target.trigger 'pasteTextHtml', text: string, originalEvent: @originalEvent
else
# Firefox & Safari(text-only)
if -1 != Array.prototype.indexOf.call clipboardData.types, 'text/plain'
text = clipboardData.getData 'Text'
setTimeout =>
@_target.trigger 'pasteText', text: text, originalEvent: @originalEvent
, 1
@_checkImagesInContainer (src)=>
@_handleImage src, @originalEvent
# IE
if clipboardData = window.clipboardData
if (text = clipboardData.getData 'Text')?.length
setTimeout =>
@_target.trigger 'pasteText', text: text, originalEvent: @originalEvent
@_target.trigger '_pasteCheckContainerDone'
, 1
else
for file in clipboardData.files
@_handleImage URL.createObjectURL(file), @originalEvent
@_checkImagesInContainer (src)=>
@_handleImage src, @originalEvent
null
_handleImage: (src, e, name)->
if src.match /^webkit\-fake\-url\:\/\//
return @_target.trigger 'pasteImageError',
message: "You are trying to paste an image in Safari, however we are unable to retieve its data."
@_target.trigger 'pasteImageStart'
loader = new Image()
loader.crossOrigin = "anonymous"
loader.onload = =>
canvas = document.createElement 'canvas'
canvas.width = loader.width
canvas.height = loader.height
ctx = canvas.getContext '2d'
ctx.drawImage loader, 0, 0, canvas.width, canvas.height
dataURL = null
try
dataURL = canvas.toDataURL 'image/png'
blob = dataURLtoBlob dataURL
if dataURL
@_target.trigger 'pasteImage',
blob: blob
dataURL: dataURL
width: loader.width
height: loader.height,
originalEvent: e,
name: name
@_target.trigger 'pasteImageEnd'
loader.onerror = =>
@_target.trigger 'pasteImageError',
message: "Failed to get image from: #{src}"
url: src
@_target.trigger 'pasteImageEnd'
loader.src = src
_checkImagesInContainer: (cb)->
timespan = Math.floor 1000 * Math.random()
img["_paste_marked_#{timespan}"] = true for img in @_container.find('img')
setTimeout =>
for img in @_container.find('img')
unless img["_paste_marked_#{timespan}"]
cb img.src
$(img).remove()
@_target.trigger '_pasteCheckContainerDone'
, 1