Skip to content

Commit 575aced

Browse files
committed
implement copy paste
1 parent 551148d commit 575aced

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<script src="./lib/drawingPath.js"></script>
2626
<script src="./lib/drawingText.js"></script>
2727
<script src="./lib/upload.js"></script>
28+
<script src="./lib/copyPaste.js"></script>
2829

2930
<script src="./lib/utils.js"></script>
3031
<script src="./lib/zoom.js"></script>

lib/copyPaste.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
(function () {
2+
'use strict';
3+
const copyPaste = (canvas) => {
4+
5+
// copy
6+
document.addEventListener('copy', (e) => {
7+
if (!canvas.getActiveObject()) return
8+
9+
// copy image as dataUrl
10+
if (canvas.getActiveObject().type === 'image') {
11+
e.preventDefault()
12+
13+
e.clipboardData.setData('text/plain', canvas.getActiveObject().toDataURL())
14+
}
15+
16+
17+
// if selection is not an image, copy as JSON
18+
if (canvas.getActiveObject().type !== 'image') {
19+
e.preventDefault()
20+
canvas.getActiveObject().clone((cloned) => {
21+
e.clipboardData.setData('text/plain', JSON.stringify(cloned.toJSON()))
22+
})
23+
}
24+
})
25+
26+
27+
28+
// JSON string validator
29+
const isJSONObjectString = (s) => {
30+
try {
31+
const o = JSON.parse(s);
32+
return !!o && (typeof o === 'object') && !Array.isArray(o)
33+
} catch {
34+
return false
35+
}
36+
}
37+
38+
39+
// base64 validator
40+
const isBase64String = (str) => {
41+
try {
42+
str = str.split('base64,').pop()
43+
window.atob(str)
44+
return true
45+
} catch (e) {
46+
return false
47+
}
48+
}
49+
50+
51+
52+
// paste
53+
document.addEventListener('paste', (e) => {
54+
let pasteTextData = e.clipboardData.getData('text')
55+
56+
// check if base64 image
57+
if (pasteTextData && isBase64String(pasteTextData)) {
58+
fabric.Image.fromURL(pasteTextData, (img) => {
59+
img.set({
60+
left: 0,
61+
top: 0
62+
})
63+
img.scaleToHeight(100)
64+
img.scaleToWidth(100)
65+
canvas.add(img)
66+
canvas.setActiveObject(img)
67+
canvas.trigger('object:modified')
68+
})
69+
70+
return
71+
}
72+
73+
74+
// check if there's an image in clipboard items
75+
if (e.clipboardData.items.length > 0) {
76+
for (let i = 0; i < e.clipboardData.items.length; i++) {
77+
if (e.clipboardData.items[i].type.indexOf('image') === 0) {
78+
let blob = e.clipboardData.items[i].getAsFile()
79+
if (blob !== null) {
80+
let reader = new FileReader()
81+
reader.onload = (f) => {
82+
fabric.Image.fromURL(f.target.result, (img) => {
83+
img.set({
84+
left: 0,
85+
top: 0
86+
})
87+
img.scaleToHeight(100)
88+
img.scaleToWidth(100)
89+
canvas.add(img)
90+
canvas.setActiveObject(img)
91+
canvas.trigger('object:modified')
92+
})
93+
}
94+
reader.readAsDataURL(blob)
95+
}
96+
}
97+
}
98+
}
99+
100+
101+
// check if JSON and type is valid
102+
let validTypes = ['rect', 'circle', 'line', 'path', 'polygon', 'polyline', 'textbox', 'group']
103+
if (isJSONObjectString(pasteTextData)) {
104+
let obj = JSON.parse(pasteTextData)
105+
if (!validTypes.includes(obj.type)) return
106+
107+
// insert and select
108+
fabric.util.enlivenObjects([obj], function (objects) {
109+
objects.forEach(function (o) {
110+
o.set({
111+
left: 0,
112+
top: 0
113+
})
114+
canvas.add(o)
115+
o.setCoords()
116+
canvas.setActiveObject(o)
117+
})
118+
canvas.renderAll()
119+
canvas.trigger('object:modified')
120+
})
121+
}
122+
})
123+
124+
}
125+
126+
window.ImageEditor.prototype.initializeCopyPaste = copyPaste;
127+
})()

lib/core.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
this.initializePathDrawing(this.canvas);
155155
this.initializeTextBoxDrawing(this.canvas);
156156
this.initializeUpload(this.canvas);
157+
this.initializeCopyPaste(this.canvas);
157158

158159
this.extendHideShowToolPanel();
159160
this.initializeZoomEvents();

0 commit comments

Comments
 (0)