-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from layerssss/contenteditable
refactor
- Loading branch information
Showing
5 changed files
with
402 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "paste.js", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"authors": [ | ||
"Michael Yin <[email protected]>" | ||
], | ||
|
@@ -16,6 +16,6 @@ | |
"index.html" | ||
], | ||
"dependencies": { | ||
"jquery": "~2.1.0" | ||
"jquery": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,127 @@ | ||
$ = jQuery | ||
readImagesFromEditable = (element, cb)-> | ||
setTimeout (-> | ||
$(element).find('img').each (i, img)-> | ||
getImageData img.src, cb | ||
), 1 | ||
getImageData = (src, cb)-> | ||
loader = new Image() | ||
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' | ||
catch | ||
if dataURL | ||
cb | ||
dataURL: dataURL | ||
width: loader.width | ||
height: loader.height | ||
loader.src = src | ||
$ = 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 @ | ||
paste = Paste.mountNonInputable el | ||
@ | ||
$.fn.pastableTextarea = -> | ||
for el in @ | ||
paste = Paste.mountTextarea el | ||
@ | ||
$.fn.pastableContenteditable = -> | ||
for el in @ | ||
paste = Paste.mountContenteditable el | ||
@ | ||
|
||
$.paste = -> | ||
div = document.createElement 'div' | ||
div.contentEditable = true | ||
$(div).css | ||
createHiddenEditable = -> | ||
$(document.createElement 'div') | ||
.attr 'contenteditable', true | ||
.css | ||
width: 1 | ||
height: 1 | ||
position: 'fixed' | ||
left: -100 | ||
overflow: 'hidden' | ||
# backgroundColor: '#ccc' | ||
.on 'paste', (ev)-> | ||
if ev.originalEvent?.clipboardData? | ||
clipboardData = ev.originalEvent.clipboardData | ||
if clipboardData.items #webkit | ||
for item in clipboardData.items | ||
if item.type.match /^image\// | ||
reader = new FileReader() | ||
reader.onload = (event)=> | ||
getImageData event.target.result, (data)-> | ||
$(div).trigger 'pasteImage', data | ||
reader.readAsDataURL item.getAsFile() | ||
if item.type == 'text/plain' | ||
item.getAsString (string)-> | ||
$(div).trigger 'pasteText', text: string | ||
else | ||
if clipboardData.types.length | ||
if (text = clipboardData.getData 'Text')?.length | ||
$(div).trigger 'pasteText', text: text | ||
else | ||
readImagesFromEditable div, (data)-> | ||
$(div).trigger 'pasteImage', data | ||
if clipboardData = window.clipboardData # ie | ||
if (text = clipboardData.getData 'Text')?.length | ||
$(div).trigger 'pasteText', text: text | ||
else | ||
readImagesFromEditable div, (data)-> | ||
$(div).trigger 'pasteImage', data | ||
|
||
setTimeout (-> | ||
$(div).html('') | ||
), 2 | ||
return $ div | ||
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', => paste._container.focus() | ||
|
||
paste._container.on 'focus', => $(nonInputable).addClass 'pastable-focus' | ||
paste._container.on 'blur', => $(nonInputable).removeClass 'pastable-focus' | ||
|
||
|
||
@mountTextarea: (textarea)-> | ||
return @mountContenteditable textarea unless window.ClipboardEvent | ||
# Firefox only | ||
paste = new Paste createHiddenEditable().insertBefore(textarea), textarea | ||
$(textarea).on 'keypress', (ev)=> | ||
return unless 'v' == String.fromCharCode ev.charCode | ||
return unless ev.ctrlKey || ev.metaKey | ||
paste._container.focus() | ||
$(paste._target).on 'pasteImage', => | ||
$(textarea).focus() | ||
$(paste._target).on 'pasteText', => | ||
$(textarea).focus() | ||
|
||
$(textarea).on 'focus', => $(textarea).addClass 'pastable-focus' | ||
$(textarea).on 'blur', => $(textarea).removeClass 'pastable-focus' | ||
|
||
@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)=> | ||
if ev.originalEvent?.clipboardData? | ||
clipboardData = ev.originalEvent.clipboardData | ||
if clipboardData.items | ||
# Chrome & Safari(text-only) | ||
for item in clipboardData.items | ||
if item.type.match /^image\// | ||
reader = new FileReader() | ||
reader.onload = (event)=> | ||
@_handleImage event.target.result | ||
reader.readAsDataURL item.getAsFile() | ||
if item.type == 'text/plain' | ||
item.getAsString (string)=> | ||
@_target.trigger 'pasteText', text: string | ||
else | ||
# Firefox | ||
if clipboardData.types.length | ||
text = clipboardData.getData 'Text' | ||
@_target.trigger 'pasteText', text: text | ||
else | ||
@_checkImagesInContainer (src)=> | ||
@_handleImage src | ||
# IE | ||
if clipboardData = window.clipboardData | ||
if (text = clipboardData.getData 'Text')?.length | ||
@_target.trigger 'pasteText', text: text | ||
else | ||
for file in clipboardData.files | ||
@_handleImage URL.createObjectURL(file) | ||
@_checkImagesInContainer -> | ||
|
||
_handleImage: (src)-> | ||
loader = new Image() | ||
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' | ||
if dataURL | ||
@_target.trigger 'pasteImage', | ||
dataURL: dataURL | ||
width: loader.width | ||
height: loader.height | ||
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') | ||
cb img.src unless img["_paste_marked_#{timespan}"] | ||
$(img).remove() | ||
, 1 |
Oops, something went wrong.